Skip to content

Commit 996bb5a

Browse files
committed
Implement Object.prototype.toLocaleString
JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent b8e4328 commit 996bb5a

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ecma-builtins.h"
1818
#include "ecma-conversion.h"
1919
#include "ecma-exceptions.h"
20+
#include "ecma-function-object.h"
2021
#include "ecma-gc.h"
2122
#include "ecma-globals.h"
2223
#include "ecma-helpers.h"
@@ -157,7 +158,38 @@ ecma_builtin_object_prototype_object_value_of (ecma_value_t this_arg) /**< this
157158
static ecma_completion_value_t
158159
ecma_builtin_object_prototype_object_to_locale_string (ecma_value_t this_arg) /**< this argument */
159160
{
160-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
161+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
162+
/* 1. */
163+
ECMA_TRY_CATCH (obj_val,
164+
ecma_op_to_object (this_arg),
165+
return_value);
166+
167+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_val);
168+
ecma_string_t *to_string_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_TO_STRING_UL);
169+
170+
/* 2. */
171+
ECMA_TRY_CATCH (to_string_val,
172+
ecma_op_object_get (obj_p, to_string_magic_string_p),
173+
return_value);
174+
175+
/* 3. */
176+
if (!ecma_op_is_callable (to_string_val))
177+
{
178+
return_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE));
179+
}
180+
else
181+
{
182+
/* 4. */
183+
ecma_object_t *to_string_func_obj_p = ecma_get_object_from_value (to_string_val);
184+
return_value = ecma_op_function_call (to_string_func_obj_p, this_arg, NULL, 0);
185+
}
186+
ECMA_FINALIZE (to_string_val)
187+
188+
ecma_deref_ecma_string (to_string_magic_string_p);
189+
190+
ECMA_FINALIZE (obj_val)
191+
192+
return return_value;
161193
} /* ecma_builtin_object_prototype_object_to_locale_string */
162194

163195
/**
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 obj1 = {};
17+
obj1.toString = function () { return "mystring"; }
18+
19+
assert (obj1.toLocaleString() === "mystring");
20+
21+
var obj2 = {a: 3};
22+
assert (obj2.toLocaleString() === "[object Object]");
23+
24+
25+
var obj3 = {toLocaleString: function() { throw ReferenceError ("foo"); }};
26+
try {
27+
obj3.toLocaleString();
28+
29+
assert (false);
30+
} catch (e) {
31+
assert (e.message === "foo");
32+
assert (e instanceof ReferenceError);
33+
}
34+
35+
// Test invalid toString call
36+
var obj4 = {toString: 2};
37+
try {
38+
obj4.toLocaleString();
39+
40+
assert (false);
41+
} catch (e) {
42+
assert (e instanceof TypeError);
43+
}
44+
45+
// Test undefined toString call
46+
var obj5 = {};
47+
var obj6;
48+
obj5.toString = obj6
49+
try {
50+
obj5.toLocaleString();
51+
52+
assert (false);
53+
} catch (e) {
54+
assert (e instanceof TypeError);
55+
}

0 commit comments

Comments
 (0)