[njs] Fixed %TypedArray%.prototype.fill().
Dmitry Volyntsev
xeioex at nginx.com
Tue Jan 21 13:04:46 UTC 2020
details: https://hg.nginx.org/njs/rev/a34f1293edba
branches:
changeset: 1308:a34f1293edba
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Tue Jan 21 16:03:30 2020 +0300
description:
Fixed %TypedArray%.prototype.fill().
For subarrays starting from non-zero offset.
diffstat:
src/njs_typed_array.c | 30 ++++++++++++++++--------------
src/njs_typed_array.h | 6 ++----
src/test/njs_unit_test.c | 11 +++++++++++
3 files changed, 29 insertions(+), 18 deletions(-)
diffs (134 lines):
diff -r 5cf918cc0d06 -r a34f1293edba src/njs_typed_array.c
--- a/src/njs_typed_array.c Tue Jan 21 16:01:48 2020 +0300
+++ b/src/njs_typed_array.c Tue Jan 21 16:03:30 2020 +0300
@@ -491,7 +491,7 @@ njs_typed_array_prototype_fill(njs_vm_t
int16_t i16;
int32_t i32;
uint8_t u8;
- int64_t start, end;
+ int64_t start, end, offset;
uint32_t i, length;
njs_int_t ret;
njs_value_t *this, *setval, lvalue;
@@ -535,20 +535,22 @@ njs_typed_array_prototype_fill(njs_vm_t
njs_set_typed_array(&vm->retval, array);
buffer = array->buffer;
+ offset = array->offset;
switch (array->type) {
case NJS_OBJ_TYPE_UINT8_CLAMPED_ARRAY:
- if (num < 0) {
- num = 0;
+ if (isnan(num) || num < 0) {
+ u8 = 0;
} else if (num > 255) {
- num = 255;
+ u8 = 255;
+
+ } else {
+ u8 = lrint(num);
}
- u8 = lrint(num);
-
- for (i = start; i < end; i++) {
- buffer->u.u8[i] = u8;
+ if (start < end) {
+ memset(&buffer->u.u8[start + offset], u8, end - start);
}
break;
@@ -557,8 +559,8 @@ njs_typed_array_prototype_fill(njs_vm_t
case NJS_OBJ_TYPE_INT8_ARRAY:
i8 = njs_number_to_int32(num);
- for (i = start; i < end; i++) {
- buffer->u.u8[i] = i8;
+ if (start < end) {
+ memset(&buffer->u.u8[start + offset], i8, end - start);
}
break;
@@ -568,7 +570,7 @@ njs_typed_array_prototype_fill(njs_vm_t
i16 = njs_number_to_int32(num);
for (i = start; i < end; i++) {
- buffer->u.u16[i] = i16;
+ buffer->u.u16[i + offset] = i16;
}
break;
@@ -578,7 +580,7 @@ njs_typed_array_prototype_fill(njs_vm_t
i32 = njs_number_to_int32(num);
for (i = start; i < end; i++) {
- buffer->u.u32[i] = i32;
+ buffer->u.u32[i + offset] = i32;
}
break;
@@ -587,7 +589,7 @@ njs_typed_array_prototype_fill(njs_vm_t
f32 = num;
for (i = start; i < end; i++) {
- buffer->u.f32[i] = f32;
+ buffer->u.f32[i + offset] = f32;
}
break;
@@ -597,7 +599,7 @@ njs_typed_array_prototype_fill(njs_vm_t
/* NJS_OBJ_TYPE_FLOAT64_ARRAY. */
for (i = start; i < end; i++) {
- buffer->u.f64[i] = num;
+ buffer->u.f64[i + offset] = num;
}
}
diff -r 5cf918cc0d06 -r a34f1293edba src/njs_typed_array.h
--- a/src/njs_typed_array.h Tue Jan 21 16:01:48 2020 +0300
+++ b/src/njs_typed_array.h Tue Jan 21 16:03:30 2020 +0300
@@ -103,11 +103,9 @@ njs_typed_array_set(njs_typed_array_t *a
switch (array->type) {
case NJS_OBJ_TYPE_UINT8_CLAMPED_ARRAY:
- if (v < 0) {
+ if (isnan(v) || v < 0) {
v = 0;
- }
-
- if (v > 255) {
+ } else if (v > 255) {
v = 255;
}
diff -r 5cf918cc0d06 -r a34f1293edba src/test/njs_unit_test.c
--- a/src/test/njs_unit_test.c Tue Jan 21 16:01:48 2020 +0300
+++ b/src/test/njs_unit_test.c Tue Jan 21 16:03:30 2020 +0300
@@ -5005,6 +5005,11 @@ static njs_unit_test_t njs_test[] =
" return (a[0] === 1 && a[1] === 12 && a[2] === 3 && a.length == 3)})"),
njs_str("true") },
+ { njs_str(NJS_TYPED_ARRAY_LIST
+ ".every(v=>{var a = new v([0,0,0,0,0]).fill(8, -1, -3); "
+ " return njs.dump(a) === `${v.name} [0,0,0,0,0]`;})"),
+ njs_str("true") },
+
{ njs_str(NJS_INT_TYPED_ARRAY_LIST
".every(v=>{var a = new v([1,2,3]); a.fill('qq', 1, 2); "
" return (a[0] === 1 && a[1] === 0 && a[2] === 3 && a.length == 3)})"),
@@ -5341,6 +5346,12 @@ static njs_unit_test_t njs_test[] =
njs_str("true") },
{ njs_str(NJS_TYPED_ARRAY_LIST
+ ".every(v=>{var a = new v([1,2,3,4]); "
+ " a.subarray(1,10).fill(0);"
+ " return njs.dump(a) === `${v.name} [1,0,0,0]`;})"),
+ njs_str("true") },
+
+ { njs_str(NJS_TYPED_ARRAY_LIST
".every(v=>{var a = new v([1,2,3]); "
" var r = a.subarray(1,3);"
" return a.buffer === r.buffer;})"),
More information about the nginx-devel
mailing list