[njs] Fixed handling of empty block statements.

Dmitry Volyntsev xeioex at nginx.com
Mon May 20 16:07:00 UTC 2019


details:   https://hg.nginx.org/njs/rev/14ec3b2096af
branches:  
changeset: 971:14ec3b2096af
user:      hongzhidao <hongzhidao at gmail.com>
date:      Mon May 20 23:14:29 2019 +0800
description:
Fixed handling of empty block statements.

This closes #165 issue on Github.

diffstat:

 njs/njs_generator.c      |   2 +-
 njs/njs_parser.c         |  18 +++++++-----------
 njs/test/njs_unit_test.c |   3 +++
 3 files changed, 11 insertions(+), 12 deletions(-)

diffs (55 lines):

diff -r 88263426432d -r 14ec3b2096af njs/njs_generator.c
--- a/njs/njs_generator.c	Fri May 17 17:01:10 2019 +0300
+++ b/njs/njs_generator.c	Mon May 20 23:14:29 2019 +0800
@@ -1558,7 +1558,7 @@ njs_generate_block_statement(njs_vm_t *v
         return ret;
     }
 
-    ret = njs_generate_statement(vm, generator, node->left);
+    ret = njs_generate_statement(vm, generator, node);
     if (nxt_slow_path(ret != NXT_OK)) {
         return ret;
     }
diff -r 88263426432d -r 14ec3b2096af njs/njs_parser.c
--- a/njs/njs_parser.c	Fri May 17 17:01:10 2019 +0300
+++ b/njs/njs_parser.c	Mon May 20 23:14:29 2019 +0800
@@ -468,19 +468,15 @@ njs_parser_block_statement(njs_vm_t *vm,
         }
     }
 
-    if (parser->node != NULL) {
-        /* The statement is not empty block or just semicolon. */
-
-        node = njs_parser_node_new(vm, parser, NJS_TOKEN_BLOCK);
-        if (nxt_slow_path(node == NULL)) {
-            return NJS_TOKEN_ERROR;
-        }
-
-        node->left = parser->node;
-        node->right = NULL;
-        parser->node = node;
+    node = njs_parser_node_new(vm, parser, NJS_TOKEN_BLOCK);
+    if (nxt_slow_path(node == NULL)) {
+        return NJS_TOKEN_ERROR;
     }
 
+    node->left = parser->node;
+    node->right = NULL;
+    parser->node = node;
+
     njs_parser_scope_end(vm, parser);
 
     return njs_parser_token(vm, parser);
diff -r 88263426432d -r 14ec3b2096af njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri May 17 17:01:10 2019 +0300
+++ b/njs/test/njs_unit_test.c	Mon May 20 23:14:29 2019 +0800
@@ -6016,6 +6016,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("function f() { return 1; } { function f() { return 2; } { function f() { return 3; } }} f()"),
       nxt_string("1") },
 
+    { nxt_string("{function f() {} {} f() }"),
+      nxt_string("undefined") },
+
     { nxt_string("{ var f; function f() {} }"),
       nxt_string("SyntaxError: \"f\" has already been declared in 1") },
 


More information about the nginx-devel mailing list