Skip to content

Commit 8d17cfb

Browse files
committed
CR
1 parent 8ed5569 commit 8d17cfb

File tree

2 files changed

+35
-45
lines changed

2 files changed

+35
-45
lines changed

runtime/js/effect.js

+28-45
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ is resumed first.
4141
4242
The handlers are CPS-transformed functions: they actually take an
4343
additional parameter which is the current low-level continuation.
44-
45-
Effect and exception handlers are CPS, single-version functions, meaning that
46-
they are ordinary functions, unlike CPS-transformed functions which, if double
47-
translation is enabled, exist in both direct style and continuation-passing
48-
style. Low-level continuations are also ordinary functions.
4944
*/
5045

5146
//Provides: caml_exn_stack
@@ -92,12 +87,30 @@ function caml_raise_unhandled(eff) {
9287
//Provides: uncaught_effect_handler
9388
//Requires: caml_resume_stack, caml_raise_unhandled
9489
//If: effects
90+
//If: !doubletranslate
9591
function uncaught_effect_handler(eff, k, ms) {
9692
// Resumes the continuation k by raising exception Unhandled.
9793
caml_resume_stack(k[1], ms);
9894
caml_raise_unhandled(eff);
9995
}
10096

97+
//Provides: uncaught_effect_handler_cps
98+
//Requires: caml_resume_stack, caml_raise_unhandled
99+
//If: effects
100+
//If: doubletranslate
101+
function uncaught_effect_handler_cps(eff, k, ms, cont) {
102+
// Resumes the continuation k by raising exception Unhandled.
103+
caml_resume_stack(k[1], ms);
104+
caml_raise_unhandled(eff);
105+
}
106+
107+
//Provides: uncaught_effect_handler
108+
//Requires: uncaught_effect_handler_cps
109+
//If: effects
110+
//If: doubletranslate
111+
//Weakdef
112+
var uncaught_effect_handler = { cps : uncaught_effect_handler_cps };
113+
101114
//Provides: caml_fiber_stack
102115
//If: effects
103116
// This has the shape {h, r:{k, x, e}} where h is a triple of handlers
@@ -149,22 +162,8 @@ function caml_pop_fiber() {
149162
return rem.k;
150163
}
151164

152-
//Provides: caml_prepare_tramp
153-
//If: effects
154-
//If: !doubletranslate
155-
function caml_prepare_tramp(handler) {
156-
return handler;
157-
}
158-
159-
//Provides: caml_prepare_tramp
160-
//If: effects
161-
//If: doubletranslate
162-
function caml_prepare_tramp(handler) {
163-
return { cps: handler };
164-
}
165-
166165
//Provides: caml_perform_effect
167-
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_exn_stack, caml_fiber_stack, caml_prepare_tramp
166+
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_exn_stack, caml_fiber_stack, caml_get_cps_fun
168167
//If: effects
169168
function caml_perform_effect(eff, cont, k0) {
170169
// Allocate a continuation if we don't already have one
@@ -178,50 +177,34 @@ function caml_perform_effect(eff, cont, k0) {
178177
// The handler is defined in Stdlib.Effect, so we know that the arity matches
179178
var k1 = caml_pop_fiber();
180179
return caml_stack_check_depth()
181-
? handler(eff, cont, k1, k1)
182-
: caml_trampoline_return(caml_prepare_tramp(handler), [eff, cont, k1, k1]);
183-
}
184-
185-
//Provides: caml_call_fun
186-
//Requires: caml_call_gen
187-
//If: effects
188-
//If: !doubletranslate
189-
function caml_call_fun(f, args) {
190-
return caml_call_gen(f, args);
191-
}
192-
193-
//Provides: caml_call_fun
194-
//Requires: caml_call_gen_cps
195-
//If: effects
196-
//If: doubletranslate
197-
function caml_call_fun(f, args) {
198-
return caml_call_gen_cps(f, args);
180+
? (caml_get_cps_fun(handler))(eff, cont, k1, k1)
181+
: caml_trampoline_return(handler, [eff, cont, k1, k1]);
199182
}
200183

201-
//Provides: caml_get_fun
184+
//Provides: caml_get_cps_fun
202185
//If: effects
203186
//If: !doubletranslate
204-
function caml_get_fun(f) {
187+
function caml_get_cps_fun(f) {
205188
return f;
206189
}
207190

208-
//Provides: caml_get_fun
191+
//Provides: caml_get_cps_fun
209192
//If: effects
210193
//If: doubletranslate
211-
function caml_get_fun(f) {
194+
function caml_get_cps_fun(f) {
212195
return f.cps;
213196
}
214197

215198
//Provides: caml_alloc_stack
216-
//Requires: caml_pop_fiber, caml_fiber_stack, caml_stack_check_depth, caml_trampoline_return, caml_call_fun, caml_get_fun
199+
//Requires: caml_pop_fiber, caml_fiber_stack, caml_stack_check_depth, caml_trampoline_return, caml_call_gen_cps
217200
//If: effects
218201
//Version: >= 5.0
219202
function caml_alloc_stack(hv, hx, hf) {
220203
function call(i, x) {
221204
var f = caml_fiber_stack.h[i];
222205
var args = [x, caml_pop_fiber()];
223206
return caml_stack_check_depth()
224-
? caml_call_fun(f, args)
207+
? caml_call_gen_cps(f, args)
225208
: caml_trampoline_return(f, args);
226209
}
227210
function hval(x) {
@@ -232,7 +215,7 @@ function caml_alloc_stack(hv, hx, hf) {
232215
// Call [hx] in the parent fiber
233216
return call(2, e);
234217
}
235-
return [0, hval, [0, hexn, 0], [0, hv, hx, caml_get_fun(hf)], 0];
218+
return [0, hval, [0, hexn, 0], [0, hv, hx, hf], 0];
236219
}
237220

238221
//Provides: caml_alloc_stack

runtime/js/stdlib.js

+7
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,13 @@ function caml_call_gen(f, args) {
127127
}
128128
}
129129

130+
//Provides: caml_call_gen_cps
131+
//Requires: caml_call_gen
132+
//If: effects
133+
//If: !doubletranslate
134+
//Weakdef
135+
var caml_call_gen_cps = caml_call_gen;
136+
130137
//Provides: caml_call_gen_tuple (const, shallow)
131138
//Requires: caml_fiber_stack, caml_cps_closure
132139
//If: effects

0 commit comments

Comments
 (0)