JSInterpPtr js_create_interp (JSInterpOptions *options) { JSInterpPtr interp; JSByteCode *bc; JSInterpOptions default_options; interp = js_calloc (NULL, 1, sizeof (*interp)); if (interp == NULL) return NULL; if (options == NULL) { js_init_default_options (&default_options); options = &default_options; } memcpy (&interp->options, options, sizeof (*options)); /* Create virtual machine. */ interp->vm = js_vm_create (options->stack_size, options->dispatch_method, options->verbose, options->stacktrace_on_error, options->s_stdin, options->s_stdout, options->s_stderr); if (interp->vm == NULL) { js_free (interp); return NULL; } if (!options->no_compiler) { /* Define compiler to the virtual machine. */ bc = js_bc_read_data (js_compiler_bytecode, js_compiler_bytecode_len); js_vm_execute (interp->vm, bc); js_bc_free (bc); } /* Initialize our extensions. */ if (!js_define_module (interp, js_core_globals)) { js_vm_destroy (interp->vm); js_free (interp); return NULL; } /* Ok, we'r done. */ return interp; }
int main(){ JSVirtualMachine *vm; JSByteCode *bc = NULL; beginSerial(9600); vm = js_vm_create(128, 0, 0); //bc = read_eeprom_bytecode(0x00); //bc = js_bc_read_data(_bytecode, _bytecode_size); //init_builtin_digitalio(vm); //int s = js_vm_execute(vm, bc); //js_bc_free(bc); js_vm_destroy(vm); }
void js_destroy_interp (JSInterpPtr interp) { js_vm_destroy (interp->vm); js_free (interp); }
JSVirtualMachine *js_vm_create( unsigned int stack_size #ifdef JS_RUNTIME_WARNING , unsigned int verbose #endif #ifdef JS_RUNTIME_DEBUG , int stacktrace_on_error #endif #ifdef JS_IOSTREAM , JSIOStream * s_stdin, JSIOStream * s_stdout, JSIOStream * s_stderr #endif ) { JSVirtualMachine *vm; vm = js_calloc(NULL, 1, sizeof(*vm)); if (vm == NULL) return NULL; #ifdef JS_RUNTIME_WARNING vm->verbose = verbose; vm->warn_undef = 1; #endif #ifdef JS_RUNTIME_DEBUG vm->stacktrace_on_error = stacktrace_on_error; #endif /* Set the system streams. */ #ifdef JS_IOSTREAM vm->s_stdin = s_stdin; vm->s_stdout = s_stdout; vm->s_stderr = s_stderr; #endif /* Resolve the dispatch method. */ vm->dispatch_execute = js_vm_switch0_exec; #ifdef JS_RUNTIME_DEBUG vm->dispatch_func_name = js_vm_switch0_func_name; vm->dispatch_debug_position = js_vm_switch0_debug_position; #endif vm->stack_size = stack_size; vm->stack = js_malloc(NULL, vm->stack_size * sizeof(*vm->stack)); if (vm->stack == NULL) { js_free(vm); return NULL; } /* Set the initial stack pointer. */ vm->sp = vm->stack + vm->stack_size - 1; vm->gc.trigger = GC_TRIGGER; /* We need a toplevel here. */ { JSErrorHandlerFrame handler; int result = 1; memset(&handler, 0, sizeof(handler)); handler.next = vm->error_handler; vm->error_handler = &handler; if (setjmp(vm->error_handler->error_jmp)) /* An error occurred. */ result = 0; else { /* Intern some commonly used symbols. */ vm->syms.s___proto__ = js_vm_intern(vm, "__proto__"); vm->syms.s_prototype = js_vm_intern(vm, "prototype"); vm->syms.s_toSource = js_vm_intern(vm, "toSource"); vm->syms.s_toString = js_vm_intern(vm, "toString"); vm->syms.s_valueOf = js_vm_intern(vm, "valueOf"); /* Intern system built-in objects. */ intern_builtins(vm); } /* Pop the error handler. */ vm->error_handler = vm->error_handler->next; if (result == 0) { /* Argh, the initialization failed. */ js_vm_destroy(vm); return NULL; } } return vm; }