Description
Hi,
I believe this is a super low-hanging fruit that would greatly improve interop with external libraries. Please read on for a proposed really simple fix.
Is your feature request related to a problem? Please describe.
Cross-referencing ocaml/dune#4854
I have externals that are implemented with bindings to cstubs via ctypes in the general case, and via a custom WASM implementation in the JS case.
While I wait for dune
to implement conditional linking or something comparable, I now have in my code:
external js_hook_sha256: bytes -> bytes = "js_hook_sha256"
let sha256 =
if Sys.backend_type = Other "js_of_ocaml" then
js_hook_sha256
else
FastNativeCryptoLib.SHA256.hash
along with a dummy C stub for js_hook_sha256
just to guarantee successful linking in non-JS scenarios.
The issue is that right now I end up with an ENORMOUS amount of dependencies onto ctypes that are not eliminated because js_of_ocaml does not know that the else
branch can be eliminated at compile-time.
Describe the solution you'd like
I would like js_of_ocaml to partially evaluate if Sys.backend_type = Other "js_of_ocaml" then e1 else e2
into e1
Quoting @hhugo :
Jsoo already does static evaluation. It's just a matter of adding a new case I guess. See
js_of_ocaml/compiler/lib/eval.ml
Line 147 in c929312
Describe alternatives you've considered
See original bug report on the dune bug tracker for every other solution I've considered.
Thanks,
Jonathan