RPA Toolkit
Added support for script args.
authorMartin Stoilov <martin@rpasearch.com>
Sun, 29 May 2011 04:17:22 +0000 (21:17 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Sun, 29 May 2011 04:17:22 +0000 (21:17 -0700)
rjs/rjs.c
rjs/rjs.h
rjs/rjscompiler.c
tests/testrjs/build/linux/testrjs.mk
tests/testrjs/rjs-args.c [new file with mode: 0644]

index 1c3e44a..3b2a9ff 100644 (file)
--- a/rjs/rjs.c
+++ b/rjs/rjs.c
@@ -1,3 +1,4 @@
+#include <stdarg.h>
 #include "rmem.h"
 #include "rjsobject.h"
 #include "rjs.h"
@@ -7,6 +8,7 @@
 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 rvm_switable_t rjsswitable[] = {
                {"print", rjs_engine_print},
                {"dbgprint", rjs_engine_dbgprint},
@@ -188,6 +190,8 @@ rvmreg_t * rjs_engine_exec(rjs_engine_t *jse, const rchar *script, rsize_t size)
 {
        if (rjs_engine_compile(jse, script, size) < 0)
                return NULL;
+       RVM_CPUREG_SETU(jse->cpu, FP, 0);
+       RVM_CPUREG_SETU(jse->cpu, SP, 0);
        if (rjs_engine_run(jse) < 0)
                return NULL;
        return RVM_CPUREG_PTR(jse->cpu, R0);
@@ -200,6 +204,88 @@ rvmreg_t *rjs_engine_exec_s(rjs_engine_t *jse, const rchar *script)
 }
 
 
+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);
+
+       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");
+       }
+       rvm_reg_setjsobject((rvmreg_t*)v->data.ptr, (robject_t*)a);
+       return 0;
+}
+
+
+static rint rjs_compiler_addarg(rjs_compiler_t *co, rvmreg_t *arg)
+{
+       rvm_varmap_t *v;
+       rjs_object_t *a;
+       rvmreg_t *count;
+       rlong index;
+
+       v = rvm_scope_tiplookup_s(co->scope, "ARGS");
+       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");
+       R_ASSERT(index >= 0);
+       count = (rvmreg_t *)r_harray_slot(a->harray, index);
+       rvm_reg_setlong(count, RVM_REG_GETL(count) + 1);
+
+       return 0;
+}
+
+
+rvmreg_t *rjs_engine_vexec(rjs_engine_t *jse, const rchar *script, rsize_t size, rsize_t nargs, va_list args)
+{
+       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);
+       }
+       RVM_CPUREG_SETU(jse->cpu, FP, 0);
+       RVM_CPUREG_SETU(jse->cpu, SP, 0);
+       if (rjs_engine_run(jse) < 0)
+               return NULL;
+       return RVM_CPUREG_PTR(jse->cpu, R0);
+}
+
+
+rvmreg_t *rjs_engine_args_exec(rjs_engine_t *jse, const rchar *script, rsize_t size, rsize_t nargs, ...)
+{
+       rvmreg_t *ret;
+       va_list args;
+       va_start(args, nargs);
+       ret = rjs_engine_vexec(jse, script, size, nargs, args);
+       va_end(args);
+       return ret;
+}
+
+
+rvmreg_t *rjs_engine_args_exec_s(rjs_engine_t *jse, const rchar *script, rsize_t nargs, ...)
+{
+       rvmreg_t *ret;
+       va_list args;
+       va_start(args, nargs);
+       ret = rjs_engine_vexec(jse, script, r_strlen(script), nargs, args);
+       va_end(args);
+       return ret;
+}
+
+
 static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins)
 {
        rvmreg_t *r = (rvmreg_t *)RVM_STACK_ADDR(cpu->stack, RVM_CPUREG_GETU(cpu, FP) + 1);
index d8d46f8..466cfa1 100644 (file)
--- a/rjs/rjs.h
+++ b/rjs/rjs.h
@@ -6,6 +6,7 @@
 extern "C" {
 #endif
 
+#include <stdarg.h>
 #include "rtypes.h"
 #include "rvmcpu.h"
 #include "rjsparser.h"
@@ -45,6 +46,9 @@ rint rjs_engine_run(rjs_engine_t *jse);
 rint rjs_engine_addswitable(rjs_engine_t *jse, const rchar *tabname, rvm_switable_t *switalbe);
 rint rjs_engine_dumpast(rjs_engine_t *jse, const rchar *script, rsize_t size);
 rvmreg_t *rjs_engine_exec(rjs_engine_t *jse, const rchar *script, rsize_t size);
+rvmreg_t *rjs_engine_vexec(rjs_engine_t *jse, const rchar *script, rsize_t size, rsize_t nargs, va_list args);
+rvmreg_t *rjs_engine_args_exec(rjs_engine_t *jse, const rchar *script, rsize_t size, rsize_t nargs, ...);
+rvmreg_t *rjs_engine_args_exec_s(rjs_engine_t *jse, const rchar *script, rsize_t nargs, ...);
 rvmreg_t *rjs_engine_exec_s(rjs_engine_t *jse, const rchar *script);
 void rjs_engine_abort(rjs_engine_t *jse, rjs_error_t *error);
 rjs_engine_t *rjs_engine_get(rvmcpu_t *cpu);
index faeaef9..d289c56 100644 (file)
@@ -264,6 +264,8 @@ rlong rjs_compiler_record2unaryopcode(rparecord_t *prec)
 }
 
 
+
+
 rint rjs_compiler_rh_program(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
        rjs_coctx_global_t ctx;
index a21c50e..1f8b6fd 100644 (file)
@@ -15,6 +15,7 @@ LIBS += -lrjs -lrpa -lrvm -lrlib -lpthread -lm --static
 
 
 TESTS   += $(OUTDIR)/rjs-simple
+TESTS   += $(OUTDIR)/rjs-args
 
 
 all : $(OUTDIR) $(TESTS)
diff --git a/tests/testrjs/rjs-args.c b/tests/testrjs/rjs-args.c
new file mode 100644 (file)
index 0000000..cf811ae
--- /dev/null
@@ -0,0 +1,23 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/mman.h>
+
+#include "rstring.h"
+#include "rjs.h"
+
+
+int main(int argc, char *argv[])
+{
+       rjs_engine_t *jse = rjs_engine_create();
+       char test_script1[] = "print('ARGS[0]: ' + ARGS[0]); print('ARGS[1]: ' + ARGS[1]);print('ARGS.count: ' + ARGS.count);(ARGS[0]+ARGS[1])*3;";
+
+//     jse->debugexec = 1;
+       r_printf("result: %ld\n", rvm_reg_long(rjs_engine_args_exec_s(jse, test_script1, 2, rvm_reg_create_long(2), rvm_reg_create_long(3))));
+       rjs_engine_destroy(jse);
+       return 0;
+}