Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasm_of_ocaml support #11093

Merged
merged 26 commits into from
Nov 5, 2024
Merged

Conversation

vouillon
Copy link
Member

@vouillon vouillon commented Nov 4, 2024

@hhugo @rgrinberg This is an alternative design. What do you think about it?

This adds a wasm mode. All the js_of_ocaml options are duplicated as wasm_of_ocaml options. There is a (js_of_ocaml (enabled_if <blang expression>)) and (wasm_of_ocaml (enabled_if <blang expression>)) option in env and executable stanzas to control whether the js and wasm modes are enabled.

I have not updated the documentation yet.

doc/wasmoo.rst Outdated
.. code:: console

$ dune build ./foo.bc.wasm.js
$ node _build/default/foo.bc.wasm.js
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do I need to specify the submodes here? Dune could find out what to build for this wasm.js target on its own, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation has not been updated. That should be the following above:

(executable (name foo) (modes wasm))

But yes, you can just use dune build.

Not that this was just copied from the Js_of_ocaml part of the doc:

dune/doc/jsoo.rst

Lines 38 to 44 in b409963

And then request the ``.js`` target:
.. code:: console
$ dune build ./foo.bc.js
$ node _build/default/foo.bc.js
hello from js

@vouillon vouillon force-pushed the wasm_of_ocaml-alternative branch 2 times, most recently from 83731d5 to ec321a5 Compare November 4, 2024 13:20
Copy link
Collaborator

@hhugo hhugo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a quick look, I think I prefer this approach better.

It seems you have not implemented the generation of a bc.js when js_of_ocaml is disabled, do you still want it ?

Js_of_ocaml.Mode.Set.inter jsoo_enabled_modes jsoo_is_whole_program
in
let bytecode_exe_needed =
L.Map.foldi
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L.Map.exists

| Wasm -> Exe.Linkage.wasm
in
if Js_of_ocaml.Mode.Pair.select ~mode:jsoo_mode jsoo_is_whole_program
then [ Exe.Linkage.byte_for_jsoo; linkage ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have an issue if byte_for_jsoo appears twice (once for js and once for wasm)

| JS
| Wasm

let equal = Poly.equal
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let equal = Poly.equal
let equal (a : t) b = Poly.equal a b

let union = Pair.map2 ~f:( || )
let is_empty (x : t) = not (x.js || x.wasm)

let to_list (x : t) =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let to_list (x : t) =
let to_list ({ wasm; js}: t) =

Comment on lines 162 to 163
match mode with
| Mode.JS -> "js"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
match mode with
| Mode.JS -> "js"
match (mode : Mode.t) with
| JS -> "js"

@@ -242,6 +358,7 @@ module Env = struct
; sourcemap = None
; runtest_alias = None
; flags = Flags.default ~profile
; enabled_if = Some Blang.true_
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why Some and not None ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so that we don't need a case for None there:

(match parent.enabled_if with
| Some (Const default) -> default
| _ -> assert false)

(match js_of_ocaml.enabled_if with
| Some (Const default) -> Memo.return default
| _ -> assert false)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be either to read if there were less hidden invariants. What do you think of the following ?

diff --git a/src/dune_rules/jsoo/js_of_ocaml.ml b/src/dune_rules/jsoo/js_of_ocaml.ml
index dc2fad30a..2c8c8e185 100644
--- a/src/dune_rules/jsoo/js_of_ocaml.ml
+++ b/src/dune_rules/jsoo/js_of_ocaml.ml
@@ -358,7 +358,7 @@ module Env = struct
     ; sourcemap = None
     ; runtest_alias = None
     ; flags = Flags.default ~profile
-    ; enabled_if = Some Blang.true_
+    ; enabled_if = None
     }
   ;;
 end
diff --git a/src/dune_rules/jsoo/jsoo_rules.ml b/src/dune_rules/jsoo/jsoo_rules.ml
index b10ac8f27..98b29afeb 100644
--- a/src/dune_rules/jsoo/jsoo_rules.ml
+++ b/src/dune_rules/jsoo/jsoo_rules.ml
@@ -12,18 +12,16 @@ let compute_env ~mode =
         let local = Js_of_ocaml.Mode.select ~mode local.js_of_ocaml local.wasm_of_ocaml in
         let* parent = parent in
         let+ enabled_if =
-          match local.enabled_if with
-          | None ->
-            Memo.return
-              (match parent.enabled_if with
-               | Some (Const default) -> default
-               | _ -> assert false)
-          | Some enabled_if -> Expander.eval_blang expander enabled_if
+          match Option.first_some local.enabled_if parent.enabled_if with
+          | Some enabled_if ->
+            let+ v = Expander.eval_blang expander enabled_if in
+            Some (Blang.Const v)
+          | None -> Memo.return None
         in
         { Js_of_ocaml.Env.compilation_mode =
             Option.first_some local.compilation_mode parent.compilation_mode
         ; sourcemap = Option.first_some local.sourcemap parent.sourcemap
-        ; enabled_if = Some (Const enabled_if)
+        ; enabled_if
         ; runtest_alias = Option.first_some local.runtest_alias parent.runtest_alias
         ; flags =
             Js_of_ocaml.Flags.make
@@ -601,8 +599,8 @@ let jsoo_enabled
   | None ->
     let* js_of_ocaml = jsoo_env ~dir ~mode in
     (match js_of_ocaml.enabled_if with
-     | Some (Const default) -> Memo.return default
-     | _ -> assert false)
+     | Some enabled_if -> eval enabled_if
+     | None -> Memo.return true)
 ;;
 
 let jsoo_enabled_modes ~expander ~dir ~in_context =

