[njs] Fixed line number in reporting variable reference errors.

Dmitry Volyntsev xeioex at nginx.com
Thu Feb 14 13:34:32 UTC 2019


details:   https://hg.nginx.org/njs/rev/3852b2044dfd
branches:  
changeset: 778:3852b2044dfd
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Thu Feb 14 15:21:02 2019 +0300
description:
Fixed line number in reporting variable reference errors.

This closes #94 issue on Github.

diffstat:

 njs/njs_generator.c      |  42 ++-------------------------------
 njs/njs_parser.c         |  60 +++++++++++++++++++++++++++++++++--------------
 njs/njs_parser.h         |  15 ++++++++----
 njs/njs_variable.c       |   3 +-
 njs/test/njs_unit_test.c |   6 ++++
 5 files changed, 63 insertions(+), 63 deletions(-)

diffs (210 lines):

diff -r 2a425a8ea90f -r 3852b2044dfd njs/njs_generator.c
--- a/njs/njs_generator.c	Thu Feb 14 14:40:50 2019 +0300
+++ b/njs/njs_generator.c	Thu Feb 14 15:21:02 2019 +0300
@@ -162,9 +162,6 @@ static nxt_noinline nxt_int_t njs_genera
 static nxt_int_t njs_generate_function_debug(njs_vm_t *vm, nxt_str_t *name,
     njs_function_lambda_t *lambda, njs_parser_node_t *node);
 
