]> git.kaiwu.me - njs.git/commitdiff
Making function expression binding immutable according the specs.
authorDmitry Volyntsev <xeioex@nginx.com>
Thu, 12 May 2022 04:08:21 +0000 (21:08 -0700)
committerDmitry Volyntsev <xeioex@nginx.com>
Thu, 12 May 2022 04:08:21 +0000 (21:08 -0700)
This closes #56 issue on Github.

src/njs_parser.c
src/test/njs_unit_test.c

index 162736db3a73548f1b4de77ba52fcba07189f384..f4c805aa4ee83942c04d7e8dc81526be31de860d 100644 (file)
@@ -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) {
index e80cd9704eb949ba737109c65281872cb4b88722..cb1c6020cb6dce7ea11e14a3f8d711af226c0240 100644 (file)
@@ -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") },