Skip to content

Commit be8ae3a

Browse files
authored
Add iterator close support for for-of statement (#3401)
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent f1dd59e commit be8ae3a

5 files changed

Lines changed: 290 additions & 6 deletions

File tree

jerry-core/ecma/base/ecma-gc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ ecma_gc_free_executable_object (ecma_object_t *object_p) /**< object */
815815

816816
do
817817
{
818-
context_top_p[-1] &= (uint32_t) ~VM_CONTEXT_HAS_LEX_ENV;
818+
context_top_p[-1] &= (uint32_t) ~(VM_CONTEXT_HAS_LEX_ENV | VM_CONTEXT_CLOSE_ITERATOR);
819819

820820
uint32_t offsets = vm_get_context_value_offsets (context_top_p);
821821

jerry-core/vm/vm-stack.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "ecma-helpers.h"
1919
#include "vm-defines.h"
2020
#include "vm-stack.h"
21+
#include "ecma-iterator-object.h"
2122

2223
/** \addtogroup vm Virtual machine
2324
* @{
@@ -79,8 +80,15 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
7980
#if ENABLED (JERRY_ES2015)
8081
case VM_CONTEXT_FOR_OF:
8182
{
83+
ecma_value_t iterator = vm_stack_top_p[-3];
84+
85+
if (context_info & VM_CONTEXT_CLOSE_ITERATOR)
86+
{
87+
ecma_op_iterator_close (iterator);
88+
}
89+
ecma_free_value (iterator);
90+
8291
ecma_free_value (vm_stack_top_p[-2]);
83-
ecma_free_value (vm_stack_top_p[-3]);
8492
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION);
8593
vm_stack_top_p -= PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION;
8694
break;

jerry-core/vm/vm-stack.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@
2828
/**
2929
* Create context on the vm stack.
3030
*/
31-
#define VM_CREATE_CONTEXT(type, end_offset) ((ecma_value_t) ((type) | ((end_offset) << 6)))
31+
#define VM_CREATE_CONTEXT(type, end_offset) ((ecma_value_t) ((type) | ((end_offset) << 7)))
3232

3333
/**
3434
* Create context on the vm stack with environment.
3535
*/
3636
#define VM_CREATE_CONTEXT_WITH_ENV(type, end_offset) \
37-
((ecma_value_t) ((type) | ((end_offset) << 6) | VM_CONTEXT_HAS_LEX_ENV))
37+
(VM_CREATE_CONTEXT ((type),(end_offset)) | VM_CONTEXT_HAS_LEX_ENV)
3838

3939
/**
4040
* Get type of a vm context.
@@ -44,13 +44,18 @@
4444
/**
4545
* Get the end position of a vm context.
4646
*/
47-
#define VM_GET_CONTEXT_END(value) ((value) >> 6)
47+
#define VM_GET_CONTEXT_END(value) ((value) >> 7)
4848

4949
/**
5050
* This flag is set if the context has a lexical environment.
5151
*/
5252
#define VM_CONTEXT_HAS_LEX_ENV 0x20
5353

54+
/**
55+
* This flag is set if the iterator close operation should be invoked during a for-of context break.
56+
*/
57+
#define VM_CONTEXT_CLOSE_ITERATOR 0x40
58+
5459
/**
5560
* Context types for the vm stack.
5661
*/

jerry-core/vm/vm.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3485,7 +3485,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
34853485

34863486
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION);
34873487
stack_top_p += PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION;
3488-
stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_FOR_OF, branch_offset);
3488+
stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_FOR_OF, branch_offset) | VM_CONTEXT_CLOSE_ITERATOR;
34893489
stack_top_p[-2] = next_value;
34903490
stack_top_p[-3] = iterator;
34913491

