Skip to content

Commit 11b3bb8

Browse files
Add %TypedArray%.prototype.subarray([ begin [, end ] ]) support.
JerryScript-DCO-1.0-Signed-off-by: AnthonyCalandra [email protected]
1 parent ac9fce1 commit 11b3bb8

File tree

7 files changed

+156
-0
lines changed

7 files changed

+156
-0
lines changed

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,41 @@ ecma_typedarray_helper_get_shift_size (uint8_t builtin_id) /**< the builtin id o
9595
}
9696
} /* ecma_typedarray_helper_get_shift_size */
9797

98+
/**
99+
* Get the built-in TypedArray type from a magic string.
100+
*
101+
* @return uint8_t
102+
*/
103+
uint8_t
104+
ecma_typedarray_helper_get_builtin_id (ecma_object_t *obj_p) /**< typedarray object **/
105+
{
106+
#define TYPEDARRAY_ID_CASE(magic_id, builtin_id) \
107+
case magic_id: \
108+
{ \
109+
return builtin_id; \
110+
}
111+
112+
switch (ecma_object_get_class_name (obj_p))
113+
{
114+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_INT8_ARRAY_UL, ECMA_BUILTIN_ID_INT8ARRAY)
115+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_UINT8_ARRAY_UL, ECMA_BUILTIN_ID_UINT8ARRAY)
116+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_UINT8_CLAMPED_ARRAY_UL, ECMA_BUILTIN_ID_UINT8CLAMPEDARRAY)
117+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_INT16_ARRAY_UL, ECMA_BUILTIN_ID_INT16ARRAY)
118+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_UINT16_ARRAY_UL, ECMA_BUILTIN_ID_UINT16ARRAY)
119+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_INT32_ARRAY_UL, ECMA_BUILTIN_ID_INT32ARRAY)
120+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_UINT32_ARRAY_UL, ECMA_BUILTIN_ID_UINT32ARRAY)
121+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_FLOAT32_ARRAY_UL, ECMA_BUILTIN_ID_FLOAT32ARRAY)
122+
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
123+
TYPEDARRAY_ID_CASE (LIT_MAGIC_STRING_FLOAT64_ARRAY_UL, ECMA_BUILTIN_ID_FLOAT64ARRAY)
124+
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
125+
default:
126+
{
127+
JERRY_UNREACHABLE ();
128+
}
129+
}
130+
#undef TYPEDARRAY_ID_CASE
131+
} /* ecma_typedarray_helper_get_builtin_id */
132+
98133
/**
99134
* Get the magic string of a TypedArray type.
100135
*

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-helpers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
bool ecma_typedarray_helper_is_typedarray (uint8_t builtin_id);
3030
uint8_t ecma_typedarray_helper_get_shift_size (uint8_t builtin_id);
31+
uint8_t ecma_typedarray_helper_get_builtin_id (ecma_object_t *obj_p);
3132
uint8_t ecma_typedarray_helper_get_magic_string (uint8_t builtin_id);
3233
uint8_t ecma_typedarray_helper_get_prototype_id (uint8_t builtin_id);
3334

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,90 @@ ecma_builtin_typedarray_prototype_object_to_string (ecma_value_t this_arg) /**<
988988
return ret_value;
989989
} /* ecma_builtin_typedarray_prototype_object_to_string */
990990

