RPA Toolkit
work on RJS object system...
authorMartin Stoilov <martin@rpasearch.com>
Tue, 23 Aug 2011 06:01:57 +0000 (23:01 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Tue, 23 Aug 2011 06:01:57 +0000 (23:01 -0700)
rjs/rjs.c
rjs/rjs.h
rvm/rvmreg.c
rvm/rvmreg.h

index 41fbb04..dd7bc24 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -34,12 +34,14 @@ static void rjs_engine_initgp(rjs_engine_t *jse);
 static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins);
 static void rjs_engine_dbgprint(rvmcpu_t *cpu, rvm_asmins_t *ins);
 static void rjs_engine_object(rvmcpu_t *cpu, rvm_asmins_t *ins);
+static void rjs_string_ltrim(rvmcpu_t *cpu, rvm_asmins_t *ins);
 
 static rvm_switable_t rjsswitable[] = {
                {"print", rjs_engine_print},
                {"dbgprint", rjs_engine_dbgprint},
                {"Object", rjs_engine_object},
                {"Array", rjs_engine_object},
+               {"string.ltrim", rjs_string_ltrim},
                {NULL, NULL},
 };
 
@@ -295,74 +297,6 @@ static void rjs_op_mapalloc(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
-static void rjs_op_arralloc(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       ruword size = RVM_CPUREG_GETU(cpu, ins->op2);
-       rcarray_t *a = r_carray_create_rvmreg();
-       if (!a) {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
-       }
-       r_carray_setlength(a, (unsigned long)size);
-       r_gc_add(cpu->gc, (robject_t*)a);
-       rvm_reg_setarray(arg1, (robject_t*)a);
-}
-
-
-static long rjs_op_mapproplookup(rmap_t *map, rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       long index = -1;
-       rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
-
-       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               index = r_map_lookup_l(map, -1, (long)RVM_REG_GETL(arg3));
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               index = r_map_lookup_d(map, -1, RVM_REG_GETD(arg3));
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               index = r_map_lookup(map, -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) {
-               index = r_map_lookup(map, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
-       }
-       return index;
-}
-
-
-static long rjs_op_stringproplookup(rstring_t *str, rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       long index = -1;
-       rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
-
-       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               index = RVM_REG_GETL(arg3);
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               index = (long)RVM_REG_GETD(arg3);
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               index = r_strtol(((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, NULL, 10);
-       }
-       return index;
-}
-
-
-static void rjs_op_proplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       long index = -1;
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
-
-       if (rvm_reg_gettype(arg2) == RVM_DTYPE_MAP) {
-               rmap_t *map = (rmap_t*)RVM_REG_GETP(arg2);
-               index = rjs_op_mapproplookup(map, cpu, ins);
-       } else if (rvm_reg_gettype(arg2) == RVM_DTYPE_STRING) {
-               rstring_t *str = (rstring_t*)RVM_REG_GETP(arg2);
-               index = rjs_op_stringproplookup(str, cpu, ins);
-       }
-       RVM_REG_CLEAR(arg1);
-       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
-       RVM_REG_SETL(arg1, index);
-}
-
-
-
 static long rjs_op_mapproplookupadd(rmap_t *map, rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        long index;
@@ -410,97 +344,63 @@ static void rjs_op_proplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
-static void rjs_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
+static long rjs_op_mapproplookup(rmap_t *map, rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       long index;
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
+       long index = -1;
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
-       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
-               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
-       }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               index = r_map_lookup_l(a, -1, (long)RVM_REG_GETL(arg3));
+               index = r_map_lookup_l(map, -1, (long)RVM_REG_GETL(arg3));
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
+               index = r_map_lookup_d(map, -1, RVM_REG_GETD(arg3));
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               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);
+               index = r_map_lookup(map, -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) {
-               index = r_map_lookup(a, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
-       } else {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+               index = r_map_lookup(map, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
        }
-
-       RVM_REG_CLEAR(arg1);
-       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
-       RVM_REG_SETL(arg1, index);
+       return index;
 }
 
 
-static void rjs_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
+static long rjs_op_stringproplookup(rstring_t *str, rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       long index;
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
+       long index = -1;
        rvmreg_t *arg3 = RVM_CPUREG_PTR(cpu, ins->op3);
-       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
-               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
-       }
        if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               index = r_map_gckey_add_l(a, cpu->gc, (long)RVM_REG_GETL(arg3), NULL);
+               index = RVM_REG_GETL(arg3);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               index = r_map_gckey_add_d(a, cpu->gc, RVM_REG_GETD(arg3), NULL);
+               index = (long)RVM_REG_GETD(arg3);
        } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               index = r_map_gckey_add(a, cpu->gc, ((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) {
-               index = r_map_gckey_add(a, cpu->gc, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1), NULL);
-       } else {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+               index = r_strtol(((rstring_t *)RVM_CPUREG_GETP(cpu, ins->op3))->s.str, NULL, 10);
        }
-
-       RVM_REG_CLEAR(arg1);
-       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
-       RVM_REG_SETL(arg1, index);
+       return index;
 }
 
 
-static void rjs_op_maplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
+static void rjs_op_proplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       long index;
+       long index = -1;
+       rboolean globalprop = 0;
        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);
-       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
-               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
-       }
-       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
-               index = r_map_lookup_l(a, -1, (long)RVM_REG_GETL(arg3));
-               if (index < 0)
-                       index = r_map_gckey_add_l(a, cpu->gc, (long)RVM_REG_GETL(arg3), NULL);
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
-               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
-               if (index < 0)
-                       index = r_map_gckey_add_d(a, cpu->gc, RVM_REG_GETD(arg3), NULL);
-       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
-               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_gckey_add(a, cpu->gc, ((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) {
-               index = r_map_lookup(a, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
-               if (index < 0)
-                       index = r_map_gckey_add(a, cpu->gc, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1), NULL);
-       } else {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       if (rvm_reg_gettype(arg2) == RVM_DTYPE_MAP) {
+               rmap_t *map = (rmap_t*)RVM_REG_GETP(arg2);
+               index = rjs_op_mapproplookup(map, cpu, ins);
+               rvm_reg_setsigned(arg1, index);
+       } else if (rvm_reg_gettype(arg2) == RVM_DTYPE_STRING) {
+               rstring_t *str = (rstring_t*)RVM_REG_GETP(arg2);
+               index = rjs_op_stringproplookup(str, cpu, ins);
+               if (index < 0) {
+                       index = rjs_op_mapproplookup(RJS_CPU2JSE(cpu)->props[RVM_DTYPE_STRING], cpu, ins);
+                       if (index >= 0)
+                               globalprop = TRUE;
+               }
+               rvm_reg_setsigned(arg1, index);
+               if (globalprop)
+                       RVM_REG_SETFLAG(arg1, RVM_INFOBIT_GLOBAL);
        }
-       RVM_REG_CLEAR(arg1);
-       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
-       RVM_REG_SETL(arg1, index);
 }
 
 
@@ -535,8 +435,7 @@ static void rjs_op_propldr(rvmcpu_t *cpu, rvm_asmins_t *ins)
        rpointer value;
        long index;
 
-       rjs_opmap_invoke_binary_handler(RJS_USERDATA2MAP(cpu->userdata2), RJS_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
-       index = (long)RVM_REG_GETL(&tmp);
+       index = (long)RVM_REG_GETL(arg1);
        rvm_reg_setundef(arg1);
        if (rvm_reg_gettype(arg2) == RVM_DTYPE_MAP) {
                rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
@@ -549,9 +448,17 @@ static void rjs_op_propldr(rvmcpu_t *cpu, rvm_asmins_t *ins)
                if (index >= s->s.size) {
                        rvm_reg_setundef(arg1);
                } else {
-                       rstring_t *allocstr = r_string_create_strsize(&s->s.str[index], 1);
-                       r_gc_add(cpu->gc, (robject_t*)allocstr);
-                       rvm_reg_setstring(arg1, allocstr);
+                       if (1||RVM_REG_TSTFLAG(arg1, RVM_INFOBIT_GLOBAL)) {
+                               rmap_t *a = RJS_CPU2JSE(cpu)->props[RVM_DTYPE_STRING];
+                               value = r_map_value(a, index);
+                               if (value) {
+                                       *arg1 = *((rvmreg_t*)value);
+                               }
+                       } else {
+                               rstring_t *allocstr = r_string_create_strsize(&s->s.str[index], 1);
+                               r_gc_add(cpu->gc, (robject_t*)allocstr);
+                               rvm_reg_setstring(arg1, allocstr);
+                       }
                }
        }
 }
@@ -690,6 +597,100 @@ static void rjs_op_mapdel(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
+static void rjs_op_maplookup(rvmcpu_t *cpu, rvm_asmins_t *ins)
+{
+       long index;
+       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);
+       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
+
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
+               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
+       }
+       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
+               index = r_map_lookup_l(a, -1, (long)RVM_REG_GETL(arg3));
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
+               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
+               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) {
+               index = r_map_lookup(a, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
+       } else {
+               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       }
+
+       RVM_REG_CLEAR(arg1);
+       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
+       RVM_REG_SETL(arg1, index);
+}
+
+
+static void rjs_op_mapadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
+{
+       long index;
+       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);
+       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
+
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
+               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
+       }
+       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
+               index = r_map_gckey_add_l(a, cpu->gc, (long)RVM_REG_GETL(arg3), NULL);
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
+               index = r_map_gckey_add_d(a, cpu->gc, RVM_REG_GETD(arg3), NULL);
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
+               index = r_map_gckey_add(a, cpu->gc, ((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) {
+               index = r_map_gckey_add(a, cpu->gc, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1), NULL);
+       } else {
+               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       }
+
+       RVM_REG_CLEAR(arg1);
+       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
+       RVM_REG_SETL(arg1, index);
+}
+
+
+static void rjs_op_maplookupadd(rvmcpu_t *cpu, rvm_asmins_t *ins)
+{
+       long index;
+       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);
+       rmap_t *a = (rmap_t*)RVM_REG_GETP(arg2);
+
+       if (rvm_reg_gettype(arg2) != RVM_DTYPE_MAP) {
+               RVM_ABORT(cpu, RVM_E_NOTOBJECT);
+       }
+       if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_SIGNED || RVM_REG_GETTYPE(arg3) == RVM_DTYPE_UNSIGNED) {
+               index = r_map_lookup_l(a, -1, (long)RVM_REG_GETL(arg3));
+               if (index < 0)
+                       index = r_map_gckey_add_l(a, cpu->gc, (long)RVM_REG_GETL(arg3), NULL);
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_DOUBLE) {
+               index = r_map_lookup_d(a, -1, RVM_REG_GETD(arg3));
+               if (index < 0)
+                       index = r_map_gckey_add_d(a, cpu->gc, RVM_REG_GETD(arg3), NULL);
+       } else if (RVM_REG_GETTYPE(arg3) == RVM_DTYPE_STRING) {
+               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_gckey_add(a, cpu->gc, ((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) {
+               index = r_map_lookup(a, -1, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1));
+               if (index < 0)
+                       index = r_map_gckey_add(a, cpu->gc, (char*)RVM_CPUREG_GETP(cpu, ins->op3), (unsigned int)RVM_CPUREG_GETL(cpu, ins->op1), NULL);
+       } else {
+               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       }
+       RVM_REG_CLEAR(arg1);
+       RVM_REG_SETTYPE(arg1, RVM_DTYPE_SIGNED);
+       RVM_REG_SETL(arg1, index);
+}
+
+
 static void rjs_op_mapaddr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
@@ -828,58 +829,43 @@ static void rjs_op_mapstr(rvmcpu_t *cpu, rvm_asmins_t *ins)
 }
 
 
