[njs] Making function expression binding immutable according the specs.

Dmitry Volyntsev xeioex at nginx.com
Thu May 12 04:09:14 UTC 2022


details:   https://hg.nginx.org/njs/rev/6a28cdbc8cb6
branches:  
changeset: 1855:6a28cdbc8cb6
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed May 11 21:08:21 2022 -0700
description:
Making function expression binding immutable according the specs.

This closes #56 issue on Github.

diffstat:

 src/njs_parser.c         |  7 ++++++-
 src/test/njs_unit_test.c |  6 ++++++
 2 files changed, 12 insertions(+), 1 deletions(-)

diffs (34 lines):

diff -r 4c8487fb0ca8 -r 6a28cdbc8cb6 src/njs_parser.c
--- a/src/njs_parser.c	Wed May 11 17:51:17 2022 -0700
+++ b/src/njs_parser.c	Wed May 11 21:08:21 2022 -0700
@@ -6954,8 +6954,13 @@ njs_parser_function_expression_after(njs
 
     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 -r 4c8487fb0ca8 -r 6a28cdbc8cb6 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed May 11 17:51:17 2022 -0700
+++ b/src/test/njs_unit_test.c	Wed May 11 21:08:21 2022 -0700
@@ -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") },



More information about the nginx-devel mailing list