-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
Conversation
// 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) { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 #if
ed 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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Woo hoo
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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: s/as/a/
…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.
This removes the need for any wasm-side
jsCall
thunks.In order to call
addFunction
either ALLOW_MEMORY_GROWTH orRESERVED_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