From f15fdaeee035789ba9c68d38aa04c17afbcdaa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zsolt=20Borb=C3=A9ly?= Date: Mon, 13 Jul 2015 15:36:37 +0200 Subject: [PATCH] Implement String.prototype.localeCompare function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com --- .../ecma-builtin-string-prototype.cpp | 43 ++++++++++++++- tests/jerry/string-prototype-localecompare.js | 54 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/string-prototype-localecompare.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index bfb6c7e8aa..d3dcdff4e1 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -286,7 +286,48 @@ static ecma_completion_value_t ecma_builtin_string_prototype_object_locale_compare (ecma_value_t this_arg, /**< this argument */ ecma_value_t arg) /**< routine's argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg); + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + /* 1. */ + ECMA_TRY_CATCH (this_check_coercible_val, + ecma_op_check_object_coercible (this_arg), + ret_value); + + /* 2. */ + ECMA_TRY_CATCH (this_to_string_val, + ecma_op_to_string (this_arg), + ret_value); + + /* 3. */ + ECMA_TRY_CATCH (arg_to_string_val, + ecma_op_to_string (arg), + ret_value); + + ecma_string_t *this_string_p = ecma_get_string_from_value (this_to_string_val); + ecma_string_t *arg_string_p = ecma_get_string_from_value (arg_to_string_val); + + ecma_number_t *result_p = ecma_alloc_number (); + + if (ecma_compare_ecma_strings_relational (this_string_p, arg_string_p)) + { + *result_p = ecma_int32_to_number (-1); + } + else if (!ecma_compare_ecma_strings (this_string_p, arg_string_p)) + { + *result_p = ecma_int32_to_number (1); + } + else + { + *result_p = ecma_int32_to_number (0); + } + + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (result_p)); + + ECMA_FINALIZE (arg_to_string_val); + ECMA_FINALIZE (this_to_string_val); + ECMA_FINALIZE (this_check_coercible_val); + + return ret_value; } /* ecma_builtin_string_prototype_object_locale_compare */ /** diff --git a/tests/jerry/string-prototype-localecompare.js b/tests/jerry/string-prototype-localecompare.js new file mode 100644 index 0000000000..a247021843 --- /dev/null +++ b/tests/jerry/string-prototype-localecompare.js @@ -0,0 +1,54 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// Copyright 2015 University of Szeged. +// +// 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 str1 = "ab"; +var str2 = "cd"; +assert (str1.localeCompare(str1) === 0); +assert (str1.localeCompare(str2) === -1); +assert (str2.localeCompare(str1) === 1); + +var x = "32"; +var y = "-32"; +assert (y.localeCompare(-31) === 1); +assert (y.localeCompare("") === 1); +assert (y.localeCompare(-32) === 0); +assert (x.localeCompare(33) === -1); +assert (x.localeCompare() === -1); +assert (x.localeCompare(null) === -1); +assert (x.localeCompare(NaN) === -1); +assert (x.localeCompare(Infinity) === -1); +assert (x.localeCompare(-Infinity) === 1); + +var array1 = ["1", 2]; +var array2 = [3, 4]; +assert (String.prototype.localeCompare.call(42, array1) === 1); +assert (String.prototype.localeCompare.call(array1, null) === -1); +assert (String.prototype.localeCompare.call(array1, array1) === 0); +assert (String.prototype.localeCompare.call(array1, array2) === -1); +assert (String.prototype.localeCompare.call(array2, array1) === 1); + +try { + var res = String.prototype.localeCompare.call(null, 0); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +try { + var res = String.prototype.localeCompare.call(); + assert (false); +} catch (e) { + assert (e instanceof TypeError); +}