Skip to content

Commit c16e065

Browse files
committed
Implement Array.prototype.toString
JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 137c5f3 commit c16e065

File tree

5 files changed

+268
-75
lines changed

5 files changed

+268
-75
lines changed

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "ecma-alloc.h"
1717
#include "ecma-array-object.h"
18+
#include "ecma-builtin-helpers.h"
1819
#include "ecma-builtins.h"
1920
#include "ecma-comparison.h"
2021
#include "ecma-conversion.h"
@@ -180,7 +181,42 @@ ecma_builtin_array_prototype_object_for_each (ecma_value_t this_arg, /**< this a
180181
static ecma_completion_value_t
181182
ecma_builtin_array_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
182183
{
183-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
184+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
185+
186+
/* 1. */
187+
ECMA_TRY_CATCH (obj_this_value,
188+
ecma_op_to_object (this_arg),
189+
return_value);
190+
191+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value);
192+
193+
ecma_string_t *join_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_JOIN);
194+
195+
/* 2. */
196+
ECMA_TRY_CATCH (join_value,
197+
ecma_op_object_get (obj_p, join_magic_string_p),
198+
return_value);
199+
200+
if (!ecma_op_is_callable (join_value))
201+
{
202+
/* 3. */
203+
return_value = ecma_builtin_helper_object_to_string (this_arg);
204+
}
205+
else
206+
{
207+
/* 4. */
208+
ecma_object_t *join_func_obj_p = ecma_get_object_from_value (join_value);
209+
210+
return_value = ecma_op_function_call (join_func_obj_p, this_arg, NULL, 0);
211+
}
212+
213+
ECMA_FINALIZE (join_value);
214+
215+
ecma_deref_ecma_string (join_magic_string_p);
216+
217+
ECMA_FINALIZE (obj_this_value);
218+
219+
return return_value;
184220
} /* ecma_builtin_array_prototype_object_to_string */
185221

186222
/**
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
17+
#include "ecma-builtin-helpers.h"
18+
19+
#include "ecma-builtins.h"
20+
#include "ecma-conversion.h"
21+
#include "ecma-exceptions.h"
22+
#include "ecma-helpers.h"
23+
#include "ecma-objects.h"
24+
25+
/** \addtogroup ecma ECMA
26+
* @{
27+
*
28+
* \addtogroup ecmabuiltinhelpers ECMA builtin helper operations
29+
* @{
30+
*/
31+
32+
/**
33+
* Common implementation of the Object.prototype.toString routine
34+
*
35+
* See also:
36+
* ECMA-262 v5, 15.2.4.2
37+
*
38+
* Used by:
39+
* - The Object.prototype.toString routine.
40+
* - The Array.prototype.toString routine as fallback.
41+
*
42+
* @return completion value
43+
* Returned value must be freed with ecma_free_completion_value.
44+
*/
45+
46+
ecma_completion_value_t
47+
ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this argument */
48+
{
49+
ecma_magic_string_id_t type_string;
50+
51+
if (ecma_is_value_undefined (this_arg))
52+
{
53+
type_string = ECMA_MAGIC_STRING_UNDEFINED_UL;
54+
}
55+
else if (ecma_is_value_null (this_arg))
56+
{
57+
type_string = ECMA_MAGIC_STRING_NULL_UL;
58+
}
59+
else
60+
{
61+
ecma_completion_value_t obj_this = ecma_op_to_object (this_arg);
62+
63+
if (!ecma_is_completion_value_normal (obj_this))
64+
{
65+
return obj_this;
66+
}
67+
68+
JERRY_ASSERT (ecma_is_value_object (ecma_get_completion_value_value (obj_this)));
69+
70+
ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this);
71+
72+
ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p,
73+
ECMA_INTERNAL_PROPERTY_CLASS);
74+
type_string = (ecma_magic_string_id_t) class_prop_p->u.internal_property.value;
75+
76+
ecma_free_completion_value (obj_this);
77+
}
78+
79+
ecma_string_t *ret_string_p;
80+
81+
/* Building string "[object #type#]" where type is 'Undefined',
82+
'Null' or one of possible object's classes.
83+
The string with null character is maximum 19 characters long. */
84+
const ssize_t buffer_size = 19;
85+
MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, ecma_char_t);
86+
87+
const ecma_char_t *left_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR);
88+
const ecma_char_t *object_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_OBJECT);
89+
const ecma_char_t *space_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR);
90+
const ecma_char_t *type_name_zt_str_p = ecma_get_magic_string_zt (type_string);
91+
const ecma_char_t *right_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR);
92+
93+
ecma_char_t *buffer_ptr = str_buffer;
94+
ssize_t buffer_size_left = buffer_size;
95+
buffer_ptr = ecma_copy_zt_string_to_buffer (left_square_zt_str_p,
96+
buffer_ptr,
97+
buffer_size_left);
98+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
99+
buffer_ptr = ecma_copy_zt_string_to_buffer (object_zt_str_p,
100+
buffer_ptr,
101+
buffer_size_left);
102+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
103+
buffer_ptr = ecma_copy_zt_string_to_buffer (space_zt_str_p,
104+
buffer_ptr,
105+
buffer_size_left);
106+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
107+
buffer_ptr = ecma_copy_zt_string_to_buffer (type_name_zt_str_p,
108+
buffer_ptr,
109+
buffer_size_left);
110+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
111+
buffer_ptr = ecma_copy_zt_string_to_buffer (right_square_zt_str_p,
112+
buffer_ptr,
113+
buffer_size_left);
114+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
115+
116+
JERRY_ASSERT (buffer_size_left >= 0);
117+
118+
ret_string_p = ecma_new_ecma_string (str_buffer);
119+
120+
MEM_FINALIZE_LOCAL_ARRAY (str_buffer);
121+
122+
return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p));
123+
} /* ecma_builtin_helper_object_to_string */
124+
125+
/**
126+
* @}
127+
* @}
128+
* @}
129+
*/
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
17+
#ifndef ECMA_OBJECT_PROTOTYPE_H
18+
#define ECMA_OBJECT_PROTOTYPE_H
19+
20+
#include "ecma-globals.h"
21+
22+
/** \addtogroup ecma ECMA
23+
* @{
24+
*
25+
* \addtogroup ecmabuiltinhelpers ECMA builtin helper operations
26+
* @{
27+
*/
28+
29+
extern ecma_completion_value_t ecma_builtin_helper_object_to_string (const ecma_value_t this_arg);
30+
31+
/**
32+
* @}
33+
* @}
34+
*/
35+
36+
#endif /* !ECMA_OBJECT_PROPERTY_H */

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

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*/
1515

