From 9fd15a79d5a982e941a32963a7071b794b2115cc Mon Sep 17 00:00:00 2001 From: hongzhidao Date: Wed, 27 Feb 2019 17:52:50 +0800 Subject: [PATCH] Shell: fixed function redeclarations. This closes #108 issue on Github. --- 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(-) diff --git a/njs/njs_parser.c b/njs/njs_parser.c index 7ba4d76c..04a537a0 100644 --- a/njs/njs_parser.c +++ b/njs/njs_parser.c @@ -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 { diff --git a/njs/njs_parser.h b/njs/njs_parser.h index 70af6514..7a8dfd8f 100644 --- a/njs/njs_parser.h +++ b/njs/njs_parser.h @@ -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 --git a/njs/njs_variable.c b/njs/njs_variable.c index 04dd5c8e..cae9b6ea 100644 --- a/njs/njs_variable.c +++ b/njs/njs_variable.c @@ -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 diff --git a/njs/test/njs_expect_test.exp b/njs/test/njs_expect_test.exp index 807e380f..86c88942 100644 --- a/njs/test/njs_expect_test.exp +++ b/njs/test/njs_expect_test.exp @@ -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" -- 2.47.3