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

feat(compiler): support target envs #1160

Merged
merged 52 commits into from
Dec 7, 2021

Conversation

cdaringe
Copy link
Contributor

@cdaringe cdaringe commented Oct 19, 2021

Problem

See #1157

Solution

  • Add config & cli options for target runtime
  • Add conditional //If: ... handling to include target runtime artifacts in compilation

runtime/fs_fake.js Outdated Show resolved Hide resolved
runtime/fs_fake.js Outdated Show resolved Hide resolved
runtime/fs_fake.js Outdated Show resolved Hide resolved
@hhugo
Copy link
Member

hhugo commented Oct 19, 2021

I suspect we need to do something in fs.js whenever we try to use MlNodeDevice.
Implement some kind of optional //Requires: maybe ?

@hhugo
Copy link
Member

hhugo commented Oct 19, 2021

We need some test in compiler/tests-compiler

compiler/lib/linker.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
@cdaringe
Copy link
Contributor Author

cdaringe commented Oct 21, 2021

Ok, so here's what I've tried:

  • Support //If: <target_env> annotations. by default, when no annotations are present, it assumes the status quo. The status quo is implicitly an isomorphic case--support javascript running in node or browser.
  • If there is a target_env specific implementation available specified by an //If: <target_env> annotation, allow it to override the isomorphic case. In fs_node.js, for example, i provided empty impls for the browser. This should be fine and good, as these functions should never be invoked, as someone targeting the browser should never be calling a FS API. Someone will undoubtedly try, and the will get the all-too-familiar cannot call FIELD_NAME of undefined error :). This is an acceptable/desirable state.
  • Update the fragment data model to included the annotation list, so we can conditionally swap the target_env implementations

@cdaringe cdaringe force-pushed the feat/support-env-targets branch 2 times, most recently from 7cb10af to 98d1d5a Compare November 1, 2021 01:55
@cdaringe cdaringe marked this pull request as ready for review November 2, 2021 07:04
@cdaringe
Copy link
Contributor Author

cdaringe commented Nov 2, 2021

Implementation complete! Currently, I only reduce the fs_node.js module. This brings an --opt=3 let () = print_endline ... prog from 19k => 15k :) I see some possible other wins as well, which we can riff on in a different issue or changeset. I tested the examples/ in browser as well.

Ready for review

@cdaringe cdaringe requested a review from hhugo November 2, 2021 07:45
@cdaringe
Copy link
Contributor Author

cdaringe commented Nov 5, 2021

Looks like yesterday's build was only grumpy because I forgot a CHANGES.md patch. Patch sent, ready for next CI attempt

@cdaringe
Copy link
Contributor Author

Hey @hhugo, truly no rush, but ready for another round of reviews. I believe the feature is complete :)

compiler/bin-js_of_ocaml/cmd_arg.ml Outdated Show resolved Hide resolved
compiler/bin-js_of_ocaml/compile.ml Outdated Show resolved Hide resolved
compiler/bin-js_of_ocaml/compile.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
Copy link
Contributor Author

@cdaringe cdaringe left a comment

Choose a reason for hiding this comment

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

thanks for the helpful feedback. comments all replied to, most with patches :)

compiler/bin-js_of_ocaml/compile.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
@cdaringe cdaringe requested a review from hhugo November 12, 2021 07:17
compiler/lib/linker.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
@cdaringe
Copy link
Contributor Author

Thanks for all of the patience with this back and forth conversation. I acted on all of your feedback 👍

CHANGES.md Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
compiler/lib/linker.ml Outdated Show resolved Hide resolved
@cdaringe
Copy link
Contributor Author

patches sent for all feedback :)

@cdaringe cdaringe requested a review from hhugo November 28, 2021 05:56
README.md Outdated
@@ -55,7 +55,7 @@ ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml-ppx -linkpkg -o cubes
Then, run the `js_of_ocaml` compiler to produce JavaScript code:

