[njs] Modules: added support for Buffer object where string is expected.

Dmitry Volyntsev xeioex at nginx.com
Wed Nov 18 08:32:49 UTC 2020


details:   https://hg.nginx.org/njs/rev/e97f76121196
branches:  
changeset: 1567:e97f76121196
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Tue Nov 17 13:22:34 2020 +0000
description:
Modules: added support for Buffer object where string is expected.

diffstat:

 nginx/ngx_http_js_module.c    |   2 +-
 nginx/ngx_js.c                |   2 +-
 nginx/ngx_stream_js_module.c  |   8 +----
 src/njs.h                     |   6 ++++
 src/njs_vm.c                  |  61 +++++++++++++++++++++++++++++++++++++++++++
 src/test/njs_externals_test.c |   8 ++--
 6 files changed, 75 insertions(+), 12 deletions(-)

diffs (175 lines):

diff -r 24717ec294b8 -r e97f76121196 nginx/ngx_http_js_module.c
--- a/nginx/ngx_http_js_module.c	Tue Nov 17 13:16:47 2020 +0000
+++ b/nginx/ngx_http_js_module.c	Tue Nov 17 13:22:34 2020 +0000
@@ -2371,7 +2371,7 @@ ngx_http_js_ext_subrequest(njs_vm_t *vm,
     arg = njs_arg(args, nargs, 2);
 
     if (njs_value_is_string(arg)) {
-        if (njs_vm_value_to_string(vm, &args_arg, arg) != NJS_OK) {
+        if (ngx_js_string(vm, arg, &args_arg) != NJS_OK) {
             njs_vm_error(vm, "failed to convert args");
             return NJS_ERROR;
         }
diff -r 24717ec294b8 -r e97f76121196 nginx/ngx_js.c
--- a/nginx/ngx_js.c	Tue Nov 17 13:16:47 2020 +0000
+++ b/nginx/ngx_js.c	Tue Nov 17 13:22:34 2020 +0000
@@ -105,7 +105,7 @@ ngx_int_t
 ngx_js_string(njs_vm_t *vm, njs_value_t *value, njs_str_t *str)
 {
     if (value != NULL && !njs_value_is_null_or_undefined(value)) {
-        if (njs_vm_value_to_string(vm, str, value) == NJS_ERROR) {
+        if (njs_vm_value_to_bytes(vm, str, value) == NJS_ERROR) {
             return NGX_ERROR;
         }
 
diff -r 24717ec294b8 -r e97f76121196 nginx/ngx_stream_js_module.c
--- a/nginx/ngx_stream_js_module.c	Tue Nov 17 13:16:47 2020 +0000
+++ b/nginx/ngx_stream_js_module.c	Tue Nov 17 13:22:34 2020 +0000
@@ -942,9 +942,7 @@ ngx_stream_js_ext_on(njs_vm_t *vm, njs_v
         return NJS_ERROR;
     }
 
-    if (njs_vm_value_to_string(vm, &name, njs_arg(args, nargs, 1))
-        == NJS_ERROR)
-    {
+    if (ngx_js_string(vm, njs_arg(args, nargs, 1), &name) == NJS_ERROR) {
         njs_vm_error(vm, "failed to convert event arg");
         return NJS_ERROR;
     }
@@ -991,9 +989,7 @@ ngx_stream_js_ext_off(njs_vm_t *vm, njs_
         return NJS_ERROR;
     }
 
-    if (njs_vm_value_to_string(vm, &name, njs_arg(args, nargs, 1))
-        == NJS_ERROR)
-    {
+    if (ngx_js_string(vm, njs_arg(args, nargs, 1), &name) == NJS_ERROR) {
         njs_vm_error(vm, "failed to convert event arg");
         return NJS_ERROR;
     }
diff -r 24717ec294b8 -r e97f76121196 src/njs.h
--- a/src/njs.h	Tue Nov 17 13:16:47 2020 +0000
+++ b/src/njs.h	Tue Nov 17 13:22:34 2020 +0000
@@ -339,6 +339,12 @@ NJS_EXPORT njs_int_t njs_vm_value_buffer
     const u_char *start, uint32_t size);
 
 /*
+ * Converts a value to bytes.
+ */
+NJS_EXPORT njs_int_t njs_vm_value_to_bytes(njs_vm_t *vm, njs_str_t *dst,
+    njs_value_t *src);
+
+/*
  * Converts a value to string.
  */
 NJS_EXPORT njs_int_t njs_vm_value_to_string(njs_vm_t *vm, njs_str_t *dst,
diff -r 24717ec294b8 -r e97f76121196 src/njs_vm.c
--- a/src/njs_vm.c	Tue Nov 17 13:16:47 2020 +0000
+++ b/src/njs_vm.c	Tue Nov 17 13:22:34 2020 +0000
@@ -1116,6 +1116,67 @@ njs_vm_value_to_string(njs_vm_t *vm, njs
 
 
 njs_int_t
+njs_vm_value_to_bytes(njs_vm_t *vm, njs_str_t *dst, njs_value_t *src)
+{
+    u_char              *start;
+    size_t              size;
+    njs_int_t           ret;
+    njs_value_t         value;
+    njs_typed_array_t   *array;
+    njs_array_buffer_t  *buffer;
+
+    if (njs_slow_path(src == NULL)) {
+        return NJS_ERROR;
+    }
+
+    ret = NJS_OK;
+    value = *src;
+
+    switch (value.type) {
+    case NJS_TYPED_ARRAY:
+    case NJS_DATA_VIEW:
+        array = njs_typed_array(&value);
+        buffer = njs_typed_array_buffer(array);
+        if (njs_slow_path(njs_is_detached_buffer(buffer))) {
+            njs_type_error(vm, "detached buffer");
+            return NJS_ERROR;
+        }
+
+        dst->start = &buffer->u.u8[array->offset];
+        dst->length = array->byte_length;
+        break;
+
+    default:
+        ret = njs_value_to_string(vm, &value, &value);
+        if (njs_slow_path(ret != NJS_OK)) {
+            return NJS_ERROR;
+        }
+
+        size = value.short_string.size;
+
+        if (size != NJS_STRING_LONG) {
+            start = njs_mp_alloc(vm->mem_pool, size);
+            if (njs_slow_path(start == NULL)) {
+                njs_memory_error(vm);
+                return NJS_ERROR;
+            }
+
+            memcpy(start, value.short_string.start, size);
+
+        } else {
+            size = value.long_string.size;
+            start = value.long_string.data->start;
+        }
+
+        dst->length = size;
+        dst->start = start;
+    }
+
+    return ret;
+}
+
+
+njs_int_t
 njs_vm_value_string_copy(njs_vm_t *vm, njs_str_t *retval,
     njs_value_t *value, uintptr_t *next)
 {
diff -r 24717ec294b8 -r e97f76121196 src/test/njs_externals_test.c
--- a/src/test/njs_externals_test.c	Tue Nov 17 13:16:47 2020 +0000
+++ b/src/test/njs_externals_test.c	Tue Nov 17 13:22:34 2020 +0000
@@ -130,7 +130,7 @@ njs_unit_test_r_uri(njs_vm_t *vm, njs_ob
     field = (njs_str_t *) (p + njs_vm_prop_magic32(prop));
 
     if (setval != NULL) {
-        return njs_vm_value_to_string(vm, field, setval);
+        return njs_vm_value_to_bytes(vm, field, setval);
     }
 
     return njs_vm_value_string_set(vm, retval, field->start, field->length);
@@ -358,7 +358,7 @@ njs_unit_test_r_method(njs_vm_t *vm, njs
         return NJS_ERROR;
     }
 
-    ret = njs_vm_value_to_string(vm, &s, njs_arg(args, nargs, 1));
+    ret = njs_vm_value_to_bytes(vm, &s, njs_arg(args, nargs, 1));
     if (ret == NJS_OK && s.length == 3 && memcmp(s.start, "YES", 3) == 0) {
         return njs_vm_value_string_set(vm, njs_vm_retval(vm), r->uri.start,
                                        r->uri.length);
@@ -388,7 +388,7 @@ njs_unit_test_r_create(njs_vm_t *vm, njs
         goto memory_error;
     }
 
-    if (njs_vm_value_to_string(vm, &sr->uri, njs_arg(args, nargs, 1))
+    if (njs_vm_value_to_bytes(vm, &sr->uri, njs_arg(args, nargs, 1))
         != NJS_OK)
     {
         return NJS_ERROR;
@@ -424,7 +424,7 @@ njs_unit_test_r_bind(njs_vm_t *vm, njs_v
         return NJS_ERROR;
     }
 
-    if (njs_vm_value_to_string(vm, &name, njs_arg(args, nargs, 1)) != NJS_OK) {
+    if (njs_vm_value_to_bytes(vm, &name, njs_arg(args, nargs, 1)) != NJS_OK) {
         return NJS_ERROR;
     }
 


More information about the nginx-devel mailing list