Skip to content

Compiler: use globalThis, drop joo_global_object #1193

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

Merged
merged 4 commits into from
Jan 3, 2022
Merged

Compiler: use globalThis, drop joo_global_object #1193

merged 4 commits into from
Jan 3, 2022

Conversation

hhugo
Copy link
Member

@hhugo hhugo commented Dec 18, 2021

fix #699

  • implement compatibility for the old joo_global_object

@hhugo
Copy link
Member Author

hhugo commented Dec 18, 2021

cc @copy, @jchavarri, @TyOverby
Anyone interested reviewing and testing this ?

@TyOverby
Copy link
Collaborator

I can try to test this on the Jane Street codebase next week

@hhugo
Copy link
Member Author

hhugo commented Dec 21, 2021

Tests pass on node 10 (which doesn't have support for globalThis) and node 14 and 16.
I was able to run test referencing the old joo_global_object.

@TyOverby, I'd like to make a new release soon including this change. Please let me know once you've run your testing

@hhugo
Copy link
Member Author

hhugo commented Dec 21, 2021

It also fixed the issue reported by @jchavarri in #699 (comment)

Copy link
Contributor

@jchavarri jchavarri left a comment

Choose a reason for hiding this comment

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

Thank you!

I have more questions than anything else, but otherwise looks great to me.

@hhugo
Copy link
Member Author

hhugo commented Dec 21, 2021

Thank you!

I have more questions than anything else, but otherwise looks great to me.

Thanks for the review, can you close threads once you're happy with the answer

@jchavarri
Copy link
Contributor

jchavarri commented Dec 21, 2021

@hhugo I don't think I can resolve conversations as I didn't open the pull request nor have write access to the repo.

@TyOverby
Copy link
Collaborator

TyOverby commented Dec 24, 2021

@hhugo I got this branch building internally, but we have so much javascript that uses "joo_global_object" that it'll take a long time for me to make the necessary changes in order to tell if there are any other issues.

Also, I must admit that this feature confuses me a bit; I would have expected that you'd use globalThis to generate wrapping-functions that look like this:

(function (joo_global_object) {
   // ... code goes here ...
})(globalThis)

Where we still keep joo_global_object, but the way that the object is acquired is by looking at globalThis.

Instead, it appears that the function-parameter name is also changed, which requires all of the rest of the changes in this feature.

@TyOverby
Copy link
Collaborator

Also, what does "implement compatibility for the old joo_global_object" refer to?

@TyOverby
Copy link
Collaborator

Here's how I assumed that the feature would be implemented: #1195

@hhugo
Copy link
Member Author

hhugo commented Dec 24, 2021

@TyOverby , it is better to read this PR commit by commit.

The first commit just changes how we compute the global object (using globalThis), similar to your #1195.

The second commit cleans up the old joo_global_object name, renaming it to globalThis. We don't need a Jsoo-specific name, just use what the JS ecosystem uses.

The third commit make the stubs using the old joo_global_object work again (by introducing avar joo_global_object = globalThis statement if needed).

@hhugo
Copy link
Member Author

hhugo commented Dec 24, 2021

but we have so much javascript that uses "joo_global_object" that it'll take a long time for me to make the necessary changes in order to tell if there are any other issues.

I don't understand. You don't need to change anything, it should just work out of the box

@TyOverby
Copy link
Collaborator

I don't understand. You don't need to change anything, it should just work out of the box

Sadly something appears to be broken as every test in the tree fails with the message "undefined variable joo_global_object"

@TyOverby
Copy link
Collaborator

The second commit cleans up the old joo_global_object name, renaming it to globalThis. We don't need a Jsoo-specific name, just use what the JS ecosystem uses.

What is the benefit of this approach?

@TyOverby
Copy link
Collaborator

I don't understand. You don't need to change anything, it should just work out of the box

Sadly something appears to be broken as every test in the tree fails with the message "undefined variable joo_global_object"

I won't be able to investigate this further for a few days due to the holidays, but I'll dig deeper on Monday

@TyOverby
Copy link
Collaborator

the failure happens here:

https://github.com/janestreet/core/blob/master/core/src/strftime.js#L43

if(joo_global_object)joo_global_object.strftime = adaptedStrftime}
   ^
ReferenceError: joo_global_object is not defined

@TyOverby
Copy link
Collaborator

Fixing that instance just causes the same error to happen in other places that we use joo_global_object. Is there a flag or something that we need to turn on in order to get the "it just works" shim?

@TyOverby
Copy link
Collaborator

introducing avar joo_global_object = globalThis statement if needed

I can confirm that some sections get this added to the top of them, but not all. Of note, at least two sections use joo_global_object but don't get the shim.

@hhugo
Copy link
Member Author

hhugo commented Dec 28, 2021

introducing avar joo_global_object = globalThis statement if needed

I can confirm that some sections get this added to the top of them, but not all. Of note, at least two sections use joo_global_object but don't get the shim.

Thanks for testing, I have a theory that I should be able to test/fix early next year

@hhugo
Copy link
Member Author

hhugo commented Dec 30, 2021

@TyOverby, I think I've fixed your issues in my last commit.
core_kernel/src/runtime.js and virtual_dom/src/hooks.js are using joo_global_object but were not getting the shim.

(Also note that core_kernel declare strftime.js but doesn't seem to use it)

@TyOverby
Copy link
Collaborator

TyOverby commented Dec 31, 2021

@TyOverby, I think I've fixed your issues in my last commit.

Yep! I can confirm that the issue is no longer present!

Sadly it made another problem visible; I don't know if it's directly caused by this pull-request, but I'm seeing some strange behavior. A single javascript file (it's a 3rd party js library that we pull in) is being split between IIFEs like so:

runtime.js

var x = (function () { ...})();
if (typeof window !== 'undefined') window.x = x;
if (typeof globalThis !== 'undefined') globalThis.x = x;

Goes through Js_of_ocaml and out comes this:

output.bc.js

(function(globalThis) {
    var x = (function () { ...})();
}(globalThis));

(function(globalThis) {
    if (typeof window !== 'undefined') window.x = x;
    if (typeof globalThis !== 'undefined') globalThis.x = x;
}(globalThis));

Because the definition of x out of scope from the place that uses it, the 2nd IIFE will crash.

@TyOverby
Copy link
Collaborator

I spent a few hours trying to get a minimal repro but could not.
I'll be on vacation for the next two weeks, but would be happy to continue my investigation when I get back.

@TyOverby
Copy link
Collaborator

It looks like I'm able to work around that specific issue by wrapping the whole file in another IIFE.

@hhugo
Copy link
Member Author

hhugo commented Jan 2, 2022

@TyOverby, I've managed to reproduce your issue with the following

var x = (function () { ...})();
//Always .. a comment starting by //Always
if (typeof window !== 'undefined') window.x = x;
if (typeof globalThis !== 'undefined') globalThis.x = x;

The behavior was introduced by c6d8608
and should now be fixed on master by 3e7f728

I'll rebase this PR to make it easier to test for you

@hhugo
Copy link
Member Author

hhugo commented Jan 3, 2022

@TyOverby, I'm going to merge now, please make sure master works for you when you return from vacation.

@hhugo hhugo merged commit a0a8c96 into master Jan 3, 2022
@hhugo hhugo deleted the globalThis branch January 3, 2022 13:08
@TyOverby
Copy link
Collaborator

@hhugo all of our automated tests are passing; we're going to do some more manual testing next week.

Thanks for all the help!

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)
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.

joo_global_object is undefined when bundled in strict mode
3 participants