]> git.kaiwu.me - njs.git/commitdiff
Fixed Function constructor in CLI.
authorDmitry Volyntsev <xeioex@nginx.com>
Mon, 14 Feb 2022 14:10:47 +0000 (14:10 +0000)
committerDmitry Volyntsev <xeioex@nginx.com>
Mon, 14 Feb 2022 14:10:47 +0000 (14:10 +0000)
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
src/njs_variable.c
src/njs_variable.h

index 5f07ddd9caaab808532455a965d836b53aa99c48..b891c5f1d95b0664427f16252b97c8ef1609df75 100644 (file)
@@ -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");
index 481b0c4ec7ffd28a646783faa60aa11114899e84..d3a3fd813b28d29bf96a88c7413bb357044177d4 100644 (file)
@@ -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)
index db309f7d565346e820c1ec9b01298f6629ba4797..dfcc99f34f9e3290a46caa597951cdd490b04c50 100644 (file)
@@ -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,