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

Use WebAssembly.Function constructor if available #9908

Merged
merged 6 commits into from
Nov 27, 2019
Merged

Use WebAssembly.Function constructor if available #9908

merged 6 commits into from
Nov 27, 2019

Conversation

backes
Copy link
Contributor

@backes backes commented Nov 27, 2019

With the type reflection proposal, we will get a way more efficient way
to convert a JS function into a wasm exported function. Use that if
available.

@welcome
Copy link

welcome bot commented Nov 27, 2019

Thank you for submitting a pull request! If this is your first PR, make sure to add yourself to AUTHORS.

With the type reflection proposal, we will get a way more efficient way
to convert a JS function into a wasm exported function. Use that if
available.
@sbc100
Copy link
Collaborator

sbc100 commented Nov 27, 2019

Awesome! Thanks.

We should probably try to test this. Is there a nodejs flag that we can set in a unit test? I wonder if we use a recent enough node during testing? We currently use 12.9.1.

Perhaps we could have new flag such as ASSUME_LATEST_FEATURES.. that would allows us to compile out the legacy support and enable the testing of this codepath.

function convertJsFunctionToWasm(func, sig) {
#if WASM2JS
return func;
#else // WASM2JS

// If the type reflection proposal is available, use the new
// "WebAssembly.Function" constructor.
// Otherwise, construct a minimal wasm module importing the JS function and
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can this be made into a general polyfill?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At which level are you imagining this polyfill? Do you suggest implementing a 'WebAssembly.Function' function if it's not defined? That would mostly work, but the returned object would still be of different type I think.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that's what I thought. I'm not sure specific returned type matters too much (especially in this codebase), but if it does, we can always do setPrototypeOf on the returned function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What would be the benefit of doing this? Less code in the convertJsFunctionToWasm function?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, but also I thought it might be useful as a generic polyfill for other users that Emscripten just happens to use.

@backes
Copy link
Contributor Author

backes commented Nov 27, 2019

V8 has the --wasm-staging flag, which enables type reflection (including WebAssembly.Function). Node would inherit that flag.
Node 12.9.1 uses V8 7.6.303.29, which does not have this flag yet though. It's only available since V8 7.8.

If possible, I think it would generally be good to also test with --wasm-staging enabled, since these are the features that we plan to enable by default soon.

@kripken
Copy link
Member

kripken commented Nov 27, 2019

Thanks @backes ! Code lgtm.

The CI test failure looks unrelated, but it's blocking the relevant tests from running, so we'll need to figure that out.

Do you know what the standardization stage of WebAssembly.Function is, and where it's implemented?

@kripken
Copy link
Member

kripken commented Nov 27, 2019

Tests look good, thanks again @backes!

@kripken kripken merged commit 0f848e0 into emscripten-core:incoming Nov 27, 2019
@backes backes deleted the use-webassembly-function-constructor branch November 28, 2019 10:41
belraquib pushed a commit to belraquib/emscripten that referenced this pull request Dec 23, 2020
With the type reflection proposal, we will get a way more efficient way
to convert a JS function into a wasm exported function. Use that if
available.
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.

5 participants