Skip to content

Commit f54829b

Browse files
committed
[js] Fix nqp::resume
Make nqp::resume exit the CATCH block. Also fix it when used inside CONTROL blocks.
1 parent fd76c7e commit f54829b

File tree

1 file changed

+32
-21
lines changed

1 file changed

+32
-21
lines changed

src/vm/js/nqp-runtime/ctx.js

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ var categoryToName = {
2424
32768: 'DONE'
2525
};
2626

27+
class ResumeException {
28+
constructor(exception) {
29+
this.exception = exception;
30+
}
31+
}
32+
2733
class Ctx extends NQPObject {
2834
constructor(callerCtx, outerCtx, callThis) {
2935
super();
@@ -44,7 +50,6 @@ class Ctx extends NQPObject {
4450
while (ctx) {
4551
if (ctx[handler] || ctx.$$CONTROL) {
4652
exception.caught = ctx;
47-
exception.resume = false;
4853
ctx.exception = exception;
4954

5055
exceptionsStack.push(exception);
@@ -54,15 +59,17 @@ class Ctx extends NQPObject {
5459
} else {
5560
ctx.unwind.ret = ctx.$$CONTROL();
5661
}
62+
} catch (e) {
63+
if (e instanceof ResumeException && e.exception === exception) {
64+
return;
65+
} else {
66+
throw e;
67+
}
5768
} finally {
5869
exceptionsStack.pop();
5970
}
6071

61-
if (exception.resume) {
62-
return;
63-
} else {
64-
throw ctx.unwind;
65-
}
72+
throw ctx.unwind;
6673
}
6774
ctx = ctx.$$caller;
6875
}
@@ -71,28 +78,32 @@ class Ctx extends NQPObject {
7178
}
7279

7380
propagateException(exception) {
74-
if (exception.$$category) this.propagateControlException(exception);
81+
if (exception.$$category) {
82+
this.propagateControlException(exception);
83+
return;
84+
}
7585

7686
var ctx = this;
7787

7888
while (ctx) {
7989
if (ctx.$$CATCH) {
8090
exception.caught = ctx;
81-
exception.resume = false;
8291
ctx.exception = exception;
8392

8493
exceptionsStack.push(exception);
8594
try {
8695
ctx.unwind.ret = ctx.$$CATCH();
96+
} catch (e) {
97+
if (e instanceof ResumeException && e.exception === exception) {
98+
return;
99+
} else {
100+
throw e;
101+
}
87102
} finally {
88103
exceptionsStack.pop();
89104
}
90105

91-
if (exception.resume) {
92-
return;
93-
} else {
94-
throw ctx.unwind;
95-
}
106+
throw ctx.unwind;
96107
}
97108
ctx = ctx.$$caller;
98109
}
@@ -118,7 +129,7 @@ class Ctx extends NQPObject {
118129
}
119130

120131
resume(exception) {
121-
exception.resume = true;
132+
throw new ResumeException(exception);
122133
}
123134

124135
throw (exception) {
@@ -144,11 +155,9 @@ class Ctx extends NQPObject {
144155

145156
let ctx = this;
146157

147-
148158
while (ctx) {
149159
if (ctx[handler] || ctx.$$CONTROL) {
150160
exception.caught = ctx;
151-
exception.resume = false;
152161
ctx.exception = exception;
153162

154163
exceptionsStack.push(exception);
@@ -158,15 +167,17 @@ class Ctx extends NQPObject {
158167
} else {
159168
ctx.unwind.ret = ctx.$$CONTROL();
160169
}
170+
} catch (e) {
171+
if (e instanceof ResumeException && e.exception === exception) {
172+
return;
173+
} else {
174+
throw e;
175+
}
161176
} finally {
162177
exceptionsStack.pop();
163178
}
164179

165-
if (exception.resume) {
166-
return;
167-
} else {
168-
throw ctx.unwind;
169-
}
180+
throw ctx.unwind;
170181
}
171182
ctx = ctx.$$outer;
172183
}

0 commit comments

Comments
 (0)