binaryen.js icon indicating copy to clipboard operation
binaryen.js copied to clipboard

Add wasm-metadce build

Open RReverser opened this issue 5 years ago • 7 comments

As the original announcement for wasm-metadce stated, it could be used for shrinking size of Wasm in JS bundlers. /cc @kripken @surma

However, right now there are no JS/Wasm builds of wasm-metadce, and the only place it's used in remains inside Emscripten.

Could we build metadce as a JS library as part of binaryen.js bots?

RReverser avatar Dec 04 '20 12:12 RReverser

Could we build metadce as a JS library as part of binaryen.js bots?

I think so. Iirc Binaryen's build scripts already support building most, if not all, of the tools to JS so these can be run with node. While we are on it we may also investigate wasm-as, wasm-dis and wasm2js perhaps, if these seem useful to npm install. Doing something similar for the WABT binaries btw.

dcodeIO avatar Dec 04 '20 13:12 dcodeIO

Yeah that sounds good. Although, aside from installing with node, I think important part of this is ability to run this tool as a library. For that, we'd need to also add a JS interface similarly to what binaryen.js does.

RReverser avatar Dec 04 '20 13:12 RReverser

Perhaps one option requiring little effort could be to support importing the tools in JS and call their main function as an API (providing custom stdout/stderr streams). Not as sophisticated as a proper library, but perhaps would already be helpful?

dcodeIO avatar Dec 04 '20 16:12 dcodeIO

@dcodeIO I think that would be good. A generic way to turn a commandline tool into a library would let us get a lot of stuff working fast. The API might not be perfect but it would be a start to build a nicer API on top of. And doing it that way would avoid the need to refactor the metadce code into a library, etc.

I'm not sure exactly how that would work, but it seems like it could even be a generic option in upstream emscripten perhaps. But we can experiment with it in binaryen maybe, and see how it goes.

kripken avatar Dec 04 '20 16:12 kripken

I'm not sure exactly how that would work, but it seems like it could even be a generic option in upstream emscripten perhaps.

Sounds good! Iirc this is typically done by checking whether the executing module is the main module, quite similar to how one would do that in Python with __name__ == '__main__', and unconditionally export the main function. Something along the lines of:

function main(argv, options) {
  ...
  return code;
}

exports.main = main;

if (require.main === module) {
  process.exit(
    main(process.argv.slice(2), {
      stdout: process.stdout,
      stderr: process.stder
    })
  );
}

dcodeIO avatar Dec 04 '20 16:12 dcodeIO

I like this idea, yeah. Just one note: it should support passing stdin as well, as most JS build tools don't produce intermediate files and having to create those just for Binaryen tools would be not very ergonomic.

RReverser avatar Dec 04 '20 17:12 RReverser

I've meanwhile added wasm2js, but held back on wasm-metadce as I am unsure how useful it would be without a programmatic API as discussed above. Let me know if you see a use case where it would be sufficient to just have the command.

dcodeIO avatar Oct 28 '21 06:10 dcodeIO

There is a metadce build now.

dcodeIO avatar May 09 '24 10:05 dcodeIO