-static void rjs_op_addra(rvmcpu_t *cpu, rvm_asmins_t *ins)
+const char *rjs_version()
 {
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
-       rvmreg_t tmp = rvm_reg_create_signed(0);
-       rcarray_t *a = RVM_REG_GETP(arg2);
-       long index;
-
-       rjs_opmap_invoke_binary_handler(RJS_USERDATA2MAP(cpu->userdata2), RJS_OPID_CAST, cpu, &tmp, RVM_CPUREG_PTR(cpu, ins->op3), &tmp);
-       index = (long)RVM_REG_GETL(&tmp);
-
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_ARRAY || index < 0) {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
-       }
-       RVM_REG_CLEAR(arg1);
-       RVM_REG_SETTYPE(arg1, RVM_DTYPE_POINTER);
-       RVM_REG_SETP(arg1, r_carray_slot_expand(a, index));
+       return RJS_VERSION_STRING;
 }
 
 
-static void rjs_op_lda(rvmcpu_t *cpu, rvm_asmins_t *ins)
+static void rjs_string_ltrim(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
-       ruword index = RVM_CPUREG_GETU(cpu, ins->op3);
-       rcarray_t *a = RVM_REG_GETP(arg2);
+       const char *ptr, *list;
+       unsigned long size;
+       rvmreg_t *r = NULL, *l = NULL;
+       rstring_t *src, *dest;
 
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_ARRAY) {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       r = (rvmreg_t *) RVM_CPUREG_PTR(cpu, TP);
+       if (RJS_SWI_PARAMS(cpu) > 0) {
+               l = (rvmreg_t *) RJS_SWI_PARAM(cpu, 1);
+               if (rvm_reg_gettype(l) != RVM_DTYPE_STRING)
+                       RJS_SWI_ABORT(rjs_engine_get(cpu), NULL);
        }
-       *arg1 = *((rvmreg_t*)r_carray_slot_expand(a, (unsigned long)index));
-}
-
-
-static void rjs_op_sta(rvmcpu_t *cpu, rvm_asmins_t *ins)
-{
-       rvmreg_t *arg1 = RVM_CPUREG_PTR(cpu, ins->op1);
-       rvmreg_t *arg2 = RVM_CPUREG_PTR(cpu, ins->op2);
-       ruword index = RVM_CPUREG_GETU(cpu, ins->op3);
-       rcarray_t *a = RVM_REG_GETP(arg2);
-
-       if (rvm_reg_gettype(arg2) != RVM_DTYPE_ARRAY) {
-               RVM_ABORT(cpu, RVM_E_ILLEGAL);
+       if (rvm_reg_gettype(r) != RVM_DTYPE_STRING)
+               RJS_SWI_ABORT(rjs_engine_get(cpu), NULL);
+       if (l)
+               list = ((rstring_t *)RVM_REG_GETP(l))->s.str;
+       else
+               list = " \t\n\r\0";
+       src = (rstring_t *)RVM_REG_GETP(r);
+       ptr = src->s.str;
+       size = src->s.size;
+       while (size > 0) {
+               if (!r_strchr(list, *ptr))
+                       break;
+               size--;
+               ptr++;
        }
-       r_carray_replace(a, (unsigned long)index, arg1);
-}
-
-
-
-const char *rjs_version()
-{
-       return RJS_VERSION_STRING;
+       dest = r_string_create_strsize(ptr, size);
+       r_gc_add(cpu->gc, (robject_t*)dest);
+       rvm_reg_setstring(RVM_CPUREG_PTR(cpu, R0), dest);
 }
 
 
@@ -887,6 +873,7 @@ rjs_engine_t *rjs_engine_create()
 {
        rvmcpu_t *cpu;
        rvmreg_t *tp;
+       rvmreg_t tmp;
        rjs_engine_t *jse = (rjs_engine_t *) r_zmalloc(sizeof(*jse));
 
        jse->pa = rjs_parser_create();
@@ -939,7 +926,6 @@ rjs_engine_t *rjs_engine_create()
        rvm_cpu_setophandler(cpu, RJS_PROPNEXT, "RJS_PROPNEXT", rjs_op_propnext);
        rvm_cpu_setophandler(cpu, RJS_PROPPREV, "RJS_PROPPREV", rjs_op_propprev);
        rvm_cpu_setophandler(cpu, RJS_STRALLOC, "RJS_STRALLOC", rjs_op_stralloc);
-       rvm_cpu_setophandler(cpu, RJS_ARRALLOC, "RJS_ARRALLOC", rjs_op_arralloc);
        rvm_cpu_setophandler(cpu, RJS_MAPALLOC, "RJS_MAPALLOC", rjs_op_mapalloc);
 
        cpu->userdata2 = rjs_opmap_create();
@@ -950,6 +936,8 @@ rjs_engine_t *rjs_engine_create()
 
 
        jse->props[RVM_DTYPE_STRING] = r_map_create(sizeof(rvmreg_t), 3);
+       tmp = rvm_reg_create_swi(rvm_cpu_swilookup_s(cpu, "rjsswitable", "string.ltrim"));
+       r_map_add_s(jse->props[RVM_DTYPE_STRING], "ltrim", &tmp);
        r_gc_add(jse->cpu->gc, (robject_t*)jse->props[RVM_DTYPE_STRING]);
 
        return jse;
index e0bedd9..e3c9ff2 100644 (file)
--- a/rjs/rjs.h
+++ b/rjs/rjs.h
@@ -53,6 +53,8 @@ typedef struct rjs_engine_s {
 #define RJS_SWI_PARAM(__cpu__, __n__) RVM_STACK_ADDR((__cpu__)->stack, RVM_CPUREG_GETU(__cpu__, FP) + (__n__))
 #define RJS_SWI_PARAMS(__cpu__) (RVM_CPUREG_GETU((__cpu__), SP) - RVM_CPUREG_GETU((__cpu__), FP))
 #define RJS_SWI_ABORT(__j__, __e__) do { rjs_engine_abort((__j__), (__e__)); return; } while (0)
+#define RJS_CPU2JSE(__cpu__) ((rjs_engine_t*)(__cpu__)->userdata1)
+
 
 #define RJS_CAST                       RVM_USER130             /* Cast: op1 = (op3)op2 */
 #define RJS_EMOV                       RVM_USER131
index ee63770..7bdb3fb 100644 (file)
 #include "rlib/rref.h"
 
 
-static void rvm_reg_array_oncopy(rarray_t *array)
-{
-       unsigned int index;
-       rvmreg_t *r;
-
-       for (index = 0; index < r_array_length(array); index++) {
-               r = (rvmreg_t *)r_array_slot(array, index);
-               rvm_reg_copy(r, r);
-       }
-}
-
-
-static void rvm_reg_array_oncleanup(rarray_t *array)
-{
-       unsigned int index;
-       rvmreg_t *r;
-
-       for (index = 0; index < r_array_length(array); index++) {
-               r = (rvmreg_t *)r_array_slot(array, index);
-               rvm_reg_cleanup(r);
-       }
-}
-
-
-static void rvm_reg_carray_oncopy(rcarray_t *array)
-{
-       unsigned int index;
-       rvmreg_t *r;
-       unsigned int len = r_carray_length(array);
-
-
-       for (index = 0; index < len; index++) {
-               r = (rvmreg_t *)r_carray_slot(array, index);
-               rvm_reg_copy(r, r);
-       }
-}
-
-
-static void rvm_reg_carray_oncleanup(rcarray_t *array)
-{
-       unsigned int index;
-       rvmreg_t *r;
-       unsigned int len = r_carray_length(array);
-
-
-       for (index = 0; index < len; index++) {
-               r = (rvmreg_t *)r_carray_slot(array, index);
-               rvm_reg_cleanup(r);
-       }
-}
-
-
 /*
  * Recursively go over array data to unref any GC managed
  * objects. We need this because when GC is destroying arrays,
@@ -151,40 +99,6 @@ void rvm_reg_array_unref_gcdata(robject_t *obj)
 }
 
 
-
-rarray_t *r_array_create_rvmreg()
-{
-       rarray_t *array = r_array_create(sizeof(rvmreg_t));
-       if (array) {
-               array->oncopy = rvm_reg_array_oncopy;
-               array->oncleanup = rvm_reg_array_oncleanup;
-       }
-       return array;
-}
-
-
-rcarray_t *r_carray_create_rvmreg()
-{
-       rcarray_t *array = r_carray_create(sizeof(rvmreg_t));
-       if (array) {
-               array->oncopy = rvm_reg_carray_oncopy;
-               array->oncleanup = rvm_reg_carray_oncleanup;
-       }
-       return array;
-}
-
-
-rharray_t *r_harray_create_rvmreg()
-{
-       rharray_t *harray = r_harray_create(sizeof(rvmreg_t));
-       if (harray) {
-               harray->members->oncopy = rvm_reg_carray_oncopy;
-               harray->members->oncleanup = rvm_reg_carray_oncleanup;
-       }
-       return harray;
-}
-
-
 rvmreg_t rvm_reg_create_string_ansi(const char *s)
 {
        rvmreg_t r;
@@ -207,26 +121,9 @@ rvmreg_t rvm_reg_create_string(const rstr_t *s)
 }
 
 
-rvmreg_t rvm_reg_create_array()
-{
-       rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
-       rvm_reg_setarray(&r, (robject_t*)r_carray_create_rvmreg());
-       return r;
-}
-
-
-rvmreg_t rvm_reg_create_harray()
-{
-       rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
-       rvm_reg_setharray(&r, (robject_t*)r_harray_create_rvmreg());
-       return r;
-}
-
-
 void rvm_reg_setstring(rvmreg_t *r, rstring_t *ptr)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETP(r, ptr);
        RVM_REG_SETTYPE(r, RVM_DTYPE_STRING);
        RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
@@ -235,48 +132,72 @@ void rvm_reg_setstring(rvmreg_t *r, rstring_t *ptr)
 
 void rvm_reg_setpair(rvmreg_t *r, ruint32 p1, ruint32 p2)
 {
-
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETPAIR(r, p1, p2);
        RVM_REG_SETTYPE(r, RVM_DTYPE_PAIR);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setstrptr(rvmreg_t *r, char *s, unsigned int size)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETSTR(r, s, size);
        RVM_REG_SETTYPE(r, RVM_DTYPE_STRPTR);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setarray(rvmreg_t *r, robject_t *ptr)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETP(r, ptr);
        RVM_REG_SETTYPE(r, RVM_DTYPE_ARRAY);
        RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
-void rvm_reg_setjsobject(rvmreg_t *r, robject_t *ptr)
+void rvm_reg_setmap(rvmreg_t *r, robject_t *ptr)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETP(r, ptr);
        RVM_REG_SETTYPE(r, RVM_DTYPE_MAP);
        RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
+void rvm_reg_setjsobject(rvmreg_t *r, robject_t *ptr)
+{
+       rvm_reg_setmap(r, ptr);
+}
+
+
 void rvm_reg_setharray(rvmreg_t *r, robject_t *ptr)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETP(r, ptr);
        RVM_REG_SETTYPE(r, RVM_DTYPE_HARRAY);
        RVM_REG_SETFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
+
+void rvm_reg_setswi(rvmreg_t *r, ruword swiid)
+{
+       r_memset(r, 0, sizeof(*r));
+       RVM_REG_SETU(r, swiid);
+       RVM_REG_SETTYPE(r, RVM_DTYPE_SWIID);
+}
+
+
+rvmreg_t rvm_reg_create_swi(ruword swiid)
+{
+       rvmreg_t r;
+       rvm_reg_setswi(&r, swiid);
+       return r;
+}
+
+
 rvmreg_t rvm_reg_create_pair(ruint32 p1, ruint32 p2)
 {
        rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
        rvm_reg_setpair(&r, p1, p2);
        return r;
 }
@@ -285,7 +206,6 @@ rvmreg_t rvm_reg_create_pair(ruint32 p1, ruint32 p2)
 rvmreg_t rvm_reg_create_strptr(char *s, unsigned int size)
 {
        rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
        rvm_reg_setstrptr(&r, s, size);
        return r;
 }
@@ -294,9 +214,7 @@ rvmreg_t rvm_reg_create_strptr(char *s, unsigned int size)
 rvmreg_t rvm_reg_create_double(double d)
 {
        rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
-       RVM_REG_SETD(&r, d);
-       RVM_REG_SETTYPE(&r, RVM_DTYPE_DOUBLE);
+       rvm_reg_setdouble(&r, d);
        return r;
 }
 
@@ -304,7 +222,6 @@ rvmreg_t rvm_reg_create_double(double d)
 rvmreg_t rvm_reg_create_signed(rword l)
 {
        rvmreg_t r;
-       r_memset(&r, 0, sizeof(r));
        rvm_reg_setsigned(&r, l);
        return r;
 }
@@ -312,9 +229,6 @@ rvmreg_t rvm_reg_create_signed(rword l)
 
 void rvm_reg_cleanup(rvmreg_t *reg)
 {
-       if (rvm_reg_tstflag(reg, RVM_INFOBIT_ROBJECT)) {
-               r_object_destroy((robject_t*)RVM_REG_GETP(reg));
-       }
        RVM_REG_CLEAR(reg);
 }
 
@@ -368,48 +282,48 @@ void rvm_reg_clrflag(rvmreg_t *r, ruint16 flag)
 
 void rvm_reg_setundef(rvmreg_t *r)
 {
-       RVM_REG_CLEAR(r);
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETTYPE(r, RVM_DTYPE_UNDEF);
 }
 
 
 void rvm_reg_setunsigned(rvmreg_t *r, ruword u)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETU(r, u);
        RVM_REG_SETTYPE(r, RVM_DTYPE_UNSIGNED);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setsigned(rvmreg_t *r, rword l)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETL(r, l);
        RVM_REG_SETTYPE(r, RVM_DTYPE_SIGNED);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setboolean(rvmreg_t *r, rboolean b)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETU(r, b ? 1 : 0);
        RVM_REG_SETTYPE(r, RVM_DTYPE_BOOLEAN);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setdouble(rvmreg_t *r, double d)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETD(r, d);
        RVM_REG_SETTYPE(r, RVM_DTYPE_DOUBLE);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
 void rvm_reg_setpointer(rvmreg_t *r, rpointer p)
 {
+       r_memset(r, 0, sizeof(*r));
        RVM_REG_SETP(r, p);
        RVM_REG_SETTYPE(r, RVM_DTYPE_POINTER);
-       RVM_REG_CLRFLAG(r, RVM_INFOBIT_ROBJECT);
 }
 
 
index c4aedd2..e660594 100644 (file)
@@ -59,6 +59,7 @@ extern "C" {
 #define RVM_DTYPE_USERDEF(__n__) (RVM_DTYPE_USER + (__n__))
 
 #define RVM_INFOBIT_ROBJECT (1 << 0)
+#define RVM_INFOBIT_GLOBAL (1 << 1)
 #define RVM_INFOBIT_LAST (1 << 15)
 #define RVM_INFOBIT_ALL (RVM_INFOBIT_ROBJECT | RVM_INFOBIT_LAST)
 
@@ -157,13 +158,7 @@ typedef struct rvmreg_s {
        rvmreg_flags_t flags;
 } rvmreg_t;
 
-/* Create array with rvmreg elements */
-rarray_t *r_array_create_rvmreg();
-/* Create harray with rvmreg elements */
-rharray_t *r_harray_create_rvmreg();
-/* Create carray with rvmreg elements */
-rcarray_t *r_carray_create_rvmreg();
-
+rvmreg_t rvm_reg_create_swi(ruword swiid);
 rvmreg_t rvm_reg_create_string_ansi(const char *s);
 rvmreg_t rvm_reg_create_string(const rstr_t *s);
 rvmreg_t rvm_reg_create_array();
@@ -172,7 +167,6 @@ rvmreg_t rvm_reg_create_double(double d);
 rvmreg_t rvm_reg_create_signed(rword l);
 rvmreg_t rvm_reg_create_pair(ruint32 p1, ruint32 p2);
 rvmreg_t rvm_reg_create_strptr(char *s, unsigned int size);
-rvmreg_t *rvm_reg_copy(rvmreg_t *dst, const rvmreg_t *src);
 rvmreg_type_t rvm_reg_gettype(const rvmreg_t *r);
 rboolean rvm_reg_tstflag(const rvmreg_t *r, ruint16 flag);
 void rvm_reg_init(rvmreg_t *reg);
@@ -193,6 +187,8 @@ void rvm_reg_setstring(rvmreg_t *r, rstring_t *ptr);
 void rvm_reg_setarray(rvmreg_t *r, robject_t *ptr);
 void rvm_reg_setharray(rvmreg_t *r, robject_t *ptr);
 void rvm_reg_setjsobject(rvmreg_t *r, robject_t *ptr);
+void rvm_reg_setmap(rvmreg_t *r, robject_t *ptr);
+void rvm_reg_setswi(rvmreg_t *r, ruword swiid);
 int rvm_reg_str2num(rvmreg_t *dst, const rvmreg_t *src);
 int rvm_reg_str2signed(rvmreg_t *dst, const rvmreg_t *ssrc);
 int rvm_reg_str2double(rvmreg_t *dst, const rvmreg_t *ssrc);