@@ -3587,6 +3587,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
35873587
case VM_OC_CONTEXT_END:
35883588
{
35893589
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
3590+
JERRY_ASSERT (!(stack_top_p[-1] & VM_CONTEXT_CLOSE_ITERATOR));
35903591

35913592
ecma_value_t context_type = VM_GET_CONTEXT_TYPE (stack_top_p[-1]);
35923593

@@ -3653,6 +3654,7 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
36533654
case VM_OC_JUMP_AND_EXIT_CONTEXT:
36543655
{
36553656
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
3657+
JERRY_ASSERT (!jcontext_has_pending_exception ());
36563658

36573659
branch_offset += (int32_t) (byte_code_start_p - frame_ctx_p->byte_code_start_p);
36583660

@@ -3670,6 +3672,14 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
36703672
byte_code_p = frame_ctx_p->byte_code_start_p + branch_offset;
36713673
}
36723674

3675+
#if ENABLED (JERRY_ES2015)
3676+
if (jcontext_has_pending_exception ())
3677+
{
3678+
result = ECMA_VALUE_ERROR;
3679+
goto error;
3680+
}
3681+
#endif /* ENABLED (JERRY_ES2015) */
3682+
36733683
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
36743684
continue;
36753685
}
@@ -3944,10 +3954,27 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
39443954
JERRY_ASSERT (VM_GET_CONTEXT_TYPE (stack_top_p[-1]) == VM_CONTEXT_FINALLY_RETURN);
39453955
JERRY_ASSERT (VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth == stack_top_p);
39463956

3957+
#if ENABLED (JERRY_ES2015)
3958+
if (jcontext_has_pending_exception ())
3959+
{
3960+
stack_top_p[-1] = (ecma_value_t) (stack_top_p[-1] - VM_CONTEXT_FINALLY_RETURN + VM_CONTEXT_FINALLY_THROW);
3961+
ecma_free_value (result);
3962+
result = jcontext_take_exception ();
3963+
}
3964+
#endif /* ENABLED (JERRY_ES2015) */
3965+
39473966
byte_code_p = frame_ctx_p->byte_code_p;
39483967
stack_top_p[-2] = result;
39493968
continue;
39503969
}
3970+
3971+
#if ENABLED (JERRY_ES2015)
3972+
if (jcontext_has_pending_exception ())
3973+
{
3974+
ecma_free_value (result);
3975+
result = ECMA_VALUE_ERROR;
3976+
}
3977+
#endif /* ENABLED (JERRY_ES2015) */
39513978
}
39523979
else if (jcontext_has_pending_exception () && !jcontext_has_pending_abort ())
39533980
{
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
// Copyright JS Foundation and other contributors, http://js.foundation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
function createIterable(arr, methods = {}) {
16+
let iterable = function *() {
17+
let idx = 0;
18+
while (idx < arr.length) {
19+
yield arr[idx];
20+
idx++;
21+
}
22+
}();
23+
iterable['return'] = methods['return'];
24+
iterable['throw'] = methods['throw'];
25+
26+
return iterable;
27+
};
28+
29+
function close1() {
30+
var closed = false;
31+
var iter = createIterable([1, 2, 3], {
32+
'return': function() { closed = true; return {}; }
33+
});
34+
for (var it of iter) break;
35+
return closed;
36+
}
37+
38+
assert(close1());
39+
40+
function close2() {
41+
var closed = false;
42+
var iter = createIterable([1, 2, 3], {
43+
'return': function() { closed = true; return {}; }
44+
});
45+
try {
46+
for (var it of iter) throw 0;
47+
assert(false);
48+
} catch(e){
49+
assert(e === 0);
50+
}
51+
return closed;
52+
}
53+
54+
assert(close2());
55+
56+
function close3() {
57+
var closed = false;
58+
var iter = createIterable([1, 2, 3], {
59+
'return': function() { closed = true; return {}; }
60+
});
61+
for (var it of iter) continue;
62+
return closed;
63+
}
64+
65+
assert(!close3());
66+
67+
function close4() {
68+
var closed = false;
69+
var iter = createIterable([1, 2, 3], {
70+
'return': function() { closed = true; throw 6; }
71+
});
72+
try {
73+
for (var it of iter) throw 5;
74+
assert(false);
75+
} catch(e) {
76+
assert(e === 5);
77+
}
78+
return closed;
79+
}
80+
81+
assert(close4());
82+
83+
function close5() {
84+
var closed_called = 0;
85+
var iter = createIterable([1, 2, 3], {
86+
'return': function() { closed_called++; throw 6; }
87+
});
88+
try {
89+
for (var it of iter) {
90+
for (var it of iter) {
91+
throw 5;
92+
}
93+
assert(false);
94+
}
95+
assert(false);
96+
} catch(e) {
97+
assert(e === 5);
98+
}
99+
return closed_called === 2;
100+
}
101+
102+
assert(close5());
103+
104+
function close6() {
105+
var closed = false;
106+
var iter = createIterable([1, 2, 3], {
107+
'return': function() { closed = true; return {}; }
108+
});
109+
for (var it of iter) {};
110+
111+
return closed;
112+
}
113+
114+
assert(!close6());
115+
116+
var close7_result = false;
117+
function close7() {
118+
var iter = createIterable([1, 2, 3], {
119+
'return': function() { close7_result = true; throw "5"; }
120+
});
121+
122+
for (var it of iter) {
123+
return "foo";
124+
}
125+
}
126+
127+
try {
128+
close7();
129+
assert(false);
130+
} catch (e) {
131+
assert(close7_result);
132+
assert(e === "5");
133+
}
134+
135+
function close8() {
136+
var iter = createIterable([1, 2, 3], {
137+
'return': function() { close8_result = true; throw "5"; }
138+
});
139+
140+
for (var it of iter) {
141+
throw "foo";
142+
}
143+
}
144+
145+
var close8_result = false;
146+
try {
147+
close8();
148+
assert(false);
149+
} catch (e) {
150+
assert(e === "foo");
151+
assert(close8_result);
152+
}
153+
154+
function close9() {
155+
var closed = false;
156+
var iter = createIterable([1, 2, 3], {
157+
'return': function() { closed = true; throw "5"; }
158+
});
159+
160+
try {
161+
for (var it of iter) {
162+
break;
163+
}
164+
} finally {
165+
assert(closed);
166+
throw "foo"
167+
}
168+
}
169+
170+
try {
171+
close9();
172+
assert(false);
173+
} catch (e) {
174+
assert(e === "foo");
175+
}
176+
177+
function close10() {
178+
var closed = false;
179+
var iter = createIterable([1, 2, 3], {
180+
'return': function() { closed = true; return {}; }
181+
});
182+
183+
try {
184+
for (var it of iter) {
185+
return "foo";
186+
}
187+
} finally {
188+
assert(closed);
189+
throw "bar";
190+
}
191+
}
192+
193+
try {
194+
close10();
195+
assert(false);
196+
} catch (e) {
197+
assert(e === "bar");
198+
}
199+
200+
function close11() {
201+
var closed = false;
202+
var iter = createIterable([1, 2, 3], {
203+
'return': function() { closed = true; throw "5"; }
204+
});
205+
206+
try {
207+
for (var it of iter) {
208+
return "foo";
209+
}
210+
} finally {
211+
assert(closed);
212+
throw "bar";
213+
}
214+
}
215+
216+
try {
217+
close11();
218+
assert(false);
219+
} catch (e) {
220+
assert(e === "bar");
221+
}
222+
223+
function close12() {
224+
var closed = false;
225+
var iter = createIterable([1, 2, 3], {
226+
'return': function() { closed = true; throw "5"; }
227+
});
228+
229+
try {
230+
for (var it of iter) {
231+
throw "foo";
232+
}
233+
} finally {
234+
assert(closed);
235+
throw "bar";
236+
}
237+
}
238+
239+
try {
240+
close12();
241+
assert(false);
242+
} catch (e) {
243+
assert(e === "bar");
244+
}

0 commit comments

Comments
 (0)