[njs] Added support for ArrayBuffer in TextDecoder.prototype.decode().

Alexander Borisov alexander.borisov at nginx.com
Mon Sep 7 14:57:02 UTC 2020


details:   https://hg.nginx.org/njs/rev/4ef85ee61700
branches:  
changeset: 1520:4ef85ee61700
user:      Alexander Borisov <alexander.borisov at nginx.com>
date:      Mon Sep 07 17:55:10 2020 +0300
description:
Added support for ArrayBuffer in TextDecoder.prototype.decode().

This closes #331 issue on Github.

diffstat:

 src/njs_encoding.c       |  43 ++++++++++++++++++++++++++-----------------
 src/test/njs_unit_test.c |   6 ++++++
 2 files changed, 32 insertions(+), 17 deletions(-)

diffs (80 lines):

diff -r 9a25433b4c76 -r 4ef85ee61700 src/njs_encoding.c
--- a/src/njs_encoding.c	Mon Sep 07 17:54:47 2020 +0300
+++ b/src/njs_encoding.c	Mon Sep 07 17:55:10 2020 +0300
@@ -532,16 +532,17 @@ static njs_int_t
 njs_text_decoder_decode(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
     njs_index_t unused)
 {
-    u_char                   *dst;
-    size_t                   size;
-    ssize_t                  length;
-    njs_int_t                ret;
-    njs_bool_t               stream;
-    njs_value_t              retval, *this, *typed_array, *options;
-    const u_char             *start, *end;
-    njs_unicode_decode_t     ctx;
-    njs_encoding_decode_t    *data;
-    const njs_typed_array_t  *array;
+    u_char                    *dst;
+    size_t                    size;
+    ssize_t                   length;
+    njs_int_t                 ret;
+    njs_bool_t                stream;
+    njs_value_t               retval, *this, *value, *options;
+    const u_char              *start, *end;
+    njs_unicode_decode_t      ctx;
+    njs_encoding_decode_t     *data;
+    const njs_typed_array_t   *array;
+    const njs_array_buffer_t  *buffer;
 
     static const njs_value_t  stream_str = njs_string("stream");
 
@@ -558,17 +559,25 @@ njs_text_decoder_decode(njs_vm_t *vm, nj
     }
 
     if (njs_fast_path(nargs > 1)) {
-        typed_array = njs_argument(args, 1);
-        if (njs_slow_path(!njs_is_typed_array(typed_array))) {
+        value = njs_argument(args, 1);
+
+        if (njs_is_typed_array(value)) {
+            array = njs_typed_array(value);
+
+            start = array->buffer->u.u8;
+            end = start + array->byte_length;
+
+        } else if (njs_is_array_buffer(value)) {
+            buffer = njs_array_buffer(value);
+
+            start = buffer->u.u8;
+            end = start + buffer->size;
+
+        } else {
             njs_type_error(vm, "The \"input\" argument must be an instance "
                            "of TypedArray");
             return NJS_ERROR;
         }
-
-        array = njs_typed_array(typed_array);
-
-        start = array->buffer->u.u8;
-        end = start + array->byte_length;
     }
 
     if (nargs > 2) {
diff -r 9a25433b4c76 -r 4ef85ee61700 src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c	Mon Sep 07 17:54:47 2020 +0300
+++ b/src/test/njs_unit_test.c	Mon Sep 07 17:55:10 2020 +0300
@@ -18327,6 +18327,12 @@ static njs_unit_test_t  njs_test[] =
 
     { njs_str("TextDecoder.prototype.decode.apply({}, new Uint8Array([1]))"),
       njs_str("TypeError: \"this\" is not a TextDecoder") },
+
+    { njs_str("var de = new TextDecoder();"
+              "var buf = new Uint32Array([1,2,3]).buffer;"
+              "var en = new TextEncoder();"
+              "njs.dump(en.encode(de.decode(buf)))"),
+      njs_str("Uint8Array [1,0,0,0,2,0,0,0,3,0,0,0]") },
 };
 
 


More information about the nginx-devel mailing list