Skip to content

[BUG] RuntimeError: illegal cast, when performing an effect with wasm_of_ocaml #1839

@Nymphium

Description

@Nymphium

Describe the bug
When running a program that uses an effect, a runtime error (RuntimeError: illegal cast) occurs instead of the expected uncaught effect exception. The minimal reproducible example is:

type _ Effect.t += Print : string -> unit Effect.t
let () = Effect.perform (Print "Hello, WASM!")

The bug can be reproducable in this repo: https://github.com/Nymphium/hellowasm/tree/effect

open Effect

type _ Effect.t += Print : string -> unit Effect.t

let () =
  try perform (Print "Hello, WASM!") with
  | effect Print s, k -> print_endline s |> Deep.continue k
;;
$ dune build ./app/main.bc.wasm.js
$ node `./_build/default/app/main.bc.wasm.js`
/Users/nymphium/works/github/ocaml-helloworld/_build/default/app/main.bc.wasm.js:455
           throw: e=>{throw e;},
                      ^

RuntimeError: illegal cast
    at capture_continuation (wasm://wasm/0003914e:wasm-function[100]:0x7e98)
    at toplevel (wasm://wasm/0003914e:wasm-function[162]:0x9d50)
    at caml_main (wasm://wasm/0003914e:wasm-function[102]:0x7ecd)
    at entry_point (wasm://wasm/0003914e:wasm-function[122]:0x8439)
    at link (/Users/nymphium/works/github/ocaml-helloworld/_build/default/app/main.bc.wasm.js:559:14)

Node.js v22.13.1

Expected behavior
The program should print Hello, WASM! (or, in the case where the effect is handled, print the string and continue execution) rather than throwing a runtime error.

Versions
Version of packages used to reproduce the bug
(ocamlc, js_of_ocaml, ..)

  • OCaml Compiler 5.3.0
  • dune 3.17.2
  • wasm_of_ocaml 6.0.1
  • wasm-opt 119
  • NodeJS 22.13.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions