Skip to content

Commit f9109ba

Browse files
committed
Implement Object.prototype.toLocaleString
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent b8e4328 commit f9109ba

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var obj1 = {};
2+
obj1.toString = function () { return "mystring"; }
3+
4+
assert (obj1.toLocaleString() === "mystring");
5+
6+
var obj2 = {a: 3};
7+
assert (obj2.toLocaleString() === "[object Object]");
8+
9+
10+
var obj3 = {toLocaleString: function() { throw ReferenceError ("foo"); }};
11+
try {
12+
obj3.toLocaleString();
13+
14+
assert (false);
15+
} catch (e) {
16+
assert (e.message === "foo");
17+
assert (e instanceof ReferenceError);
18+
}
19+
20+
// Test invalid toString call
21+
var obj4 = {toString: 2};
22+
try {
23+
obj4.toLocaleString();
24+
25+
assert (false);
26+
} catch (e) {
27+
assert (e instanceof TypeError);
28+
}
29+
30+
// Test undefined toString call
31+
var obj5 = {};
32+
var obj6;
33+
obj5.toString = obj6
34+
try {
35+
obj5.toLocaleString();
36+
37+
assert (false);
38+
} catch (e) {
39+
assert (e instanceof TypeError);
40+
}

0 commit comments

Comments
 (0)