Add %TypedArray%.prototype.fill(value, [ begin [, end ] ]) support. (#2415)
JerryScript-DCO-1.0-Signed-off-by: Anthony Calandra anthony@anthony-calandra.com
This commit is contained in:
@@ -1194,6 +1194,88 @@ ecma_builtin_typedarray_prototype_subarray (ecma_value_t this_arg, /**< this arg
|
||||
return ret_value;
|
||||
} /* 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 */
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
|
||||
@@ -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 */
|
||||
|
||||
|
||||
@@ -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) \
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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');
|
||||
Reference in New Issue
Block a user