From bb76463934896896e0d4f95eefa5482596edd4f6 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Thu, 15 Nov 2018 20:31:35 +0300 Subject: [PATCH] Fixed local scope this. --- njs/njs_parser.c | 23 +++++++++++++++++------ njs/test/njs_unit_test.c | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/njs/njs_parser.c b/njs/njs_parser.c index 9f88041c..dad90874 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -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; } diff --git a/njs/test/njs_unit_test.c b/njs/test/njs_unit_test.c index bd7132f5..7f5aa906 100644 --- a/njs/test/njs_unit_test.c +++ b/njs/test/njs_unit_test.c @@ -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]") }, -- 2.47.3