diff --git a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-float64array-prototype.c b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-float64array-prototype.c index fba6bd7c10..268c355fc6 100644 --- a/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-float64array-prototype.c +++ b/jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-float64array-prototype.c @@ -40,5 +40,6 @@ * @} * @} */ + #endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */ #endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */ 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 6b09a71e56..e179e0e43a 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 @@ -767,6 +767,227 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument return ret_val; } /* ecma_builtin_typedarray_prototype_set */ +/** + * TypedArray.prototype's 'toString' single element operation routine based + * on the Array.prototype's 'toString' single element operation routine + * + * See also: + * ECMA-262 v5.1, 15.4.4.2 + * + * @return ecma_value_t value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */ + uint32_t index) /**< array index */ +{ + ecma_value_t ret_value = ECMA_VALUE_EMPTY; + ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index); + ecma_value_t index_value = ecma_op_object_get (obj_p, index_string_p); + ecma_deref_ecma_string (index_string_p); + + if (ECMA_IS_VALUE_ERROR (index_value)) + { + return index_value; + } + + if (ecma_is_value_undefined (index_value) + || ecma_is_value_null (index_value)) + { + ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); + } + else + { + ret_value = ecma_op_to_string (index_value); + } + + ecma_free_value (index_value); + return ret_value; +} /* ecma_op_typedarray_get_to_string_at_index */ + +/** + * The TypedArray.prototype.toString's separator creation routine based on + * the Array.prototype.toString's separator routine + * + * See also: + * ECMA-262 v5.1, 15.4.4.2 4th step + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_op_typedarray_get_separator_string (ecma_value_t separator) /**< possible separator */ +{ + if (ecma_is_value_undefined (separator)) + { + return ecma_make_magic_string_value (LIT_MAGIC_STRING_COMMA_CHAR); + } + + return ecma_op_to_string (separator); +} /* ecma_op_typedarray_get_separator_string */ + +/** + * The TypedArray.prototype object's 'join' routine basen on + * the Array.porottype object's 'join' + * + * See also: + * ECMA-262 v5, 15.4.4.5 + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_builtin_typedarray_prototype_join (const ecma_value_t this_arg, /**< this argument */ + const ecma_value_t separator_arg) /**< separator argument */ +{ + /* 1. */ + ecma_value_t obj_value = ecma_op_to_object (this_arg); + + if (ECMA_IS_VALUE_ERROR (obj_value)) + { + return obj_value; + } + ecma_object_t *obj_p = ecma_get_object_from_value (obj_value); + + /* 2. */ + ecma_value_t length_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_LENGTH); + + if (ECMA_IS_VALUE_ERROR (length_value)) + { + ecma_free_value (obj_value); + return length_value; + } + + ecma_value_t ret_value = ECMA_VALUE_EMPTY; + + ECMA_OP_TO_NUMBER_TRY_CATCH (length_number, + length_value, + ret_value); + + /* 3. */ + uint32_t length = ecma_number_to_uint32 (length_number); + /* 4-5. */ + ecma_value_t separator_value = ecma_op_typedarray_get_separator_string (separator_arg); + if (ECMA_IS_VALUE_ERROR (separator_value)) + { + ecma_free_value (length_value); + ecma_free_value (obj_value); + return separator_value; + } + + if (length == 0) + { + /* 6. */ + ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); + } + else + { + ecma_string_t *separator_string_p = ecma_get_string_from_value (separator_value); + + /* 7-8. */ + ecma_value_t first_value = ecma_op_typedarray_get_to_string_at_index (obj_p, 0); + if (ECMA_IS_VALUE_ERROR (first_value)) + { + ecma_free_value (separator_value); + ecma_free_value (length_value); + ecma_free_value (obj_value); + return first_value; + } + + ecma_string_t *return_string_p = ecma_get_string_from_value (first_value); + ecma_ref_ecma_string (return_string_p); + if (ecma_is_value_empty (ret_value)) + { + /* 9-10. */ + for (uint32_t k = 1; k < length; k++) + { + /* 10.a */ + return_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p); + + /* 10.b, 10.c */ + ecma_value_t next_string_value = ecma_op_typedarray_get_to_string_at_index (obj_p, k); + if (ECMA_IS_VALUE_ERROR (next_string_value)) + { + ecma_free_value (first_value); + ecma_free_value (separator_value); + ecma_free_value (length_value); + ecma_free_value (obj_value); + return next_string_value; + } + + /* 10.d */ + ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value); + return_string_p = ecma_concat_ecma_strings (return_string_p, next_string_p); + + ecma_free_value (next_string_value); + } + ret_value = ecma_make_string_value (return_string_p); + } + else + { + ecma_deref_ecma_string (return_string_p); + } + + ecma_free_value (first_value); + } + ecma_free_value (separator_value); + + ecma_free_value (length_value); + ecma_free_value (obj_value); + ECMA_OP_TO_NUMBER_FINALIZE (length_number); + return ret_value; +} /* ecma_builtin_typedarray_prototype_join */ + +/** + * The TypedArray.prototype object's 'toString' routine basen on + * the Array.porottype object's 'toString' + * + * See also: + * ECMA-262 v5, 15.4.4.2 + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_builtin_typedarray_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */ +{ + ecma_value_t ret_value = ECMA_VALUE_EMPTY; + + /* 1. */ + ecma_value_t obj_this_value = ecma_op_to_object (this_arg); + if (ECMA_IS_VALUE_ERROR (obj_this_value)) + { + return obj_this_value; + } + ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value); + + /* 2. */ + ecma_value_t join_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_JOIN); + if (ECMA_IS_VALUE_ERROR (join_value)) + { + ecma_free_value (obj_this_value); + return join_value; + } + + if (!ecma_op_is_callable (join_value)) + { + /* 3. */ + ret_value = ecma_builtin_helper_object_to_string (this_arg); + } + else + { + /* 4. */ + ecma_object_t *join_func_obj_p = ecma_get_object_from_value (join_value); + + ret_value = ecma_op_function_call (join_func_obj_p, this_arg, NULL, 0); + } + + ecma_free_value (join_value); + ecma_free_value (obj_this_value); + + return ret_value; +} /* ecma_builtin_typedarray_prototype_object_to_string */ + /** * @} * @} 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 60dffd9c06..88844322b4 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 @@ -45,6 +45,10 @@ ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_LENGTH, ecma_builtin_typedarray_prototype_length_getter, ECMA_PROPERTY_FIXED) +/* Routine properties: + * (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */ +ROUTINE (LIT_MAGIC_STRING_TO_STRING_UL, ecma_builtin_typedarray_prototype_object_to_string, 0, 0) +ROUTINE (LIT_MAGIC_STRING_JOIN, ecma_builtin_typedarray_prototype_join, 1, 1) ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_typedarray_prototype_every, 2, 1) ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_typedarray_prototype_some, 2, 1) ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_typedarray_prototype_for_each, 2, 1) diff --git a/jerry-core/lit/lit-magic-strings.inc.h b/jerry-core/lit/lit-magic-strings.inc.h index 6ac2eb0546..850d202013 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -110,7 +110,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec") #if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from") #endif -#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) +#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \ +|| !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_JOIN, "join") #endif LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_KEYS, "keys") diff --git a/tests/jerry/es2015/typedArray-join.js b/tests/jerry/es2015/typedArray-join.js new file mode 100644 index 0000000000..ce53a35983 --- /dev/null +++ b/tests/jerry/es2015/typedArray-join.js @@ -0,0 +1,24 @@ +/* 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 float_array = new Float32Array([1.125, 5.5, -1.25, -0.0]); +var int_array = new Int8Array([3, 2, 1, 100, -30]); +var uint_array = new Uint8Array([3, 2, 1, 100, -30]); +var empty_array = new Uint32Array(); + +assert(float_array.join() === float_array.toString()); +assert(int_array.join('-') === "3-2-1-100--30"); +assert(uint_array.join('=') === "3=2=1=100=226"); +assert(empty_array.join('_') === ""); diff --git a/tests/jerry/es2015/typedArray-tostring.js b/tests/jerry/es2015/typedArray-tostring.js new file mode 100644 index 0000000000..27fc33a485 --- /dev/null +++ b/tests/jerry/es2015/typedArray-tostring.js @@ -0,0 +1,24 @@ +/* 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 float_array = new Float32Array([1.125, 5.5, -1.25, -0.0]); +var int_array = new Int8Array([3, 2, 1, 100, -30]) +var uint_array = new Uint8Array([3, 2, 1, 100, -30]) +var empty_array = new Uint32Array(); + +assert(float_array.toString() === "1.125,5.5,-1.25,0"); +assert(int_array.toString() === "3,2,1,100,-30"); +assert(uint_array.toString() === "3,2,1,100,226"); +assert(empty_array.toString() === "");