From: Alexander Borisov Date: Mon, 7 Sep 2020 14:55:10 +0000 (+0300) Subject: Added support for ArrayBuffer in TextDecoder.prototype.decode(). X-Git-Tag: 0.4.4~11 X-Git-Url: http://git.kaiwu.me/postgresql/log/contrib/postgres_fdw/static/gitweb.js?a=commitdiff_plain;h=0b2427440e7715ccdeec4dd217b5d07b2738214f;p=njs.git Added support for ArrayBuffer in TextDecoder.prototype.decode(). This closes #331 issue on Github. --- diff --git a/src/njs_encoding.c b/src/njs_encoding.c index c68ecfad..3cc7cb63 100644 --- a/src/njs_encoding.c +++ b/src/njs_encoding.c @@ -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) { diff --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 39f82634..ed59118a 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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]") }, };