Skip to content

A WebAssembly runtime built on Kotlin Multiplatform

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

CharlieTap/chasm

Repository files navigation


chasm logo

badge badge badge badge badge badge


Chasm is an experimental 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

Setup

dependencies {
    implementation("io.github.charlietap.chasm:chasm:0.6.1")
}

Usage

Invoking functions

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")

Imports

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

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)

License

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.