Skip to content

Ability to iterate over webassembly contents with JS api #2370

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

Closed
hummeleBop opened this issue Oct 7, 2019 · 13 comments
Closed

Ability to iterate over webassembly contents with JS api #2370

hummeleBop opened this issue Oct 7, 2019 · 13 comments

Comments

@hummeleBop
Copy link

hummeleBop commented Oct 7, 2019

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. ]

@brmorr
Copy link
Contributor

brmorr commented 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
Copy link
Contributor

Partial solution in the following commit:
29da5c9

@RReverser
Copy link
Member

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
Copy link
Contributor

bvibber commented 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 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.

@dcodeIO
Copy link
Contributor

dcodeIO commented Dec 23, 2019

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.

@kripken
Copy link
Member

kripken commented Dec 23, 2019

I tagged version 90 now.

@dcodeIO
Copy link
Contributor

dcodeIO commented 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 the binaryen.ready promise.

@bvibber
Copy link
Contributor

bvibber commented 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
Copy link
Contributor

bvibber commented 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.

bvibber added a commit to bvibber/binaryen that referenced this issue Jan 1, 2020
module.getNumFunctionTableSegments() gives the number of segments.

module.getFunctionTableSegmentInfoByIndex() yields:
```
{
  offset,
  functions: ["func1", "func2"]
}
```

Another piece for WebAssembly#2370
bvibber added a commit to bvibber/binaryen that referenced this issue Jan 1, 2020
module.getNumFunctionTableSegments() gives the number of segments.

module.getFunctionTableSegmentInfoByIndex() yields:
```
{
  offset,
  functions: ["func1", "func2"]
}
```

Another piece for WebAssembly#2370
bvibber added a commit to bvibber/binaryen that referenced this issue Jan 1, 2020
module.getNumFunctionTableSegments() gives the number of segments.

module.getFunctionTableSegmentInfoByIndex() yields:
```
{
  offset,
  functions: ["func1", "func2"]
}
```

Another piece for WebAssembly#2370
bvibber added a commit to bvibber/binaryen that referenced this issue Jan 1, 2020
module.getNumFunctionTableSegments() gives the number of segments.

module.getFunctionTableSegmentInfoByIndex() yields:
```
{
  offset,
  functions: ["func1", "func2"]
}
```

Another piece for WebAssembly#2370
@bvibber
Copy link
Contributor

bvibber commented Jan 2, 2020

#2554 does the function table accessor better than the version I posted; closing mine.

@RReverser
Copy link
Member

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
Copy link
Contributor

bvibber commented 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
Copy link
Member

tlively commented Jan 13, 2025

Looks like the features requested here are mostly implemented. Please open new issues for any follow-up feature requests.

@tlively tlively closed this as completed Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants