From: Dmitry Volyntsev Date: Thu, 12 May 2022 04:08:21 +0000 (-0700) Subject: Making function expression binding immutable according the specs. X-Git-Tag: 0.7.4~9 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=fa2272c06c2af1498b7ca7a0f8ca52139610c5cf;p=njs.git Making function expression binding immutable according the specs. This closes #56 issue on Github. --- 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") },