diff options
author | Jo-Philipp Wich <jo@mein.io> | 2021-05-25 19:14:20 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2021-05-25 21:02:40 +0200 |
commit | d81bad7683165df8b9e3a8f8edf253129ce6f4b2 (patch) | |
tree | 66e1df3c8a40f4bbaa1220b0cb5d26972fbb3bbd | |
parent | c4f4b38a84ac2f762f9a3acfb2f22d6b0d971899 (diff) |
main, lib: move allocation of globals object into lib function
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
-rw-r--r-- | lib.c | 23 | ||||
-rw-r--r-- | lib.h | 1 | ||||
-rw-r--r-- | main.c | 41 |
3 files changed, 35 insertions, 30 deletions
@@ -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; +} @@ -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); @@ -55,28 +55,6 @@ print_usage(const char *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) { char *name = strdup(key); @@ -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) { |