-
Notifications
You must be signed in to change notification settings - Fork 3
Description
Why
Currently, the Wasm modules generated by scala-wasm
depend on helper functions in JavaScript, making it possible to run them on JavaScript engines like V8, but not on standalone Wasm runtimes such as wasmtime
or wasmedge
.
While WebAssembly was initially designed to run in web browsers, it has recently been adopted in various environments due to its portability, fast loading and execution times, and security benefits.
For example, WebAssembly is being used in:
- Plugins/Extensions (Envoy, UDF in TiDB)
- Serverless/FaaS (Fastly, Fermyon Spin, Cloudflare workers)
- Containers/k8s (runwasi, containerd-shims)
- Cloud Apps (wasm-cloud, Wasm Worker Server)
- Smart Contracts (Ethereum Wasm, Near Protocol)
(I personally believe the significant benefit of compiling Scala to WebAssembly lies in the ability to run it in these environments (as well as the Component Model))
However, to leverage WebAssembly in these environments, the binaries need to be executable on standalone Wasm runtimes, which is not possible with the current state of the generated binaries.
How
To achieve this, two major tasks are required (and the latter will likely be a significant challenge):
- (1) Support
WASI (WebAssembly System Interface)
(at least WASI preview 1 for now). This would enable access to the file system and other resources, regardless of the host environment.- For more information on WASI, please refer to the short blog post I wrote on gist
- (2) Remove dependencies on JavaScript. (if it's targeted to pure-WASI)
- Implementations of strings, closures, and other features currently rely on JavaScript, but these would need to be implemented in pure WebAssembly.
- Math functions and RegExp implementations would also need to be handled on the WebAssembly side. Could the implementations from Scala Native be reused somehow? (It seems that the
Kotlin/Wasm
RegExp engine reuses the implementation from Kotlin/Native.) - Any JavaScript dependencies, including
js.Dynamic
, would become unavailable (some of them might be replaceable with WASI?) - Third-party libraries that depend on JavaScript might need to be re-implemented for standalone WebAssembly targets. (It should be considered whether to have separate builds for standalone Wasm or introduce annotations to switch implementations at link time, similar to Scala Native.)
State of WasmGC support on standalone Wasm runtimes
- ✅ wasmedge 0.14.0
- 🚧 wasmtime
- 🚧 wamr
- 🤔 wasmer