RPA Toolkit
added long, double key support for the rmap_t.
authorMartin Stoilov <martin@rpasearch.com>
Thu, 9 Jun 2011 06:19:47 +0000 (23:19 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Thu, 9 Jun 2011 06:19:47 +0000 (23:19 -0700)
rjs/rjs.c
rlib/build/linux/rlib.mk
rlib/rjsobject.c [deleted file]
rlib/rjsobject.h [deleted file]
rlib/rmap.c
rlib/rmap.h
rvm/rvmcpu.c
rvm/rvmreg.c
rvm/rvmreg.h

index 0d43d86..abcf2c0 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -208,15 +208,15 @@ static rint rjs_compiler_argarray_setup(rjs_compiler_t *co)
 {
        rvm_varmap_t *v;
        rvmreg_t count = rvm_reg_create_long(0);
-       rjs_object_t *a;
+       rmap_t *a;
 
        v = rvm_scope_tiplookup_s(co->scope, "ARGS");
        if (!v) {
                return -1;
        }
-       a = rjs_object_create(sizeof(rvmreg_t));
+       a = r_map_create(sizeof(rvmreg_t), 7);
        rvm_gc_add(co->cpu->gc, (robject_t*)a);
-       r_harray_add_s(a->harray, "count", &count);
+       r_map_add_s(a, "count", &count);
        rvm_reg_setjsobject((rvmreg_t*)v->data.ptr, (robject_t*)a);
        return 0;
 }
@@ -225,7 +225,7 @@ static rint rjs_compiler_argarray_setup(rjs_compiler_t *co)
 static rint rjs_compiler_addarg(rjs_compiler_t *co, rvmreg_t *arg)
 {
        rvm_varmap_t *v;
-       rjs_object_t *a;
+       rmap_t *a;
        rvmreg_t *count;
        rlong index;
 
@@ -233,11 +233,11 @@ static rint rjs_compiler_addarg(rjs_compiler_t *co, rvmreg_t *arg)
        if (!v) {
                return -1;
        }
-       a = (rjs_object_t*)RVM_REG_GETP((rvmreg_t*)v->data.ptr);
-       r_carray_add(a->narray, arg);
-       index = r_harray_lookup_s(a->harray, "count");
+       a = (rmap_t*)RVM_REG_GETP((rvmreg_t*)v->data.ptr);
+       index = r_map_lookup_s(a, -1, "count");
        R_ASSERT(index >= 0);
-       count = (rvmreg_t *)r_harray_slot(a->harray, index);
+       count = (rvmreg_t *)r_map_value(a, index);
+       r_map_add_l(a, RVM_REG_GETL(count), arg);
        rvm_reg_setlong(count, RVM_REG_GETL(count) + 1);
 
        return 0;
index ce0defc..3fd0c9e 100644 (file)
@@ -11,7 +11,6 @@ RLIB_OBJECTS +=       $(OUTDIR)/rmem.o
 RLIB_OBJECTS +=        $(OUTDIR)/rmath.o
 RLIB_OBJECTS +=        $(OUTDIR)/ratomic.o
 RLIB_OBJECTS +=        $(OUTDIR)/rspinlock.o
-RLIB_OBJECTS +=        $(OUTDIR)/rjsobject.o
 RLIB_OBJECTS +=        $(OUTDIR)/rharray.o
 RLIB_OBJECTS +=        $(OUTDIR)/rcarray.o
 RLIB_OBJECTS +=        $(OUTDIR)/rarray.o
diff --git a/rlib/rjsobject.c b/rlib/rjsobject.c
deleted file mode 100644 (file)
index 898c61b..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#include "rjsobject.h"
-#include "rmem.h"
-
-
-
-void rjs_object_cleanup(robject_t *obj)
-{
-       rjs_object_t *jso = (rjs_object_t *)obj;
-       r_object_destroy((robject_t*)jso->harray);
-       r_object_destroy((robject_t*)jso->narray);
-}
-
-
-robject_t *rjs_object_init(robject_t *obj, ruint32 type, r_object_cleanupfun cleanup, r_object_copyfun copy, ruint elt_size)
-{
-       rjs_object_t *jso = (rjs_object_t *)obj;
-
-       r_object_init(obj, type, cleanup, copy);
-       jso->harray = r_harray_create(elt_size);
-       jso->narray = r_carray_create(elt_size);
-       return obj;
-}
-
-
-rjs_object_t *rjs_object_create(ruint elt_size)
-{
-       rjs_object_t *jso;
-       jso = (rjs_object_t*)r_object_create(sizeof(*jso));
-       rjs_object_init((robject_t*)jso, R_OBJECT_JSOBJECT, rjs_object_cleanup, rjs_object_copy, elt_size);
-       return jso;
-}
-
-
-robject_t *rjs_object_copy(const robject_t *obj)
-{
-       rjs_object_t *jso;
-       jso = (rjs_object_t*)r_object_create(sizeof(*jso));
-       jso->harray = (rharray_t*)r_object_v_copy((robject_t *)((rjs_object_t *)obj)->harray);
-       jso->narray = (rcarray_t*)r_object_v_copy((robject_t *)((rjs_object_t *)obj)->narray);
-       return (robject_t*)jso;
-}
-
diff --git a/rlib/rjsobject.h b/rlib/rjsobject.h
deleted file mode 100644 (file)
index af8f709..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-#ifndef _RJSOBJECT_H_
-#define _RJSOBJECT_H_
-
-#include "rtypes.h"
-#include "robject.h"
-#include "rcarray.h"
-#include "rharray.h"
-
-/*
- * This class targets the JavaScript Object implementation. Although I think it is pretty useful
- * collection primitive that is worth staying in RLIB. In the future I might either rename it or
- * move it to RJS.
- */
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-
-typedef struct rjs_object_s rjs_object_t;
-
-
-struct rjs_object_s {
-       robject_t obj;
-       rcarray_t *narray;
-       rharray_t *harray;
-};
-
-robject_t *rjs_object_init(robject_t *obj, ruint32 type, r_object_cleanupfun cleanup, r_object_copyfun copy, ruint elt_size);
-rjs_object_t *rjs_object_create();
-
-/*
- * Virtual methods implementation
- */
-void rjs_object_cleanup(robject_t *obj);
-robject_t *rjs_object_copy(const robject_t *obj);
-
-#ifdef __cplusplus
-}
-#endif
-
-#endif
index 76cf038..be74d10 100644 (file)
@@ -151,6 +151,24 @@ rlong r_map_add_s(rmap_t *map, const rchar *name, rconstpointer pval)
 }
 
 
