[njs] Duplicate function names are disabled.
Igor Sysoev
igor at sysoev.ru
Fri Jul 22 12:04:53 UTC 2016
details: http://hg.nginx.org/njs/rev/cdb8d20935ee
branches:
changeset: 132:cdb8d20935ee
user: Igor Sysoev <igor at sysoev.ru>
date: Fri Jul 22 14:38:06 2016 +0300
description:
Duplicate function names are disabled.
diffstat:
njs/njs_parser.c | 4 ++--
njs/njs_parser.h | 1 +
njs/njs_variable.c | 31 +++++++++++++++++++++++++++++++
njs/njs_variable.h | 1 +
njs/test/njs_unit_test.c | 9 +++++++++
5 files changed, 44 insertions(+), 2 deletions(-)
diffs (110 lines):
diff -r f0c94dd2bdb3 -r cdb8d20935ee njs/njs_parser.c
--- a/njs/njs_parser.c Fri Jul 22 14:15:54 2016 +0300
+++ b/njs/njs_parser.c Fri Jul 22 14:38:06 2016 +0300
@@ -290,7 +290,6 @@ njs_parser_match(njs_vm_t *vm, njs_parse
static njs_token_t
njs_parser_function_declaration(njs_vm_t *vm, njs_parser_t *parser)
{
- nxt_uint_t level;
njs_token_t token;
njs_value_t *value;
njs_variable_t *var;
@@ -313,7 +312,7 @@ njs_parser_function_declaration(njs_vm_t
return NJS_TOKEN_ILLEGAL;
}
- var = njs_parser_variable(vm, parser, &level);
+ var = njs_parser_name_alloc(vm, parser);
if (nxt_slow_path(var == NULL)) {
return NJS_TOKEN_ERROR;
}
@@ -2241,6 +2240,7 @@ njs_parser_error(njs_vm_t *vm, njs_parse
"SyntaxError: Invalid Unicode code point \"%.*s\" in %u",
"SyntaxError: Unterminated RegExp \"%.*s\" in %u",
"SyntaxError: Invalid RegExp flags \"%.*s\" in %u",
+ "SyntaxError: Duplicate declaration \"%.*s\" in %u",
};
msg = errors[err];
diff -r f0c94dd2bdb3 -r cdb8d20935ee njs/njs_parser.h
--- a/njs/njs_parser.h Fri Jul 22 14:15:54 2016 +0300
+++ b/njs/njs_parser.h Fri Jul 22 14:38:06 2016 +0300
@@ -324,6 +324,7 @@ typedef enum {
NJS_PARSER_ERROR_UNICODE,
NJS_PARSER_ERROR_UNTERMINATED_REGEXP,
NJS_PARSER_ERROR_REGEXP_FLAGS,
+ NJS_PARSER_ERROR_DUPLICATE_DECLARATION,
} njs_parser_error_t;
diff -r f0c94dd2bdb3 -r cdb8d20935ee njs/njs_variable.c
--- a/njs/njs_variable.c Fri Jul 22 14:15:54 2016 +0300
+++ b/njs/njs_variable.c Fri Jul 22 14:38:06 2016 +0300
@@ -54,6 +54,37 @@ static const nxt_lvlhsh_proto_t njs_var
njs_variable_t *
+njs_parser_name_alloc(njs_vm_t *vm, njs_parser_t *parser)
+{
+ nxt_int_t ret;
+ njs_variable_t *var;
+ nxt_lvlhsh_query_t lhq;
+
+ var = njs_variable_alloc(vm, parser, &parser->lexer->text);
+ if (nxt_slow_path(var == NULL)) {
+ return NULL;
+ }
+
+ lhq.key_hash = parser->lexer->key_hash;
+ lhq.key = parser->lexer->text;
+ lhq.replace = 0;
+ lhq.value = var;
+ lhq.proto = &njs_variables_hash_proto;
+ lhq.pool = vm->mem_cache_pool;
+
+ ret = nxt_lvlhsh_insert(&parser->variables_hash, &lhq);
+
+ if (nxt_fast_path(ret == NXT_OK)) {
+ return var;
+ }
+
+ (void) njs_parser_error(vm, parser, NJS_PARSER_ERROR_DUPLICATE_DECLARATION);
+
+ return NULL;
+}
+
+
+njs_variable_t *
njs_parser_variable(njs_vm_t *vm, njs_parser_t *parser, nxt_uint_t *level)
{
nxt_int_t ret;
diff -r f0c94dd2bdb3 -r cdb8d20935ee njs/njs_variable.h
--- a/njs/njs_variable.h Fri Jul 22 14:15:54 2016 +0300
+++ b/njs/njs_variable.h Fri Jul 22 14:38:06 2016 +0300
@@ -26,6 +26,7 @@ typedef struct {
} njs_variable_t;
+njs_variable_t *njs_parser_name_alloc(njs_vm_t *vm, njs_parser_t *parser);
njs_variable_t *njs_parser_variable(njs_vm_t *vm, njs_parser_t *parser,
nxt_uint_t *level);
njs_value_t *njs_variable_value(njs_parser_t *parser, njs_index_t index);
diff -r f0c94dd2bdb3 -r cdb8d20935ee njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c Fri Jul 22 14:15:54 2016 +0300
+++ b/njs/test/njs_unit_test.c Fri Jul 22 14:38:06 2016 +0300
@@ -67,6 +67,15 @@ static njs_unit_test_t njs_test[] =
{ nxt_string("var a = 1; var b; a"),
nxt_string("1") },
+ { nxt_string("function f(){} function f(){}"),
+ nxt_string("SyntaxError: Duplicate declaration \"f\" in 1") },
+
+ { nxt_string("var f = 1; function f() {}"),
+ nxt_string("SyntaxError: Duplicate declaration \"f\" in 1") },
+
+ { nxt_string("function f() {} var f = 1; f"),
+ nxt_string("1") },
+
/* Numbers. */
{ nxt_string("999999999999999999999"),
More information about the nginx-devel
mailing list