From fa2272c06c2af1498b7ca7a0f8ca52139610c5cf Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Wed, 11 May 2022 21:08:21 -0700 Subject: [PATCH] Making function expression binding immutable according the specs. This closes #56 issue on Github. --- src/njs_parser.c | 7 ++++++- src/test/njs_unit_test.c | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/njs_parser.c b/src/njs_parser.c index 162736db..f4c805aa 100644 --- a/src/njs_parser.c +++ b/src/njs_parser.c @@ -6954,8 +6954,13 @@ njs_parser_function_expression_after(njs_parser_t *parser, var = (njs_variable_t *) parser->target; + if (var->self) { + var->init = 1; + var->type = NJS_VARIABLE_CONST; + } + var->index = njs_scope_index(var->scope->type, var->scope->items, - NJS_LEVEL_LOCAL, NJS_VARIABLE_VAR); + NJS_LEVEL_LOCAL, var->type); var->scope->items++; if (var->self) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index e80cd970..cb1c6020 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -197,6 +197,12 @@ static njs_unit_test_t njs_test[] = { njs_str("var func = function x(x) {return x}; func()"), njs_str("undefined") }, + { njs_str("var func = function f() {f = null; return f;}; func()"), + njs_str("TypeError: assignment to constant variable") }, + + { njs_str("var func = function f() {let f = null; return f;}; func()"), + njs_str("null") }, + #if 0 /* TODO */ { njs_str("var a; Object.getOwnPropertyDescriptor(this, 'a').value"), njs_str("undefined") }, -- 2.47.3