Skip to content

Commit 874722f

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

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-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
@@ -988,6 +988,88 @@ 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_get_object_builtin_id (src_typedarray_p);
1058+
ecma_value_t arguments_p[3] =
1059+
{
1060+
ecma_make_object_value (src_typedarray_arraybuffer_p),
1061+
begin_byte_offset,
1062+
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+
return ret_value;
1071+
} /* ecma_builtin_typedarray_prototype_subarray */
1072+
9911073
/**
9921074
* @}
9931075
* @}

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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 Uint8Array([1, 2, 3]);
17+
print(a.subarray().toString());
18+
//assert(a.subarray(0, 2).toString() === "");

0 commit comments

Comments
 (0)