From d81bad7683165df8b9e3a8f8edf253129ce6f4b2 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 25 May 2021 19:14:20 +0200 Subject: main, lib: move allocation of globals object into lib function Signed-off-by: Jo-Philipp Wich --- lib.c | 23 +++++++++++++++++++++++ lib.h | 1 + main.c | 41 +++++++++++------------------------------ 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/lib.c b/lib.c index c3a1599..466d56a 100644 --- a/lib.c +++ b/lib.c @@ -2583,3 +2583,26 @@ uc_lib_init(uc_value_t *scope) { uc_add_proto_functions(scope, 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])); + + ucv_object_add(global, "REQUIRE_SEARCH_PATH", arr); + + /* register global property */ + ucv_object_add(global, "global", ucv_get(global)); + + return global; +} diff --git a/lib.h b/lib.h index aad9a9d..e243272 100644 --- a/lib.h +++ b/lib.h @@ -26,6 +26,7 @@ typedef struct { } uc_cfunction_list; void uc_lib_init(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 10db64a..078de6c 100644 --- a/main.c +++ b/main.c @@ -54,28 +54,6 @@ print_usage(const char *app) basename(app)); } -static void -globals_init(uc_vm *vm, uc_value_t *scope, int argc, char **argv) -{ - const char *path[] = { LIB_SEARCH_PATH }; - uc_value_t *arr; - size_t i; - - arr = ucv_array_new(vm); - - for (i = 0; i < sizeof(path) / sizeof(path[0]); i++) - ucv_array_push(arr, ucv_string_new(path[i])); - - ucv_object_add(scope, "REQUIRE_SEARCH_PATH", arr); - - arr = ucv_array_new(vm); - - for (i = 0; i < (size_t)argc; i++) - ucv_array_push(arr, ucv_string_new(argv[i])); - - ucv_object_add(scope, "ARGV", arr); -} - static void register_variable(uc_value_t *scope, const char *key, uc_value_t *val) { @@ -99,10 +77,10 @@ parse(uc_parse_config *config, uc_source *src, uc_value_t *env, uc_value_t *modules, int argc, char **argv) { - uc_value_t *globals = NULL; + uc_value_t *globals = NULL, *arr; uc_function_t *entry; uc_vm vm = { 0 }; - int rc = 0; + int i, rc = 0; char *err; uc_vm_init(&vm, config); @@ -116,10 +94,16 @@ parse(uc_parse_config *config, uc_source *src, goto out; } - globals = ucv_object_new(&vm); + /* allocate global scope */ + globals = uc_alloc_global(&vm); - /* load global variables */ - globals_init(&vm, globals, argc, argv); + /* register ARGV array */ + arr = ucv_array_new_length(&vm, argc); + + for (i = 0; i < argc; i++) + ucv_array_push(arr, ucv_string_new(argv[i])); + + ucv_object_add(globals, "ARGV", arr); /* load env variables */ if (env) { @@ -130,9 +114,6 @@ parse(uc_parse_config *config, uc_source *src, /* load std functions into global scope */ uc_lib_init(globals); - /* create instance of global scope, set "global" property on it */ - ucv_object_add(globals, "global", ucv_get(globals)); - rc = uc_vm_execute(&vm, entry, globals, modules); if (rc) { -- cgit v1.2.3