[njs] Fixed TextDecoder.prototype.decode() with non-zero TypedArray offset.
Alexander Borisov
alexander.borisov at nginx.com
Mon Sep 7 14:57:04 UTC 2020
details: https://hg.nginx.org/njs/rev/f8c8e23d2bbd
branches:
changeset: 1521:f8c8e23d2bbd
user: Alexander Borisov <alexander.borisov at nginx.com>
date: Mon Sep 07 17:55:24 2020 +0300
description:
Fixed TextDecoder.prototype.decode() with non-zero TypedArray offset.
diffstat:
src/njs_encoding.c | 4 ++--
src/njs_typed_array.c | 3 +--
src/njs_typed_array.h | 14 ++++++++++++++
src/test/njs_unit_test.c | 11 +++++++++++
4 files changed, 28 insertions(+), 4 deletions(-)
diffs (86 lines):
diff -r 4ef85ee61700 -r f8c8e23d2bbd src/njs_encoding.c
--- a/src/njs_encoding.c Mon Sep 07 17:55:10 2020 +0300
+++ b/src/njs_encoding.c Mon Sep 07 17:55:24 2020 +0300
@@ -214,7 +214,7 @@ njs_text_encoder_encode_into(njs_vm_t *v
end = start + str.length;
array = njs_typed_array(dest);
- to = njs_typed_array_buffer(array)->u.u8;
+ to = njs_typed_array_start(array);
to_end = to + array->byte_length;
cp = 0;
@@ -564,7 +564,7 @@ njs_text_decoder_decode(njs_vm_t *vm, nj
if (njs_is_typed_array(value)) {
array = njs_typed_array(value);
- start = array->buffer->u.u8;
+ start = njs_typed_array_start(array);
end = start + array->byte_length;
} else if (njs_is_array_buffer(value)) {
diff -r 4ef85ee61700 -r f8c8e23d2bbd src/njs_typed_array.c
--- a/src/njs_typed_array.c Mon Sep 07 17:55:10 2020 +0300
+++ b/src/njs_typed_array.c Mon Sep 07 17:55:24 2020 +0300
@@ -610,8 +610,7 @@ njs_typed_array_prototype_byte_offset(nj
array = njs_typed_array(this);
- njs_set_number(&vm->retval, array->offset
- * njs_typed_array_element_size(array->type));
+ njs_set_number(&vm->retval, njs_typed_array_offset(array));
return NJS_OK;
}
diff -r 4ef85ee61700 -r f8c8e23d2bbd src/njs_typed_array.h
--- a/src/njs_typed_array.h Mon Sep 07 17:55:10 2020 +0300
+++ b/src/njs_typed_array.h Mon Sep 07 17:55:24 2020 +0300
@@ -55,6 +55,20 @@ njs_typed_array_length(const njs_typed_a
}
+njs_inline uint32_t
+njs_typed_array_offset(const njs_typed_array_t *array)
+{
+ return array->offset * njs_typed_array_element_size(array->type);
+}
+
+
+njs_inline u_char *
+njs_typed_array_start(const njs_typed_array_t *array)
+{
+ return &array->buffer->u.u8[njs_typed_array_offset(array)];
+}
+
+
njs_inline double
njs_typed_array_prop(const njs_typed_array_t *array, uint32_t index)
{
diff -r 4ef85ee61700 -r f8c8e23d2bbd src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Mon Sep 07 17:55:10 2020 +0300
+++ b/src/test/njs_unit_test.c Mon Sep 07 17:55:24 2020 +0300
@@ -18226,6 +18226,11 @@ static njs_unit_test_t njs_test[] =
"var res = en.encodeInto('ααααα', utf8); njs.dump(res)"),
njs_str("{read:5,written:10}") },
+ { njs_str("var en = new TextEncoder();"
+ "var utf8 = new Uint8Array(10);"
+ "en.encodeInto('ααααα', utf8.subarray(2)); utf8[0]"),
+ njs_str("0") },
+
{ njs_str("var str = String.bytesFrom([0xCE]);"
"var en = new TextEncoder();"
"var utf8 = new Uint8Array(3);"
@@ -18333,6 +18338,12 @@ static njs_unit_test_t njs_test[] =
"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]") },
+
+ { njs_str("var de = new TextDecoder();"
+ "var buf = new Uint32Array([1,2,3]).subarray(1,2);"
+ "var en = new TextEncoder();"
+ "njs.dump(en.encode(de.decode(buf)))"),
+ njs_str("Uint8Array [2,0,0,0]") },
};
More information about the nginx-devel
mailing list