From 1850cfbb556d1a5c9fd5f4846df0331e8dbda570 Mon Sep 17 00:00:00 2001 From: Laszlo Vidacs Date: Mon, 8 Jun 2015 16:51:31 +0200 Subject: [PATCH] String.prototype.concat() JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com --- .../ecma-builtin-string-prototype.cpp | 55 ++++++++++++++++++- tests/jerry/string_prototype_concat.js | 55 +++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/string_prototype_concat.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp index 1ca00a9b83..d2d139c2ee 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-string-prototype.cpp @@ -143,7 +143,60 @@ ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this ar const ecma_value_t* argument_list_p, /**< arguments list */ ecma_length_t arguments_number) /**< number of arguments */ { - ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, argument_list_p, arguments_number); + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + /* 1 */ + ECMA_TRY_CATCH (check_coercible_val, + ecma_op_check_object_coercible (this_arg), + ret_value); + + /* 2 */ + ECMA_TRY_CATCH (to_string_val, + ecma_op_to_string (this_arg), + ret_value); + + /* 3 */ + // No copy performed + + /* 4 */ + ecma_string_t *string_to_return = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (to_string_val)); + + JERRY_ASSERT (ecma_string_get_length (string_to_return) >= 0); + + /* 5 */ + for (uint32_t arg_index = 0; + arg_index < arguments_number && ecma_is_completion_value_empty (ret_value); + ++arg_index) + { + /* 5a */ + /* 5b */ + ecma_string_t *string_temp = string_to_return; + + ECMA_TRY_CATCH (get_arg_string, + ecma_op_to_string (argument_list_p[arg_index]), + ret_value); + + string_to_return = ecma_concat_ecma_strings (string_to_return, ecma_get_string_from_value (get_arg_string)); + + ecma_deref_ecma_string (string_temp); + + ECMA_FINALIZE (get_arg_string); + } + + /* 6 */ + if (ecma_is_completion_value_empty (ret_value)) + { + ret_value = ecma_make_normal_completion_value (ecma_make_string_value (string_to_return)); + } + else + { + ecma_deref_ecma_string (string_to_return); + } + + ECMA_FINALIZE (to_string_val); + ECMA_FINALIZE (check_coercible_val); + + return ret_value; } /* ecma_builtin_string_prototype_object_concat */ /** diff --git a/tests/jerry/string_prototype_concat.js b/tests/jerry/string_prototype_concat.js new file mode 100644 index 0000000000..7a890f8446 --- /dev/null +++ b/tests/jerry/string_prototype_concat.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. + +// check properties +assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').configurable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').enumerable === false); + +assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').writable === false); + +// simple checks +var s1 = "Hello "; +var s2 = "world!"; +var s3 = " "; +assert(s1.concat(s2, s3, 3, 10, " ", ".") === "Hello world! 310 ."); +assert("Hello ".concat(s1) === "Hello Hello "); + +assert(s1.concat(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9) === "Hello 012345678901234567890123456789"); + +// check undefined +var y; +assert("Check ".concat(y) === "Check undefined"); + +// check toString error in this object +var y = {}; +y.toString = function () { throw new ReferenceError ("foo");} +y.concat = String.prototype.concat; +try { + y.concat("cat"); + assert(false); +} catch (e) { + assert(e instanceof ReferenceError); +} + +// check toString error in arguments +var x = {}; +x.toString = function () { throw new ReferenceError ("foo");} +try { + "a".concat(x); + assert(false); +} catch (e) { + assert(e instanceof ReferenceError); +}