Compiling Ocaml Effects to Javascript

I am currently working on the compilation of the algebraic effects and handlers introduced in multicore OCaml for the js_of_ocaml backend.

The bytecode and native backends of the OCaml compiler rely on the fact that in effect handlers the continuation can be called at most once to efficiently implement them. This method requires control over the runtime as it manipulates the stack. Since we do not have control over the stack in JavaScript we need to find a way to implement algebraic effects and handlers using existing primitives. In the setting of the -calculus, the traditional way to introduce control primitives is to use Continuation Passing Style (CPS) to make the control flow of the program explicit. Starting from a program using effect primitives we transform it to an equivalent program in CPS which can be compiled to JavaScript.

The actual process is actually a bit different because for maintenance reasons we do not want to implement this transformation in the OCaml compiler but directly on the bytecode which has a stable representation.

Another concern is that a full CPS transformation can induce a severe performance cost so we

...