Welcome! Log In Create A New Profile

Advanced

[njs] Fixed function redeclaration.

Dmitry Volyntsev
January 25, 2022 08:42AM
details: https://hg.nginx.org/njs/rev/d29cddd07a32
branches:
changeset: 1815:d29cddd07a32
user: Dmitry Volyntsev <xeioex@nginx.com>
date: Tue Jan 25 13:18:20 2022 +0000
description:
Fixed function redeclaration.

Previously, the existing lambda structure was reused resulting in the
properties of the previously defined function was merged into a new one.

The bug was introduced in 66bd2cc7fd87 (0.7.0).

diffstat:

src/njs_generator.c | 8 +++-----
src/njs_variable.c | 41 ++++++++++++++++++++---------------------
src/test/njs_unit_test.c | 10 ++++++++++
3 files changed, 33 insertions(+), 26 deletions(-)

diffs (96 lines):

diff -r d776b59196c5 -r d29cddd07a32 src/njs_generator.c
--- a/src/njs_generator.c Fri Jan 21 14:31:30 2022 +0000
+++ b/src/njs_generator.c Tue Jan 25 13:18:20 2022 +0000
@@ -3684,11 +3684,9 @@ njs_generate_function_scope(njs_vm_t *vm
lambda->nlocal = node->scope->items;
lambda->temp = node->scope->temp;

- if (node->scope->declarations != NULL) {
- arr = node->scope->declarations;
- lambda->declarations = arr->start;
- lambda->ndeclarations = arr->items;
- }
+ arr = node->scope->declarations;
+ lambda->declarations = (arr != NULL) ? arr->start : NULL;
+ lambda->ndeclarations = (arr != NULL) ? arr->items : 0;

return NJS_OK;
}
diff -r d776b59196c5 -r d29cddd07a32 src/njs_variable.c
--- a/src/njs_variable.c Fri Jan 21 14:31:30 2022 +0000
+++ b/src/njs_variable.c Tue Jan 25 13:18:20 2022 +0000
@@ -56,34 +56,33 @@ njs_variable_function_add(njs_parser_t *
return NULL;
}

- if (var->index == NJS_INDEX_ERROR || !var->function) {
- root = njs_function_scope(scope);
- if (njs_slow_path(scope == NULL)) {
- return NULL;
- }
+ root = njs_function_scope(scope);
+ if (njs_slow_path(scope == NULL)) {
+ return NULL;
+ }

- ctor = parser->node->token_type != NJS_TOKEN_ASYNC_FUNCTION_DECLARATION;
+ ctor = parser->node->token_type != NJS_TOKEN_ASYNC_FUNCTION_DECLARATION;

- lambda = njs_function_lambda_alloc(parser->vm, ctor);
- if (lambda == NULL) {
- return NULL;
- }
+ lambda = njs_function_lambda_alloc(parser->vm, ctor);
+ if (lambda == NULL) {
+ return NULL;
+ }

- var->value.data.u.lambda = lambda;
+ njs_set_invalid(&var->value);
+ var->value.data.u.lambda = lambda;

- declr = njs_variable_scope_function_add(parser, root);
- if (njs_slow_path(declr == NULL)) {
- return NULL;
- }
+ declr = njs_variable_scope_function_add(parser, root);
+ if (njs_slow_path(declr == NULL)) {
+ return NULL;
+ }

- var->index = njs_scope_index(root->type, root->items, NJS_LEVEL_LOCAL,
- type);
+ var->index = njs_scope_index(root->type, root->items, NJS_LEVEL_LOCAL,
+ type);

- declr->value = &var->value;
- declr->index = var->index;
+ declr->value = &var->value;
+ declr->index = var->index;

- root->items++;
- }
+ root->items++;

var->type = NJS_VARIABLE_FUNCTION;
var->function = 1;
diff -r d776b59196c5 -r d29cddd07a32 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Fri Jan 21 14:31:30 2022 +0000
+++ b/src/test/njs_unit_test.c Tue Jan 25 13:18:20 2022 +0000
@@ -10568,6 +10568,16 @@ static njs_unit_test_t njs_test[] =
"myFoo(1,2);" ),
njs_str("") },

+ { njs_str("function f(...rest) {};"
+ "function f(a, b) {return a + b};"
+ "f(1,2)"),
+ njs_str("3") },
+
+ { njs_str("function f() { function q() {} };"
+ "function f() { };"
+ "f()"),
+ njs_str("undefined") },
+
/* arrow functions. */

{ njs_str("()"),
_______________________________________________
nginx-devel mailing list -- nginx-devel@nginx.org
To unsubscribe send an email to nginx-devel-leave@nginx.org
Subject Author Views Posted

[njs] Fixed function redeclaration.

Dmitry Volyntsev 139 January 25, 2022 08:42AM



Sorry, you do not have permission to post/reply in this forum.

Online Users

Guests: 110
Record Number of Users: 6 on February 13, 2018
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready