summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--lib.c30
-rw-r--r--lib.h1
-rw-r--r--main.c4
-rw-r--r--vm.c39
-rw-r--r--vm.h2
5 files changed, 37 insertions, 39 deletions
diff --git a/lib.c b/lib.c
index f2b6fb1..750acfb 100644
--- a/lib.c
+++ b/lib.c
@@ -2950,33 +2950,3 @@ uc_load_stdlib(uc_value_t *scope)
{
uc_add_functions(scope, uc_stdlib_functions);
}
-
-uc_value_t *
-uc_alloc_global(uc_vm *vm)
-{
- const char *path[] = { LIB_SEARCH_PATH };
- uc_value_t *global, *arr;
- size_t i;
-
- global = ucv_object_new(vm);
-
- /* build default require() search path */
- arr = ucv_array_new(vm);
-
- for (i = 0; i < ARRAY_SIZE(path); i++)
- ucv_array_push(arr, ucv_string_new(path[i]));
-
- /* register module related constants */
- ucv_object_add(global, "REQUIRE_SEARCH_PATH", arr);
- ucv_object_add(global, "modules", ucv_object_new(vm));
-
- /* register global math constants */
- ucv_object_add(global, "NaN", ucv_double_new(NAN));
- ucv_object_add(global, "Infinity", ucv_double_new(INFINITY));
-
- /* register global property */
- ucv_object_add(global, "global", ucv_get(global));
-
-
- return global;
-}
diff --git a/lib.h b/lib.h
index 23992b9..aa805c5 100644
--- a/lib.h
+++ b/lib.h
@@ -28,7 +28,6 @@ typedef struct {
extern const uc_cfunction_list uc_stdlib_functions[];
void uc_load_stdlib(uc_value_t *scope);
-uc_value_t *uc_alloc_global(uc_vm *vm);
bool format_source_context(uc_stringbuf_t *buf, uc_source *src, size_t off, bool compact);
bool format_error_context(uc_stringbuf_t *buf, uc_source *src, uc_value_t *stacktrace, size_t off);
diff --git a/main.c b/main.c
index 5d14c83..41b608e 100644
--- a/main.c
+++ b/main.c
@@ -96,7 +96,7 @@ parse(uc_parse_config *config, uc_source *src,
}
/* allocate global scope */
- globals = uc_alloc_global(&vm);
+ globals = uc_vm_scope_get(&vm);
/* register ARGV array */
arr = ucv_array_new_length(&vm, argc);
@@ -115,7 +115,7 @@ parse(uc_parse_config *config, uc_source *src,
/* load std functions into global scope */
uc_load_stdlib(globals);
- rc = uc_vm_execute(&vm, entry, globals, modules);
+ rc = uc_vm_execute(&vm, entry, modules);
if (rc) {
rc = 1;
diff --git a/vm.c b/vm.c
index 82d25c4..94c99e6 100644
--- a/vm.c
+++ b/vm.c
@@ -100,6 +100,37 @@ uc_vm_reset_callframes(uc_vm *vm)
ucv_put(uc_vm_callframe_pop(vm));
}
+static uc_value_t *
+uc_vm_alloc_global_scope(uc_vm *vm)
+{
+ const char *path[] = { LIB_SEARCH_PATH };
+ uc_value_t *scope, *arr;
+ size_t i;
+
+ scope = ucv_object_new(vm);
+
+ /* build default require() search path */
+ arr = ucv_array_new(vm);
+
+ for (i = 0; i < ARRAY_SIZE(path); i++)
+ ucv_array_push(arr, ucv_string_new(path[i]));
+
+ /* register module related constants */
+ ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr);
+ ucv_object_add(scope, "modules", ucv_object_new(vm));
+
+ /* register scope math constants */
+ ucv_object_add(scope, "NaN", ucv_double_new(NAN));
+ ucv_object_add(scope, "Infinity", ucv_double_new(INFINITY));
+
+ /* register global property */
+ ucv_object_add(scope, "global", ucv_get(scope));
+
+ uc_vm_scope_set(vm, scope);
+
+ return scope;
+}
+
void uc_vm_init(uc_vm *vm, uc_parse_config *config)
{
char *s = getenv("TRACE");
@@ -121,6 +152,8 @@ void uc_vm_init(uc_vm *vm, uc_parse_config *config)
vm->output = stdout;
uc_vm_reset_stack(vm);
+
+ uc_vm_alloc_global_scope(vm);
}
void uc_vm_free(uc_vm *vm)
@@ -2278,15 +2311,13 @@ uc_vm_preload(uc_vm *vm, uc_value_t *modules)
}
uc_vm_status_t
-uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *modules)
+uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *modules)
{
uc_closure_t *closure = (uc_closure_t *)ucv_closure_new(vm, fn, false);
uc_callframe *frame;
uc_stringbuf_t *buf;
uc_vm_status_t rv;
- uc_vm_scope_set(vm, ucv_get(globals));
-
uc_vector_grow(&vm->callframes);
frame = &vm->callframes.entries[vm->callframes.count++];
@@ -2316,8 +2347,6 @@ 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);
- uc_vm_scope_set(vm, NULL);
-
return rv;
}
diff --git a/vm.h b/vm.h
index 66772f4..2dae69d 100644
--- a/vm.h
+++ b/vm.h
@@ -124,6 +124,6 @@ uc_exception_type_t uc_vm_call(uc_vm *vm, bool mcall, size_t nargs);
void __attribute__((format(printf, 3, 0)))
uc_vm_raise_exception(uc_vm *vm, uc_exception_type_t type, const char *fmt, ...);
-uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *globals, uc_value_t *modules);
+uc_vm_status_t uc_vm_execute(uc_vm *vm, uc_function_t *fn, uc_value_t *modules);
#endif /* __VM_H_ */