| Some x -> Memo.return x
in
let runtime_files = javascript_files @ wasm_files in
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird. Is it the case that wasm_files is always empty when mode is js ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's the case:

and+ wasm_files =
match mode with
| Mode.JS -> return []
| Wasm ->
field
"wasm_files"
(Dune_lang.Syntax.since Stanza.syntax (3, 17) >>> repeat string)
~default:[]

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could add an assertion.

@hhugo
Copy link
Collaborator

hhugo commented Nov 4, 2024

@vouillon could you submit a version of ocsigen/js_of_ocaml#1724 using this PR ?

@vouillon
Copy link
Member Author

vouillon commented Nov 4, 2024

It seems you have not implemented the generation of a bc.js when js_of_ocaml is disabled, do you still want it ?

It does not fit well. For the wasm_of_ocaml test suite, I have switched to using dune test stanzas and only had to add a few rules to copy a bc.wasm.js file to bc.js.

@vouillon
Copy link
Member Author

vouillon commented Nov 4, 2024

See ocsigen/js_of_ocaml#1726

@hhugo
Copy link
Collaborator

hhugo commented Nov 5, 2024

This looks good, I left one last suggestion. The documentation still needs to be updated

vouillon and others added 15 commits November 5, 2024 12:46
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Rudi Grinberg <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
@@ -110,11 +110,17 @@ end = struct
(List.rev_concat_map
~f:(List.rev_map ~f:(fun f -> Section.Lib, f))
(let { Mode.Dict.byte; native } = Lib_info.archives lib in
let jsoo_files =
(* A same runtime file can be used both for jsoo and wasmoo *)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does that comment still make sense now that jsoo and wasm options are different ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it still makes sense. In particular, one may want to pass a JavaScript file to wasm_of_ocaml to get some primitive purity information. Or one need to include some JavaScript code both when generating JavaScript and Wasm code. For instance, I have this for the virtual_dom package:

 (js_of_ocaml
  (javascript_files ../lib/virtualdom.compiled.js ./thunk.js ./hooks.js))
 (wasm_of_ocaml
  (javascript_files ../lib/virtualdom.compiled.js ./thunk.js ./hooks.js))

let name = Path.basename fn in
let dir = in_build_dir build_context ~config [ lib_name ] in
let in_context =
{ Js_of_ocaml.In_context.flags = Js_of_ocaml.Flags.standard
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be Js_of_ocaml.In_context.default ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is your code...
But yes, that should work.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is your code...

I know

(Js_of_ocaml.In_buildable.decode ~executable ~mode:JS)
~default:Js_of_ocaml.In_buildable.default
and+ wasm_of_ocaml =
let executable =
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be lifted up and shared with the one inside js_of_ocaml above

@hhugo
Copy link
Collaborator

hhugo commented Nov 5, 2024

@hhugo I'm not completely sure what to do about the ```(wasm_of_ocaml (sourcemap ))option. The distinction betweenfile` and `inline` does not really make sense: there is no inline source maps in Wasm and the source maps are going to be in the `.bc.wasm.asset` directory. I can keep the three options `no`, file or ``inline`` to stay in line with Js_of_ocaml. Or I can have just two options ``yes`` / ``no``.

I don't know either. We could also let dune complain if one tries to use inline with wasm.

Signed-off-by: Jérôme Vouillon <[email protected]>
@vouillon
Copy link
Member Author

vouillon commented Nov 5, 2024

I don't know either. We could also let dune complain if one tries to use inline with wasm.

Or rather it should complain for file since one does not generate a separate .map target file?

"compilation_mode"
(Dune_lang.Syntax.since Stanza.syntax (3, 17) >>> Compilation_mode.decode)
else return None
only_in_library
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be only_in_executable

Signed-off-by: Jérôme Vouillon <[email protected]>
=================

Dune has full support for building wasm_of_ocaml libraries and executables transparently.
There's no need to customise or enable anything to compile OCaml
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the documentation mention the external dependencies that are required for the build ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now explicitly point to the github repository above.

Copy link
Collaborator

@hhugo hhugo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. One will need to squash commits before merging

Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
Signed-off-by: Jérôme Vouillon <[email protected]>
@vouillon
Copy link
Member Author

vouillon commented Nov 5, 2024

I have made another clean-up pass.
I have also disallowed the enable_if option in libraries, like other build-related options.

@vouillon vouillon changed the title Wasm of ocaml support: alternative design Wasm of ocaml support Nov 5, 2024
@vouillon vouillon changed the title Wasm of ocaml support Wasm_of_ocaml support Nov 5, 2024
with:
ocaml-compiler: 4.14.x
opam-pin: false
opam-depext: false
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opam depext is unused in setup-ocaml v3

Signed-off-by: Jérôme Vouillon <[email protected]>
@rgrinberg rgrinberg merged commit 3995bb5 into ocaml:main Nov 5, 2024
28 checks passed
@rgrinberg
Copy link
Member

LGTM. Feel free to send the docs in another PR

@vouillon vouillon deleted the wasm_of_ocaml-alternative branch November 6, 2024 10:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants