Skip to content

Commit 1850cfb

Browse files
committed
String.prototype.concat()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent 53801e3 commit 1850cfb

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

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

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,60 @@ ecma_builtin_string_prototype_object_concat (ecma_value_t this_arg, /**< this ar
143143
const ecma_value_t* argument_list_p, /**< arguments list */
144144
ecma_length_t arguments_number) /**< number of arguments */
145145
{
146-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg, argument_list_p, arguments_number);
146+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
147+
148+
/* 1 */
149+
ECMA_TRY_CATCH (check_coercible_val,
150+
ecma_op_check_object_coercible (this_arg),
151+
ret_value);
152+
153+
/* 2 */
154+
ECMA_TRY_CATCH (to_string_val,
155+
ecma_op_to_string (this_arg),
156+
ret_value);
157+
158+
/* 3 */
159+
// No copy performed
160+
161+
/* 4 */
162+
ecma_string_t *string_to_return = ecma_copy_or_ref_ecma_string (ecma_get_string_from_value (to_string_val));
163+
164+
JERRY_ASSERT (ecma_string_get_length (string_to_return) >= 0);
165+
166+
/* 5 */
167+
for (uint32_t arg_index = 0;
168+
arg_index < arguments_number && ecma_is_completion_value_empty (ret_value);
169+
++arg_index)
170+
{
171+
/* 5a */
172+
/* 5b */
173+
ecma_string_t *string_temp = string_to_return;
174+
175+
ECMA_TRY_CATCH (get_arg_string,
176+
ecma_op_to_string (argument_list_p[arg_index]),
177+
ret_value);
178+
179+
string_to_return = ecma_concat_ecma_strings (string_to_return, ecma_get_string_from_value (get_arg_string));
180+
181+
ecma_deref_ecma_string (string_temp);
182+
183+
ECMA_FINALIZE (get_arg_string);
184+
}
185+
186+
/* 6 */
187+
if (ecma_is_completion_value_empty (ret_value))
188+
{
189+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (string_to_return));
190+
}
191+
else
192+
{
193+
ecma_deref_ecma_string (string_to_return);
194+
}
195+
196+
ECMA_FINALIZE (to_string_val);
197+
ECMA_FINALIZE (check_coercible_val);
198+
199+
return ret_value;
147200
} /* ecma_builtin_string_prototype_object_concat */
148201

149202
/**
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+
// check properties
17+
assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').configurable === false);
18+
19+
assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').enumerable === false);
20+
21+
assert(Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').writable === false);
22+
23+
// simple checks
24+
var s1 = "Hello ";
25+
var s2 = "world!";
26+
var s3 = " ";
27+
assert(s1.concat(s2, s3, 3, 10, " ", ".") === "Hello world! 310 .");
28+
assert("Hello ".concat(s1) === "Hello Hello ");
29+
30+
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");
31+
32+
// check undefined
33+
var y;
34+
assert("Check ".concat(y) === "Check undefined");
35+
36+
// check toString error in this object
37+
var y = {};
38+
y.toString = function () { throw new ReferenceError ("foo");}
39+
y.concat = String.prototype.concat;
40+
try {
41+
y.concat("cat");
42+
assert(false);
43+
} catch (e) {
44+
assert(e instanceof ReferenceError);
45+
}
46+
47+
// check toString error in arguments
48+
var x = {};
49+
x.toString = function () { throw new ReferenceError ("foo");}
50+
try {
51+
"a".concat(x);
52+
assert(false);
53+
} catch (e) {
54+
assert(e instanceof ReferenceError);
55+
}

0 commit comments

Comments
 (0)