This project demonstrates how to compile a C function (subtraction) into WebAssembly using Emscripten's MODULARIZE
method and invoke it from a Node.js script.
-
Clone the Repository
git clone https://github.com/iamswethaa/wasm-modularize.git
-
Install Emscripten
-
Clone the Emscripten SDK repository
git clone https://github.com/emscripten-core/emsdk.git cd emsdk
-
Install and activate the latest version
./emsdk install latest ./emsdk activate latest
-
Set up the environment variables
./emsdk_env.sh
-
Compile C to WebAssembly
cd .. emcc sub.c -s WASM=1 -s MODULARIZE=1 -s ENVIRONMENT=node -s EXPORTED_FUNCTIONS='["_sub"]' -s EXPORTED_RUNTIME_METHODS='["ccall"]' -o sub.js
This command produce two files :
- sub.js - which is your JavScript Glue Code
- sub.wasm - which is your compiled WebAssembly module
-
Run the Script
node main.js