From: Dmitry Volyntsev Date: Tue, 7 Dec 2021 13:15:19 +0000 (+0000) Subject: Fixed function redeclaration in CLI when interactive mode is on. X-Git-Tag: 0.7.1~31 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=6155deb358224079611b4bea61f87d765a7262cf;p=njs.git Fixed function redeclaration in CLI when interactive mode is on. The issue was introduced in 0a2a0b5a74f4 (0.6.0). --- diff --git a/src/njs_generator.c b/src/njs_generator.c index e0fb30d0..1c608f2b 100644 --- a/src/njs_generator.c +++ b/src/njs_generator.c @@ -3583,11 +3583,7 @@ njs_generate_function_declaration(njs_vm_t *vm, njs_generator_t *generator, return njs_generator_stack_pop(vm, generator, NULL); } - if (njs_is_function(&var->value)) { - lambda = njs_function(&var->value)->u.lambda; - } else { - lambda = var->value.data.u.lambda; - } + lambda = njs_variable_lambda(var); lex_entry = njs_lexer_entry(node->u.reference.unique_id); if (njs_slow_path(lex_entry == NULL)) { diff --git a/src/njs_parser.c b/src/njs_parser.c index 4f2aa307..9bfbf240 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -6747,7 +6747,7 @@ njs_parser_function_declaration(njs_parser_t *parser, njs_lexer_token_t *token, return NJS_ERROR; } - node->u.value.data.u.lambda = var->value.data.u.lambda; + node->u.value.data.u.lambda = njs_variable_lambda(var); node->left = (njs_parser_node_t *) unique_id; diff --git a/src/njs_variable.h b/src/njs_variable.h index 56d54e95..db309f7d 100644 --- a/src/njs_variable.h +++ b/src/njs_variable.h @@ -93,6 +93,17 @@ njs_variable_node_alloc(njs_vm_t *vm, njs_variable_t *var, uintptr_t key) return node; } +njs_inline njs_function_lambda_t * +njs_variable_lambda(njs_variable_t * var) +{ + if (njs_is_function(&var->value)) { + /* may be set by generator in njs_generate_function_declaration(). */ + return njs_function(&var->value)->u.lambda; + } + + return var->value.data.u.lambda; +} + njs_inline void njs_variable_node_free(njs_vm_t *vm, njs_variable_node_t *node) diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index c11b47ed..6683ccc7 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -21408,6 +21408,14 @@ static njs_unit_test_t njs_shell_test[] = "Number.prototype.test" ENTER), njs_str("test") }, + { njs_str("function f(a) {return a}" ENTER + "function f(a) {return a}; f(2)" ENTER), + njs_str("2") }, + + { njs_str("function f() {return 1}" ENTER + "function f(a) {return 1}; f(2)" ENTER), + njs_str("1") }, + { njs_str("try {(new Function('function foo(){return 1}; ()=>{}breakhere'))} catch (e) {}" ENTER "foo()" ENTER), njs_str("ReferenceError: \"foo\" is not defined\n"