991+
/**
992+
* The %TypedArray%.prototype object's 'subarray' routine.
993+
*
994+
* See also:
995+
* ES2015, 22.2.3.26
996+
*
997+
* @return ecma value
998+
* Returned value must be freed with ecma_free_value.
999+
*/
1000+
static ecma_value_t
1001+
ecma_builtin_typedarray_prototype_subarray (ecma_value_t this_arg, /**< this argument */
1002+
ecma_value_t begin, /**< begin */
1003+
ecma_value_t end) /**< end */
1004+
{
1005+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
1006+
1007+
/* 2.~ 4. */
1008+
if (!ecma_is_typedarray (this_arg))
1009+
{
1010+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray."));
1011+
}
1012+
1013+
ecma_object_t *src_typedarray_p = ecma_get_object_from_value (this_arg);
1014+
1015+
/* 5. buffer */
1016+
ecma_object_t *src_typedarray_arraybuffer_p = ecma_typedarray_get_arraybuffer (src_typedarray_p);
1017+
1018+
/* 6. srcLength */
1019+
ecma_length_t src_length = ecma_typedarray_get_length (src_typedarray_p);
1020+
1021+
/* 9. beginIndex, 12. endIndex */
1022+
uint32_t begin_index_uint32 = 0, end_index_uint32 = 0;
1023+
1024+
/* 7. relativeBegin */
1025+
ECMA_OP_TO_NUMBER_TRY_CATCH (relative_begin, begin, ret_value);
1026+
begin_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_begin, src_length);
1027+
1028+
if (ecma_is_value_undefined (end))
1029+
{
1030+
end_index_uint32 = (uint32_t) src_length;
1031+
}
1032+
else
1033+
{
1034+
/* 10. relativeEnd */
1035+
ECMA_OP_TO_NUMBER_TRY_CATCH (relative_end, end, ret_value);
1036+
1037+
end_index_uint32 = ecma_builtin_helper_array_index_normalize (relative_end, src_length);
1038+
1039+
ECMA_OP_TO_NUMBER_FINALIZE (relative_end);
1040+
}
1041+
1042+
ECMA_OP_TO_NUMBER_FINALIZE (relative_begin);
1043+
1044+
/* 13. newLength */
1045+
ecma_length_t subarray_length = JERRY_MAX (end_index_uint32 - begin_index_uint32, 0);
1046+
1047+
/* 15. elementSize */
1048+
uint8_t shift = ecma_typedarray_get_element_size_shift (src_typedarray_p);
1049+
uint8_t element_size = (uint8_t) (1 << shift);
1050+
1051+
/* 16. srcByteOffset */
1052+
ecma_length_t src_byte_offset = ecma_typedarray_get_offset (src_typedarray_p);
1053+
1054+
/* 17. beginByteOffset */
1055+
ecma_length_t begin_byte_offset = src_byte_offset + begin_index_uint32 * element_size;
1056+
1057+
uint8_t src_builtin_id = ecma_typedarray_helper_get_builtin_id (src_typedarray_p);
1058+
ecma_value_t arguments_p[3] =
1059+
{
1060+
ecma_make_object_value (src_typedarray_arraybuffer_p),
1061+
ecma_make_uint32_value (begin_byte_offset),
1062+
ecma_make_uint32_value (subarray_length)
1063+
};
1064+
1065+
if (ecma_is_value_empty (ret_value))
1066+
{
1067+
ret_value = ecma_typedarray_helper_dispatch_construct (arguments_p, 3, src_builtin_id);
1068+
}
1069+
1070+
ecma_free_value (arguments_p[1]);
1071+
ecma_free_value (arguments_p[2]);
1072+
return ret_value;
1073+
} /* ecma_builtin_typedarray_prototype_subarray */
1074+
9911075
/**
9921076
* @}
9931077
* @}

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
@@ -58,6 +58,7 @@ ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_typedarray_prototype_red
5858
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)
61+
ROUTINE (LIT_MAGIC_STRING_SUBARRAY, ecma_builtin_typedarray_prototype_subarray, 2, 2)
6162

6263
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
6364

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PARSE_INT, "parseInt")
334334
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_HOURS_UL, "setHours")
335335
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_MONTH_UL, "setMonth")
336336
#endif
337+
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
338+
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SUBARRAY, "subarray")
339+
#endif
337340
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_STRING_UL, "toString")
338341
#if !defined (CONFIG_DISABLE_ANNEXB_BUILTIN)
339342
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_UNESCAPE, "unescape")

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ LIT_MAGIC_STRING_IS_SEALED_UL = "isSealed"
162162
LIT_MAGIC_STRING_PARSE_INT = "parseInt"
163163
LIT_MAGIC_STRING_SET_HOURS_UL = "setHours"
164164
LIT_MAGIC_STRING_SET_MONTH_UL = "setMonth"
165+
LIT_MAGIC_STRING_SUBARRAY = "subarray"
165166
LIT_MAGIC_STRING_TO_STRING_UL = "toString"
166167
LIT_MAGIC_STRING_UNESCAPE = "unescape"
167168
LIT_MAGIC_STRING_WRITABLE = "writable"
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.subarray().toString() === '1,2,3,4,5');
18+
assert(a.subarray(3).toString() === '4,5');
19+
assert(a.subarray(1, 3).toString() === '2,3');
20+
assert(a.subarray(-3).toString() === '3,4,5');
21+
assert(a.subarray(-3, -1).toString() === '3,4');
22+
assert(a.subarray(3, 2).toString() === '');
23+
assert(a.subarray(-2, -3).toString() === '');
24+
assert(a.subarray(4, 1).toString() === '');
25+
assert(a.subarray(-1, -4).toString() === '');
26+
assert(a.subarray(1).subarray(1).toString() === '3,4,5');
27+
assert(a.subarray(1, 4).subarray(1, 2).toString() === '3');
28+
29+
var b = new Uint8Array([]);
30+
assert(b.subarray(123456, -123456).toString() === '');
31+
assert(b.subarray().subarray().toString() === '');

0 commit comments

Comments
 (0)