WebAssembly / binaryen

Optimizer and compiler/toolchain library for WebAssembly
Apache License 2.0
7.4k stars 729 forks source link

Dynamic compilation #1636

Open ponyatov opened 6 years ago

ponyatov commented 6 years ago

I asked some here https://www.quora.com/Does-WebAssembly-have-the-ability-to-do-a-dynamic-compilation-in-run-time-using-a-client-s-browser

Is it possible to use binaryen to generate and compile functions in runtime?

I need to generate and compile functions in runtime in mobile phone browser. A user can write himself custom functions definitions, or they can be autogenerated by inference over knowledge database (what I want to make much later). In offline, no server available most of the time.

Closer to the original question I asked about something like Java JSR223 or .NET DLR: mechanics to do dynamic code generation in runtime when an application runs in a client session, and do all the things which can any dynamic programming language with JIT provides.

kripken commented 6 years ago

Yes, with the binaryen.js port, you can generate programs in Binaryen IR and compile them to wasm, all in JS. See for example this code sample from the test suite: https://github.com/WebAssembly/binaryen/blob/master/test/binaryen.js/hello-world.js

If you want to write the programs in C, though, then you need to compile a frontend compiler as well. That's a lot larger, but also an option, see this port: https://github.com/tbfleming/cib