int cmd_run(FILE * f, int argc, char ** argv) { struct microjs_vm vm; struct microjs_rt rt; int32_t stack[16]; /* local stack */ uint8_t * code; int ret; if (argc < 3) return SHELL_ERR_ARG_MISSING; if (argc > 3) return SHELL_ERR_EXTRA_ARGS; DCC_LOG(LOG_TRACE, "db_js_lookup()..."); if ((code = db_js_lookup(argv[1], argv[2])) == NULL) return SHELL_ERR_ARG_INVALID; DCC_LOG1(LOG_TRACE, "code=%08x.", code); rt.data_sz = sizeof(slcdev_vm_data); rt.stack_sz = sizeof(stack); /* initialize virtual machine instance */ microjs_vm_init(&vm, &rt, NULL, slcdev_vm_data, stack); DCC_LOG(LOG_TRACE, "microjs_exec..."); if ((ret = microjs_exec(&vm, code, 4096)) != 0) { fprintf(f, "# exec error: %d\n", ret); return -1; } fprintf(f, "\n"); return 0; }
int js(FILE * f, char * script, unsigned int len) { struct symtab * symtab = (struct symtab *)slcdev_symbuf; /* symbols */ uint8_t code[512]; /* compiled code */ uint32_t sdtbuf[64]; /* compiler buffer */ int32_t stack[16]; /* exec stack */ struct microjs_sdt * microjs; struct microjs_rt * rt; struct microjs_vm vm; struct symstat symstat; uint32_t start_clk; uint32_t stop_clk; int code_sz; int ret; profclk_init(); /* initialize compiler */ if ((microjs = microjs_sdt_init(sdtbuf, sizeof(sdtbuf), symtab, &slcdev_lib)) == NULL) { fprintf(f, "# internal error\n"); return -1; } symstat = symtab_state_save(symtab); microjs_sdt_begin(microjs, code, sizeof(code)); start_clk = profclk_get(); if ((ret = microjs_compile(microjs, script, len)) < 0) { symtab_state_rollback(symtab, symstat); fprintf(f, "# compile error: %d\n", -ret); DCC_LOG1(LOG_WARNING, "compile error: %d", ret); microjs_sdt_error(f, microjs, ret); DCC_LOG(LOG_TRACE, "..."); return -1; } if ((ret = microjs_sdt_end(microjs)) < 0) { symtab_state_rollback(symtab, symstat); fprintf(f, "# compile error: %d\n", -ret); DCC_LOG1(LOG_WARNING, "compile error: %d", ret); microjs_sdt_error(f, microjs, ret); DCC_LOG(LOG_TRACE, "..."); return -1; } stop_clk = profclk_get(); code_sz = ret; rt = symtab_rt_get(symtab); if (SLCDEV_VERBOSE()) { fprintf(f, " - Compile time: %d us.\n", profclk_us(stop_clk - start_clk)); fprintf(f, " - code: %d\n", code_sz); fprintf(f, " - data: %d of %d\n", rt->data_sz, sizeof(slcdev_vm_data)); fprintf(f, " - stack: %d of %d\n", rt->stack_sz, sizeof(stack)); } if (rt->data_sz > sizeof(slcdev_vm_data)) { fprintf(f, "# data overlow. %d bytes required\n", rt->data_sz); return -1; } if (rt->stack_sz > sizeof(stack)) { fprintf(f, "# stack overflow. %d bytes required\n", rt->stack_sz); return -1; } #if MICROJS_TRACE_ENABLED microjs_vm_tracef = f; #endif /* initialize virtual machine instance */ microjs_vm_init(&vm, rt, NULL, slcdev_vm_data, stack); start_clk = profclk_get(); if ((ret = microjs_exec(&vm, code)) != 0){ fprintf(f, "# exec error: %d\n", ret); return -1; } if (SLCDEV_VERBOSE()) { stop_clk = profclk_get(); fprintf(f, "Exec time: %d us.\n", profclk_us(stop_clk - start_clk)); } return 0; }
void sim_js_exec(struct microjs_vm * vm, struct ss_device * dev, struct db_dev_model * model, uint8_t code[]) { microjs_exec(vm, code); }