From: Dmitry Volyntsev Date: Tue, 25 Jan 2022 13:18:20 +0000 (+0000) Subject: Fixed function redeclaration. X-Git-Tag: 0.7.2~1 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/postgres_fdw.c?a=commitdiff_plain;h=12d17739a358a7d02a2331b5cf132257be4599b2;p=njs.git 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). --- diff --git a/src/njs_generator.c b/src/njs_generator.c index b39f8830..22f1bf82 100644 --- a/src/njs_generator.c +++ b/src/njs_generator.c @@ -3684,11 +3684,9 @@ njs_generate_function_scope(njs_vm_t *vm, njs_generator_t *prev, 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 --git a/src/njs_variable.c b/src/njs_variable.c index d47836c7..481b0c4e 100644 --- a/src/njs_variable.c +++ b/src/njs_variable.c @@ -56,34 +56,33 @@ njs_variable_function_add(njs_parser_t *parser, njs_parser_scope_t *scope, 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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 35e2f132..f666a3ea 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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("()"),