]> git.kaiwu.me - njs.git/commitdiff
Fixed function declaration with the same name as a variable.
authorhongzhidao <hongzhidao@gmail.com>
Sat, 13 Apr 2019 15:38:53 +0000 (23:38 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Sat, 13 Apr 2019 15:38:53 +0000 (23:38 +0800)
This closes #126 issue on Github.

njs/njs_variable.c
njs/test/njs_unit_test.c

index f4596baad13432a4653e2c84b53ca0d1e7ffcc2c..7eaa3597a437389197e32622a6642d33defb4d47 100644 (file)
@@ -65,6 +65,11 @@ njs_variable_add(njs_vm_t *vm, njs_parser_scope_t *scope, nxt_str_t *name,
 
     if (nxt_lvlhsh_find(&scope->variables, &lhq) == NXT_OK) {
         var = lhq.value;
+
+        if (type == NJS_VARIABLE_FUNCTION) {
+            var->type = type;
+        }
+
         return var;
     }
 
index a7aeb48ab1ff718c79d8b9cb1409a697a2ae4528..fb340ac7d072bcf1bf6280513b0723f3381cac78 100644 (file)
@@ -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") },