From fabcef655caac5c0517436c08f0fc392f55c94e8 Mon Sep 17 00:00:00 2001 From: Dmitry Volyntsev Date: Tue, 21 Jan 2020 16:03:30 +0300 Subject: [PATCH] Fixed %TypedArray%.prototype.fill(). For subarrays starting from non-zero offset. --- 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(-) diff --git a/src/njs_typed_array.c b/src/njs_typed_array.c index fa43900a..aa8689a5 100644 --- a/src/njs_typed_array.c +++ b/src/njs_typed_array.c @@ -491,7 +491,7 @@ njs_typed_array_prototype_fill(njs_vm_t *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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; - u8 = lrint(num); + } else { + 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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, 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 *vm, njs_value_t *args, /* NJS_OBJ_TYPE_FLOAT64_ARRAY. */ for (i = start; i < end; i++) { - buffer->u.f64[i] = num; + buffer->u.f64[i + offset] = num; } } diff --git a/src/njs_typed_array.h b/src/njs_typed_array.h index 4a99b1be..a9938a13 100644 --- a/src/njs_typed_array.h +++ b/src/njs_typed_array.h @@ -103,11 +103,9 @@ njs_typed_array_set(njs_typed_array_t *array, uint32_t index, double v) 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 --git a/src/test/njs_unit_test.c b/src/test/njs_unit_test.c index 0da94018..cd4e3ed7 100644 --- a/src/test/njs_unit_test.c +++ b/src/test/njs_unit_test.c @@ -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)})"), @@ -5340,6 +5345,12 @@ static njs_unit_test_t njs_test[] = " return njs.dump(a) === `${v.name} [1,0,0,4]`;})"), 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);" -- 2.47.3