Skip to content

Commit 8a06ce6

Browse files
committed
another improvement
1 parent 1ac11c8 commit 8a06ce6

File tree

2 files changed

+56
-22
lines changed

2 files changed

+56
-22
lines changed

compiler/lib/effects.ml

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,34 @@ let cps_block ~st ~k pc block =
543543
in
544544

545545
let rewrite_instr x e =
546-
let perform_effect ~effect_ ~continuation ~tail =
546+
let perform_effect ~effect_ continuation_and_tail =
547547
Some
548548
(fun ~k ->
549549
let e =
550-
Prim (Extern "caml_perform_effect", [ Pv effect_; continuation; tail; Pv k ])
550+
match Config.target () with
551+
| `JavaScript -> (
552+
match continuation_and_tail with
553+
| None -> Prim (Extern "caml_perform_effect", [ Pv effect_; Pv k ])
554+
| Some (continuation, tail) ->
555+
Prim
556+
( Extern "caml_reperform_effect"
557+
, [ Pv effect_; continuation; tail; Pv k ] ))
558+
| `Wasm -> (
559+
(* temporary until we finish the change to the wasmoo
560+
runtime *)
561+
match continuation_and_tail with
562+
| None ->
563+
Prim
564+
( Extern "caml_perform_effect"
565+
, [ Pv effect_
566+
; Pc (Int Targetint.zero)
567+
; Pc (Int Targetint.zero)
568+
; Pv k
569+
] )
570+
| Some (continuation, tail) ->
571+
Prim
572+
( Extern "caml_perform_effect"
573+
, [ Pv effect_; continuation; tail; Pv k ] ))
551574
in
552575
let x = Var.fresh () in
553576
[ Let (x, e) ], Return x)
@@ -573,13 +596,9 @@ let cps_block ~st ~k pc block =
573596
~check:true
574597
~f
575598
[ arg; k' ])
576-
| Prim (Extern "%perform", [ Pv effect_ ]) ->
577-
perform_effect
578-
~effect_
579-
~continuation:(Pc (Int Targetint.zero))
580-
~tail:(Pc (Int Targetint.zero))
599+
| Prim (Extern "%perform", [ Pv effect_ ]) -> perform_effect ~effect_ None
581600
| Prim (Extern "%reperform", [ Pv effect_; continuation; tail ]) ->
582-
perform_effect ~effect_ ~continuation ~tail
601+
perform_effect ~effect_ (Some (continuation, tail))
583602
| _ -> None
584603
in
585604

runtime/js/effect.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,12 @@ function caml_make_unhandled_effect_exn(eff) {
134134
//Provides: caml_perform_effect
135135
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_fiber_stack
136136
//Requires: caml_make_unhandled_effect_exn, caml_current_stack
137-
//Requires: caml_resume_stack, caml_continuation_use_noexc
138137
//If: effects
139138
//Version: >= 5.0
140-
function caml_perform_effect(eff, cont, last, k0) {
139+
function caml_perform_effect(eff, k0) {
141140
if (caml_fiber_stack === 0) {
142141
var exn = caml_make_unhandled_effect_exn(eff);
143-
if (!cont) throw exn;
144-
else {
145-
var stack = caml_continuation_use_noexc(cont);
146-
caml_resume_stack(stack, last, k0);
147-
throw exn;
148-
}
142+
throw exn;
149143
}
150144
// Get current effect handler
151145
var handler = caml_current_stack.h[3];
@@ -155,13 +149,34 @@ function caml_perform_effect(eff, cont, last, k0) {
155149
// Move to parent fiber and execute the effect handler there
156150
// The handler is defined in Stdlib.Effect, so we know that the arity matches
157151
var k1 = caml_pop_fiber();
158-
if (!cont) {
159-
//Perform
160-
cont = [245 /*continuation*/, last_fiber, 0];
161-
} else {
162-
//Reperform
163-
last.e = last_fiber;
152+
var cont = [245 /*continuation*/, last_fiber, 0];
153+
return caml_stack_check_depth()
154+
? handler(eff, cont, last_fiber, k1)
155+
: caml_trampoline_return(handler, [eff, cont, last_fiber, k1]);
156+
}
157+
158+
//Provides: caml_reperform_effect
159+
//Requires: caml_pop_fiber, caml_stack_check_depth, caml_trampoline_return, caml_fiber_stack
160+
//Requires: caml_make_unhandled_effect_exn, caml_current_stack
161+
//Requires: caml_resume_stack, caml_continuation_use_noexc
162+
//If: effects
163+
//Version: >= 5.0
164+
function caml_reperform_effect(eff, cont, last, k0) {
165+
if (caml_fiber_stack === 0) {
166+
var exn = caml_make_unhandled_effect_exn(eff);
167+
var stack = caml_continuation_use_noexc(cont);
168+
caml_resume_stack(stack, last, k0);
169+
throw exn;
164170
}
171+
// Get current effect handler
172+
var handler = caml_current_stack.h[3];
173+
var last_fiber = caml_current_stack;
174+
last_fiber.k = k0;
175+
last_fiber.e = 0;
176+
// Move to parent fiber and execute the effect handler there
177+
// The handler is defined in Stdlib.Effect, so we know that the arity matches
178+
var k1 = caml_pop_fiber();
179+
last.e = last_fiber;
165180
return caml_stack_check_depth()
166181
? handler(eff, cont, last_fiber, k1)
167182
: caml_trampoline_return(handler, [eff, cont, last_fiber, k1]);

0 commit comments

Comments
 (0)