1616
#include "ecma-alloc.h"
17+
#include "ecma-builtin-helpers.h"
1718
#include "ecma-builtins.h"
1819
#include "ecma-conversion.h"
1920
#include "ecma-exceptions.h"
@@ -55,80 +56,7 @@
5556
static ecma_completion_value_t
5657
ecma_builtin_object_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
5758
{
58-
ecma_magic_string_id_t type_string;
59-
60-
if (ecma_is_value_undefined (this_arg))
61-
{
62-
type_string = ECMA_MAGIC_STRING_UNDEFINED_UL;
63-
}
64-
else if (ecma_is_value_null (this_arg))
65-
{
66-
type_string = ECMA_MAGIC_STRING_NULL_UL;
67-
}
68-
else
69-
{
70-
ecma_completion_value_t obj_this = ecma_op_to_object (this_arg);
71-
72-
if (!ecma_is_completion_value_normal (obj_this))
73-
{
74-
return obj_this;
75-
}
76-
77-
JERRY_ASSERT (ecma_is_value_object (ecma_get_completion_value_value (obj_this)));
78-
79-
ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this);
80-
81-
ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p,
82-
ECMA_INTERNAL_PROPERTY_CLASS);
83-
type_string = (ecma_magic_string_id_t) class_prop_p->u.internal_property.value;
84-
85-
ecma_free_completion_value (obj_this);
86-
}
87-
88-
ecma_string_t *ret_string_p;
89-
90-
/* Building string "[object #type#]" where type is 'Undefined',
91-
'Null' or one of possible object's classes.
92-
The string with null character is maximum 19 characters long. */
93-
const ssize_t buffer_size = 19;
94-
MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, ecma_char_t);
95-
96-
const ecma_char_t *left_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR);
97-
const ecma_char_t *object_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_OBJECT);
98-
const ecma_char_t *space_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR);
99-
const ecma_char_t *type_name_zt_str_p = ecma_get_magic_string_zt (type_string);
100-
const ecma_char_t *right_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR);
101-
102-
ecma_char_t *buffer_ptr = str_buffer;
103-
ssize_t buffer_size_left = buffer_size;
104-
buffer_ptr = ecma_copy_zt_string_to_buffer (left_square_zt_str_p,
105-
buffer_ptr,
106-
buffer_size_left);
107-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
108-
buffer_ptr = ecma_copy_zt_string_to_buffer (object_zt_str_p,
109-
buffer_ptr,
110-
buffer_size_left);
111-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
112-
buffer_ptr = ecma_copy_zt_string_to_buffer (space_zt_str_p,
113-
buffer_ptr,
114-
buffer_size_left);
115-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
116-
buffer_ptr = ecma_copy_zt_string_to_buffer (type_name_zt_str_p,
117-
buffer_ptr,
118-
buffer_size_left);
119-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
120-
buffer_ptr = ecma_copy_zt_string_to_buffer (right_square_zt_str_p,
121-
buffer_ptr,
122-
buffer_size_left);
123-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
124-
125-
JERRY_ASSERT (buffer_size_left >= 0);
126-
127-
ret_string_p = ecma_new_ecma_string (str_buffer);
128-
129-
MEM_FINALIZE_LOCAL_ARRAY (str_buffer);
130-
131-
return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p));
59+
return ecma_builtin_helper_object_to_string (this_arg);
13260
} /* ecma_builtin_object_prototype_object_to_string */
13361

13462
/**
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
// Our own join method if the internal join is not implemented.
17+
function join(sep)
18+
{
19+
sep = sep ? sep : ",";
20+
var result = "";
21+
22+
for (var i = 0; i < this.length; ++i) {
23+
result += this[i];
24+
if (i + 1 < this.length) {
25+
result += sep;
26+
}
27+
}
28+
29+
return result;
30+
}
31+
32+
// Force fallback to object.prototype.toString()
33+
Array.prototype.join = 1;
34+
35+
assert ([1].toString() === "[object Array]");
36+
37+
Array.prototype.join = join;
38+
39+
assert ([1, 2].toString() === "1,2");
40+
41+
var test = [1,2,3];
42+
test.join = function() { throw ReferenceError ("foo"); };
43+
44+
try {
45+
test.toString();
46+
47+
assert (false);
48+
} catch (e) {
49+
assert (e.message === "foo");
50+
assert (e instanceof ReferenceError);
51+
}
52+
53+
54+
// Test if the join returns a ReferenceError
55+
var arr = [1,2]
56+
Object.defineProperty(arr, 'join', { 'get' : function () {throw new ReferenceError ("foo"); } });
57+
try {
58+
arr.toString();
59+
60+
assert (false);
61+
} catch (e) {
62+
assert (e.message === "foo");
63+
assert (e instanceof ReferenceError);
64+
}

0 commit comments

Comments
 (0)