diff options
5 files changed, 190 insertions, 0 deletions
diff --git a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c index 4c6e090a..de59a65e 100644 --- a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c +++ b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c @@ -1195,6 +1195,88 @@ ecma_builtin_typedarray_prototype_subarray (ecma_value_t this_arg, /**< this arg } /* ecma_builtin_typedarray_prototype_subarray */ /** + * The %TypedArray%.prototype object's 'fill' routine. + * + * See also: + * ES2015, 22.2.3.8, 22.1.3.6 + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_builtin_typedarray_prototype_fill (ecma_value_t this_arg, /**< this argument */ + ecma_value_t value, /**< value */ + ecma_value_t begin, /**< begin */ + ecma_value_t end) /**< end */ +{ + if (!ecma_is_typedarray (this_arg)) + { + return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray.")); + } + + ecma_number_t value_num; + ecma_value_t ret_value = ecma_get_number (value, &value_num); + + if (!ecma_is_value_empty (ret_value)) + { + return ret_value; + } + + ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg); + ecma_object_t *typedarray_arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p); + lit_utf8_byte_t *buffer_p = ecma_arraybuffer_get_buffer (typedarray_arraybuffer_p); + ecma_length_t length = ecma_typedarray_get_length (typedarray_p); + + uint32_t begin_index_uint32 = 0, end_index_uint32 = 0; + + ECMA_OP_TO_NUMBER_TRY_CATCH (relative_begin, begin, ret_value); + begin_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_begin, length); + + if (ecma_is_value_undefined (end)) + { + end_index_uint32 = (uint32_t) length; + } + else + { + ECMA_OP_TO_NUMBER_TRY_CATCH (relative_end, end, ret_value); + + end_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_end, length); + + ECMA_OP_TO_NUMBER_FINALIZE (relative_end); + } + + ECMA_OP_TO_NUMBER_FINALIZE (relative_begin); + + if (!ecma_is_value_empty (ret_value)) + { + return ret_value; + } + + ecma_length_t subarray_length = 0; + + if (end_index_uint32 > begin_index_uint32) + { + subarray_length = end_index_uint32 - begin_index_uint32; + } + + uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p); + ecma_length_t byte_offset = ecma_typedarray_get_offset (typedarray_p); + lit_magic_string_id_t class_id = ecma_object_get_class_name (typedarray_p); + + uint8_t element_size = (uint8_t) (1 << shift); + uint32_t byte_index = byte_offset + begin_index_uint32 * element_size; + uint32_t limit = byte_index + subarray_length * element_size; + + while (byte_index < limit) + { + ecma_set_typedarray_element (buffer_p + byte_index, value_num, class_id); + byte_index += element_size; + } + + return ecma_copy_value (this_arg); +} /* ecma_builtin_typedarray_prototype_fill */ + +/** * @} * @} * @} diff --git a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h index 02634ddf..0fbd5213 100644 --- a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h +++ b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h @@ -59,6 +59,7 @@ ROUTINE (LIT_MAGIC_STRING_FILTER, ecma_builtin_typedarray_prototype_filter, 2, 1 ROUTINE (LIT_MAGIC_STRING_REVERSE, ecma_builtin_typedarray_prototype_reverse, 0, 0) ROUTINE (LIT_MAGIC_STRING_SET, ecma_builtin_typedarray_prototype_set, 2, 1) ROUTINE (LIT_MAGIC_STRING_SUBARRAY, ecma_builtin_typedarray_prototype_subarray, 2, 2) +ROUTINE (LIT_MAGIC_STRING_FILL, ecma_builtin_typedarray_prototype_fill, 3, 1) #endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */ diff --git a/jerry-core/lit/lit-magic-strings.inc.h b/jerry-core/lit/lit-magic-strings.inc.h index 874b5cf5..950725b7 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -108,6 +108,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EVAL, "eval") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec") #endif #if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN) +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FILL, "fill") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from") #endif #if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \ diff --git a/jerry-core/lit/lit-magic-strings.ini b/jerry-core/lit/lit-magic-strings.ini index acaa707d..3cd2161c 100644 --- a/jerry-core/lit/lit-magic-strings.ini +++ b/jerry-core/lit/lit-magic-strings.ini @@ -65,6 +65,7 @@ LIT_MAGIC_STRING_CALL = "call" LIT_MAGIC_STRING_CEIL = "ceil" LIT_MAGIC_STRING_EVAL = "eval" LIT_MAGIC_STRING_EXEC = "exec" +LIT_MAGIC_STRING_FILL = "fill" LIT_MAGIC_STRING_FROM = "from" LIT_MAGIC_STRING_JOIN = "join" LIT_MAGIC_STRING_KEYS = "keys" diff --git a/tests/jerry/es2015/typedArray-fill.js b/tests/jerry/es2015/typedArray-fill.js new file mode 100644 index 00000000..01288d15 --- /dev/null +++ b/tests/jerry/es2015/typedArray-fill.js @@ -0,0 +1,105 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +var a = new Int32Array([1, 2, 3, 4, 5]); +assert(a.fill(0).toString() === '0,0,0,0,0'); +assert(a.toString() === '0,0,0,0,0'); +assert(a.fill(1, 3).toString() === '0,0,0,1,1'); +assert(a.toString() === '0,0,0,1,1'); +assert(a.fill(2, 1, 3).toString() === '0,2,2,1,1'); +assert(a.toString() === '0,2,2,1,1'); +assert(a.fill(3, -3).toString() === '0,2,3,3,3'); +assert(a.toString() === '0,2,3,3,3'); +assert(a.fill(4, -3, -1).toString() === '0,2,4,4,3'); +assert(a.toString() === '0,2,4,4,3'); +assert(a.fill(5, 3, 2).toString() === '0,2,4,4,3'); +assert(a.toString() === '0,2,4,4,3'); +assert(a.fill(6, -2, -3).toString() === '0,2,4,4,3'); +assert(a.toString() === '0,2,4,4,3'); +assert(a.fill(7, 4, 1).toString() === '0,2,4,4,3'); +assert(a.toString() === '0,2,4,4,3'); +assert(a.fill(8, -1, -4).toString() === '0,2,4,4,3'); +assert(a.toString() === '0,2,4,4,3'); +assert(a.fill(9, 1).fill(10, 1).toString() === '0,10,10,10,10'); +assert(a.toString() === '0,10,10,10,10'); +assert(a.fill(11, 0, 4).fill(12, 1, 2).toString() === '11,12,11,11,10'); +assert(a.toString() === '11,12,11,11,10'); +assert(a.fill(13, 999, 1000).fill(14, -1000, -999).toString() === '11,12,11,11,10'); +assert(a.toString() === '11,12,11,11,10'); +assert(a.fill(14, 0, 0).toString() === '11,12,11,11,10'); +assert(a.toString() === '11,12,11,11,10'); +assert(a.fill(15, a.length, a.length).toString() === '11,12,11,11,10'); +assert(a.toString() === '11,12,11,11,10'); +assert(a.fill(NaN).toString() === '0,0,0,0,0'); // NaN gets coerced into an integer. +assert(a.toString() === '0,0,0,0,0'); +assert(a.fill({ valueOf: () => 16 }).toString() === '16,16,16,16,16'); +assert(a.toString() === '16,16,16,16,16'); + +var b = new Uint8Array(); +assert(b.fill(1).toString() === ''); +assert(b.toString() === ''); +assert(b.fill(2, 0, 0).toString() === ''); +assert(b.toString() === ''); +assert(b.fill(3, b.length, b.length).toString() === ''); +assert(b.toString() === ''); + +var c = new Uint8Array([0]); +assert(c.fill(256).toString() === '0'); +assert(c.toString() === '0'); +assert(c.fill(257).toString() === '1'); +assert(c.toString() === '1'); + +try { + c.fill({}); +} catch (e) { + assert(e instanceof TypeError); + assert(c.toString() === '1'); +} + +var d = new Float32Array([0]); +assert(d.fill(NaN).toString() === 'NaN'); +assert(d.toString() === 'NaN'); + +var ab = new ArrayBuffer(4); +var e = new Uint8Array(ab); +assert(e.fill(0).toString() === '0,0,0,0'); + +var f = new Uint32Array(ab); +assert(f.fill(1).toString() === '1'); +assert(e.toString() === '1,0,0,0'); + +var g = new Uint8Array(ab, 1, 2); +assert(g.toString() === '0,0'); +assert(g.fill(2).toString() === '2,2'); +assert(e.toString() === '1,2,2,0'); +assert(g.fill(3, -1).toString() === '2,3'); +assert(e.toString() === '1,2,3,0'); +assert(g.fill(4, 0, 2).toString() === '4,4'); +assert(e.toString() === '1,4,4,0'); +assert(g.fill(5, 0, 999).toString() === '5,5'); +assert(e.toString() === '1,5,5,0'); +assert(g.fill(6, -999, 999).toString() === '6,6'); +assert(e.toString() === '1,6,6,0'); +assert(g.fill(7, -999, 0).toString() === '6,6'); +assert(e.toString() === '1,6,6,0'); + +var ab2 = new ArrayBuffer(4); +var h = new Uint8Array(ab2); +var i = new Uint16Array(ab2, 0, 1); +assert(i.fill(1).toString() === '1'); +assert(h.toString() === '1,0,0,0'); +var j = new Uint16Array(ab2, 2, 1); +assert(j.fill(1).toString() === '1'); +assert(h.toString() === '1,0,1,0'); |