[njs] Allowing to create external objects with NULL external pointer.

Dmitry Volyntsev xeioex at nginx.com
Thu Aug 19 16:18:52 UTC 2021


details:   https://hg.nginx.org/njs/rev/0fb3ced41fdc
branches:  
changeset: 1688:0fb3ced41fdc
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Aug 13 12:20:46 2021 +0000
description:
Allowing to create external objects with NULL external pointer.

diffstat:

 src/njs_extern.c         |   8 ++------
 src/test/njs_unit_test.c |  12 ++++++++++--
 2 files changed, 12 insertions(+), 8 deletions(-)

diffs (75 lines):

diff -r 377743cd9059 -r 0fb3ced41fdc src/njs_extern.c
--- a/src/njs_extern.c	Wed Aug 11 11:44:12 2021 +0800
+++ b/src/njs_extern.c	Fri Aug 13 12:20:46 2021 +0000
@@ -179,12 +179,6 @@ njs_external_prop_handler(njs_vm_t *vm, 
         *retval = *setval;
 
     } else {
-        external = njs_vm_external(vm, NJS_PROTO_ID_ANY, value);
-        if (njs_slow_path(external == NULL)) {
-            njs_value_undefined_set(retval);
-            return NJS_OK;
-        }
-
         ov = njs_mp_alloc(vm->mem_pool, sizeof(njs_object_value_t));
         if (njs_slow_path(ov == NULL)) {
             njs_memory_error(vm);
@@ -203,6 +197,8 @@ njs_external_prop_handler(njs_vm_t *vm, 
         ov->object.__proto__ = &vm->prototypes[NJS_OBJ_TYPE_OBJECT].object;
         ov->object.slots = slots;
 
+        external = njs_vm_external(vm, NJS_PROTO_ID_ANY, value);
+
         njs_set_data(&ov->value, external, njs_value_external_tag(value));
         njs_set_object_value(retval, ov);
     }
diff -r 377743cd9059 -r 0fb3ced41fdc src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Wed Aug 11 11:44:12 2021 +0800
+++ b/src/test/njs_unit_test.c	Fri Aug 13 12:20:46 2021 +0000
@@ -20862,7 +20862,8 @@ static njs_unit_test_t  njs_shared_test[
       njs_str("false") },
 
     { njs_str("isFin()"),
-      njs_str("ReferenceError: \"isFin\" is not defined") },
+      njs_str("ReferenceError: \"isFin\" is not defined\n"
+              "    at main (:1)\n") },
 
     { njs_str("isNaN(function(){})"),
       njs_str("true") },
@@ -20918,6 +20919,11 @@ static njs_unit_test_t  njs_shared_test[
 
     { njs_str("$r.bind('XXX', 37); XXX"),
       njs_str("37") },
+
+    { njs_str("var fs = require('fs'); fs.readFileSync()"),
+      njs_str("TypeError: \"path\" must be a string or Buffer\n"
+              "    at fs.readFileSync (native)\n"
+              "    at main (:1)\n") },
 };
 
 
@@ -21403,6 +21409,7 @@ typedef struct {
     njs_bool_t  module;
     njs_uint_t  repeat;
     njs_bool_t  unsafe;
+    njs_bool_t  backtrace;
 } njs_opts_t;
 
 
@@ -21456,6 +21463,7 @@ njs_unit_test(njs_unit_test_t tests[], s
 
         options.module = opts->module;
         options.unsafe = opts->unsafe;
+        options.backtrace = opts->backtrace;
 
         vm = njs_vm_create(&options);
         if (vm == NULL) {
@@ -22829,7 +22837,7 @@ static njs_test_suite_t  njs_suites[] =
       njs_unit_test },
 
     { njs_str("shared"),
-      { .externals = 1, .repeat = 128, .unsafe = 1 },
+      { .externals = 1, .repeat = 128, .unsafe = 1, .backtrace = 1 },
       njs_shared_test,
       njs_nitems(njs_shared_test),
       njs_unit_test },


More information about the nginx-devel mailing list