[njs] Fixed format of the reference exception.

Alexander Borisov alexander.borisov at nginx.com
Mon May 27 17:32:43 UTC 2019


details:   https://hg.nginx.org/njs/rev/7ae4f639fa43
branches:  
changeset: 985:7ae4f639fa43
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Mon May 27 19:06:34 2019 +0300
description:
Fixed format of the reference exception.

Previously, it did not include filename.

diffstat:

 njs/njs_generator.c          |   6 ++++++
 njs/njs_vm.c                 |  13 +++++++++++--
 njs/njs_vm.h                 |   1 +
 njs/test/njs_expect_test.exp |  13 +++++++++++--
 4 files changed, 29 insertions(+), 4 deletions(-)

diffs (91 lines):

diff -r ff51b3e9ea05 -r 7ae4f639fa43 njs/njs_generator.c
--- a/njs/njs_generator.c	Wed May 22 07:54:58 2019 +0800
+++ b/njs/njs_generator.c	Mon May 27 19:06:34 2019 +0300
@@ -3288,6 +3288,7 @@ static nxt_int_t
 njs_generate_reference_error(njs_vm_t *vm, njs_generator_t *generator,
                              njs_parser_node_t *node)
 {
+    njs_ret_t                     ret;
     njs_vmcode_reference_error_t  *ref_err;
 
     if (nxt_slow_path(!node->u.reference.not_defined)) {
@@ -3301,6 +3302,11 @@ njs_generate_reference_error(njs_vm_t *v
 
     ref_err->token_line = node->token_line;
 
+    ret = njs_name_copy(vm, &ref_err->file, &node->scope->file);
+    if (nxt_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
     return njs_name_copy(vm, &ref_err->name, &node->u.reference.name);
 }
 
diff -r ff51b3e9ea05 -r 7ae4f639fa43 njs/njs_vm.c
--- a/njs/njs_vm.c	Wed May 22 07:54:58 2019 +0800
+++ b/njs/njs_vm.c	Mon May 27 19:06:34 2019 +0300
@@ -2659,12 +2659,21 @@ njs_ret_t
 njs_vmcode_reference_error(njs_vm_t *vm, njs_value_t *invld1,
     njs_value_t *invld2)
 {
+    nxt_str_t                     *file;
     njs_vmcode_reference_error_t  *ref_err;
 
     ref_err = (njs_vmcode_reference_error_t *) vm->current;
 
-    njs_reference_error(vm, "\"%V\" is not defined in %uD", &ref_err->name,
-                        ref_err->token_line);
+    file = &ref_err->file;
+
+    if (file->length != 0 && !vm->options.quiet) {
+        njs_reference_error(vm, "\"%V\" is not defined in %V:%uD",
+                            &ref_err->name, file, ref_err->token_line);
+
+    } else {
+        njs_reference_error(vm, "\"%V\" is not defined in %uD", &ref_err->name,
+                            ref_err->token_line);
+    }
 
     return NJS_ERROR;
 }
diff -r ff51b3e9ea05 -r 7ae4f639fa43 njs/njs_vm.h
--- a/njs/njs_vm.h	Wed May 22 07:54:58 2019 +0800
+++ b/njs/njs_vm.h	Mon May 27 19:06:34 2019 +0300
@@ -857,6 +857,7 @@ typedef struct {
 typedef struct {
     njs_vmcode_t               code;
     nxt_str_t                  name;
+    nxt_str_t                  file;
     uint32_t                   token_line;
 } njs_vmcode_reference_error_t;
 
diff -r ff51b3e9ea05 -r 7ae4f639fa43 njs/test/njs_expect_test.exp
--- a/njs/test/njs_expect_test.exp	Wed May 22 07:54:58 2019 +0800
+++ b/njs/test/njs_expect_test.exp	Mon May 27 19:06:34 2019 +0300
@@ -668,6 +668,17 @@ njs_test {
 
 # modules
 
+# FIXME:
+# During import, the variable is declared regardless of the result of the import.
+# Because of this, in the console mode, checking the variable after the import
+# error may give an incorrect result.
+#
+# For example:
+# {"import ref from 'ref_exception.js'\r\n"
+# "ReferenceError: \"undeclared\" is not defined in ref_exception.js:1"}
+# {"ref\r\n"
+# "ReferenceError: \"ref\" is not defined in shell:1\r\n"}
+
 njs_test {
     {"import lib1 from 'lib1.js'; import lib2 from 'lib1.js'\r\n"
      "undefined\r\n"}
@@ -685,8 +696,6 @@ njs_test {
      "Non-default export is not supported in export_non_default.js:3\r\n"}
     {"import ref from 'ref_exception.js'\r\n"
      "ReferenceError: \"undeclared\" is not defined in ref_exception.js:1"}
-    {"ref\r\n"
-     "ReferenceError: \"ref\" is not defined in shell:1\r\n"}
     {"var ref\r\n"
      "undefined\r\n"}
     {"import ref from 'ref_exception.js'\r\n"


More information about the nginx-devel mailing list