Skip to content

Commit 81a8762

Browse files
committed
String.prototype.concat()
JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs [email protected]
1 parent cefeea0 commit 81a8762

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

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

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,72 @@ 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 internal list required since ToString does not have side-effects
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+
if (ecma_is_value_undefined (argument_list_p[arg_index]))
176+
{
177+
// FIXME: use ecma_get_string_from_value when it recognizes undefined
178+
ecma_string_t *string_undef = ecma_new_ecma_string ((unsigned char *)"undefined");
179+
180+
string_to_return = ecma_concat_ecma_strings (string_to_return, string_undef);
181+
182+
ecma_deref_ecma_string (string_undef);
183+
}
184+
else
185+
{
186+
ECMA_TRY_CATCH (get_arg_string,
187+
ecma_op_to_string (argument_list_p[arg_index]),
188+
ret_value);
189+
190+
string_to_return = ecma_concat_ecma_strings (string_to_return, ecma_get_string_from_value (get_arg_string));
191+
192+
ECMA_FINALIZE (get_arg_string);
193+
}
194+
195+
ecma_deref_ecma_string (string_temp);
196+
}
197+
198+
/* 6 */
199+
if (ecma_is_completion_value_empty (ret_value))
200+
{
201+
ret_value = ecma_make_normal_completion_value (ecma_make_string_value (string_to_return));
202+
}
203+
else
204+
{
205+
ecma_deref_ecma_string (string_to_return);
206+
}
207+
208+
ECMA_FINALIZE (to_string_val);
209+
ECMA_FINALIZE (check_coercible_val);
210+
211+
return ret_value;
147212
} /* ecma_builtin_string_prototype_object_concat */
148213

149214
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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("C:"+Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').configurable === "C:false");
18+
19+
assert("E:"+Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').enumerable === "E:false");
20+
21+
assert("E:"+Object.getOwnPropertyDescriptor(String.prototype.concat, 'length').writable === "E: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 x;
34+
assert("Check ".concat(x) === "Check undefined");

0 commit comments

Comments
 (0)