]> git.kaiwu.me - njs.git/commitdiff
Fixed parsing object literals.
authorDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Feb 2019 15:09:02 +0000 (18:09 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Fri, 1 Feb 2019 15:09:02 +0000 (18:09 +0300)
njs/njs_parser.c
njs/test/njs_unit_test.c

index bd767a862902deee1c57129792d6e27101a9e101..22de2110c7900cb59a81b966c5aba52999cc005f 100644 (file)
@@ -2163,9 +2163,18 @@ njs_parser_object(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *obj)
             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_parser_t *parser, njs_parser_node_t *obj)
         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;
index dcf00916087b2eb14dbe6085e9e8b082d74decfd..95fae275f760687120d1b9c87b0ac2ef857e237e 100644 (file)
@@ -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; "