Skip to content

Commit 474a805

Browse files
author
Zsolt Borbély
committed
Implement String.prototype.localeCompare function
JerryScript-DCO-1.0-Signed-off-by: Zsolt Borbély zsborbely.u-szeged@partner.samsung.com
1 parent bc0ca7b commit 474a805

File tree

2 files changed

+111
-1
lines changed

2 files changed

+111
-1
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,48 @@ static ecma_completion_value_t
286286
ecma_builtin_string_prototype_object_locale_compare (ecma_value_t this_arg, /**< this argument */
287287
ecma_value_t arg) /**< routine's argument */
288288
{
289-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, arg);
289+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
290+
291+
/* 1. */
292+
ECMA_TRY_CATCH (this_check_coercible_val,
293+
ecma_op_check_object_coercible (this_arg),
294+
ret_value);
295+
296+
/* 2. */
297+
ECMA_TRY_CATCH (this_to_string_val,
298+
ecma_op_to_string (this_arg),
299+
ret_value);
300+
301+
/* 3. */
302+
ECMA_TRY_CATCH (arg_to_string_val,
303+
ecma_op_to_string (arg),
304+
ret_value);
305+
306+
ecma_string_t *this_string_p = ecma_get_string_from_completion_value (this_to_string_val);
307+
ecma_string_t *arg_string_p = ecma_get_string_from_completion_value (arg_to_string_val);
308+
309+
ecma_number_t *result_p = ecma_alloc_number ();
310+
311+
if (ecma_compare_ecma_strings_relational (this_string_p, arg_string_p))
312+
{
313+
*result_p = ecma_int32_to_number (-1);
314+
}
315+
else if (!ecma_compare_ecma_strings (this_string_p, arg_string_p))
316+
{
317+
*result_p = ecma_int32_to_number (1);
318+
}
319+
else
320+
{
321+
*result_p = ecma_int32_to_number (0);
322+
}
323+
324+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (result_p));
325+
326+
ECMA_FINALIZE (arg_to_string_val);
327+
ECMA_FINALIZE (this_to_string_val);
328+
ECMA_FINALIZE (this_check_coercible_val);
329+
330+
return ret_value;
290331
} /* ecma_builtin_string_prototype_object_locale_compare */
291332

292333
/**
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var str1 = "ab";
17+
var str2 = "cd";
18+
assert (str1.localeCompare(str1) === 0);
19+
assert (str1.localeCompare(str2) === -1);
20+
assert (str2.localeCompare(str1) === 1);
21+
22+
var x = "32";
23+
var y = "-32";
24+
assert (y.localeCompare(-31) === 1);
25+
assert (y.localeCompare("") === 1);
26+
assert (y.localeCompare(-32) === 0);
27+
assert (x.localeCompare(33) === -1);
28+
assert (x.localeCompare() === -1);
29+
assert (x.localeCompare(null) === -1);
30+
assert (x.localeCompare(NaN) === -1);
31+
assert (x.localeCompare(Infinity) === -1);
32+
assert (x.localeCompare(-Infinity) === 1);
33+
34+
var array = [-1, "2", 4, undefined];
35+
36+
try {
37+
var res = array.localeCompare(array);
38+
assert (false);
39+
} catch (e) {
40+
assert (e instanceof TypeError);
41+
}
42+
43+
try {
44+
var res = array.localeCompare(33);
45+
assert (false);
46+
} catch (e) {
47+
assert (e instanceof TypeError);
48+
}
49+
50+
try {
51+
var res = null.localeCompare(0);
52+
assert (false);
53+
} catch (e) {
54+
assert (e instanceof TypeError);
55+
}
56+
57+
try {
58+
var res = {}.localeCompare();
59+
assert (false);
60+
} catch (e) {
61+
assert (e instanceof TypeError);
62+
}
63+
64+
try {
65+
var res = localeCompare();
66+
assert (false);
67+
} catch (e) {
68+
assert (e instanceof ReferenceError);
69+
}

0 commit comments

Comments
 (0)