Skip to content

Commit a456c90

Browse files
AnthonyCalandrayichoi
authored andcommitted
Add %TypedArray%.prototype.fill(value, [ begin [, end ] ]) support. (#2415)
JerryScript-DCO-1.0-Signed-off-by: Anthony Calandra anthony@anthony-calandra.com
1 parent d5cd32b commit a456c90

File tree

5 files changed

+190
-0
lines changed

5 files changed

+190
-0
lines changed

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,88 @@ ecma_builtin_typedarray_prototype_subarray (ecma_value_t this_arg, /**< this arg
11941194
return ret_value;
11951195
} /* ecma_builtin_typedarray_prototype_subarray */
11961196

1197+
/**
1198+
* The %TypedArray%.prototype object's 'fill' routine.
1199+
*
1200+
* See also:
1201+
* ES2015, 22.2.3.8, 22.1.3.6
1202+
*
1203+
* @return ecma value
1204+
* Returned value must be freed with ecma_free_value.
1205+
*/
1206+
static ecma_value_t
1207+
ecma_builtin_typedarray_prototype_fill (ecma_value_t this_arg, /**< this argument */
1208+
ecma_value_t value, /**< value */
1209+
ecma_value_t begin, /**< begin */
1210+
ecma_value_t end) /**< end */
1211+
{
1212+
if (!ecma_is_typedarray (this_arg))
1213+
{
1214+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1215+
}
1216+
1217+
ecma_number_t value_num;
1218+
ecma_value_t ret_value = ecma_get_number (value, &value_num);
1219+
1220+
if (!ecma_is_value_empty (ret_value))
1221+
{
1222+
return ret_value;
1223+
}
1224+
1225+
ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg);
1226+
ecma_object_t *typedarray_arraybuffer_p = ecma_typedarray_get_arraybuffer (typedarray_p);
1227+
lit_utf8_byte_t *buffer_p = ecma_arraybuffer_get_buffer (typedarray_arraybuffer_p);
1228+
ecma_length_t length = ecma_typedarray_get_length (typedarray_p);
1229+
1230+
uint32_t begin_index_uint32 = 0, end_index_uint32 = 0;
1231+
1232+
ECMA_OP_TO_NUMBER_TRY_CATCH (relative_begin, begin, ret_value);
1233+
begin_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_begin, length);
1234+
1235+
if (ecma_is_value_undefined (end))
1236+
{
1237+
end_index_uint32 = (uint32_t) length;
1238+
}
1239+
else
1240+
{
1241+
ECMA_OP_TO_NUMBER_TRY_CATCH (relative_end, end, ret_value);
1242+
1243+
end_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_end, length);
1244+
1245+
ECMA_OP_TO_NUMBER_FINALIZE (relative_end);
1246+
}
1247+
1248+
ECMA_OP_TO_NUMBER_FINALIZE (relative_begin);
1249+
1250+
if (!ecma_is_value_empty (ret_value))
1251+
{
1252+
return ret_value;
1253+
}
1254+
1255+
ecma_length_t subarray_length = 0;
1256+
1257+
if (end_index_uint32 > begin_index_uint32)
1258+
{
1259+
subarray_length = end_index_uint32 - begin_index_uint32;
1260+
}
1261+
1262+
uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p);
1263+
ecma_length_t byte_offset = ecma_typedarray_get_offset (typedarray_p);
1264+
lit_magic_string_id_t class_id = ecma_object_get_class_name (typedarray_p);
1265+
1266+
uint8_t element_size = (uint8_t) (1 << shift);
1267+
uint32_t byte_index = byte_offset + begin_index_uint32 * element_size;
1268+
uint32_t limit = byte_index + subarray_length * element_size;
1269+
1270+
while (byte_index < limit)
1271+
{
1272+
ecma_set_typedarray_element (buffer_p + byte_index, value_num, class_id);
1273+
byte_index += element_size;
1274+
}
1275+
1276+
return ecma_copy_value (this_arg);
1277+
} /* ecma_builtin_typedarray_prototype_fill */
1278+
11971279
/**
11981280
* @}
11991281
* @}

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ ROUTINE (LIT_MAGIC_STRING_FILTER, ecma_builtin_typedarray_prototype_filter, 2, 1
5959
ROUTINE (LIT_MAGIC_STRING_REVERSE, ecma_builtin_typedarray_prototype_reverse, 0, 0)
6060
ROUTINE (LIT_MAGIC_STRING_SET, ecma_builtin_typedarray_prototype_set, 2, 1)
6161
ROUTINE (LIT_MAGIC_STRING_SUBARRAY, ecma_builtin_typedarray_prototype_subarray, 2, 2)
62+
ROUTINE (LIT_MAGIC_STRING_FILL, ecma_builtin_typedarray_prototype_fill, 3, 1)
6263

6364
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
6465

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EVAL, "eval")
108108
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
109109
#endif
110110
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
111+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FILL, "fill")
111112
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
112113
#endif
113114
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \

jerry-core/lit/lit-magic-strings.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ LIT_MAGIC_STRING_CALL = "call"
6565
LIT_MAGIC_STRING_CEIL = "ceil"
6666
LIT_MAGIC_STRING_EVAL = "eval"
6767
LIT_MAGIC_STRING_EXEC = "exec"
68+
LIT_MAGIC_STRING_FILL = "fill"
6869
LIT_MAGIC_STRING_FROM = "from"
6970
LIT_MAGIC_STRING_JOIN = "join"
7071
LIT_MAGIC_STRING_KEYS = "keys"
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var a = new Int32Array([1, 2, 3, 4, 5]);
17+
assert(a.fill(0).toString() === '0,0,0,0,0');
18+
assert(a.toString() === '0,0,0,0,0');
19+
assert(a.fill(1, 3).toString() === '0,0,0,1,1');
20+
assert(a.toString() === '0,0,0,1,1');
21+
assert(a.fill(2, 1, 3).toString() === '0,2,2,1,1');
22+
assert(a.toString() === '0,2,2,1,1');
23+
assert(a.fill(3, -3).toString() === '0,2,3,3,3');
24+
assert(a.toString() === '0,2,3,3,3');
25+
assert(a.fill(4, -3, -1).toString() === '0,2,4,4,3');
26+
assert(a.toString() === '0,2,4,4,3');
27+
assert(a.fill(5, 3, 2).toString() === '0,2,4,4,3');
28+
assert(a.toString() === '0,2,4,4,3');
29+
assert(a.fill(6, -2, -3).toString() === '0,2,4,4,3');
30+
assert(a.toString() === '0,2,4,4,3');
31+
assert(a.fill(7, 4, 1).toString() === '0,2,4,4,3');
32+
assert(a.toString() === '0,2,4,4,3');
33+
assert(a.fill(8, -1, -4).toString() === '0,2,4,4,3');
34+
assert(a.toString() === '0,2,4,4,3');
35+
assert(a.fill(9, 1).fill(10, 1).toString() === '0,10,10,10,10');
36+
assert(a.toString() === '0,10,10,10,10');
37+
assert(a.fill(11, 0, 4).fill(12, 1, 2).toString() === '11,12,11,11,10');
38+
assert(a.toString() === '11,12,11,11,10');
39+
assert(a.fill(13, 999, 1000).fill(14, -1000, -999).toString() === '11,12,11,11,10');
40+
assert(a.toString() === '11,12,11,11,10');
41+
assert(a.fill(14, 0, 0).toString() === '11,12,11,11,10');
42+
assert(a.toString() === '11,12,11,11,10');
43+
assert(a.fill(15, a.length, a.length).toString() === '11,12,11,11,10');
44+
assert(a.toString() === '11,12,11,11,10');
45+
assert(a.fill(NaN).toString() === '0,0,0,0,0'); // NaN gets coerced into an integer.
46+
assert(a.toString() === '0,0,0,0,0');
47+
assert(a.fill({ valueOf: () => 16 }).toString() === '16,16,16,16,16');
48+
assert(a.toString() === '16,16,16,16,16');
49+
50+
var b = new Uint8Array();
51+
assert(b.fill(1).toString() === '');
52+
assert(b.toString() === '');
53+
assert(b.fill(2, 0, 0).toString() === '');
54+
assert(b.toString() === '');
55+
assert(b.fill(3, b.length, b.length).toString() === '');
56+
assert(b.toString() === '');
57+
58+
var c = new Uint8Array([0]);
59+
assert(c.fill(256).toString() === '0');
60+
assert(c.toString() === '0');
61+
assert(c.fill(257).toString() === '1');
62+
assert(c.toString() === '1');
63+
64+
try {
65+
c.fill({});
66+
} catch (e) {
67+
assert(e instanceof TypeError);
68+
assert(c.toString() === '1');
69+
}
70+
71+
var d = new Float32Array([0]);
72+
assert(d.fill(NaN).toString() === 'NaN');
73+
assert(d.toString() === 'NaN');
74+
75+
var ab = new ArrayBuffer(4);
76+
var e = new Uint8Array(ab);
77+
assert(e.fill(0).toString() === '0,0,0,0');
78+
79+
var f = new Uint32Array(ab);
80+
assert(f.fill(1).toString() === '1');
81+
assert(e.toString() === '1,0,0,0');
82+
83+
var g = new Uint8Array(ab, 1, 2);
84+
assert(g.toString() === '0,0');
85+
assert(g.fill(2).toString() === '2,2');
86+
assert(e.toString() === '1,2,2,0');
87+
assert(g.fill(3, -1).toString() === '2,3');
88+
assert(e.toString() === '1,2,3,0');
89+
assert(g.fill(4, 0, 2).toString() === '4,4');
90+
assert(e.toString() === '1,4,4,0');
91+
assert(g.fill(5, 0, 999).toString() === '5,5');
92+
assert(e.toString() === '1,5,5,0');
93+
assert(g.fill(6, -999, 999).toString() === '6,6');
94+
assert(e.toString() === '1,6,6,0');
95+
assert(g.fill(7, -999, 0).toString() === '6,6');
96+
assert(e.toString() === '1,6,6,0');
97+
98+
var ab2 = new ArrayBuffer(4);
99+
var h = new Uint8Array(ab2);
100+
var i = new Uint16Array(ab2, 0, 1);
101+
assert(i.fill(1).toString() === '1');
102+
assert(h.toString() === '1,0,0,0');
103+
var j = new Uint16Array(ab2, 2, 1);
104+
assert(j.fill(1).toString() === '1');
105+
assert(h.toString() === '1,0,1,0');

0 commit comments

Comments
 (0)