]> git.kaiwu.me - njs.git/commitdiff
Added support for ArrayBuffer in TextDecoder.prototype.decode().
authorAlexander Borisov <alexander.borisov@nginx.com>
Mon, 7 Sep 2020 14:55:10 +0000 (17:55 +0300)
committerAlexander Borisov <alexander.borisov@nginx.com>
Mon, 7 Sep 2020 14:55:10 +0000 (17:55 +0300)
This closes #331 issue on Github.

src/njs_encoding.c
src/test/njs_unit_test.c

index c68ecfad9e6783ac9f841b2307f5862acc902b38..3cc7cb63b13f52f7d52b1da969d6c4c97fd969c1 100644 (file)
@@ -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, njs_value_t *args, njs_uint_t nargs,
     }
 
     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) {
index 39f82634d66babb7b7a10f18b2a4e8e64b367d9f..ed59118ab39ca25851250bda38830a3ce132ca1c 100644 (file)
@@ -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]") },
 };