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

Fixed imports with late binding and this #4225

Merged
merged 6 commits into from
Nov 7, 2024

Conversation

RunDevelopment
Copy link
Contributor

Fixes #4214

The problem in #4214 was that function like document.write can't be imported as a function reference into WASM directly, because document.write does not capture the this argument (here document). Or in other words, there is a difference between:

export function __wbg_reload_90d82b22b83c1d99(text) {
    document.write(text); // `write` has `document` as the `this` arg
};

and

// there is no `this` arg, so `write` will fail when invoked in WASM
export const __wbg_reload_90d82b22b83c1d99 = typeof document.write === "function" ? document.write(text) : notDefined('document.write');

However, aside from this not being captured, the (let's called it) direct import style also didn't support late binding. If the object or function is only defined after the WASM module is instantiated, then the direct import style will cause an error.

This PR fixes both issues by always generating a wrapper function for imported function from global scope.

Copy link
Collaborator

@daxpedda daxpedda left a comment

Choose a reason for hiding this comment

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

Nice!

Missing a changelog entry!

@daxpedda daxpedda added the waiting for author Waiting for author to respond label Nov 7, 2024
@RunDevelopment
Copy link
Contributor Author

Added changelog.

@daxpedda daxpedda merged commit 1f739b6 into rustwasm:main Nov 7, 2024
41 checks passed
@RunDevelopment RunDevelopment deleted the issue4214 branch November 7, 2024 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for author Waiting for author to respond
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0.2.95 breaks imports that rely on context when using js_namespace
2 participants