Implement addFunction under wasm backend purely in terms of the wasm table#8255
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.
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); // 42There was a problem hiding this comment.
If the size of the generated module also reduced? Are you suggesting we use this version?
There was a problem hiding this comment.
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.
Perhaps the most useful wasm module ever on per-byte basis?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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).
| // '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. |
…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
jsCallthunks.In order to call
addFunctioneither 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