int js_vm_switch0_exec (JSVirtualMachine *vm, JSByteCode *bc, JSSymtabEntry *symtab, unsigned int num_symtab_entries, unsigned int consts_offset, unsigned int anonymous_function_offset, /*unsigned char *debug_info, unsigned int debug_info_len,*/ JSNode *object, JSNode *func, unsigned int argc, JSNode *argv) { int i; unsigned int ui; Function *global_f = NULL; Function *f; unsigned char *code = NULL; char buf[512]; if (bc) { /* Executing byte-code. */ /* Find the code section. */ for (i = 0; i < bc->num_sects; i++) if (bc->sects[i].type == JS_BCST_CODE) code = bc->sects[i].data; assert (code != NULL); /* Enter all functions to the known functions of the VM. */ for (i = 0; i < num_symtab_entries; i++) { /* Need one function. */ f = js_vm_alloc_destroyable (vm, sizeof (*f)); f->destroy = function_destroy; f->name = js_strdup (vm, symtab[i].name); f->length = symtab[i + 1].offset - symtab[i].offset + 1; f->code = js_malloc (vm, f->length); memcpy (f->code, code + symtab[i].offset, f->length - 1); f->code[f->length - 1] = 1; /* op `done' */ /* Link the code to our environment. */ link_code (vm, f->code, f->length, consts_offset); if (strcmp (symtab[i].name, JS_GLOBAL_NAME) == 0) global_f = f; else { int is_anonymous = 0; /* Check for the anonymous function. */ if (symtab[i].name[0] == '.' && symtab[i].name[1] == 'F' && symtab[i].name[2] == ':') is_anonymous = 1; if (vm->verbose > 3) { sprintf_P (buf, vmswt0_string_0, symtab[i].name, symtab[i].offset, symtab[i + 1].offset - symtab[i].offset); if (is_anonymous) sprintf_P (buf + strlen (buf), vmswt0_string_1, anonymous_function_offset); strcat (buf, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } if (is_anonymous) { sprintf (buf, ".F:%u", (unsigned int) atoi (symtab[i].name + 3) + anonymous_function_offset); ui = js_vm_intern (vm, buf); } else ui = js_vm_intern (vm, symtab[i].name); vm->globals[ui].type = JS_FUNC; vm->globals[ui].u.vfunction = js_vm_make_function (vm, f); } } } else { /* Applying arguments to function. */ if (func->type != JS_FUNC) { sprintf_P (vm->error, vmswt0_string_2); return 0; } if (vm->verbose > 1) { sprintf_P (buf, vmswt0_string_3, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } f = func->u.vfunction->implementation; execute_code (vm, object, f, argc, argv); } if (global_f) { if (vm->verbose > 1) { sprintf_P (buf, vmswt0_string_4, global_f->name, JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); } /* Execute. */ execute_code (vm, NULL, global_f, 0, NULL); } return 1; }
static void execute_code (JSVirtualMachine *vm, Function *f, unsigned int argc, JSNode *argv) { JSNode *sp; JSNode *fp; JSNode *function; JSNode builtin_result; Compiled *pc; JSInt32 i, j; JSInt8 i8; /* Create the initial stack frame by hand. */ sp = vm->sp; /* Protect the function from gc. */ JS_SP0->type = JS_FUNC; JS_SP0->u.vfunction = js_vm_make_function (vm, f); JS_PUSH (); /* Push arguments to the stack. */ i = argc; for (i--; i >= 0; i--) { JS_COPY (JS_SP0, &argv[i]); JS_PUSH (); } /* Empty this pointer. */ JS_SP0->type = JS_NULL; JS_PUSH (); /* Init fp and pc so our SUBROUTINE_CALL will work. */ fp = NULL; pc = NULL; JS_SUBROUTINE_CALL (f); /* Ok, now we are ready to run. */ while (1) { switch ((pc++)->u.op) { /* include eswitch.h */ #include "eswitch.h" /* end include eswitch.h */ default: fprintf (stderr, "execute_code: unknown opcode %d\n", (pc - 1)->u.op); exit (1); break; } } done: /* All done. */ JS_COPY (&vm->exec_result, JS_SP1); }
static void execute_code (JSVirtualMachine *vm, JSNode *object, Function *f, unsigned int argc, JSNode *argv) { JSNode *sp; JSNode *fp; JSNode *function; JSNode builtin_result; unsigned char *pc; JSInt32 i, j; JSInt8 i8; char buf[512]; /* Create the initial stack frame by hand. */ sp = vm->sp; /* Protect the function from gc. */ JS_SP0->type = JS_FUNC; JS_SP0->u.vfunction = js_vm_make_function (vm, f); JS_PUSH (); /* Push arguments to the stack. */ i = argc; for (i--; i >= 0; i--) { JS_COPY (JS_SP0, &argv[i]); JS_PUSH (); } /* This pointer. */ if (object) JS_COPY (JS_SP0, object); else JS_SP0->type = JS_NULL; JS_PUSH (); /* Init fp and pc so our SUBROUTINE_CALL will work. */ fp = NULL; pc = NULL; JS_SUBROUTINE_CALL (f); /* Ok, now we are ready to run. */ while (1) { switch (*pc++) { /* include eswt0.h */ #include "eswt0.h" /* end include eswt0.h */ default: sprintf_P (buf, vmswt0_string_5, *(pc - 1), JS_HOST_LINE_BREAK); //js_iostream_write (vm->s_stderr, buf, strlen (buf)); //js_iostream_flush (vm->s_stderr); abort (); break; } } done: /* All done. */ JS_COPY (&vm->exec_result, JS_SP1); }
int js_vm_switch_exec (JSVirtualMachine *vm, JSByteCode *bc, JSSymtabEntry *symtab, unsigned int num_symtab_entries, unsigned int consts_offset, unsigned char *debug_info, unsigned int debug_info_len, char *func_name, JSNode *func, unsigned int argc, JSNode *argv) { int i; unsigned int ui; Function *global_f = NULL; Function *f; unsigned char *code = NULL; if (bc) { /* Executing byte-code. */ /* Find the code section. */ for (i = 0; i < bc->num_sects; i++) if (bc->sects[i].type == JS_BCST_CODE) code = bc->sects[i].data; assert (code != NULL); /* Enter all functions to the known functions of the VM. */ for (i = 0; i < num_symtab_entries; i++) { /* Link the code to our environment. */ f = link_code (vm, code + symtab[i].offset, symtab[i + 1].offset - symtab[i].offset, consts_offset); f->name = js_strdup (vm, symtab[i].name); if (strcmp (symtab[i].name, JS_GLOBAL_NAME) == 0) global_f = f; else { if (vm->verbose > 3) printf ("VM: link: %s(): start=%d, length=%d\n", symtab[i].name, symtab[i].offset, symtab[i + 1].offset - symtab[i].offset); ui = js_vm_intern (vm, symtab[i].name); vm->globals[ui].type = JS_FUNC; vm->globals[ui].u.vfunction = js_vm_make_function (vm, f); } } } else { JSNode *n; /* Applying arguments to function. */ if (func_name) { /* Lookup function. */ JSSymbol sym; sym = js_vm_intern (vm, func_name); n = &vm->globals[sym]; if (n->type != JS_FUNC) { sprintf (vm->error, "undefined function `%s' in apply", func_name); return 0; } if (vm->verbose > 1) printf ("VM: calling %s()\n", func_name); } else { /* Use the function the caller gave us. */ n = func; if (n->type != JS_FUNC) { sprintf (vm->error, "illegal function in apply"); return 0; } if (vm->verbose > 1) printf ("VM: calling function\n"); } f = n->u.vfunction->implementation; execute_code (vm, f, argc, argv); } if (global_f) { if (vm->verbose > 1) printf ("VM: exec: %s\n", global_f->name); /* Execute. */ execute_code (vm, global_f, 0, NULL); } return 1; }