```
js_of_ocaml cubes.byte
js_of_ocaml --target-env=browser cubes.byte
Copy link
Member

@hhugo hhugo Nov 29, 2021

Choose a reason for hiding this comment

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

Maybe add a section to the README to explain the target-env flag. We should update the manual as well

@hhugo
Copy link
Member

hhugo commented Nov 29, 2021

I've pushed some changes to your branch directly, hope you don't mind.

Things I've done:

  • refactor load_fragment to try to make the logic easier to follow
  • remove some let open as it make code harder to understand

Let me know what you think

@hhugo
Copy link
Member

hhugo commented Nov 29, 2021

I think I'm happy with where we are, modulo some missing documentation.
I'm planning to squash all commits into a single one at the end, unless you disagree.
Let me know what you think

@hhugo
Copy link
Member

hhugo commented Nov 30, 2021

@cdaringe, will you be able to write some documentation ?

@cdaringe
Copy link
Contributor Author

cdaringe commented Nov 30, 2021 via email

@cdaringe
Copy link
Contributor Author

cdaringe commented Dec 5, 2021

hey @hhugo, sorry for the delay. I've been ill for a week.

956e1ae

  1. it's not much, but felt it was probably all that was warranted, based on the current docs organization
  2. i saw make doc, but not clear on how to actually render .wiki files

let me know if you want a deeper writeup somewhere

@hhugo hhugo merged commit 54e6659 into ocsigen:master Dec 7, 2021
@hhugo
Copy link
Member

hhugo commented Dec 7, 2021

Thanks for the hard work.

@smorimoto
Copy link
Member

HELL YEAH! Thank you for your efforts on this @cdaringe 🙂

@cdaringe cdaringe deleted the feat/support-env-targets branch December 7, 2021 16:25
hhugo added a commit to hhugo/opam-repository that referenced this pull request Jan 24, 2022
…s_of_ocaml-ppx_deriving_json, js_of_ocaml-ppx, js_of_ocaml-lwt and js_of_ocaml-compiler (4.0.0)

CHANGES:

## Features/Changes
* Compiler: add --target-env flag, for JS runtime specific compilation targets (ocsigen/js_of_ocaml#1160).
* Compiler: static evaluation of backend_type (ocsigen/js_of_ocaml#1166)
* Compiler: speedup emitting js files (ocsigen/js_of_ocaml#1174)
* Compiler: simplify (a | 0) >>> 0 into (a >>> 0) (ocsigen/js_of_ocaml#1177)
* Compiler: improve static evaluation of cond (ocsigen/js_of_ocaml#1178)
* Compiler: be more consistent dealing with js vs ocaml strings (ocsigen/js_of_ocaml#984)
* Compiler: Compiler: add BigInt to provided symbols (fix ocsigen/js_of_ocaml#1168) (ocsigen/js_of_ocaml#1191)
* Compiler: use globalThis, drop joo_global_object ocsigen/js_of_ocaml#1193
* Compiler: new -Werror flag to turn wanrings into errors (ocsigen/js_of_ocaml#1222)
* Compiler: make the inlining less agressive, reduce size, improve pref (ocsigen/js_of_ocaml#1220)
* Compiler: rename internal library js_of_ocaml-compiler.runtime to js_of_ocaml-compiler.runtime-files
* Lib: new runtime library to improve compatibility with Brr and gen_js_api
* Lib: add messageEvent to Dom_html (ocsigen/js_of_ocaml#1164)
* Lib: add PerformanceObserver API (ocsigen/js_of_ocaml#1164)
* Lib: add CSSStyleDeclaration.{setProperty, getPropertyValue, getPropertyPriority, removeProperty} (ocsigen/js_of_ocaml#1170)
* Lib: make window.{inner,outer}{Width,Height} non-optional
* Lib: introduce Js_of_ocaml.Js_error module, deprecate Js_of_ocaml.Js.Error exception.
* Lib: add deprecation warning for deprecated code
* PPX: json can now be derived for mutable records (ocsigen/js_of_ocaml#1184)
* Runtime: use crypto.getRandomValues when available (ocsigen/js_of_ocaml#1209)
* Misc: move js_of_ocaml-ocamlbuild out to its own repo
* Misc: add support for OCaml 4.14 (ocsigen/js_of_ocaml#1173)

## Bug fixes
* Compiler: fix sourcemap warning for empty cma (ocsigen/js_of_ocaml#1169)
* Compiler: Strengthen bound checks. (ocsigen/js_of_ocaml#1172)
* Compiler: fix `--wrap-with-fun` under node (ocsigen/js_of_ocaml#653, ocsigen/js_of_ocaml#1171)
* Compiler: fix parsing of annotaions in js stubs (ocsigen/js_of_ocaml#1212, fix ocsigen/js_of_ocaml#1213)
* Ppx: allow apostrophe in lident (fix ocsigen/js_of_ocaml#1183) (ocsigen/js_of_ocaml#1192)
* Runtime: fix float parsing in hexadecimal form
* Runtime: fix implementation of caml_js_instanceof
* Graphics: fix mouse_{x,y} (ocsigen/js_of_ocaml#1206)
@TheSpyder
Copy link

how do I set this option in a dune build? I tried (js_of_ocaml (flags (:standard --target-env=browser))) but then dune passes the flag to js_of_ocaml link where it's an unknown option.

@TheSpyder
Copy link

ah that's ocaml/dune#5563, my apologies

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants