Skip to content

Ability to iterate over webassembly contents with JS api #2370

Closed

Description

@hummeleBop

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

Activity

  1. brmorr commented on Oct 13, 2019

    @brmorr
    Contributor

    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.

  2. COFFEETALES commented on Oct 22, 2019

    @COFFEETALES
    Contributor

    Partial solution in the following commit:
    29da5c9

  3. RReverser commented on Nov 23, 2019

    @RReverser
    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.

  4. bvibber commented on Dec 23, 2019

    @bvibber
    Contributor

    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.

  5. dcodeIO commented on Dec 23, 2019

    @dcodeIO
    Contributor

    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.

  6. kripken commented on Dec 23, 2019

    @kripken
    Member

    I tagged version 90 now.

  7. dcodeIO commented on Dec 24, 2019

    @dcodeIO
    Contributor

    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.

  8. bvibber commented on Dec 27, 2019

    @bvibber
    Contributor

    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.

  9. bvibber commented on Dec 31, 2019

    @bvibber
    Contributor

    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.

  10. bvibber commented on Jan 2, 2020

    @bvibber
    Contributor

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

  11. RReverser commented on Feb 29, 2020

    @RReverser
    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?

  12. bvibber commented on Mar 1, 2020

    @bvibber
    Contributor

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

  13. tlively commented on Jan 13, 2025

    @tlively
    Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions