Skip to content

Commit a4fd8cd

Browse files
committed
Implement Array.prototype.toString
JerryScript-DCO-1.0-Signed-off-by: Peter Gal [email protected]
1 parent b8e4328 commit a4fd8cd

File tree

5 files changed

+265
-75
lines changed

5 files changed

+265
-75
lines changed

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
#include "ecma-builtins.h"
1818
#include "ecma-conversion.h"
1919
#include "ecma-exceptions.h"
20+
#include "ecma-function-object.h"
2021
#include "ecma-gc.h"
2122
#include "ecma-globals.h"
2223
#include "ecma-helpers.h"
2324
#include "ecma-objects.h"
25+
#include "ecma-object-prototype.h"
2426
#include "ecma-string-object.h"
2527
#include "ecma-try-catch-macro.h"
2628
#include "jrt.h"
@@ -56,7 +58,42 @@
5658
static ecma_completion_value_t
5759
ecma_builtin_array_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
5860
{
59-
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
61+
ecma_completion_value_t return_value = ecma_make_empty_completion_value ();
62+
63+
/* 1. */
64+
ECMA_TRY_CATCH (obj_this_value,
65+
ecma_op_to_object (this_arg),
66+
return_value);
67+
68+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value);
69+
70+
ecma_string_t *join_magic_string_p = ecma_get_magic_string (ECMA_MAGIC_STRING_JOIN);
71+
72+
/* 2. */
73+
ECMA_TRY_CATCH (join_value,
74+
ecma_op_object_get (obj_p, join_magic_string_p),
75+
return_value);
76+
77+
if (!ecma_op_is_callable (join_value))
78+
{
79+
/* 3. */
80+
return_value = ecma_op_object_prototype_tostring (this_arg);
81+
}
82+
else
83+
{
84+
/* 4. */
85+
ecma_object_t *join_func_obj_p = ecma_get_object_from_value (join_value);
86+
87+
return_value = ecma_op_function_call (join_func_obj_p, this_arg, NULL, 0);
88+
}
89+
90+
ECMA_FINALIZE (join_value);
91+
92+
ecma_deref_ecma_string (join_magic_string_p);
93+
94+
ECMA_FINALIZE (obj_this_value);
95+
96+
return return_value;
6097
} /* ecma_builtin_array_prototype_object_to_string */
6198

6299
/**

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

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "ecma-globals.h"
2222
#include "ecma-helpers.h"
2323
#include "ecma-objects.h"
24+
#include "ecma-object-prototype.h"
2425
#include "ecma-string-object.h"
2526
#include "ecma-try-catch-macro.h"
2627
#include "jrt.h"
@@ -54,80 +55,7 @@
5455
static ecma_completion_value_t
5556
ecma_builtin_object_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
5657
{
57-
ecma_magic_string_id_t type_string;
58-
59-
if (ecma_is_value_undefined (this_arg))
60-
{
61-
type_string = ECMA_MAGIC_STRING_UNDEFINED_UL;
62-
}
63-
else if (ecma_is_value_null (this_arg))
64-
{
65-
type_string = ECMA_MAGIC_STRING_NULL_UL;
66-
}
67-
else
68-
{
69-
ecma_completion_value_t obj_this = ecma_op_to_object (this_arg);
70-
71-
if (!ecma_is_completion_value_normal (obj_this))
72-
{
73-
return obj_this;
74-
}
75-
76-
JERRY_ASSERT (ecma_is_value_object (ecma_get_completion_value_value (obj_this)));
77-
78-
ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this);
79-
80-
ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p,
81-
ECMA_INTERNAL_PROPERTY_CLASS);
82-
type_string = (ecma_magic_string_id_t) class_prop_p->u.internal_property.value;
83-
84-
ecma_free_completion_value (obj_this);
85-
}
86-
87-
ecma_string_t *ret_string_p;
88-
89-
/* Building string "[object #type#]" where type is 'Undefined',
90-
'Null' or one of possible object's classes.
91-
The string with null character is maximum 19 characters long. */
92-
const ssize_t buffer_size = 19;
93-
MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, ecma_char_t);
94-
95-
const ecma_char_t *left_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR);
96-
const ecma_char_t *object_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_OBJECT);
97-
const ecma_char_t *space_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR);
98-
const ecma_char_t *type_name_zt_str_p = ecma_get_magic_string_zt (type_string);
99-
const ecma_char_t *right_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR);
100-
101-
ecma_char_t *buffer_ptr = str_buffer;
102-
ssize_t buffer_size_left = buffer_size;
103-
buffer_ptr = ecma_copy_zt_string_to_buffer (left_square_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 (object_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 (space_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-
buffer_ptr = ecma_copy_zt_string_to_buffer (type_name_zt_str_p,
116-
buffer_ptr,
117-
buffer_size_left);
118-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
119-
buffer_ptr = ecma_copy_zt_string_to_buffer (right_square_zt_str_p,
120-
buffer_ptr,
121-
buffer_size_left);
122-
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
123-
124-
JERRY_ASSERT (buffer_size_left >= 0);
125-
126-
ret_string_p = ecma_new_ecma_string (str_buffer);
127-
128-
MEM_FINALIZE_LOCAL_ARRAY (str_buffer);
129-
130-
return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p));
58+
return ecma_op_object_prototype_tostring (this_arg);
13159
} /* ecma_builtin_object_prototype_object_to_string */
13260

