From 6e79bb148e127419e6023187a92f2d672aa599e6 Mon Sep 17 00:00:00 2001 From: Szilagyi Adam Date: Fri, 13 Sep 2019 13:40:56 +0200 Subject: [PATCH] Add toLocaleString for TypedArrays (#2987) The algorithm is based on ECMA-262 v6, 22.2.3.27 Co-authored-by: Tibor Dusnoki tdusnoki@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu --- .../ecma-builtin-typedarray-prototype.c | 129 ++++++++++++++++++ .../ecma-builtin-typedarray-prototype.inc.h | 1 + .../typedarray-prototype-tolocalestring.js | 105 ++++++++++++++ 3 files changed, 235 insertions(+) create mode 100644 tests/jerry/es2015/typedarray-prototype-tolocalestring.js 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 f93d86ff5..42249e755 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 @@ -1977,6 +1977,135 @@ ecma_builtin_typedarray_prototype_slice (ecma_value_t this_arg, /**< this argume return new_typedarray; } /* ecma_builtin_typedarray_prototype_slice */ +/** + * The TypedArray.prototype's 'toLocaleString' single element operation routine. + * + * See also: + * ECMA-262 v6, 22.1.3.26 steps 7-10 and 12.b-e + * + * @return ecma value + * Returned value must be freed with ecma_free_value. +*/ +static ecma_value_t +ecma_builtin_typedarray_prototype_to_locale_string_helper (ecma_object_t *this_obj, /**< TypedArray object */ + uint32_t index) /** array index */ +{ + lit_magic_string_id_t class_id = ecma_object_get_class_name (this_obj); + lit_utf8_byte_t *typedarray_buffer_p = ecma_typedarray_get_buffer (this_obj); + + ecma_value_t ret_value = ECMA_VALUE_EMPTY; + ecma_number_t element_num = ecma_get_typedarray_element (typedarray_buffer_p + index, class_id); + ecma_value_t element_value = ecma_make_number_value (element_num); + + ecma_value_t element_obj = ecma_op_to_object (element_value); + + if (ECMA_IS_VALUE_ERROR (element_obj)) + { + ecma_free_value (element_value); + return element_obj; + } + + ecma_object_t *element_obj_p = ecma_get_object_from_value (element_obj); + + ecma_value_t func_value = ecma_op_object_get_by_magic_id (element_obj_p, + LIT_MAGIC_STRING_TO_LOCALE_STRING_UL); + + if (ECMA_IS_VALUE_ERROR (func_value)) + { + ecma_free_value (element_value); + ecma_deref_object (element_obj_p); + return func_value; + } + + if (ecma_op_is_callable (func_value)) + { + ecma_object_t *func_obj = ecma_get_object_from_value (func_value); + ecma_value_t call_value = ecma_op_function_call (func_obj, + element_obj, + NULL, + 0); + + ecma_deref_object (func_obj); + + if (ECMA_IS_VALUE_ERROR (call_value)) + { + ecma_free_value (element_value); + ecma_deref_object (element_obj_p); + return call_value; + } + + ret_value = ecma_op_to_string (call_value); + } + else + { + ecma_free_value (func_value); + ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("'toLocaleString' is missing or not a function.")); + } + + ecma_deref_object (element_obj_p); + ecma_free_value (element_value); + + return ret_value; +} /* ecma_builtin_typedarray_prototype_to_locale_string_helper */ + +/** + * The %TypedArray%.prototype object's 'toLocaleString' routine + * + * See also: + * ECMA-262 v6, 22.2.3.27 + * + * @return ecma value + * Returned value must be freed with ecma_free_value. + */ +static ecma_value_t +ecma_builtin_typedarray_prototype_to_locale_string (ecma_value_t this_arg) /**< this argument */ +{ + if (!ecma_is_typedarray (this_arg)) + { + return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not a TypedArray.")); + } + + ecma_object_t *typedarray_p = ecma_get_object_from_value (this_arg); + uint32_t length = ecma_typedarray_get_length (typedarray_p); + uint8_t shift = ecma_typedarray_get_element_size_shift (typedarray_p); + uint8_t element_size = (uint8_t) (1 << shift); + uint32_t limit = length * element_size; + + if (length == 0) + { + return ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); + } + + ecma_value_t first_element = ecma_builtin_typedarray_prototype_to_locale_string_helper (typedarray_p, 0); + + if (ECMA_IS_VALUE_ERROR (first_element)) + { + return first_element; + } + + ecma_string_t *return_string_p = ecma_get_string_from_value (first_element); + + for (uint32_t k = element_size; k < limit; k += element_size) + { + return_string_p = ecma_append_magic_string_to_string (return_string_p, LIT_MAGIC_STRING_COMMA_CHAR); + ecma_value_t next_element = ecma_builtin_typedarray_prototype_to_locale_string_helper (typedarray_p, k); + + if (ECMA_IS_VALUE_ERROR (next_element)) + { + ecma_free_value (first_element); + return next_element; + } + + ecma_string_t *next_element_p = ecma_get_string_from_value (next_element); + return_string_p = ecma_concat_ecma_strings (return_string_p, next_element_p); + ecma_deref_ecma_string (next_element_p); + } + + ecma_free_value (first_element); + + return ecma_make_string_value (return_string_p); +} /* ecma_builtin_typedarray_prototype_to_locale_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 342178a8e..7ca5b43af 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 @@ -74,6 +74,7 @@ ROUTINE (LIT_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_typedarray_prototype_index_o ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_typedarray_prototype_last_index_of, NON_FIXED, 1) ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ecma_builtin_typedarray_prototype_copy_within, NON_FIXED, 2) ROUTINE (LIT_MAGIC_STRING_SLICE, ecma_builtin_typedarray_prototype_slice, NON_FIXED, 2) +ROUTINE (LIT_MAGIC_STRING_TO_LOCALE_STRING_UL, ecma_builtin_typedarray_prototype_to_locale_string, 0, 0) #if ENABLED (JERRY_ES2015_BUILTIN_ITERATOR) diff --git a/tests/jerry/es2015/typedarray-prototype-tolocalestring.js b/tests/jerry/es2015/typedarray-prototype-tolocalestring.js new file mode 100644 index 000000000..9926c44b4 --- /dev/null +++ b/tests/jerry/es2015/typedarray-prototype-tolocalestring.js @@ -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 int_typedarrays = [ + new Uint8Array([0, 1, 2, 3, 4, 5]), + new Uint16Array([0, 1, 2, 3, 4, 5]), + new Uint32Array([0, 1, 2, 3, 4, 5]), + new Int8Array([0, 1, 2, 3, 4, 5]), + new Int16Array([0, 1, 2, 3, 4, 5]), + new Int32Array([0, 1, 2, 3, 4, 5]) +]; + +var float_typedarrays = [ + new Float32Array([0, 1, 2, 3, 4, 5]), + new Float64Array([0, 1, 2, 3, 4, 5]), +]; + +int_typedarrays.forEach(function(e){ + // Test with normal inputs + assert(e.toLocaleString() === "0,1,2,3,4,5"); + e.set([3, 3, 6, 1, 2, 9]); + assert(e.toLocaleString() === "3,3,6,1,2,9"); + e.set([32, 12, 2, 8, 32, 1]); + assert(e.toLocaleString() === "32,12,2,8,32,1"); + // Test with undefined + e.set([12, undefined, 0, undefined, 3, 5]); + assert(e.toLocaleString() === "12,0,0,0,3,5"); + // Test with null + e.set([null, 3, 3, 3, null, 10]); + assert(e.toLocaleString() === "0,3,3,3,0,10"); + e.set([12, null, 21, null, null, null]); + assert(e.toLocaleString() === "12,0,21,0,0,0"); +}); + +float_typedarrays.forEach(function(e){ + // Test with normal inputs + assert(e.toLocaleString() === "0,1,2,3,4,5"); + e.set([3, 3, 6, 1, 2, 9]); + assert(e.toLocaleString() === "3,3,6,1,2,9"); + e.set([32, 12, 2, 8, 32, 1]); + assert(e.toLocaleString() === "32,12,2,8,32,1"); + // Test with undefined + e.set([12, undefined, 0, undefined, 3, 5]); + assert(e.toLocaleString() === "12,NaN,0,NaN,3,5"); + // Test with null + e.set([null, 3, 3, 3, null, 10]); + assert(e.toLocaleString() === "0,3,3,3,0,10"); + e.set([12, null, 21, null, null, null]); + assert(e.toLocaleString() === "12,0,21,0,0,0"); +}); + +// Test empty TypedArrays +var empty_typedarrays = [ + new Uint8Array([]), + new Uint16Array([]), + new Uint32Array([]), + new Float32Array([]), + new Float64Array([]), + new Int8Array([]), + new Int16Array([]), + new Int32Array([]) +]; + +empty_typedarrays.forEach(function(e){ + assert(e.toLocaleString() === ""); +}); + +Number.prototype.toLocaleString = function() { return "5" } + +var typedarrays = [ + new Uint8Array([10]), + new Uint16Array([10]), + new Uint32Array([10]), + new Float32Array([10]), + new Float64Array([10]), + new Int8Array([10]), + new Int16Array([10]), + new Int32Array([10]) +]; + +typedarrays.forEach(function(e){ + assert(e.toLocaleString() === "5"); +}); + +Number.prototype.toLocaleString = "foo"; + +typedarrays.forEach(function(e){ + try { + assert(e.toLocaleString() === "foo"); + assert(false); + } catch (err) { + assert(err instanceof TypeError); + } +});