Skip to content

WebAssembly Standalone

Alon Zakai edited this page Nov 7, 2017 · 40 revisions

By default emcc creates a combination of JS files and WebAssembly, where the JS loads the WebAssembly which contains the compiled code. This was designed to support C/C++ programs, and provides a bunch of runtime support code by default that makes sense for that use case. There is also progress towards an option to emit standalone WebAssembly files, that do not include JavaScript or runtime support, which is detailed here.

Status: This is all a work in progress! Things will change and break.

Overview

This approach to creating a standalone WebAssembly module is based on Emscripten's side module concept. A side module makes sense here, since it is a form of dynamic library, and does not link in system libraries automatically, etc. - it is a self-contained compilation output.

Usage

Currently you need the incoming branches in emscripten. master is fine for binaryen.

Build with

emcc [params for your input files, optimization level, etc.] -s WASM=1 -s SIDE_MODULE=1 -o target.wasm

That will emit the output dynamic library as target.wasm.

To use a side module, see loadWebAssemblyModule in src/runtime.js for some example loading code. An explanation of how that works is next.

Details

  • The conventions for a wasm dynamic library are here.
  • Note that there is no special handling of a C stack. A module can have one internally if it wants one (it needs to ask for the memory for it, then handle it however it wants).

Example

Full example.

Clone this wiki locally