From 7954df3e82baf41ec0389690d3649d44039b5c3e Mon Sep 17 00:00:00 2001 From: Peter Gal Date: Tue, 12 May 2015 16:38:46 +0200 Subject: [PATCH] Implement Object.prototype.toLocaleString JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com --- .../ecma-builtin-object-prototype.cpp | 34 +++++++++++- .../jerry/object_prototype_tolocalestring.js | 55 +++++++++++++++++++ 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/object_prototype_tolocalestring.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp index e22ddf25bc..cd304e3047 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.cpp @@ -17,6 +17,7 @@ #include "ecma-builtins.h" #include "ecma-conversion.h" #include "ecma-exceptions.h" +#include "ecma-function-object.h" #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" @@ -157,7 +158,38 @@ ecma_builtin_object_prototype_object_value_of (ecma_value_t this_arg) /**< this static ecma_completion_value_t ecma_builtin_object_prototype_object_to_locale_string (ecma_value_t this_arg) /**< this argument */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg); + ecma_completion_value_t return_value = ecma_make_empty_completion_value (); + /* 1. */ + ECMA_TRY_CATCH (obj_val, + ecma_op_to_object (this_arg), + return_value); + + ecma_object_t *obj_p = ecma_get_object_from_value (obj_val); + ecma_string_t *to_string_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_TO_STRING_UL); + + /* 2. */ + ECMA_TRY_CATCH (to_string_val, + ecma_op_object_get (obj_p, to_string_magic_string_p), + return_value); + + /* 3. */ + if (!ecma_op_is_callable (to_string_val)) + { + return_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + } + else + { + /* 4. */ + ecma_object_t *to_string_func_obj_p = ecma_get_object_from_value (to_string_val); + return_value = ecma_op_function_call (to_string_func_obj_p, this_arg, NULL, 0); + } + ECMA_FINALIZE (to_string_val); + + ecma_deref_ecma_string (to_string_magic_string_p); + + ECMA_FINALIZE (obj_val); + + return return_value; } /* ecma_builtin_object_prototype_object_to_locale_string */ /** diff --git a/tests/jerry/object_prototype_tolocalestring.js b/tests/jerry/object_prototype_tolocalestring.js new file mode 100644 index 0000000000..9be6e495b2 --- /dev/null +++ b/tests/jerry/object_prototype_tolocalestring.js @@ -0,0 +1,55 @@ +// 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 obj1 = {}; +obj1.toString = function () { return "mystring"; } + +assert (obj1.toLocaleString() === "mystring"); + +var obj2 = {a: 3}; +assert (obj2.toLocaleString() === "[object Object]"); + + +var obj3 = {toLocaleString: function() { throw ReferenceError ("foo"); }}; +try { + obj3.toLocaleString(); + + assert (false); +} catch (e) { + assert (e.message === "foo"); + assert (e instanceof ReferenceError); +} + +// Test invalid toString call +var obj4 = {toString: 2}; +try { + obj4.toLocaleString(); + + assert (false); +} catch (e) { + assert (e instanceof TypeError); +} + +// Test undefined toString call +var obj5 = {}; +var obj6; +obj5.toString = obj6 +try { + obj5.toLocaleString(); + + assert (false); +} catch (e) { + assert (e instanceof TypeError); +}