Skip to content

Cache document.currentScript for non-ESM output #2520

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

Closed
wants to merge 15 commits into from
Closed

Cache document.currentScript for non-ESM output #2520

wants to merge 15 commits into from

Conversation

ghost
Copy link

@ghost ghost commented Apr 10, 2021

Fixes issue #2502.

Currently, when using non-ESM module output (i.e. scripts output), the generated JavaScript accesses the Document#currentScript getter at runtime, which will always have a different (incorrect) value by the time that the user runs their code to load their Wasm.

With this change, the default source for ESM and web script output is cached in an immutable variable, and accessed when the user doesn't pass an argument.

Other output targets should remain untouched.

As an added side-effect, the default source is always constructed, this should add a small amount of extra overhead to loading the glue JS, but if the user loads the Wasm multiple times (for some reason?), it is only constructed once, which should improve performance.

@ghost ghost changed the title Cache document.currentScript for non-EMS output Cache document.currentScript for non-ESM output Apr 11, 2021
@alexcrichton
Copy link
Contributor

Thanks! Could you leave a comment in the source for why it's done this way?

@ghost

This comment has been minimized.

@alexcrichton
Copy link
Contributor

This comment looks fine, thanks for that! I think this needs a rebase now after a previous merge, want to do that and I can merge?

@ghost
Copy link
Author

ghost commented Apr 19, 2021

Sure, I'll rebase and deal with the conflicts soon.

};

let ts = self.ts_for_init_fn(
has_memory,
!self.config.omit_default_module_path && !default_module_path.is_empty(),
!self.config.omit_default_module_path && !default_module_path_creator.is_empty(),
Copy link
Author

@ghost ghost Apr 19, 2021

Choose a reason for hiding this comment

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

If I'm following this logic correctly, the entire expression could be shortened to the following:

!default_module_path_creator.is_empty()

cc @diceride

If someone omits the default path, and chooses, say, Node.js, there still won't be a default path, therefore we can just check if there isn't a default path via .is_empty().

@ghost
Copy link
Author

ghost commented Apr 19, 2021

@alexcrichton It seems like a bunch of runtime tests are now failing after rebasing from the recent merges.

Could you take a look into it and help me debug this?

@alexcrichton
Copy link
Contributor

I think you need to run the tests locally with BLESS=1 to regenerate the expected output since I believe it's changing here

@alexcrichton
Copy link
Contributor

(or the test expectations of what the output is just need updating)

@ghost ghost closed this Aug 18, 2021
This pull request was closed.
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.

1 participant