I'm stuck with how to load a C++ function in a React code, using wasm compiler.
My C++ is composed of two files, which result after compilation in a 160kb wasm file. Here is the command I currently use for compilation (running on macOS).
em++ ../main.cpp ../stringFormat.cpp -s WASM=1 -s EXPORT_ALL=1 -s MODULARIZE=1 -O3 --closure 1 -o lss.js -std=c++11
I then copy the lss and the wasm file together in my React code, in the same folder.
src
- utils
- wasm
lss.js
lss.wasm
But then, whenever I try to import lss.js in another file, my app crashes with a bunch of undefined expressions.
My js file
import * as lss from '../wasm/lss'
./src/utils/wasm/lss.js
Line 10:6: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 11:69: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 11:117: 'read' is not defined no-undef
Line 11:197: 'readbuffer' is not defined no-undef
Line 11:214: 'read' is not defined no-undef
Line 11:336: 'quit' is not defined no-undef
Line 11:367: Unexpected use of 'print' no-restricted-globals
Line 11:430: Unexpected use of 'print' no-restricted-globals
Line 11:493: 'printErr' is not defined no-undef
Line 12:1: Unexpected use of 'print' no-restricted-globals
Line 12:22: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 12:26: Unexpected use of 'self' no-restricted-globals
Line 14:307: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 23:174: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 29:10: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 29:152: 'readline' is not defined no-undef
Line 29:260: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 29:350: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 29:433: Expected an assignment or function call and instead saw an expression no-unused-expressions
Line 30:19: Expected an assignment or function call and instead saw an expression no-unused-expressions
...
I also tried to generate standalone wasm file by adding a SIDE_MODULE=1 flag on compilation.
// My util function to load wasm file to js
const loadWebAssembly = (filename, imports = {}) =>
WebAssembly
.instantiateStreaming(fetch(filename), imports)
.then(({instance}) => instance.exports);
// wasm file is now in my static folder, in public/
const lss = loadWebAssembly('wasm/lss.wasm')
// splitIntoCommonSequences is the C++ function I try to call
.then(module => Promise.resolve(module.splitIntoCommonSequences));
export {lss};
But then I got another error.
WebAssembly.instantiate(): Import #0 module="env" error: module is not an object or function
I tried to figure out how to declare a proper import object for my situation without any success. Is their any better solution ? How do I know what to put inside my import object ?
Thanks !