]> git.kaiwu.me - njs.git/commitdiff
Variables may be accessed incorrectly by nested functions.
authorIgor Sysoev <igor@sysoev.ru>
Sun, 2 Apr 2017 09:36:05 +0000 (12:36 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Sun, 2 Apr 2017 09:36:05 +0000 (12:36 +0300)
njs/njs_parser.c
njs/test/njs_unit_test.c

index c2eca2b56764e6432f20d42fbe9d201b2f60cb0b..7fee2a46dffa0e2ba1fb2a0e9261f080ce4d22ec 100644 (file)
@@ -215,6 +215,11 @@ njs_parser_scope_begin(njs_vm_t *vm, njs_parser_t *parser, njs_scope_t type)
 
     if (parent != NULL) {
         nxt_queue_insert_tail(&parent->nested, &scope->link);
+
+        if (nesting == 0) {
+            /* Inherit function nesting in blocks. */
+            scope->nesting = parent->nesting;
+        }
     }
 
     return NXT_OK;
index 36aa855b4142c571141147fc5c5ab0eecf7b33d7..7891ea5af1e5908bfa1cbf876fc401a99f321862 100644 (file)
@@ -4221,6 +4221,11 @@ static njs_unit_test_t  njs_test[] =
                  "var y = f(); y()"),
       nxt_string("4") },
 
+    { nxt_string("function f() { var x = 4;"
+                 "function g() { if (1) { return 2 + x; } }; return g }"
+                 "var y = f(); y()"),
+      nxt_string("6") },
+
     /* Recursive fibonacci. */
 
     { nxt_string("function fibo(n) {"