Skip to content

Commit ca12c16

Browse files
committed
Implemented Array.prototype.pop().
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent 198afd0 commit ca12c16

File tree

3 files changed

+182
-0
lines changed

3 files changed

+182
-0
lines changed

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

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,33 @@
4444
* @{
4545
*/
4646

47+
/**
48+
* Helper function to set an object's length property
49+
*
50+
* @return completion value (return value of the [[Put]] method)
51+
* Returned value must be freed with ecma_free_completion_value.
52+
*/
53+
static ecma_completion_value_t
54+
ecma_builtin_array_prototype_helper_set_length (ecma_object_t *object, /**< object*/
55+
uint32_t length) /**< new length */
56+
{
57+
ecma_completion_value_t ret_value;
58+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
59+
60+
ecma_number_t *len_p = ecma_alloc_number ();
61+
*len_p = ecma_uint32_to_number (length);
62+
63+
ret_value = ecma_op_object_put (object,
64+
magic_string_length_p,
65+
ecma_make_number_value (len_p),
66+
true),
67+
68+
ecma_dealloc_number (len_p);
69+
ecma_deref_ecma_string (magic_string_length_p);
70+
71+
return ret_value;
72+
} /* ecma_builtin_array_prototype_helper_set_length */
73+
4774
/**
4875
* The Array.prototype object's 'toString' routine
4976
*
@@ -59,6 +86,85 @@ ecma_builtin_array_prototype_object_to_string (ecma_value_t this_arg) /**< this
5986
ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg);
6087
} /* ecma_builtin_array_prototype_object_to_string */
6188

89+
/**
90+
* The Array.prototype object's 'pop' routine
91+
*
92+
* See also:
93+
* ECMA-262 v5, 15.4.4.6
94+
*
95+
* @return completion value
96+
* Returned value must be freed with ecma_free_completion_value.
97+
*/
98+
static ecma_completion_value_t
99+
ecma_builtin_array_prototype_object_pop (ecma_value_t this_arg) /**< this argument */
100+
{
101+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
102+
103+
/* 1. */
104+
ECMA_TRY_CATCH (obj_this,
105+
ecma_op_to_object (this_arg),
106+
ret_value);
107+
108+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
109+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
110+
111+
/* 2. */
112+
ECMA_TRY_CATCH (len_value,
113+
ecma_op_object_get (obj_p, magic_string_length_p),
114+
ret_value);
115+
116+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
117+
118+
/* 3. */
119+
uint32_t len = ecma_number_to_uint32 (len_number);
120+
121+
/* 4. */
122+
if (len == 0)
123+
{
124+
/* 4.a */
125+
ECMA_TRY_CATCH (set_length_value,
126+
ecma_builtin_array_prototype_helper_set_length (obj_p, 0),
127+
ret_value);
128+
129+
/* 4.b */
130+
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_UNDEFINED);
131+
132+
ECMA_FINALIZE (set_length_value)
133+
}
134+
else
135+
{
136+
len--;
137+
/* 5.a */
138+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (len);
139+
140+
/* 5.b */
141+
ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
142+
143+
/* 5.c */
144+
ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, index_str_p, true), ret_value);
145+
146+
/* 5.d */
147+
ECMA_TRY_CATCH (set_length_value,
148+
ecma_builtin_array_prototype_helper_set_length (obj_p, len),
149+
ret_value);
150+
151+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (get_value, true));
152+
153+
ECMA_FINALIZE (set_length_value);
154+
ECMA_FINALIZE (del_value);
155+
ECMA_FINALIZE (get_value);
156+
157+
ecma_deref_ecma_string (index_str_p);
158+
}
159+
160+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
161+
ECMA_FINALIZE (len_value);
162+
ecma_deref_ecma_string (magic_string_length_p);
163+
ECMA_FINALIZE (obj_this);
164+
165+
return ret_value;
166+
} /* ecma_builtin_array_prototype_object_pop */
167+
62168
/**
63169
* The Array.prototype object's 'push' routine
64170
*

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_POP, ecma_builtin_array_prototype_object_pop, 0, 0)
6263
ROUTINE (ECMA_MAGIC_STRING_PUSH, ecma_builtin_array_prototype_object_push, NON_FIXED, 1)
6364

6465
#undef OBJECT_ID

tests/jerry/array_prototype_pop.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
var array = ["foo", [], Infinity, 4]
17+
assert(array.length === 4);
18+
19+
assert(array.pop() === 4)
20+
assert(array.length === 3);
21+
22+
assert(array.pop() === Infinity);
23+
assert(array.length === 2);
24+
25+
var a = array.pop()
26+
assert(a instanceof Array);
27+
assert(array.length === 1);
28+
29+
assert(array.pop() === "foo");
30+
assert(array.length === 0);
31+
32+
assert(array.pop() === undefined);
33+
assert(array.length === 0);
34+
35+
// Checking behavior when unable to get length
36+
var obj = { pop : Array.prototype.pop };
37+
Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } });
38+
39+
try {
40+
obj.pop();
41+
assert(false);
42+
} catch (e) {
43+
assert(e.message === "foo");
44+
assert(e instanceof ReferenceError);
45+
}
46+
47+
// Checking behavior when unable to set length
48+
var obj = { pop : Array.prototype.pop };
49+
Object.defineProperty(obj, 'length', { 'set' : function () {throw new ReferenceError ("foo"); } });
50+
51+
try {
52+
obj.pop();
53+
assert(false);
54+
} catch (e) {
55+
assert(e.message === "foo");
56+
assert(e instanceof ReferenceError);
57+
}
58+
59+
// Checking behavior when no length property defined
60+
var obj = { pop : Array.prototype.pop };
61+
assert(obj.length === undefined)
62+
assert(obj.pop() === undefined)
63+
assert(obj.length === 0)
64+
65+
// Checking behavior when unable to get element
66+
var obj = { pop : Array.prototype.pop, length : 1 };
67+
Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } });
68+
69+
try {
70+
obj.pop();
71+
assert(false);
72+
} catch (e) {
73+
assert(e.message === "foo");
74+
assert(e instanceof ReferenceError);
75+
}

0 commit comments

Comments
 (0)