This project demonstrates how to use a stateful C++ object, foo.h/cpp, as a stateful JavaScript object, FooWasm.js, that runs in a web application using WebAssembly.
- Install emscripten: https://emscripten.org/docs/getting_started/downloads.html
source <emscripten_path>/emsdk/emsdk_env.sh
./bin/build-emcpp.sh
(builds WasmSample_WASM.js and WasmSample_WASM.wasm to the ./build directory)- Copy ./build/WasmSample_WASM.js and ./build/WasmSample_WASM.wasm to ./src
./bin/start_dev_browser
- Open http://localhost:9100/ in a browser
Note: the server used in ./bin_start_dev_browser
returns the wrong mime-type which can cause this application to not work on some browsers. To user a different server that returns the correct mime-type, run emrun --port 8080 ./browser_build/
instead of ./bin/start_dev_browser
and open http://localhost:8080/browser_build/ in a browser.
The full stack from the stateful C++ Foo object to the browser is:
- foo.h/cpp
- foo_bridge.h/cpp provides stateless wrappers which use pointer reference to interact with instances
- index.cpp registers emscripten bindings
- Build WasmSample_WASM.js/.wasm using
./bin/build-emcpp.sh
- FooWasm.js encapsulates WasmSample_WASM module for instance implementation
- index.js requires("WasmSample_WASM") and constructs a FooWasm.js
- Webpack script creates
./browser_build
with index.html, wasm-sample-bundle.js, and WasmSample_WASM.wasm and starts a dev server using./bin/start_dev_browser
- Browser loads and executes application