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; }
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; }