diff options
-rw-r--r-- | lib.c | 17 | ||||
-rw-r--r-- | vm.c | 19 | ||||
-rw-r--r-- | vm.h | 3 |
3 files changed, 28 insertions, 11 deletions
@@ -1565,12 +1565,15 @@ uc_require_ucode(uc_vm *vm, const char *path, uc_value_t *scope, uc_value_t **re uc_vm_stack_push(vm, closure); - prev_scope = vm->globals; - vm->globals = scope ? scope : prev_scope; + if (scope) { + prev_scope = ucv_get(uc_vm_scope_get(vm)); + uc_vm_scope_set(vm, ucv_get(scope)); + } extype = uc_vm_call(vm, false, 0); - vm->globals = prev_scope; + if (scope) + uc_vm_scope_set(vm, prev_scope); if (extype == EXCEPTION_NONE) *res = uc_vm_stack_pop(vm); @@ -1588,7 +1591,7 @@ uc_require_path(uc_vm *vm, const char *path_template, const char *name, uc_value uc_value_t *modtable; bool rv; - modtable = ucv_property_get(vm->globals, "modules"); + modtable = ucv_property_get(uc_vm_scope_get(vm), "modules"); *res = ucv_object_get(modtable, name, &rv); if (rv) @@ -1646,7 +1649,7 @@ uc_require(uc_vm *vm, size_t nargs) return NULL; name = ucv_string_get(val); - search = ucv_property_get(vm->globals, "REQUIRE_SEARCH_PATH"); + search = ucv_property_get(uc_vm_scope_get(vm), "REQUIRE_SEARCH_PATH"); if (ucv_type(search) != UC_ARRAY) { uc_vm_raise_exception(vm, EXCEPTION_RUNTIME, @@ -2176,10 +2179,10 @@ uc_include(uc_vm *vm, size_t nargs) ucv_object_foreach(scope, key, val) ucv_object_add(sc, key, ucv_get(val)); - ucv_prototype_set(sc, ucv_get(vm->globals)); + ucv_prototype_set(sc, ucv_get(uc_vm_scope_get(vm))); } else { - sc = ucv_get(vm->globals); + sc = ucv_get(uc_vm_scope_get(vm)); } if (uc_require_ucode(vm, p, sc, &rv)) @@ -2285,8 +2285,7 @@ uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *mod uc_stringbuf_t *buf; uc_vm_status_t rv; - vm->globals = globals; - ucv_get(globals); + uc_vm_scope_set(vm, ucv_get(globals)); uc_vector_grow(&vm->callframes); @@ -2317,8 +2316,7 @@ uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *mod else rv = uc_vm_execute_chunk(vm); - ucv_put(vm->globals); - vm->globals = NULL; + uc_vm_scope_set(vm, NULL); return rv; } @@ -2336,3 +2334,16 @@ uc_vm_call(uc_vm *vm, bool mcall, size_t nargs) return vm->exception.type; } + +uc_value_t * +uc_vm_scope_get(uc_vm *vm) +{ + return vm->globals; +} + +void +uc_vm_scope_set(uc_vm *vm, uc_value_t *ctx) +{ + ucv_put(vm->globals); + vm->globals = ctx; +} @@ -112,6 +112,9 @@ extern uint32_t insns[__I_MAX]; void uc_vm_init(uc_vm *vm, uc_parse_config *config); void uc_vm_free(uc_vm *vm); +uc_value_t *uc_vm_scope_get(uc_vm *vm); +void uc_vm_scope_set(uc_vm *vm, uc_value_t *ctx); + void uc_vm_stack_push(uc_vm *vm, uc_value_t *value); uc_value_t *uc_vm_stack_pop(uc_vm *vm); uc_value_t *uc_vm_stack_peek(uc_vm *vm, size_t offset); |