From 4bc44a5e93cf574430a3ae9f5b15c9cd00311182 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Fri, 7 Jun 2024 21:46:30 -0700 Subject: [PATCH] Fixed tracking of argument scope. This properly catches unsupported "await" in arguments. This fixes #730 issue on Github. --- src/njs_parser.c | 11 ++++++++--- src/njs_parser.h | 1 - src/njs_vm.h | 1 + src/test/njs_unit_test.c | 4 ++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/njs_parser.c b/src/njs_parser.c index 570d65c0c..819e09d11 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -2816,6 +2816,8 @@ static njs_int_t njs_parser_arguments(njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current) { + njs_int_t ret; + /* * ) * ArgumentList ) @@ -2827,7 +2829,10 @@ njs_parser_arguments(njs_parser_t *parser, njs_lexer_token_t *token, return njs_parser_stack_pop(parser); } - parser->scope->in_args = 1; + ret = njs_parser_scope_begin(parser, NJS_SCOPE_ARGUMENTS, 0); + if (njs_slow_path(ret != NJS_OK)) { + return NJS_ERROR; + } njs_parser_next(parser, njs_parser_argument_list); @@ -2840,7 +2845,7 @@ static njs_int_t njs_parser_parenthesis_or_comma(njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current) { - parser->scope->in_args = 0; + njs_parser_scope_end(parser); if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) { njs_lexer_consume_token(parser->lexer, 1); @@ -3575,7 +3580,7 @@ njs_parser_await(njs_parser_t *parser, njs_lexer_token_t *token, return NJS_ERROR; } - if (parser->scope->in_args) { + if (parser->scope->type == NJS_SCOPE_ARGUMENTS) { njs_parser_syntax_error(parser, "await in arguments not supported"); return NJS_ERROR; } diff --git a/src/njs_parser.h b/src/njs_parser.h index 6e14647a7..9f0a9cb42 100644 --- a/src/njs_parser.h +++ b/src/njs_parser.h @@ -26,7 +26,6 @@ struct njs_parser_scope_s { uint8_t arrow_function; uint8_t dest_disable; uint8_t async; - uint8_t in_args; }; diff --git a/src/njs_vm.h b/src/njs_vm.h index 6d09caf8f..3755ef634 100644 --- a/src/njs_vm.h +++ b/src/njs_vm.h @@ -22,6 +22,7 @@ typedef struct njs_generator_s njs_generator_t; typedef enum { NJS_SCOPE_GLOBAL = 0, NJS_SCOPE_FUNCTION, + NJS_SCOPE_ARGUMENTS, NJS_SCOPE_BLOCK } njs_scope_t; diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index f5d2e808d..ba96162a2 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -19938,6 +19938,10 @@ static njs_unit_test_t njs_test[] = "(async function() {f(await 111)})"), njs_str("SyntaxError: await in arguments not supported in 1") }, + { njs_str("function f(a) {}" + "(async function() {f(f(1), await 111)})"), + njs_str("SyntaxError: await in arguments not supported in 1") }, + { njs_str("async () => [await x(1)(),]; async () => [await x(1)()]"), njs_str("[object AsyncFunction]") },