Skip to content

Commit 1b87db8

Browse files
committed
CR
1 parent e2d421b commit 1b87db8

File tree

2 files changed

+21
-31
lines changed

2 files changed

+21
-31
lines changed

runtime/js/effect.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ argument. Its stack of exception handlers is stored in
1111
Exception handlers are pushed into this stack
1212
when entering a [try ... with ...] and popped on exit.
1313
Handlers are stored in [caml_current_stack.h]
14-
and the remaining fibers are stored in [caml_fiber_stack].
14+
and the remaining fibers are stored in [caml_current_stack.e].
1515
To install an effect handler, we push a new fiber into the execution context.
1616
1717
We have basically the following type for reified continuations (type
@@ -47,7 +47,12 @@ additional parameter which is the current low-level continuation.
4747

4848
//Provides: caml_current_stack
4949
//If: effects
50-
var caml_current_stack = {};
50+
// This has the shape {k, x, h, e} where
51+
// - h is a triple of handlers (see effect.ml)
52+
// - k is the low level continuation
53+
// - x is the exception stack
54+
// - e is the fiber stack of the parent fiber.
55+
var caml_current_stack = {k:0, x:0, h:0, e:0};
5156

5257
//Provides: caml_push_trap
5358
//Requires: caml_current_stack
@@ -69,17 +74,8 @@ function caml_pop_trap() {
6974
return h;
7075
}
7176

72-
//Provides: caml_fiber_stack
73-
//If: effects
74-
// This has the shape {k, x, h, e} where
75-
// - h is a triple of handlers (see effect.ml)
76-
// - k is the low level continuation
77-
// - x is the exception stack
78-
// - e is the fiber stack of the parent fiber.
79-
var caml_fiber_stack = 0;
80-
8177
//Provides:caml_resume_stack
82-
//Requires: caml_named_value, caml_raise_constant, caml_fiber_stack
78+
//Requires: caml_named_value, caml_raise_constant
8379
//Requires: caml_pop_fiber, caml_current_stack
8480
//If: effects
8581
//Version: >= 5.0
@@ -93,24 +89,21 @@ function caml_resume_stack(stack, last, k) {
9389
// Pre OCaml 5.2, last/cont[2] was not populated.
9490
while (last.e !== 0) last = last.e;
9591
}
96-
var fiber = caml_current_stack;
97-
fiber.k = k;
98-
fiber.e = caml_fiber_stack;
99-
last.e = fiber;
100-
caml_fiber_stack = stack;
101-
return caml_pop_fiber();
92+
caml_current_stack.k = k;
93+
last.e = caml_current_stack;
94+
caml_current_stack = stack;
95+
return stack.k;
10296
}
10397

10498
//Provides: caml_pop_fiber
105-
//Requires: caml_current_stack, caml_fiber_stack
99+
//Requires: caml_current_stack
106100
//If: effects
107101
//Version: >= 5.0
108102
function caml_pop_fiber() {
109103
// Move to the parent fiber, returning the parent's low-level continuation
110-
var c = caml_fiber_stack;
111-
caml_current_stack = c;
112-
caml_fiber_stack = c.e;
104+
var c = caml_current_stack.e;
113105
caml_current_stack.e = 0;
106+
caml_current_stack = c;
114107
return c.k;
115108
}
116109

@@ -132,12 +125,12 @@ function caml_make_unhandled_effect_exn(eff) {
132125
}
133126

134127
//Provides: caml_perform_effect
135-
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_fiber_stack
128+
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return
136129
//Requires: caml_make_unhandled_effect_exn, caml_current_stack
137130
//If: effects
138131
//Version: >= 5.0
139132
function caml_perform_effect(eff, k0) {
140-
if (caml_fiber_stack === 0) {
133+
if (caml_current_stack.e === 0) {
141134
var exn = caml_make_unhandled_effect_exn(eff);
142135
throw exn;
143136
}
@@ -155,13 +148,13 @@ function caml_perform_effect(eff, k0) {
155148
}
156149

157150
//Provides: caml_reperform_effect
158-
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_fiber_stack
151+
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return
159152
//Requires: caml_make_unhandled_effect_exn, caml_current_stack
160153
//Requires: caml_resume_stack, caml_continuation_use_noexc
161154
//If: effects
162155
//Version: >= 5.0
163156
function caml_reperform_effect(eff, cont, last, k0) {
164-
if (caml_fiber_stack === 0) {
157+
if (caml_current_stack.e === 0) {
165158
var exn = caml_make_unhandled_effect_exn(eff);
166159
var stack = caml_continuation_use_noexc(cont);
167160
caml_resume_stack(stack, last, k0);

runtime/js/jslib.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,12 @@ var caml_callback = caml_call_gen;
8484
//Provides: caml_callback
8585
//If: effects
8686
//Requires: caml_stack_depth, caml_call_gen, caml_wrap_exception
87-
//Requires: caml_fiber_stack, caml_current_stack
87+
//Requires: caml_current_stack
8888
function caml_callback(f, args) {
8989
var saved_stack_depth = caml_stack_depth;
9090
var saved_current_stack = caml_current_stack;
91-
var saved_fiber_stack = caml_fiber_stack;
9291
try {
93-
caml_fiber_stack = 0;
94-
caml_current_stack = {};
92+
caml_current_stack = {k:0, x:0, h:0, e:0};
9593
var res = {
9694
joo_tramp: f,
9795
joo_args: args.concat(function (x) {
@@ -113,7 +111,6 @@ function caml_callback(f, args) {
113111
} finally {
114112
caml_stack_depth = saved_stack_depth;
115113
caml_current_stack = saved_current_stack;
116-
caml_fiber_stack = saved_fiber_stack;
117114
}
118115
return res;
119116
}

0 commit comments

Comments
 (0)