]> git.kaiwu.me - njs.git/commitdiff
Accessing the global this object caused segfault.
authorIgor Sysoev <igor@sysoev.ru>
Tue, 11 Oct 2016 14:44:05 +0000 (17:44 +0300)
committerIgor Sysoev <igor@sysoev.ru>
Tue, 11 Oct 2016 14:44:05 +0000 (17:44 +0300)
njs/njs_builtin.c
njs/njs_generator.c
njs/njs_parser.c
njs/njs_parser.h
njs/njs_vm.h
njs/test/njs_unit_test.c

index 23af28ac179e5419f2e03f1aa5cd9ad24ecff501..a5f4ae7eef7bedac0d0d35df342e666479ed3314 100644 (file)
@@ -107,7 +107,8 @@ njs_builtin_objects_create(njs_vm_t *vm)
     };
 
     static const njs_object_init_t    *object_init[] = {
-        &njs_math_object_init,
+        NULL,                         /* global this        */
+        &njs_math_object_init,        /* Math               */
     };
 
     static const njs_object_init_t    *function_init[] = {
@@ -164,12 +165,14 @@ njs_builtin_objects_create(njs_vm_t *vm)
 
     objects = vm->shared->objects;
 
-    for (i = NJS_OBJECT_MATH; i < NJS_OBJECT_MAX; i++) {
-        ret = njs_object_hash_create(vm, &objects[i].shared_hash,
-                                     object_init[i]->properties,
-                                     object_init[i]->items);
-        if (nxt_slow_path(ret != NXT_OK)) {
-            return NXT_ERROR;
+    for (i = NJS_OBJECT_THIS; i < NJS_OBJECT_MAX; i++) {
+        if (object_init[i] != NULL) {
+            ret = njs_object_hash_create(vm, &objects[i].shared_hash,
+                                         object_init[i]->properties,
+                                         object_init[i]->items);
+            if (nxt_slow_path(ret != NXT_OK)) {
+                return NXT_ERROR;
+            }
         }
 
         objects[i].shared = 1;
index 071ce3bd7748ab8ace1120d43cbff75fb0464e05..abf348acff10982c43c48dbf9ad6b55754a986f9 100644 (file)
@@ -289,6 +289,7 @@ njs_generator(njs_vm_t *vm, njs_parser_t *parser, njs_parser_node_t *node)
     case NJS_TOKEN_NAME:
         return njs_generate_name(vm, parser, node);
 
+    case NJS_TOKEN_GLOBAL_THIS:
     case NJS_TOKEN_MATH:
     case NJS_TOKEN_EVAL:
     case NJS_TOKEN_TO_STRING:
index acde715f78444141dba0cb25cbccecfbeacc634a..5a7afa833e1e5e5109be135192ceff9afdfbd419 100644 (file)
@@ -1644,8 +1644,14 @@ njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token)
     case NJS_TOKEN_THIS:
         nxt_thread_log_debug("JS: this");
 
-        node->index = NJS_INDEX_THIS;
-        break;
+        if (parser->scope != NJS_SCOPE_GLOBAL) {
+            node->index = NJS_INDEX_THIS;
+            break;
+        }
+
+        node->token = NJS_TOKEN_GLOBAL_THIS;
+
+        /* Fall through. */
 
     case NJS_TOKEN_MATH:
         return njs_parser_builtin_object(vm, parser, node);
index a2beafa56b5548cf0649dcb76db3171626e72bd7..76d118fa53aa8e9f9aa4edcc7edce251c075cfb6 100644 (file)
@@ -160,8 +160,9 @@ typedef enum {
 
     NJS_TOKEN_THIS,
 
-#define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_MATH
+#define NJS_TOKEN_FIRST_OBJECT     NJS_TOKEN_GLOBAL_THIS
 
+    NJS_TOKEN_GLOBAL_THIS,
     NJS_TOKEN_MATH,
 
     NJS_TOKEN_OBJECT_CONSTRUCTOR,
index cce7be7f7b971acd9f11e7b65187f83a98855ae7..adb9da849bb0b404d2e9bc4deef169b361d8816f 100644 (file)
@@ -741,7 +741,8 @@ enum njs_constructor_e {
 
 
 enum njs_object_e {
-    NJS_OBJECT_MATH = 0,
+    NJS_OBJECT_THIS = 0,
+    NJS_OBJECT_MATH,
 #define NJS_OBJECT_MAX         (NJS_OBJECT_MATH + 1)
 };
 
index 5d9d64477fc863b91af0d072d65cf1162664fffc..5da2cffa9b6677906d85771d8b50e05fa7377dc3 100644 (file)
@@ -4237,6 +4237,9 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("/./ instanceof Object"),
       nxt_string("true") },
 
+    { nxt_string("this"),
+      nxt_string("[object Object]") },
+
     { nxt_string("var o = Object(); o"),
       nxt_string("[object Object]") },