[njs] Fixed function redeclaration in CLI when interactive mode is on.

Dmitry Volyntsev xeioex at nginx.com
Tue Dec 7 13:15:49 UTC 2021


details:   https://hg.nginx.org/njs/rev/44f6a4ad4464
branches:  
changeset: 1759:44f6a4ad4464
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Dec 07 13:15:19 2021 +0000
description:
Fixed function redeclaration in CLI when interactive mode is on.

The issue was introduced in 0a2a0b5a74f4 (0.6.0).

diffstat:

 src/njs_generator.c      |   6 +-----
 src/njs_parser.c         |   2 +-
 src/njs_variable.h       |  11 +++++++++++
 src/test/njs_unit_test.c |   8 ++++++++
 4 files changed, 21 insertions(+), 6 deletions(-)

diffs (67 lines):

diff -r ed1756875eb5 -r 44f6a4ad4464 src/njs_generator.c
--- a/src/njs_generator.c	Fri Dec 03 13:55:22 2021 +0000
+++ b/src/njs_generator.c	Tue Dec 07 13:15:19 2021 +0000
@@ -3583,11 +3583,7 @@ njs_generate_function_declaration(njs_vm
         return njs_generator_stack_pop(vm, generator, NULL);
     }
 
-    if (njs_is_function(&var->value)) {
-        lambda = njs_function(&var->value)->u.lambda;
-    } else {
-        lambda = var->value.data.u.lambda;
-    }
+    lambda = njs_variable_lambda(var);
 
     lex_entry = njs_lexer_entry(node->u.reference.unique_id);
     if (njs_slow_path(lex_entry == NULL)) {
diff -r ed1756875eb5 -r 44f6a4ad4464 src/njs_parser.c
--- a/src/njs_parser.c	Fri Dec 03 13:55:22 2021 +0000
+++ b/src/njs_parser.c	Tue Dec 07 13:15:19 2021 +0000
@@ -6747,7 +6747,7 @@ njs_parser_function_declaration(njs_pars
         return NJS_ERROR;
     }
 
-    node->u.value.data.u.lambda = var->value.data.u.lambda;
+    node->u.value.data.u.lambda = njs_variable_lambda(var);
 
     node->left = (njs_parser_node_t *) unique_id;
 
diff -r ed1756875eb5 -r 44f6a4ad4464 src/njs_variable.h
--- a/src/njs_variable.h	Fri Dec 03 13:55:22 2021 +0000
+++ b/src/njs_variable.h	Tue Dec 07 13:15:19 2021 +0000
@@ -93,6 +93,17 @@ njs_variable_node_alloc(njs_vm_t *vm, nj
     return node;
 }
 
+njs_inline njs_function_lambda_t *
+njs_variable_lambda(njs_variable_t * var)
+{
+    if (njs_is_function(&var->value)) {
+        /* may be set by generator in njs_generate_function_declaration(). */
+        return njs_function(&var->value)->u.lambda;
+    }
+
+    return var->value.data.u.lambda;
+}
+
 
 njs_inline void
 njs_variable_node_free(njs_vm_t *vm, njs_variable_node_t *node)
diff -r ed1756875eb5 -r 44f6a4ad4464 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Fri Dec 03 13:55:22 2021 +0000
+++ b/src/test/njs_unit_test.c	Tue Dec 07 13:15:19 2021 +0000
@@ -21408,6 +21408,14 @@ static njs_unit_test_t  njs_shell_test[]
               "Number.prototype.test" ENTER),
       njs_str("test") },
 
+    { njs_str("function f(a) {return a}" ENTER
+              "function f(a) {return a}; f(2)" ENTER),
+      njs_str("2") },
+
+    { njs_str("function f() {return 1}" ENTER
+              "function f(a) {return 1}; f(2)" ENTER),
+      njs_str("1") },
+
     { njs_str("try {(new Function('function foo(){return 1}; ()=>{}breakhere'))} catch (e) {}" ENTER
               "foo()" ENTER),
       njs_str("ReferenceError: \"foo\" is not defined\n"


More information about the nginx-devel mailing list