Chasm is a WebAssembly runtime built on Kotlin Multiplatform.
The runtime targets the latest wasm specification and supports all instructions with the exception of VectorInstructions.
Additionally, the runtime supports the following Stage 4 proposals
- Tail Call
- Extended Constant Expressions
- Typed Function References
- Wasm GC
- Multiple Memories
dependencies {
implementation("io.github.charlietap.chasm:chasm:0.7.1")
}
Webassembly compilations output a Module encoded as either a .wasm or .wat file, currently chasm supports decoding only .wasm binaries
val wasmFileAsByteArray = ...
val result = module(wasmFileAsByteArray)
Once a module has been decoded you'll need to instantiate it, and for that you'll need also need a store
val store = store()
val result = instance(store, module)
Instances allow you to invoke functions that are exported from the module
val result = invoke(store, instance, "fibonacci")
Modules often depend on imports, which come in two flavours, either:
- Host functions
- Exported functions from other wasm modules
Both are represented by ExternalValue's and can be imported at instantiation time
val import = Import(
"import module name",
"import entity name",
externalValue,
)
val result = instance(store, module, listOf(import))
Host functions are kotlin functions that can be called by wasm programs at runtime. The majority of host functions represent system calls, WASI for example is intended to be integrated as imports of host functions.
Allocation of a host function requires a FunctionType
The function type describes the inputs and outputs of your function so the runtime can call it and use its results
Once you have function type you can allocate the host function like so
val functionType = FunctionType(ResultType(emptyList()), ResultType(emptyList()))
val hostFunction: HostFunction = {
println("Hello world")
emptyList()
}
val result = function(store, funcType, hostFunction)
This project is dual-licensed under both the MIT and Apache 2.0 licenses. You can choose which one you want to use the software under.
- For details on the MIT license, please see the LICENSE-MIT file.
- For details on the Apache 2.0 license, please see the LICENSE-APACHE file.