-static void njs_generate_syntax_error(njs_vm_t *vm, njs_parser_node_t *node,
-    const char *fmt, ...);
-
 
 #define njs_generate_code(generator, type, code)                              \
     do {                                                                      \
@@ -207,6 +204,9 @@ static void njs_generate_syntax_error(nj
         njs_code_offset_diff(generator, patch->jump_offset)
 
 
+#define njs_generate_syntax_error(vm, node, fmt, ...)                         \
+    njs_parser_node_error(vm, node, NJS_OBJECT_SYNTAX_ERROR, fmt, ##__VA_ARGS__)
+
 
 static const nxt_str_t  no_label = { 0, NULL };
 
@@ -3157,39 +3157,3 @@ njs_generate_function_debug(njs_vm_t *vm
 
     return NXT_OK;
 }
-
-
-static void
-njs_generate_syntax_error(njs_vm_t *vm, njs_parser_node_t *node,
-    const char *fmt, ...)
-{
-    size_t              width;
-    u_char              msg[NXT_MAX_ERROR_STR];
-    u_char              *p, *end;
-    va_list             args;
-    njs_parser_scope_t  *scope;
-
-    p = msg;
-    end = msg + NXT_MAX_ERROR_STR;
-
-    va_start(args, fmt);
-    p = nxt_vsprintf(p, end, fmt, args);
-    va_end(args);
-
-    scope = node->scope;
-
-    width = nxt_length(" in ") + scope->file.length + NXT_INT_T_LEN;
-
-    if (p > end - width) {
-        p = end - width;
-    }
-
-    if (scope->file.start != NULL) {
-        p = nxt_sprintf(p, end, " in %V:%uD", &scope->file, node->token_line);
-
-    } else {
-        p = nxt_sprintf(p, end, " in %uD", node->token_line);
-    }
-
-    njs_error_new(vm, &vm->retval, NJS_OBJECT_SYNTAX_ERROR, msg, p - msg);
-}
diff -r 2a425a8ea90f -r 3852b2044dfd njs/njs_parser.c
--- a/njs/njs_parser.c	Thu Feb 14 14:40:50 2019 +0300
+++ b/njs/njs_parser.c	Thu Feb 14 15:21:02 2019 +0300
@@ -2141,6 +2141,8 @@ njs_parser_reference(njs_vm_t *vm, njs_p
     case NJS_TOKEN_NAME:
         nxt_thread_log_debug("JS: %V", name);
 
+        node->token_line = parser->lexer->token_line;
+
         ext = njs_external_lookup(vm, name, hash);
 
         if (ext != NULL) {
@@ -2727,37 +2729,59 @@ njs_parser_trace_handler(nxt_trace_t *tr
 }
 
 
-void
-njs_parser_error(njs_vm_t *vm, njs_parser_t *parser, njs_value_type_t type,
-    const char *fmt, ...)
+static void
+njs_parser_scope_error(njs_vm_t *vm, njs_parser_scope_t *scope,
+    njs_value_type_t type, uint32_t line, const char *fmt, va_list args)
 {
-    size_t       width;
-    u_char       *p, *end;
-    u_char       msg[NXT_MAX_ERROR_STR];
-    va_list      args;
-    njs_lexer_t  *lexer;
+    size_t     width;
+    u_char     msg[NXT_MAX_ERROR_STR];
+    u_char     *p, *end;
+    nxt_str_t  *file;
+
+    file = &scope->file;
 
     p = msg;
     end = msg + NXT_MAX_ERROR_STR;
 
-    va_start(args, fmt);
     p = nxt_vsprintf(p, end, fmt, args);
-    va_end(args);
-
-    lexer = parser->lexer;
-
-    width = nxt_length(" in ") + lexer->file.length + NXT_INT_T_LEN;
+
+    width = nxt_length(" in ") + file->length + NXT_INT_T_LEN;
 
     if (p > end - width) {
         p = end - width;
     }
 
-    if (lexer->file.start != NULL) {
-        p = nxt_sprintf(p, end, " in %V:%uD", &lexer->file, lexer->line);
+    if (file->start != NULL) {
+        p = nxt_sprintf(p, end, " in %V:%uD", file, line);
 
     } else {
-        p = nxt_sprintf(p, end, " in %uD", lexer->line);
+        p = nxt_sprintf(p, end, " in %uD", line);
     }
 
     njs_error_new(vm, &vm->retval, type, msg, p - msg);
- }
+}
+
+
+void
+njs_parser_lexer_error(njs_vm_t *vm, njs_parser_t *parser,
+    njs_value_type_t type, const char *fmt, ...)
+{
+    va_list  args;
+
+    va_start(args, fmt);
+    njs_parser_scope_error(vm, parser->scope, type, parser->lexer->line, fmt,
+                           args);
+    va_end(args);
+}
+
+
+void
+njs_parser_node_error(njs_vm_t *vm, njs_parser_node_t *node,
+    njs_value_type_t type, const char *fmt, ...)
+{
+    va_list  args;
+
+    va_start(args, fmt);
+    njs_parser_scope_error(vm, node->scope, type, node->token_line, fmt, args);
+    va_end(args);
+}
diff -r 2a425a8ea90f -r 3852b2044dfd njs/njs_parser.h
--- a/njs/njs_parser.h	Thu Feb 14 14:40:50 2019 +0300
+++ b/njs/njs_parser.h	Thu Feb 14 15:21:02 2019 +0300
@@ -329,14 +329,19 @@ njs_index_t njs_variable_index(njs_vm_t 
 nxt_bool_t njs_parser_has_side_effect(njs_parser_node_t *node);
 u_char *njs_parser_trace_handler(nxt_trace_t *trace, nxt_trace_data_t *td,
     u_char *start);
-void njs_parser_error(njs_vm_t *vm, njs_parser_t *parser,
+void njs_parser_lexer_error(njs_vm_t *vm, njs_parser_t *parser,
+    njs_value_type_t type, const char *fmt, ...);
+void njs_parser_node_error(njs_vm_t *vm, njs_parser_node_t *node,
     njs_value_type_t type, const char *fmt, ...);
 
-#define njs_parser_syntax_error(vm, parser, fmt, ...)  \
-    njs_parser_error(vm, parser, NJS_OBJECT_SYNTAX_ERROR, fmt, ##__VA_ARGS__)
 
-#define njs_parser_ref_error(vm, parser, fmt, ...)  \
-    njs_parser_error(vm, parser, NJS_OBJECT_REF_ERROR, fmt, ##__VA_ARGS__)
+#define njs_parser_syntax_error(vm, parser, fmt, ...)                         \
+    njs_parser_lexer_error(vm, parser, NJS_OBJECT_SYNTAX_ERROR, fmt,          \
+                           ##__VA_ARGS__)
+
+
+#define njs_parser_ref_error(vm, parser, fmt, ...)                            \
+    njs_parser_lexer_error(vm, parser, NJS_OBJECT_REF_ERROR, fmt, ##__VA_ARGS__)
 
 
 nxt_inline njs_parser_node_t *
diff -r 2a425a8ea90f -r 3852b2044dfd njs/njs_variable.c
--- a/njs/njs_variable.c	Thu Feb 14 14:40:50 2019 +0300
+++ b/njs/njs_variable.c	Thu Feb 14 15:21:02 2019 +0300
@@ -344,7 +344,8 @@ njs_variable_resolve(njs_vm_t *vm, njs_p
 
 not_found:
 
-    njs_parser_ref_error(vm, vm->parser, "\"%V\" is not defined", &vr->name);
+    njs_parser_node_error(vm, node, NJS_OBJECT_REF_ERROR,
+                          "\"%V\" is not defined", &vr->name);
 
     return NULL;
 }
diff -r 2a425a8ea90f -r 3852b2044dfd njs/test/njs_unit_test.c
--- a/njs/test/njs_unit_test.c	Thu Feb 14 14:40:50 2019 +0300
+++ b/njs/test/njs_unit_test.c	Thu Feb 14 15:21:02 2019 +0300
@@ -2107,6 +2107,12 @@ static njs_unit_test_t  njs_test[] =
     { nxt_string("a"),
       nxt_string("ReferenceError: \"a\" is not defined in 1") },
 
+    { nxt_string("\na"),
+      nxt_string("ReferenceError: \"a\" is not defined in 2") },
+
+    { nxt_string("\n\na"),
+      nxt_string("ReferenceError: \"a\" is not defined in 3") },
+
     { nxt_string("a + a"),
       nxt_string("ReferenceError: \"a\" is not defined in 1") },
 


More information about the nginx-devel mailing list