This is a POC of how we can use WASM to compile some math functions in RDK and use them client-side in apps.
The important processes are:
- Creating a wrapper Go entry in
main.goto pass JS → Go → JS - A
wasm.tsfile which handles initializing the WASM instance and exposes wrappers around the WASM execution (to ensure WASM is ready, the function exists in WASM, and the values won't cause the function to panic) - In
+page.sveltewe can see how we are then able to call the function as if it were a function declared in TypeScript
-
Build the WASM binary:
GOOS=js GOARCH=wasm go build -o main.wasm
-
Copy the WASM JavaScript runtime:
cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" .
-
Create a wrapper main to export all the functions
-
Load the WASM instance and it will auto-inject our functions
-
The spatial math repo has an import to viam utils which cannot be compiled to WASM:
../../../go/pkg/mod/go.viam.com/utils@v0.1.176/runtime.go:38:24: undefined: syscall.SIGPIPE ../../../go/pkg/mod/go.viam.com/utils@v0.1.176/runtime_posix.go:12:33: undefined: syscall.SIGUSR1We could probably refactor a lot of the functions we need out or mark files as not to be compiled for WASM in RDK, but this will require significant work.
- Try to make
useWasma hook - Assess what actual functions we need to call for our P0 scope