From 2fe67dc2e3922da3a2cd22dc216b94bd2ce1366a Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Mon, 14 Feb 2022 14:10:47 +0000 Subject: [PATCH] Fixed Function constructor in CLI. Previously, Function constructor exported its local variables to vm->variables_hash. vm->variables_hash is used in njs CLI to query global variables during console input completion. The exporting is incorrect because it pollutes the global scope. --- src/njs_function.c | 11 ++--------- src/njs_variable.c | 29 ----------------------------- src/njs_variable.h | 2 -- 3 files changed, 2 insertions(+), 40 deletions(-) diff --git a/src/njs_function.c b/src/njs_function.c index 5f07ddd9..b891c5f1 100644 --- a/src/njs_function.c +++ b/src/njs_function.c @@ -1109,7 +1109,6 @@ njs_function_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_function_t *function; njs_generator_t generator; njs_parser_node_t *node; - njs_parser_scope_t *scope; njs_function_lambda_t *lambda; const njs_token_type_t *type; @@ -1215,20 +1214,14 @@ njs_function_constructor(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, } } - scope = parser.scope; - - ret = njs_variables_copy(vm, &scope->variables, vm->variables_hash); - if (njs_slow_path(ret != NJS_OK)) { - return ret; - } - ret = njs_generator_init(&generator, 0, 1); if (njs_slow_path(ret != NJS_OK)) { njs_internal_error(vm, "njs_generator_init() failed"); return NJS_ERROR; } - code = njs_generate_scope(vm, &generator, scope, &njs_entry_anonymous); + code = njs_generate_scope(vm, &generator, parser.scope, + &njs_entry_anonymous); if (njs_slow_path(code == NULL)) { if (!njs_is_error(&vm->retval)) { njs_internal_error(vm, "njs_generate_scope() failed"); diff --git a/src/njs_variable.c b/src/njs_variable.c index 481b0c4e..d3a3fd81 100644 --- a/src/njs_variable.c +++ b/src/njs_variable.c @@ -106,35 +106,6 @@ njs_variable_scope_function_add(njs_parser_t *parser, njs_parser_scope_t *scope) } - -njs_int_t -njs_variables_copy(njs_vm_t *vm, njs_rbtree_t *variables, - njs_rbtree_t *prev_variables) -{ - njs_rbtree_node_t *node; - njs_variable_node_t *var_node; - - node = njs_rbtree_min(prev_variables); - - while (njs_rbtree_is_there_successor(prev_variables, node)) { - var_node = (njs_variable_node_t *) node; - - var_node = njs_variable_node_alloc(vm, var_node->variable, - var_node->key); - if (njs_slow_path(var_node == NULL)) { - njs_memory_error(vm); - return NJS_ERROR; - } - - njs_rbtree_insert(variables, &var_node->node); - - node = njs_rbtree_node_successor(prev_variables, node); - } - - return NJS_OK; -} - - static njs_parser_scope_t * njs_variable_scope(njs_parser_scope_t *scope, uintptr_t unique_id, njs_variable_t **retvar, njs_variable_type_t type) diff --git a/src/njs_variable.h b/src/njs_variable.h index db309f7d..dfcc99f3 100644 --- a/src/njs_variable.h +++ b/src/njs_variable.h @@ -63,8 +63,6 @@ njs_variable_t *njs_variable_add(njs_parser_t *parser, njs_parser_scope_t *scope, uintptr_t unique_id, njs_variable_type_t type); njs_variable_t *njs_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope, uintptr_t unique_id, njs_variable_type_t type); -njs_int_t njs_variables_copy(njs_vm_t *vm, njs_rbtree_t *variables, - njs_rbtree_t *prev_variables); njs_variable_t * njs_label_add(njs_vm_t *vm, njs_parser_scope_t *scope, uintptr_t unique_id); njs_variable_t *njs_label_find(njs_vm_t *vm, njs_parser_scope_t *scope, -- 2.47.3