You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ability to iterate over webassembly contents with JS api #2370
The Js api can't be used to iterate over WebAssembly functions and exports.
This feature could be really useful.
For now, to get a function, we need, at least, to know its name.
[ Edit: In a more general way, binaryen (via its js wrapper) could be able to list the exports, the data sections, the globals and all the functions. ]
As a workaround, you can print to WAT and then regex as needed. I am doing this now to generate a list of all function names. +1 on adding this directly to the JS API.
The accessors in #2386 have landed, but the npm module doesn't seem to have updated if I install as npm install binaryen. If I install a nightly build with npm install binaryen@nightly it gives a current build which includes them.
I did find a bug in the handling of switch destination label names, for which I have a fix in #2553. Otherwise seems to be enough to both list out the functions and walk through the code in the function bodies.
For a bit of background: The latest tag of the npm package updates with new version_XY tags on the main repo. I guess there hasn't been a tag since the updates, so these features are only available in the nightlies so far.
Reporting back from the buildbot: There's a v90.0.0 now with updated definitions and docs, that also ships the (yet undocumented) Wasm variant as const binaryen = require("binaryen/wasm");. The Wasm variant requires awaiting the binaryen.ready promise.
I've got most of what I seem to need for iterating over things now on my project, except for iterating through the function table initialization segments. I'll try and put together a patch later adding that if I can work it out.
@RReverser there's not an iterator specifically for imports, but for imported memories and functions you can iterate over all mems/funcs and check which have an import module & name set.
For imported globals, there's no easy iterator last I checked; in my compiler project I had to find global references while walking through instructions in function bodies.
Activity
brmorr commentedon Oct 13, 2019
As a workaround, you can print to WAT and then regex as needed. I am doing this now to generate a list of all function names. +1 on adding this directly to the JS API.
COFFEETALES commentedon Oct 22, 2019
Partial solution in the following commit:
29da5c9
RReverser commentedon Nov 23, 2019
Sounds like @dcodeIO is working on porting #2386 to JS - is that correct?
This API would be extremely useful, as right now there is no way to perform transformations on code anywhere in the module.
bvibber commentedon Dec 23, 2019
The accessors in #2386 have landed, but the npm module doesn't seem to have updated if I install as
npm install binaryen. If I install a nightly build withnpm install binaryen@nightlyit gives a current build which includes them.I did find a bug in the handling of switch destination label names, for which I have a fix in #2553. Otherwise seems to be enough to both list out the functions and walk through the code in the function bodies.
dcodeIO commentedon Dec 23, 2019
For a bit of background: The
latesttag of the npm package updates with newversion_XYtags on the main repo. I guess there hasn't been a tag since the updates, so these features are only available in the nightlies so far.kripken commentedon Dec 23, 2019
I tagged version 90 now.
dcodeIO commentedon Dec 24, 2019
Reporting back from the buildbot: There's a v90.0.0 now with updated definitions and docs, that also ships the (yet undocumented) Wasm variant as
const binaryen = require("binaryen/wasm");. The Wasm variant requires awaiting thebinaryen.readypromise.bvibber commentedon Dec 27, 2019
Awesome, thanks!
I've got most of what I seem to need for iterating over things now on my project, except for iterating through the function table initialization segments. I'll try and put together a patch later adding that if I can work it out.
bvibber commentedon Dec 31, 2019
Also would be useful to iterate over globals; as a workaround I can find all the ones that are referenced in a get/set or an export.
C/JS accessors for function table segments
C/JS accessors for function table segments
C/JS accessors for function table segments
C/JS accessors for function table segments
bvibber commentedon Jan 2, 2020
#2554 does the function table accessor better than the version I posted; closing mine.
RReverser commentedon Feb 29, 2020
It looks like there are APIs for exports, functions and memory segments, but no API to iterate over imports, unless I'm missing something?
bvibber commentedon Mar 1, 2020
@RReverser there's not an iterator specifically for imports, but for imported memories and functions you can iterate over all mems/funcs and check which have an import module & name set.
For imported globals, there's no easy iterator last I checked; in my compiler project I had to find global references while walking through instructions in function bodies.
tlively commentedon Jan 13, 2025
Looks like the features requested here are mostly implemented. Please open new issues for any follow-up feature requests.