[njs] Shell: added $262 as external in CLI.

Dmitry Volyntsev xeioex at nginx.com
Wed May 10 02:00:08 UTC 2023


details:   https://hg.nginx.org/njs/rev/70e7701a4588
branches:  
changeset: 2112:70e7701a4588
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue May 09 18:58:52 2023 -0700
description:
Shell: added $262 as external in CLI.

diffstat:

 external/njs_shell.c          |  60 +++++++++++++++++++++++++++++++++++-------
 src/njs_array_buffer.c        |  25 +++++++++++++++++
 src/njs_array_buffer.h        |   2 +
 src/test/njs_externals_test.c |  29 +++-----------------
 4 files changed, 82 insertions(+), 34 deletions(-)

diffs (205 lines):

diff -r fc8d1b125cef -r 70e7701a4588 external/njs_shell.c
--- a/external/njs_shell.c	Tue May 09 18:18:33 2023 -0700
+++ b/external/njs_shell.c	Tue May 09 18:58:52 2023 -0700
@@ -145,6 +145,9 @@ static njs_int_t lvlhsh_key_test(njs_lvl
 static void *lvlhsh_pool_alloc(void *pool, size_t size);
 static void lvlhsh_pool_free(void *pool, void *p, size_t size);
 
+njs_int_t njs_array_buffer_detach(njs_vm_t *vm, njs_value_t *args,
+    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
+
 
 static njs_external_t  njs_ext_console[] = {
 
@@ -204,6 +207,30 @@ static njs_external_t  njs_ext_console[]
 };
 
 
+static njs_external_t  njs_ext_262[] = {
+
+    {
+        .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_array_buffer_detach,
+        }
+    },
+
+};
+
+
 static const njs_lvlhsh_proto_t  lvlhsh_proto  njs_aligned(64) = {
     NJS_LVLHSH_LARGE_SLAB,
     lvlhsh_key_test,
@@ -779,12 +806,12 @@ njs_console_init(njs_vm_t *vm, njs_conso
 static njs_int_t
 njs_externals_init(njs_vm_t *vm)
 {
-    njs_int_t           ret;
-    njs_value_t         *value;
+    njs_int_t           ret, proto_id;
     njs_console_t       *console;
-    njs_opaque_value_t  method;
+    njs_opaque_value_t  value, method;
 
     static const njs_str_t  console_name = njs_str("console");
+    static const njs_str_t  dollar_262 = njs_str("$262");
     static const njs_str_t  print_name = njs_str("print");
     static const njs_str_t  console_log = njs_str("console.log");
 
@@ -797,17 +824,13 @@ njs_externals_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
-    value = njs_mp_zalloc(njs_vm_memory_pool(vm), sizeof(njs_opaque_value_t));
-    if (njs_slow_path(value == NULL)) {
-        return NJS_ERROR;
-    }
-
-    ret = njs_vm_external_create(vm, value, njs_console_proto_id, console, 0);
+    ret = njs_vm_external_create(vm, njs_value_arg(&value),
+                                 njs_console_proto_id, console, 0);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
 
-    ret = njs_vm_bind(vm, &console_name, value, 0);
+    ret = njs_vm_bind(vm, &console_name, njs_value_arg(&value), 0);
     if (njs_slow_path(ret != NJS_OK)) {
         return NJS_ERROR;
     }
@@ -827,6 +850,23 @@ njs_externals_init(njs_vm_t *vm)
         return NJS_ERROR;
     }
 
+    proto_id = njs_vm_external_prototype(vm, njs_ext_262,
+                                         njs_nitems(njs_ext_262));
+    if (njs_slow_path(proto_id < 0)) {
+        njs_stderror("failed to add \"$262\" proto\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)) {
+        return NJS_ERROR;
+    }
+
+    ret = njs_vm_bind(vm, &dollar_262, njs_value_arg(&value), 1);
+    if (njs_slow_path(ret != NJS_OK)) {
+        return NJS_ERROR;
+    }
+
     return NJS_OK;
 }
 
diff -r fc8d1b125cef -r 70e7701a4588 src/njs_array_buffer.c
--- a/src/njs_array_buffer.c	Tue May 09 18:18:33 2023 -0700
+++ b/src/njs_array_buffer.c	Tue May 09 18:58:52 2023 -0700
@@ -240,6 +240,31 @@ njs_array_buffer_prototype_slice(njs_vm_
 }
 
 
+njs_int_t
+njs_array_buffer_detach(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_array_buffer_prototype_properties[] =
 {
     NJS_DECLARE_PROP_HANDLER("constructor",
diff -r fc8d1b125cef -r 70e7701a4588 src/njs_array_buffer.h
--- a/src/njs_array_buffer.h	Tue May 09 18:18:33 2023 -0700
+++ b/src/njs_array_buffer.h	Tue May 09 18:58:52 2023 -0700
@@ -15,6 +15,8 @@
 njs_array_buffer_t *njs_array_buffer_alloc(njs_vm_t *vm, uint64_t size,
     njs_bool_t zeroing);
 njs_int_t njs_array_buffer_writable(njs_vm_t *vm, njs_array_buffer_t *buffer);
+NJS_EXPORT njs_int_t njs_array_buffer_detach(njs_vm_t *vm, njs_value_t *args,
+    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 
 njs_inline njs_array_buffer_t *
 njs_array_buffer_slice(njs_vm_t *vm, njs_array_buffer_t *this, int64_t start,
diff -r fc8d1b125cef -r 70e7701a4588 src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c	Tue May 09 18:18:33 2023 -0700
+++ b/src/test/njs_externals_test.c	Tue May 09 18:58:52 2023 -0700
@@ -26,6 +26,10 @@ typedef struct {
 } njs_unit_test_prop_t;
 
 
+njs_int_t njs_array_buffer_detach(njs_vm_t *vm, njs_value_t *args,
+    njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
+
+
 static njs_int_t    njs_external_r_proto_id;
 
 
@@ -548,29 +552,6 @@ 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_int_t
 njs_262_bytes_from_array_like(njs_vm_t *vm, njs_value_t *value,
     njs_value_t *retval)
 {
@@ -749,7 +730,7 @@ static njs_external_t  njs_unit_test_262
         .configurable = 1,
         .enumerable = 1,
         .u.method = {
-            .native = njs_262_detach_array_buffer,
+            .native = njs_array_buffer_detach,
         }
     },
 


More information about the nginx-devel mailing list