]> git.kaiwu.me - njs.git/commitdiff
Shell: fixed function redeclarations.
authorhongzhidao <hongzhidao@gmail.com>
Wed, 27 Feb 2019 09:52:50 +0000 (17:52 +0800)
committerhongzhidao <hongzhidao@gmail.com>
Wed, 27 Feb 2019 09:52:50 +0000 (17:52 +0800)
This closes #108 issue on Github.

njs/njs_parser.c
njs/njs_parser.h
njs/njs_variable.c
njs/test/njs_expect_test.exp

index 7ba4d76c0da42b9b9cf63d85f9030b2ea4202e68..04a537a076e9bcbcb5850fb44a40d10ecd1108c2 100644 (file)
@@ -565,6 +565,33 @@ njs_parser_labelled_statement(njs_vm_t *vm, njs_parser_t *parser)
 }
 
 
+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 *vm, njs_parser_t *parser)
 
     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 *vm, njs_parser_t *parser)
             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 {
index 70af65148a452a4d80004359890d2b0c70a0899a..7a8dfd8fd3a51383766ee6ac5c74c0c479d0f8a0 100644 (file)
@@ -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;
 
 
index 04dd5c8eab5de633529c68dbfe2250fa11ed7656..cae9b6ea850ba37b398afbc1a4de605874a85d96 100644 (file)
@@ -508,7 +508,7 @@ njs_scope_next_index(njs_vm_t *vm, njs_parser_scope_t *scope,
     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
index 807e380f32fadb3407cb0a83b09f1f1537ff50d8..86c88942ef16786fc616bf28ff49a416afc91ab6 100644 (file)
@@ -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"