Discussion:
[njs] Fixed local scope this.
Dmitry Volyntsev
2018-11-15 17:32:03 UTC
Permalink
details: http://hg.nginx.org/njs/rev/93ef4b20c674
branches:
changeset: 656:93ef4b20c674
user: Dmitry Volyntsev <***@nginx.com>
date: Thu Nov 15 20:31:35 2018 +0300
description:
Fixed local scope this.

diffstat:

njs/njs_parser.c | 23 +++++++++++++++++------
njs/test/njs_unit_test.c | 17 +++++++++++++++++
2 files changed, 34 insertions(+), 6 deletions(-)

diffs (74 lines):

diff -r e11011d45499 -r 93ef4b20c674 njs/njs_parser.c
--- a/njs/njs_parser.c Thu Nov 15 20:31:35 2018 +0300
+++ b/njs/njs_parser.c Thu Nov 15 20:31:35 2018 +0300
@@ -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_pa
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 -r e11011d45499 -r 93ef4b20c674 njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Thu Nov 15 20:31:35 2018 +0300
+++ b/njs/test/njs_unit_test.c Thu Nov 15 20:31:35 2018 +0300
@@ -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]") },

Loading...