[njs] Refactored $262 object as external.

Dmitry Volyntsev xeioex at nginx.com
Sat May 6 03:11:02 UTC 2023


details:   https://hg.nginx.org/njs/rev/314a2a9fe9c7
branches:  
changeset: 2105:314a2a9fe9c7
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri May 05 20:08:55 2023 -0700
description:
Refactored $262 object as external.

This allows to decouple $262 object which is only needed
for tests from the njs core.

diffstat:

 auto/cc                       |   4 --
 auto/help                     |   2 -
 auto/options                  |   2 -
 src/njs_builtin.c             |  52 ----------------------------
 src/njs_vm.h                  |   3 -
 src/test/njs_externals_test.c |  78 +++++++++++++++++++++++++++++++++++++++++++
 src/test/njs_externals_test.h |   1 +
 src/test/njs_unit_test.c      |  22 ++++++------
 8 files changed, 90 insertions(+), 74 deletions(-)

diffs (311 lines):

diff -r 4c4e5b60c766 -r 314a2a9fe9c7 auto/cc
--- a/auto/cc	Thu May 04 22:15:46 2023 -0700
+++ b/auto/cc	Fri May 05 20:08:55 2023 -0700
@@ -185,9 +185,5 @@ if [ "$NJS_DEBUG_GENERATOR" = "YES" ]; t
         njs_define=NJS_DEBUG_GENERATOR . auto/define
 fi
 
-if [ "$NJS_TEST262" = "YES" ]; then
-        njs_define=NJS_TEST262 . auto/define
-fi
-
 # Stop on error exit status again.
 set -e
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 auto/help
--- a/auto/help	Thu May 04 22:15:46 2023 -0700
+++ b/auto/help	Fri May 05 20:08:55 2023 -0700
@@ -55,6 +55,4 @@ default: "$NJS_DEBUG_MEMORY"
 default: "$NJS_DEBUG_OPCODE"
   --debug-generator=YES     enables generator debug, \
 default: "$NJS_DEBUG_GENERATOR"
-  --test262=YES             enables test262 extentions, \
-default: "$NJS_TEST262"
 END
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 auto/options
--- a/auto/options	Thu May 04 22:15:46 2023 -0700
+++ b/auto/options	Fri May 05 20:08:55 2023 -0700
@@ -13,7 +13,6 @@ NJS_DEBUG_GENERATOR=NO
 
 NJS_ADDRESS_SANITIZER=NO
 NJS_ADDR2LINE=NO
-NJS_TEST262=YES
 
 NJS_OPENSSL=YES
 NJS_LIBXML2=YES
@@ -47,7 +46,6 @@ do
         --debug-memory=*)                NJS_DEBUG_MEMORY="$value"           ;;
         --debug-opcode=*)                NJS_DEBUG_OPCODE="$value"           ;;
         --debug-generator=*)             NJS_DEBUG_GENERATOR="$value"        ;;
-        --test262=*)                     NJS_TEST262="$value"                ;;
 
         --no-openssl)                    NJS_OPENSSL=NO                      ;;
         --no-libxml2)                    NJS_LIBXML2=NO                      ;;
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 src/njs_builtin.c
--- a/src/njs_builtin.c	Thu May 04 22:15:46 2023 -0700
+++ b/src/njs_builtin.c	Fri May 05 20:08:55 2023 -0700
@@ -35,9 +35,6 @@ static njs_int_t njs_env_hash_init(njs_v
 static const njs_object_init_t  njs_global_this_init;
 static const njs_object_init_t  njs_njs_object_init;
 static const njs_object_init_t  njs_process_object_init;
-#ifdef NJS_TEST262
-static const njs_object_init_t  njs_262_object_init;
-#endif
 
 
 static const njs_object_init_t  *njs_object_init[] = {
@@ -46,9 +43,6 @@ static const njs_object_init_t  *njs_obj
     &njs_process_object_init,
     &njs_math_object_init,
     &njs_json_object_init,
-#ifdef NJS_TEST262
-    &njs_262_object_init,
-#endif
     NULL
 };
 
@@ -1690,49 +1684,3 @@ static const njs_object_init_t  njs_proc
     njs_process_object_properties,
     njs_nitems(njs_process_object_properties),
 };
