Js_of_ocaml with project that calls C

I want to use js_of_ocaml to compile my ocaml project into javascript. However, my project makes use of a library that includes ocaml bindings to C functions. This is leading to issues when I compile my project into javascript, e.g. functions not being found. Do I need to create some kind of link from the C functions to javascript as well? I have never done anything like this before.

I don’t think you can call C functions from javascript, at least not in the browser. Obviously there are some serious security issues around that.

I think you can do so server-side if you are running javascriptt on node or some such, but you have to write wrappers for them.

1 Like

The C functions that you use must be reimplemented in Javascript. Essentially, where the native OCaml compiler expected to find an external symbol during linking with other object files, js_of_ocaml expects to find external symbols when linking JS files together. I haven’t done it enough to remember what are the dunes rules to write, though.

2 Likes

I would assume these calls could also be reimplemented in WASM as well?

1 Like

Yes, there there are a few project doing just that. For example Nomadic Labs / ocaml-secp256k1-internal · GitLab

2 Likes