Skip to content

Commit 8276255

Browse files
committed
Implemented Array.prototype.push()
JerryScript-DCO-1.0-Signed-off-by: Ilyong Cho [email protected]
1 parent 7c99170 commit 8276255

File tree

3 files changed

+127
-0
lines changed

3 files changed

+127
-0
lines changed

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

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,107 @@ ecma_builtin_array_prototype_object_to_string (ecma_value_t this_arg) /**< this
5959
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
6060
} /* ecma_builtin_array_prototype_object_to_string */
6161

62+
/**
63+
* The Array.prototype object's 'push' routine
64+
*
65+
* See also:
66+
* ECMA-262 v5, 15.4.4.7
67+
*
68+
* @return completion value
69+
* Returned value must be freed with ecma_free_completion_value.
70+
*/
71+
static ecma_completion_value_t
72+
ecma_builtin_array_prototype_object_push (ecma_value_t this_arg, /**< this argument */
73+
const ecma_value_t *argument_list_p, /**< arguments list */
74+
ecma_length_t arguments_number) /**< number of arguments */
75+
{
76+
JERRY_ASSERT (argument_list_p == NULL || arguments_number > 0);
77+
78+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value();
79+
80+
// 1.
81+
ECMA_TRY_CATCH (obj_this_value, ecma_op_to_object (this_arg), ret_value);
82+
83+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value);
84+
85+
// 2.
86+
ecma_string_t *length_str_p = ecma_new_ecma_string_from_magic_string_id (ECMA_MAGIC_STRING_LENGTH);
87+
88+
ECMA_TRY_CATCH (length_value, ecma_op_object_get (obj_p, length_str_p), ret_value);
89+
90+
// 3.
91+
ECMA_OP_TO_NUMBER_TRY_CATCH (length_var, length_value, ret_value);
92+
93+
uint32_t n = ecma_number_to_uint32 (length_var);
94+
95+
// 5.
96+
for (uint32_t index = 0;
97+
index < arguments_number;
98+
++index, ++n)
99+
{
100+
// a.
101+
ecma_value_t e_value = argument_list_p[index];
102+
103+
// b.
104+
ecma_string_t *n_str_p = ecma_new_ecma_string_from_uint32 (n);
105+
106+
ecma_completion_value_t completion = ecma_op_object_put (obj_p, n_str_p, e_value, true);
107+
108+
ecma_deref_ecma_string (n_str_p);
109+
110+
if (unlikely (ecma_is_completion_value_throw (completion)
111+
|| ecma_is_completion_value_exit (completion)))
112+
{
113+
ret_value = completion;
114+
break;
115+
}
116+
else
117+
{
118+
JERRY_ASSERT (ecma_is_completion_value_normal (completion));
119+
ecma_free_completion_value (completion);
120+
}
121+
}
122+
123+
// 6.
124+
if (ecma_is_completion_value_empty (ret_value))
125+
{
126+
ecma_number_t *num_length_p = ecma_alloc_number ();
127+
*num_length_p = ecma_uint32_to_number (n);
128+
129+
ecma_value_t num_length_value = ecma_make_number_value (num_length_p);
130+
131+
ecma_completion_value_t completion = ecma_op_object_put (obj_p,
132+
length_str_p,
133+
num_length_value,
134+
true);
135+
136+
if (unlikely (ecma_is_completion_value_throw (completion)
137+
|| ecma_is_completion_value_exit(completion)))
138+
{
139+
ret_value = completion;
140+
141+
ecma_dealloc_number (num_length_p);
142+
}
143+
else
144+
{
145+
JERRY_ASSERT (ecma_is_completion_value_normal (completion));
146+
ecma_free_completion_value (completion);
147+
148+
ret_value = ecma_make_normal_completion_value (num_length_value);
149+
}
150+
}
151+
152+
ECMA_OP_TO_NUMBER_FINALIZE (length_var);
153+
154+
ECMA_FINALIZE (length_value);
155+
156+
ecma_deref_ecma_string (length_str_p);
157+
158+
ECMA_FINALIZE (obj_this_value);
159+
160+
return ret_value;
161+
} /* ecma_builtin_array_prototype_object_push */
162+
62163
/**
63164
* @}
64165
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ NUMBER_VALUE (ECMA_MAGIC_STRING_LENGTH,
5959
/* Routine properties:
6060
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
6161
ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_array_prototype_object_to_string, 0, 0)
62+
ROUTINE (ECMA_MAGIC_STRING_PUSH, ecma_builtin_array_prototype_object_push, NON_FIXED, 1)
6263

6364
#undef OBJECT_ID
6465
#undef SIMPLE_VALUE

tests/jerry/array.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,28 @@ assert (c[3] === '3');
8181
b[0] = 1;
8282
c[0] += b[0];
8383
assert (c[0] == 1);
84+
85+
var len;
86+
var d = [];
87+
assert (d.length === 0);
88+
len = d.push();
89+
assert (d.length === 0);
90+
assert (d.length === len);
91+
len = d.push(1);
92+
assert (d.length === 1);
93+
assert (d.length === len);
94+
len = d.push(2);
95+
assert (d.length === 2);
96+
assert (d.length === len);
97+
len = d.push('a');
98+
assert (d.length === 3);
99+
assert (d.length === len);
100+
len = d.push('b', 'c', 3);
101+
assert (d.length == 6);
102+
assert (d.length === len);
103+
assert (d[0] === 1);
104+
assert (d[1] === 2);
105+
assert (d[2] === 'a');
106+
assert (d[3] === 'b');
107+
assert (d[4] === 'c');
108+
assert (d[5] === 3);

0 commit comments

Comments
 (0)