RPA Toolkit
Added support for script args - 2
authorMartin Stoilov <martin@rpasearch.com>
Sun, 29 May 2011 05:40:34 +0000 (22:40 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Sun, 29 May 2011 05:40:34 +0000 (22:40 -0700)
rjs/rjs.c
rjs/rjscompiler.c
rvm/rvmreg.c
rvm/rvmreg.h

index 3b2a9ff..d1e6c0b 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 = rjs_object_create(sizeof(rvmreg_t));
-       rvm_gc_add(co->cpu->gc, (robject_t*)a);
-       r_harray_add_s(a->harray, "count", &count);
+       rjs_object_t *a;
 
        v = rvm_scope_tiplookup_s(co->scope, "ARGS");
        if (!v) {
-               rvm_scope_addpointer_s(co->scope, "ARGS", rvm_cpu_alloc_global(co->cpu));
-               v = rvm_scope_tiplookup_s(co->scope, "ARGS");
+               return -1;
        }
+       a = rjs_object_create(sizeof(rvmreg_t));
+       rvm_gc_add(co->cpu->gc, (robject_t*)a);
+       r_harray_add_s(a->harray, "count", &count);
        rvm_reg_setjsobject((rvmreg_t*)v->data.ptr, (robject_t*)a);
        return 0;
 }
@@ -249,12 +249,14 @@ rvmreg_t *rjs_engine_vexec(rjs_engine_t *jse, const rchar *script, rsize_t size,
        rvmreg_t arg;
        rsize_t i = 0;
 
-       rjs_compiler_argarray_setup(jse->co);
        if (rjs_engine_compile(jse, script, size) < 0)
                return NULL;
-       for (i = 0; i < nargs; i++) {
-               arg = va_arg(args, rvmreg_t);
-               rjs_compiler_addarg(jse->co, &arg);
+       if (nargs > 0) {
+               rjs_compiler_argarray_setup(jse->co);
+               for (i = 0; i < nargs; i++) {
+                       arg = va_arg(args, rvmreg_t);
+                       rjs_compiler_addarg(jse->co, &arg);
+               }
        }
        RVM_CPUREG_SETU(jse->cpu, FP, 0);
        RVM_CPUREG_SETU(jse->cpu, SP, 0);
index d289c56..9252fb7 100644 (file)
@@ -264,6 +264,21 @@ rlong rjs_compiler_record2unaryopcode(rparecord_t *prec)
 }
 
 
+static rint rjs_compiler_internalvars_setup(rjs_compiler_t *co)
+{
+       rvm_varmap_t *v;
+
+       v = rvm_scope_tiplookup_s(co->scope, "ARGS");
+       if (!v) {
+               rvm_scope_addpointer_s(co->scope, "ARGS", rvm_cpu_alloc_global(co->cpu));
+               v = rvm_scope_tiplookup_s(co->scope, "ARGS");
+               R_ASSERT(v);
+               return -1;
+       }
+       rvm_reg_setundef((rvmreg_t*)v->data.ptr);
+
+       return 0;
+}
 
 
 rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
@@ -280,6 +295,7 @@ rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
 
        prec = (rparecord_t *)r_array_slot(records, rec);
        rjs_compiler_debughead(co, records, rec);
+       rjs_compiler_internalvars_setup(co);
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BRANCH, mainidx, rvm_asm(RVM_B, DA, XX, XX, 0));
        start = rvm_codegen_addins(co->cg, rvm_asm(RVM_NOP, XX, XX, XX, 0));
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_DEFAULT, allocsidx, rvm_asm(RVM_ADD, SP, FP, DA, 0));
index 9897f0d..6101f46 100644 (file)
@@ -347,6 +347,13 @@ void rvm_reg_clrflag(rvmreg_t *r, ruint16 flag)
 }
 
 
+void rvm_reg_setundef(rvmreg_t *r)
+{
+       RVM_REG_CLEAR(r);
+       RVM_REG_SETTYPE(r, RVM_DTYPE_UNDEF);
+}
+
+
 void rvm_reg_setunsigned(rvmreg_t *r, rword u)
 {
        RVM_REG_SETU(r, u);
index cc66cb1..80c0523 100644 (file)
@@ -160,6 +160,7 @@ ruint rvm_reg_gettype(const rvmreg_t *r);
 rboolean rvm_reg_tstflag(const rvmreg_t *r, ruint16 flag);
 void rvm_reg_setflag(rvmreg_t *r, ruint16 flag);
 void rvm_reg_clrflag(rvmreg_t *r, ruint16 flag);
+void rvm_reg_setundef(rvmreg_t *r);
 void rvm_reg_setunsigned(rvmreg_t *r, rword u);
 void rvm_reg_setboolean(rvmreg_t *r, ruint b);
 void rvm_reg_setlong(rvmreg_t *r, rlong l);