]> git.kaiwu.me - njs.git/commitdiff
Fixed local scope this.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 15 Nov 2018 17:31:35 +0000 (20:31 +0300)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 15 Nov 2018 17:31:35 +0000 (20:31 +0300)
njs/njs_parser.c
njs/test/njs_unit_test.c

index 9f88041ca7b8e35c052ed6172cf8d649b3b4d640..dad908748a1c46b07e148b175e34d36df32378f4 100644 (file)
@@ -1806,10 +1806,11 @@ njs_parser_token(njs_parser_t *parser)
 njs_token_t
 njs_parser_terminal(njs_vm_t *vm, njs_parser_t *parser, njs_token_t token)
 {
-    double             num;
-    njs_ret_t          ret;
-    njs_value_t        *ext;
-    njs_parser_node_t  *node;
+    double              num;
+    njs_ret_t           ret;
+    njs_value_t         *ext;
+    njs_parser_node_t   *node;
+    njs_parser_scope_t  *scope;
 
     if (token == NJS_TOKEN_OPEN_PARENTHESIS) {
 
@@ -1980,8 +1981,18 @@ 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");
 
-        if (parser->scope->type != NJS_SCOPE_GLOBAL) {
-            node->index = NJS_INDEX_THIS;
+        scope = parser->scope;
+
+        while (scope->type != NJS_SCOPE_GLOBAL) {
+            if (scope->type == NJS_SCOPE_FUNCTION) {
+                node->index = NJS_INDEX_THIS;
+                break;
+            }
+
+            scope = scope->parent;
+        }
+
+        if (node->index == NJS_INDEX_THIS) {
             break;
         }
 
index bd7132f52177d3e1157073ba183d79701dfb915c..7f5aa906d2131a8491b81fb470a8fbd8c9771bb1 100644 (file)
@@ -6485,6 +6485,8 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("/./ instanceof Object"),
       nxt_string("true") },
 
+    /* global this. */
+
     { nxt_string("this"),
       nxt_string("[object Object]") },
 
@@ -6515,6 +6517,21 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("this.NaN + 1"),
       nxt_string("NaN") },
 
+    { nxt_string("if (1) {new this}"),
+      nxt_string("TypeError: object is not a function") },
+
+    { nxt_string("if (1) {this()}"),
+      nxt_string("TypeError: object is not a function") },
+
+    { nxt_string("var ex; try {new this} catch (e) {ex = e}; ex"),
+      nxt_string("TypeError: object is not a function") },
+
+    { nxt_string("var ex; try {({}) instanceof this} catch (e) {ex = e}; ex"),
+      nxt_string("TypeError: right argument is not a function") },
+
+    { nxt_string("Function.call(this, 'var x / = 1;')"),
+      nxt_string("InternalError: Not implemented") },
+
     { nxt_string("njs"),
       nxt_string("[object Object]") },