13361
/**
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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-object-prototype.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 ecmaobjectprototypeops ECMA object prototype's operations
29+
* @{
30+
*/
31+
32+
/**
33+
* The Object.prototype object's 'toString' routine
34+
*
35+
* See also:
36+
* ECMA-262 v5, 15.2.4.2
37+
*
38+
* @return completion value
39+
* Returned value must be freed with ecma_free_completion_value.
40+
*/
41+
42+
ecma_completion_value_t
43+
ecma_op_object_prototype_tostring (const ecma_value_t this_arg) /**< this argument */
44+
{
45+
ecma_magic_string_id_t type_string;
46+
47+
if (ecma_is_value_undefined (this_arg))
48+
{
49+
type_string = ECMA_MAGIC_STRING_UNDEFINED_UL;
50+
}
51+
else if (ecma_is_value_null (this_arg))
52+
{
53+
type_string = ECMA_MAGIC_STRING_NULL_UL;
54+
}
55+
else
56+
{
57+
ecma_completion_value_t obj_this = ecma_op_to_object (this_arg);
58+
59+
if (!ecma_is_completion_value_normal (obj_this))
60+
{
61+
return obj_this;
62+
}
63+
64+
JERRY_ASSERT (ecma_is_value_object (ecma_get_completion_value_value (obj_this)));
65+
66+
ecma_object_t *obj_p = ecma_get_object_from_completion_value (obj_this);
67+
68+
ecma_property_t *class_prop_p = ecma_get_internal_property (obj_p,
69+
ECMA_INTERNAL_PROPERTY_CLASS);
70+
type_string = (ecma_magic_string_id_t) class_prop_p->u.internal_property.value;
71+
72+
ecma_free_completion_value (obj_this);
73+
}
74+
75+
ecma_string_t *ret_string_p;
76+
77+
/* Building string "[object #type#]" where type is 'Undefined',
78+
'Null' or one of possible object's classes.
79+
The string with null character is maximum 19 characters long. */
80+
const ssize_t buffer_size = 19;
81+
MEM_DEFINE_LOCAL_ARRAY (str_buffer, buffer_size, ecma_char_t);
82+
83+
const ecma_char_t *left_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_LEFT_SQUARE_CHAR);
84+
const ecma_char_t *object_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_OBJECT);
85+
const ecma_char_t *space_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_SPACE_CHAR);
86+
const ecma_char_t *type_name_zt_str_p = ecma_get_magic_string_zt (type_string);
87+
const ecma_char_t *right_square_zt_str_p = ecma_get_magic_string_zt (ECMA_MAGIC_STRING_RIGHT_SQUARE_CHAR);
88+
89+
ecma_char_t *buffer_ptr = str_buffer;
90+
ssize_t buffer_size_left = buffer_size;
91+
buffer_ptr = ecma_copy_zt_string_to_buffer (left_square_zt_str_p,
92+
buffer_ptr,
93+
buffer_size_left);
94+
buffer_size_left = buffer_size - (buffer_ptr - str_buffer) * (ssize_t) sizeof (ecma_char_t);
95+
buffer_ptr = ecma_copy_zt_string_to_buffer (object_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 (space_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 (type_name_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 (right_square_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+
112+
JERRY_ASSERT (buffer_size_left >= 0);
113+
114+
ret_string_p = ecma_new_ecma_string (str_buffer);
115+
116+
MEM_FINALIZE_LOCAL_ARRAY (str_buffer);
117+
118+
return ecma_make_normal_completion_value (ecma_make_string_value (ret_string_p));
119+
}
120+
121+
/**
122+
* @}
123+
* @}
124+
* @}
125+
*/
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 ecmaobjectprototypeops ECMA object prototype's operations
26+
* @{
27+
*/
28+
29+
extern ecma_completion_value_t ecma_op_object_prototype_tostring (const ecma_value_t this_arg);
30+
31+
/**
32+
* @}
33+
* @}
34+
*/
35+
36+
#endif /* !ECMA_OBJECT_PROPERTY_H */
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)