[njs] Shell: fixed function redeclarations.

Dmitry Volyntsev xeioex at nginx.com
Wed Feb 27 13:41:24 UTC 2019


details:   https://hg.nginx.org/njs/rev/fc69d402ec6d
branches:  
changeset: 810:fc69d402ec6d
user:      hongzhidao <hongzhidao at gmail.com>
date:      Wed Feb 27 17:52:50 2019 +0800
description:
Shell: fixed function redeclarations.

This closes #108 issue on Github.

diffstat:

 njs/njs_parser.c             |  39 +++++++++++++++++++++++++++++----------
 njs/njs_parser.h             |   4 ++++
 njs/njs_variable.c           |   2 +-
 njs/test/njs_expect_test.exp |  12 ++++++++++++
 4 files changed, 46 insertions(+), 11 deletions(-)

diffs (119 lines):

diff -r 984b7794c5f2 -r fc69d402ec6d njs/njs_parser.c
--- a/njs/njs_parser.c	Wed Feb 27 17:49:03 2019 +0800
+++ b/njs/njs_parser.c	Wed Feb 27 17:52:50 2019 +0800
@@ -565,6 +565,33 @@ njs_parser_labelled_statement(njs_vm_t *
 }
 
 
+static njs_function_t *
+njs_parser_function_alloc(njs_vm_t *vm, njs_parser_t *parser,
+    njs_variable_t *var)
+{
+    njs_value_t     *value;
+    njs_function_t  *function;
+
+    function = njs_function_alloc(vm);
+    if (nxt_slow_path(function == NULL)) {
+        return NULL;
+    }
+
+    var->value.data.u.function = function;
+    var->value.type = NJS_FUNCTION;
+    var->value.data.truth = 1;
+
+    if (var->index != NJS_INDEX_NONE
+        && njs_scope_accumulative(vm, parser->scope))
+    {
+        value = (njs_value_t *) var->index;
+        *value = var->value;
+    }
+
+    return function;
+}
+
+
 static njs_token_t
 njs_parser_function_declaration(njs_vm_t *vm, njs_parser_t *parser)
 {
@@ -613,15 +640,11 @@ njs_parser_function_declaration(njs_vm_t
 
     parser->node = node;
 
-    function = njs_function_alloc(vm);
+    function = njs_parser_function_alloc(vm, parser, var);
     if (nxt_slow_path(function == NULL)) {
         return NJS_TOKEN_ERROR;
     }
 
-    var->value.data.u.function = function;
-    var->value.type = NJS_FUNCTION;
-    var->value.data.truth = 1;
-
     token = njs_parser_function_lambda(vm, parser, function->u.lambda, token);
 
     return token;
@@ -671,15 +694,11 @@ njs_parser_function_expression(njs_vm_t 
             return token;
         }
 
-        function = njs_function_alloc(vm);
+        function = njs_parser_function_alloc(vm, parser, var);
         if (nxt_slow_path(function == NULL)) {
             return NJS_TOKEN_ERROR;
         }
 
-        var->value.data.u.function = function;
-        var->value.type = NJS_FUNCTION;
-        var->value.data.truth = 1;
-
         lambda = function->u.lambda;
 
     } else {
diff -r 984b7794c5f2 -r fc69d402ec6d njs/njs_parser.h
--- a/njs/njs_parser.h	Wed Feb 27 17:49:03 2019 +0800
+++ b/njs/njs_parser.h	Wed Feb 27 17:52:50 2019 +0800
@@ -379,6 +379,10 @@ njs_parser_global_scope(njs_vm_t *vm)
 }
 
 
+#define njs_scope_accumulative(vm, scope)                                     \
+    ((vm)->options.accumulative && (scope)->type == NJS_SCOPE_GLOBAL)
+
+
 extern const nxt_lvlhsh_proto_t  njs_keyword_hash_proto;
 
 
diff -r 984b7794c5f2 -r fc69d402ec6d njs/njs_variable.c
--- a/njs/njs_variable.c	Wed Feb 27 17:49:03 2019 +0800
+++ b/njs/njs_variable.c	Wed Feb 27 17:52:50 2019 +0800
@@ -508,7 +508,7 @@ njs_scope_next_index(njs_vm_t *vm, njs_p
     njs_value_t  *value;
     nxt_array_t  *values;
 
-    if (vm->options.accumulative && scope->type == NJS_SCOPE_GLOBAL) {
+    if (njs_scope_accumulative(vm, scope)) {
         /*
          * When non-clonable VM runs in accumulative mode all
          * global variables should be allocated in absolute scope
diff -r 984b7794c5f2 -r fc69d402ec6d njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Wed Feb 27 17:49:03 2019 +0800
+++ b/njs/test/njs_expect_test.exp	Wed Feb 27 17:52:50 2019 +0800
@@ -172,6 +172,18 @@ njs_test {
      "o.a.toDateString*o.a.toLocaleDateString*o.a.toString"}
 }
 
+# function declarations in interactive mode
+njs_test {
+    {"function a() { return 1; }\r\n"
+     "undefined\r\n>> "}
+    {"a();\r\n"
+     "1\r\n>> "}
+    {"function a() { return 2; }\r\n"
+     "undefined\r\n>> "}
+    {"a();\r\n"
+     "2\r\n>> "}
+}
+
 # console object
 njs_test {
     {"console.log()\r\n"


More information about the nginx-devel mailing list