Implement Object.prototype.toLocaleString

JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
Peter Gal
2015-05-12 16:38:46 +02:00
parent 6b09e53060
commit 7954df3e82
2 changed files with 88 additions and 1 deletions
@@ -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 */
/**
@@ -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);
}