ocaml-wasm / wasm_of_ocaml

Other
245 stars 9 forks source link

[FEATURE REQUEST] Examples for building Dune libraries #56

Open PSFW opened 3 months ago

PSFW commented 3 months ago

Is your feature request related to a problem? Please describe. Currently the examples are for building executables (Dune files with the executable Stanza), and these are output into .wat files with (export "_initialize"). I am trying to output a basic library, and export the functions of that library to use within Web Assembly.

Describe the solution you'd like I would like to be able to export multiple functions, and for examples to be provided for this. I am not sure if this is currently possible with the tool.

Describe alternatives you've considered I have tried the examples, as well as my own .ml & .mli files. I have also tried to use the --export and --wrap-with-fun options but these have not worked for me (they seem to be for JSOO). (I have even browsed this repo that was linked in another issue.)

OlivierNicole commented 3 months ago

o1-labs/integers_stubs_js#10 is an example of adding Wasm stubs to a library. It works when using the required fork of Dune referenced in the readme. Does that help solve your problem?

PSFW commented 3 months ago

Hey @OlivierNicole, thank you so much for your reply!

I have followed the example provided, and using that the change that I've applied has only been to add this stanza:

 (js_of_ocaml
  (javascript_files ./heyworld.js)
  (wasm_files heyworld.wat)
  )

The generated .wat file still contains only a single export though (for "_initialize").

Have you hand-crafted or edited the .wat file in the example that you've provided?

OlivierNicole commented 2 months ago

Sorry for the late reply.

The WebAssembly code is written by hand.

PSFW commented 2 months ago

No problem, thank you for letting me know.

OlivierNicole commented 2 months ago

I might have misunderstood your question. The primitives (declared as external in the ocaml code) need to be written by hand, but OCaml functions should be compiled to Wasm.

To build a library and not an executable, the instructions in #36 should work. I don’t think separate compilation is supported by our Dune fork yet, but @vouillon knows better.