-
-
-#if (NJS_TEST262)
-
-static njs_int_t
-njs_262_detach_array_buffer(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t unused, njs_value_t *retval)
-{
-    njs_value_t         *value;
-    njs_array_buffer_t  *buffer;
-
-    value = njs_arg(args, nargs, 1);
-    if (njs_slow_path(!njs_is_array_buffer(value))) {
-        njs_type_error(vm, "\"this\" is not an ArrayBuffer");
-        return NJS_ERROR;
-    }
-
-    buffer = njs_array_buffer(value);
-    buffer->u.data = NULL;
-    buffer->size = 0;
-
-    njs_set_null(retval);
-
-    return NJS_OK;
-}
-
-static const njs_object_prop_t  njs_262_object_properties[] =
-{
-    {
-        .type = NJS_PROPERTY,
-        .name = njs_wellknown_symbol(NJS_SYMBOL_TO_STRING_TAG),
-        .u.value = njs_string("$262"),
-        .configurable = 1,
-    },
-
-    NJS_DECLARE_PROP_LNATIVE("detachArrayBuffer", njs_262_detach_array_buffer,
-                             2, 0),
-};
-
-
-static const njs_object_init_t  njs_262_object_init = {
-    njs_262_object_properties,
-    njs_nitems(njs_262_object_properties),
-};
-
-#endif
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 src/njs_vm.h
--- a/src/njs_vm.h	Thu May 04 22:15:46 2023 -0700
+++ b/src/njs_vm.h	Fri May 05 20:08:55 2023 -0700
@@ -95,9 +95,6 @@ enum njs_object_e {
     NJS_OBJECT_PROCESS,
     NJS_OBJECT_MATH,
     NJS_OBJECT_JSON,
-#ifdef NJS_TEST262
-    NJS_OBJECT_262,
-#endif
     NJS_OBJECT_MAX
 };
 
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c	Thu May 04 22:15:46 2023 -0700
+++ b/src/test/njs_externals_test.c	Fri May 05 20:08:55 2023 -0700
@@ -547,6 +547,53 @@ njs_unit_test_constructor(njs_vm_t *vm, 
 }
 
 