+rlong r_map_add_l(rmap_t *map, long name, rconstpointer pval)
+{
+       rchar key[128];
+       r_memset(key, 0, sizeof(key));
+       r_snprintf(key, sizeof(key) - 1, "%ld", name);
+       return r_map_add_s(map, key, pval);
+}
+
+
+rlong r_map_add_d(rmap_t *map, double name, rconstpointer pval)
+{
+       rchar key[128];
+       r_memset(key, 0, sizeof(key));
+       r_snprintf(key, sizeof(key) - 1, "%.15f", name);
+       return r_map_add_s(map, key, pval);
+}
+
+
 r_mapnode_t *r_map_node(rmap_t *map, rulong index)
 {
        r_mapnode_t *node;
@@ -245,6 +263,24 @@ rlong r_map_lookup_s(rmap_t *map, rlong current, const rchar *name)
 }
 
 
+rlong r_map_lookup_l(rmap_t *map, rlong current, long name)
+{
+       rchar key[128];
+       r_memset(key, 0, sizeof(key));
+       r_snprintf(key, sizeof(key) - 1, "%ld", name);
+       return r_map_lookup_s(map, current, key);
+}
+
+
+rlong r_map_lookup_d(rmap_t *map, rlong current, double name)
+{
+       rchar key[128];
+       r_memset(key, 0, sizeof(key));
+       r_snprintf(key, sizeof(key) - 1, "%.15f", name);
+       return r_map_lookup_s(map, current, key);
+}
+
+
 rlong r_map_taillookup(rmap_t *map, rlong current, const rchar *name, rsize_t namesize)
 {
        rlong found = -1;
index 1bf5a51..f3431bd 100644 (file)
@@ -32,8 +32,12 @@ rlong r_map_lookup(rmap_t *map, rlong current, const rchar *name, rsize_t namesi
 rlong r_map_lookup_s(rmap_t *map, rlong current, const rchar *name);
 rlong r_map_taillookup(rmap_t *map, rlong current, const rchar *name, rsize_t namesize);
 rlong r_map_taillookup_s(rmap_t *map, rlong current, const rchar *name);
+rlong r_map_lookup_d(rmap_t *map, rlong current, double name);
+rlong r_map_lookup_l(rmap_t *map, rlong current, long name);
 rlong r_map_add(rmap_t *map, const rchar *name, rsize_t namesize, rconstpointer pval);
 rlong r_map_add_s(rmap_t *map, const rchar *name, rconstpointer pval);
+rlong r_map_add_d(rmap_t *map, double name, rconstpointer pval);
+rlong r_map_add_l(rmap_t *map, long name, rconstpointer pval);
 rlong r_map_setvalue(rmap_t *map, rlong index, rconstpointer pval);
 const rchar *r_map_key(rmap_t *map, rulong index);
 rpointer r_map_value(rmap_t *map, rulong index);
index c8c4c9f..5b237fc 100644 (file)
@@ -31,7 +31,6 @@
 #include "rstring.h"
 #include "rvmreg.h"
 #include "rmap.h"
-#include "rjsobject.h"
 
 #define RVM_DEFAULT_STACKSIZE (4 * 1024)
 
@@ -1520,7 +1519,6 @@ static void rvm_op_mapalloc(rvmcpu_t *cpu, rvm_asmins_t *ins)
        if (!a) {
                RVM_ABORT(cpu, RVM_E_ILLEGAL);
        }
-//     r_carray_setlength(a, RVM_CPUREG_GETU(cpu, ins->op2));
        rvm_gc_add(cpu->gc, (robject_t*)a);
        rvm_reg_setjsobject(arg1, (robject_t*)a);
 }
@@ -1543,8 +1541,6 @@ static void rvm_op_allocarr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 static void rvm_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rlong index;
-       rlong keysize;
-       rchar key[256], *pkey = key;
        rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
        rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
@@ -1554,19 +1550,16 @@ static void rvm_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_LONG || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%ld", RVM_REG_GETL(arg3));
+               index = r_map_lookup_l(a, -1, RVM_REG_GETL(arg3));
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%f", RVM_REG_GETD(arg3));
+               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               pkey = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str;
-               keysize = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size;
+               index = r_map_lookup(a, -1, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_POINTER) {
-               pkey = (rchar*)RVM_CPUREG_GETP(cpu, ins->op3);
-               keysize = RVM_CPUREG_GETL(cpu, ins->op1);
+               index = r_map_lookup(a, -1, (rchar*)RVM_CPUREG_GETP(cpu, ins->op3), RVM_CPUREG_GETL(cpu, ins->op1));
        } else {
                RVM_ABORT(cpu, RVM_E_ILLEGAL);
        }
-       index = r_map_lookup(a, -1, pkey, keysize);
 
        RVM_REG_CLEAR(arg1);
        RVM_REG_SETTYPE(arg1, RVM_DTYPE_LONG);
@@ -1577,8 +1570,6 @@ static void rvm_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
 static void rvm_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rlong index;
-       rlong keysize;
-       rchar key[256], *pkey = key;
        rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
        rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
@@ -1588,19 +1579,16 @@ static void rvm_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_LONG || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%ld", RVM_REG_GETL(arg3));
+               index = r_map_add_l(a, RVM_REG_GETL(arg3), NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%f", RVM_REG_GETD(arg3));
+               index = r_map_add_d(a, RVM_REG_GETD(arg3), NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               pkey = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str;
-               keysize = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size;
+               index = r_map_add(a, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size, NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_POINTER) {
-               pkey = (rchar*)RVM_CPUREG_GETP(cpu, ins->op3);
-               keysize = RVM_CPUREG_GETL(cpu, ins->op1);
+               index = r_map_add(a, (rchar*)RVM_CPUREG_GETP(cpu, ins->op3), RVM_CPUREG_GETL(cpu, ins->op1), NULL);
        } else {
                RVM_ABORT(cpu, RVM_E_ILLEGAL);
        }
-       index = r_map_add(a, pkey, keysize, NULL);
 
        RVM_REG_CLEAR(arg1);
        RVM_REG_SETTYPE(arg1, RVM_DTYPE_LONG);
@@ -1611,8 +1599,6 @@ static void rvm_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
 static void rvm_op_maplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rlong index;
-       rlong keysize;
-       rchar key[256], *pkey = key;
        rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
        rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
@@ -1621,23 +1607,25 @@ static void rvm_op_maplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
        if (rvm_reg_gettype(arg2) != RVM_DTYPE_JSOBJECT) {
                RVM_ABORT(cpu, RVM_E_NOTOBJECT);
        }
-
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_LONG || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%ld", RVM_REG_GETL(arg3));
+               index = r_map_lookup_l(a, -1, RVM_REG_GETL(arg3));
+               if (index < 0)
+                       index = r_map_add_l(a, RVM_REG_GETL(arg3), NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               keysize = r_snprintf(key, sizeof(key) - 1, "%f", RVM_REG_GETD(arg3));
+               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
+               if (index < 0)
+                       index = r_map_add_d(a, RVM_REG_GETD(arg3), NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               pkey = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str;
-               keysize = ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size;
+               index = r_map_lookup(a, -1, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size);
+               if (index < 0)
+                       index = r_map_add(a, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, ((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.size, NULL);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_POINTER) {
-               pkey = (rchar*)RVM_CPUREG_GETP(cpu, ins->op3);
-               keysize = RVM_CPUREG_GETL(cpu, ins->op1);
+               index = r_map_lookup(a, -1, (rchar*)RVM_CPUREG_GETP(cpu, ins->op3), RVM_CPUREG_GETL(cpu, ins->op1));
+               if (index < 0)
+                       index = r_map_add(a, (rchar*)RVM_CPUREG_GETP(cpu, ins->op3), RVM_CPUREG_GETL(cpu, ins->op1), NULL);
        } else {
                RVM_ABORT(cpu, RVM_E_ILLEGAL);
        }
-       index = r_map_lookup(a, -1, pkey, keysize);
-       if (index < 0)
-               index = r_map_add(a, pkey, keysize, NULL);
        RVM_REG_CLEAR(arg1);
        RVM_REG_SETTYPE(arg1, RVM_DTYPE_LONG);
        RVM_REG_SETL(arg1, index);
index 6101f46..9f740b1 100644 (file)
@@ -483,8 +483,8 @@ rstring_t *rvm_reg_string(const rvmreg_t *src)
 }
 
 
-rjs_object_t *rvm_reg_jsobject(const rvmreg_t *src)
+rmap_t *rvm_reg_jsobject(const rvmreg_t *src)
 {
        R_ASSERT(src && rvm_reg_gettype(src) == RVM_DTYPE_JSOBJECT);
-       return (rjs_object_t*)RVM_REG_GETP(src);
+       return (rmap_t*)RVM_REG_GETP(src);
 }
index 80c0523..68577e9 100644 (file)
@@ -6,7 +6,7 @@
 #include "rarray.h"
 #include "rharray.h"
 #include "rcarray.h"
-#include "rjsobject.h"
+#include "rmap.h"
 #include "rstring.h"
 
 
@@ -182,7 +182,7 @@ ruchar rvm_reg_boolean(const rvmreg_t *src);
 rdouble rvm_reg_double(const rvmreg_t *src);
 rpointer rvm_reg_pointer(const rvmreg_t *src);
 rstring_t *rvm_reg_string(const rvmreg_t *src);
-rjs_object_t *rvm_reg_jsobject(const rvmreg_t *src);
+rmap_t *rvm_reg_jsobject(const rvmreg_t *src);
 
 
 #ifdef __cplusplus