[njs] Fixed function declaration with the same name as a variable.

Dmitry Volyntsev xeioex at nginx.com
Mon Apr 15 13:37:49 UTC 2019


details:   https://hg.nginx.org/njs/rev/d3743d248ade
branches:  
changeset: 892:d3743d248ade
user:      hongzhidao <hongzhidao at gmail.com>
date:      Sat Apr 13 23:38:53 2019 +0800
description:
Fixed function declaration with the same name as a variable.

This closes #126 issue on Github.

diffstat:

 njs/njs_variable.c       |   5 +++++
 njs/test/njs_unit_test.c |  10 ++++++++++
 2 files changed, 15 insertions(+), 0 deletions(-)

diffs (35 lines):

diff -r ace6f73dff8d -r d3743d248ade njs/njs_variable.c
--- a/njs/njs_variable.c	Sat Apr 13 01:11:49 2019 +0800
+++ b/njs/njs_variable.c	Sat Apr 13 23:38:53 2019 +0800
@@ -65,6 +65,11 @@ njs_variable_add(njs_vm_t *vm, njs_parse
 
     if (nxt_lvlhsh_find(&scope->variables, &lhq) == NXT_OK) {
         var = lhq.value;
+
+        if (type == NJS_VARIABLE_FUNCTION) {
+            var->type = type;
+        }
+
         return var;
     }
 
diff -r ace6f73dff8d -r d3743d248ade njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Sat Apr 13 01:11:49 2019 +0800
+++ b/njs/test/njs_unit_test.c	Sat Apr 13 23:38:53 2019 +0800
@@ -6413,6 +6413,16 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("function f() { var a = 1; function baz() { return a; } return baz; } f().bind()()"),
       nxt_string("1") },
 
+    { nxt_string("function f() { var t = 1; function baz() { return t; } return baz; }"
+                 "f().bind()();"),
+      nxt_string("1") },
+
+    { nxt_string("(function(a) { var s = typeof g, q = g; var g = 1; s += typeof g; function g(b) { return a + b }; return q; })(1)(2)"),
+      nxt_string("3")},
+
+    { nxt_string("(function(a) { var g = f; var f = 1; function f() { return a; } return g; })(42)()"),
+      nxt_string("42") },
+
     { nxt_string("function f(a, b) { return a + b }"
                  "f(3,4) === f.bind()(3,4)"),
       nxt_string("true") },


More information about the nginx-devel mailing list