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

Implement addFunction under wasm backend purely in terms of the wasm table #8255

Merged
merged 5 commits into from
Mar 8, 2019

Conversation

sbc100
Copy link
Collaborator

@sbc100 sbc100 commented Mar 7, 2019

This removes the need for any wasm-side jsCall thunks.

In order to call addFunction either ALLOW_MEMORY_GROWTH or
RESERVED_FUNCTION_POINTERS is still required.

Also, if a JS function is added its signature is still required.

The corresponding binaryen change is: WebAssembly/binaryen#1938

@sbc100 sbc100 requested a review from jgravelle-google March 7, 2019 02:43
@sbc100 sbc100 changed the title Implement addFunction in terms under wasm backend purely in terms of the wasm table Implement addFunction under wasm backend purely in terms of the wasm table Mar 7, 2019
// In the future, we may get a WebAssembly.Function constructor. Until then,
// we create a wasm module that takes the JS function as an import with a given
// signature, and re-exports that as a wasm function.
function convertJsFunctionToWasm(func, sig) {
Copy link
Contributor

@binji binji Mar 7, 2019

Choose a reason for hiding this comment

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

Had a bit of fun golfing this down... :-) [edit: now 238 chars, with help from teammates]

w=(f,[R,...P],W=WebAssembly,L=x=>[x.length,...x],C=x=>'dfji'.indexOf(x)+124)=>
new W.Instance(new W.Module(new Int8Array([,97,115,109,1,,,,1,...L([1,96,...L(P.
map(C)),...L(R=='v'?[]:[C(R)])]),2,5,1,,,,,7,4,1,,,0])),{'':{'':f}}).exports['']

let convertJsFunctionToWasm = w;

const add = (x, y) => x + y;
const addi = convertJsFunctionToWasm(add, 'iii');
const addd = convertJsFunctionToWasm(add, 'ddd');
const printi = convertJsFunctionToWasm(print, 'vi');

print(addi(1.2, 2.3)); // 3
print(addd(1.2, 2.3)); // 3.5
printi(42.1);          // 42

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If the size of the generated module also reduced? Are you suggesting we use this version?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, sorry, we definitely shouldn't use this code. 😄 just had some insomnia last night so I started trying to see how small I could make it, thought you might appreciate. Feel free to ignore, haha

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps the most useful wasm module ever on per-byte basis?

Copy link
Contributor

Choose a reason for hiding this comment

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

Devil's advocate, should we not use this code? JS optimizers are extremely unlikely to shrink this code as well, and code size wins are code size wins.

Though it should be #ifed to only be shipped in release builds, and if we ever change the non-golfed version we should delete it. That is some write-only code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably best not to use it :) Does it even work with emscripten since it uses ES6 features? Was the old uglify pass ever removed?

Copy link
Member

@kripken kripken Mar 7, 2019

Choose a reason for hiding this comment

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

We do support ES6 now in all the major JS passes (but we didn't remove the old uglify versions because they let us support source maps in asm.js), and can probably assume JS is modern in places where wasm is used (but I'm not sure about that).

src/support.js Outdated Show resolved Hide resolved
src/support.js Show resolved Hide resolved
src/support.js Show resolved Hide resolved
Copy link
Contributor

@jgravelle-google jgravelle-google left a comment

Choose a reason for hiding this comment

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

Woo hoo

src/preamble.js Show resolved Hide resolved
src/support.js Outdated
// 'sig' parameter is currently only used for LLVM backend under certain
// circumstance: RESERVED_FUNCTION_POINTERS=1, EMULATED_FUNCTION_POINTERS=0.
// 'sig' parameter is required for the llvm backend but only when func is not
// already as WebAssembly function.
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: s/as/a/

src/support.js Outdated Show resolved Hide resolved
sbc100 added 5 commits March 7, 2019 21:46
…the wasm table

This removes the need for any wasm-side `jsCall` thunks.

In order to call `addFunction` either ALLOW_MEMORY_GROWTH or
RESERVED_FUNCTION_POINTERS is still required.

Also, if a JS function is added its signature is still required.
@sbc100 sbc100 merged commit 26ffccd into incoming Mar 8, 2019
@sbc100 sbc100 deleted the remove_jscall branch March 8, 2019 18:48
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.

4 participants