+static njs_int_t
+njs_262_detach_array_buffer(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
+    njs_index_t unused, njs_value_t *retval)
+{
+    njs_value_t         *value;
+    njs_array_buffer_t  *buffer;
+
+    value = njs_arg(args, nargs, 1);
+    if (njs_slow_path(!njs_is_array_buffer(value))) {
+        njs_type_error(vm, "\"this\" is not an ArrayBuffer");
+        return NJS_ERROR;
+    }
+
+    buffer = njs_array_buffer(value);
+    buffer->u.data = NULL;
+    buffer->size = 0;
+
+    njs_set_null(retval);
+
+    return NJS_OK;
+}
+
+
+static njs_external_t  njs_unit_test_262_external[] = {
+
+    {
+        .flags = NJS_EXTERN_PROPERTY | NJS_EXTERN_SYMBOL,
+        .name.symbol = NJS_SYMBOL_TO_STRING_TAG,
+        .u.property = {
+            .value = "$262",
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("detachArrayBuffer"),
+        .writable = 1,
+        .configurable = 1,
+        .enumerable = 1,
+        .u.method = {
+            .native = njs_262_detach_array_buffer,
+        }
+    },
+
+};
+
+
 static njs_external_t  njs_unit_test_r_c[] = {
 
     {
@@ -931,6 +978,37 @@ njs_externals_init_internal(njs_vm_t *vm
 
 
 njs_int_t
+njs_externals_262_init(njs_vm_t *vm)
+{
+    njs_int_t           ret, proto_id;
+    njs_opaque_value_t  value;
+
+    static const njs_str_t  dollar_262 = njs_str("$262");
+
+    proto_id = njs_vm_external_prototype(vm, njs_unit_test_262_external,
+                                       njs_nitems(njs_unit_test_262_external));
+    if (njs_slow_path(proto_id < 0)) {
+        njs_printf("njs_vm_external_prototype() failed\n");
+        return NJS_ERROR;
+    }
+
+    ret = njs_vm_external_create(vm, njs_value_arg(&value), proto_id, NULL, 1);
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_printf("njs_vm_external_create() failed\n");
+        return NJS_ERROR;
+    }
+
+    ret = njs_vm_bind(vm, &dollar_262, njs_value_arg(&value), 1);
+    if (njs_slow_path(ret != NJS_OK)) {
+        njs_printf("njs_vm_bind() failed\n");
+        return NJS_ERROR;
+    }
+
+    return NJS_OK;
+}
+
+
+njs_int_t
 njs_externals_shared_init(njs_vm_t *vm)
 {
     return njs_externals_init_internal(vm, njs_test_requests, 1, 1);
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 src/test/njs_externals_test.h
--- a/src/test/njs_externals_test.h	Thu May 04 22:15:46 2023 -0700
+++ b/src/test/njs_externals_test.h	Fri May 05 20:08:55 2023 -0700
@@ -25,6 +25,7 @@ typedef struct {
 
 
 njs_int_t njs_externals_shared_init(njs_vm_t *vm);
+njs_int_t njs_externals_262_init(njs_vm_t *vm);
 njs_int_t njs_externals_init(njs_vm_t *vm);
 njs_int_t njs_external_env_init(njs_external_env_t *env);
 njs_int_t njs_external_call(njs_vm_t *vm, const njs_str_t *fname,
diff -r 4c4e5b60c766 -r 314a2a9fe9c7 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Thu May 04 22:15:46 2023 -0700
+++ b/src/test/njs_unit_test.c	Fri May 05 20:08:55 2023 -0700
@@ -6559,12 +6559,10 @@ static njs_unit_test_t  njs_test[] =
               "           return a.map(q=>q/2).join('|') === '3|2|1'})"),
       njs_str("true") },
 
-#ifdef NJS_TEST262
     { njs_str("const arr = new Uint8Array([1,2,3]);"
               "const sep = {toString(){$262.detachArrayBuffer(arr.buffer); return ','}};"
               "arr.join(sep)"),
       njs_str("TypeError: detached buffer") },
-#endif
 
     { njs_str("Uint8Array.prototype.reduce.call(1)"),
       njs_str("TypeError: this is not a typed array") },
@@ -22519,12 +22517,6 @@ static njs_unit_test_t  njs_externals_te
     { njs_str("$r2.uri == 'αβγ' && $r2.uri === 'αβγ'"),
       njs_str("true") },
 
-#if (NJS_TEST262)
-#define N262 "$262,"
-#else
-#define N262 ""
-#endif
-
 #if (NJS_HAVE_OPENSSL)
 #define NCRYPTO "crypto,"
 #else
@@ -22532,7 +22524,7 @@ static njs_unit_test_t  njs_externals_te
 #endif
 
     { njs_str("Object.keys(this).sort()"),
-      njs_str(N262 "$r,$r2,$r3,$shared,ExternalConstructor," NCRYPTO "global,njs,process") },
+      njs_str("$262,$r,$r2,$r3,$shared,ExternalConstructor," NCRYPTO "global,njs,process") },
 
     { njs_str("Object.getOwnPropertySymbols($r2)[0] == Symbol.toStringTag"),
       njs_str("true") },
@@ -23195,14 +23187,12 @@ static njs_unit_test_t  njs_backtraces_t
               "    at Math.max (native)\n"
               "    at main (:1)\n") },
 
-#ifdef NJS_TEST262
     { njs_str("var ab = new ArrayBuffer(1);"
               "$262.detachArrayBuffer(ab);"
               "ab.byteLength"),
       njs_str("TypeError: detached buffer\n"
               "    at ArrayBuffer.prototype.byteLength (native)\n"
               "    at main (:1)\n") },
-#endif
 
     { njs_str("Object.prototype()"),
       njs_str("TypeError: (intermediate value)[\"prototype\"] is not a function\n"
@@ -23674,6 +23664,11 @@ njs_unit_test(njs_unit_test_t tests[], s
             goto done;
         }
 
+        ret = njs_externals_262_init(vm);
+        if (ret != NJS_OK) {
+            goto done;
+        }
+
         if (opts->externals) {
             ret = njs_externals_shared_init(vm);
             if (ret != NJS_OK) {
@@ -23808,6 +23803,11 @@ njs_interactive_test(njs_unit_test_t tes
             goto done;
         }
 
+        ret = njs_externals_262_init(vm);
+        if (ret != NJS_OK) {
+            goto done;
+        }
+
         if (opts->externals) {
             ret = njs_externals_shared_init(vm);
             if (ret != NJS_OK) {


More information about the nginx-devel mailing list