[njs] Fixed parsing object literals.

Dmitry Volyntsev xeioex at nginx.com
Fri Feb 1 15:26:48 UTC 2019


details:   https://hg.nginx.org/njs/rev/94fc00372558
branches:  
changeset: 750:94fc00372558
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Feb 01 18:09:02 2019 +0300
description:
Fixed parsing object literals.

diffstat:

 njs/njs_parser.c         |  15 ++++++++++-----
 njs/test/njs_unit_test.c |  12 ++++++++++++
 2 files changed, 22 insertions(+), 5 deletions(-)

diffs (56 lines):

diff -r 05379e4d6844 -r 94fc00372558 njs/njs_parser.c
--- a/njs/njs_parser.c	Fri Feb 01 19:48:30 2019 +0800
+++ b/njs/njs_parser.c	Fri Feb 01 18:09:02 2019 +0300
@@ -2163,9 +2163,18 @@ njs_parser_object(njs_vm_t *vm, njs_pars
             token = njs_parser_property_name(vm, parser, token);
             break;
 
-        default:
+        case NJS_TOKEN_NUMBER:
+        case NJS_TOKEN_STRING:
+        case NJS_TOKEN_ESCAPE_STRING:
             token = njs_parser_terminal(vm, parser, token);
             break;
+
+        default:
+            return NJS_TOKEN_ILLEGAL;
+        }
+
+        if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
+            return token;
         }
 
         object = njs_parser_node_new(vm, parser, NJS_TOKEN_OBJECT_VALUE);
@@ -2183,10 +2192,6 @@ njs_parser_object(njs_vm_t *vm, njs_pars
         propref->left = object;
         propref->right = parser->node;
 
-        if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
-            return token;
-        }
-
         token = njs_parser_match(vm, parser, token, NJS_TOKEN_COLON);
         if (nxt_slow_path(token <= NJS_TOKEN_ILLEGAL)) {
             return token;
diff -r 05379e4d6844 -r 94fc00372558 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Fri Feb 01 19:48:30 2019 +0800
+++ b/njs/test/njs_unit_test.c	Fri Feb 01 18:09:02 2019 +0300
@@ -2816,6 +2816,18 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("var o = Object.create({a:1}); o.a = 2; delete o.a; o.a"),
       nxt_string("1") },
 
+    { nxt_string("njs.dump({break:1,3:2,'a':4,\"b\":2,true:1,null:0})"),
+      nxt_string("{break:1,3:2,a:4,b:2,true:1,null:0}") },
+
+    { nxt_string("var o1 = {a:1,b:2}, o2 = {c:3}; o1.a + o2.c"),
+      nxt_string("4") },
+
+    { nxt_string("({[]:1})"),
+      nxt_string("SyntaxError: Unexpected token \"[\" in 1") },
+
+    { nxt_string("({'AB\n\\cd':1})['AB\n\\cd']"),
+      nxt_string("1") },
+
     /* Inheritance. */
 
     { nxt_string("function Foo() {this.bar = 10;}; Foo.prototype.bar = 42; "


More information about the nginx-devel mailing list