ChicoryEmscriptenHostInstaller

Emscripten / WASI Preview 1 host function installer.

Sets up WebAssembly host imports that provide the Emscripten env and WASI Preview 1 implementations.

To create a new instance, use either ChicoryEmscriptenHostInstaller {} or Builder.

Usage example:

import at.released.weh.host.EmbedderHost
import at.released.weh.bindings.chicory.ChicoryEmscriptenHostInstaller
import at.released.weh.bindings.chicory.ChicoryEmscriptenHostInstaller.ChicoryEmscriptenSetupFinalizer
import com.dylibso.chicory.runtime.HostFunction
import com.dylibso.chicory.runtime.ImportValues
import com.dylibso.chicory.runtime.Instance

// Create Host and run code
val embedderHost = EmbedderHost {
fileSystem {
unrestricted = true
}
}

// Prepare WASI and Emscripten host imports
val installer = ChicoryEmscriptenHostInstaller {
host = embedderHost
}

val wasiFunctions: List<HostFunction> = installer.setupWasiPreview1HostFunctions()
val emscriptenFinalizer: ChicoryEmscriptenSetupFinalizer = installer.setupEmscriptenFunctions()

val hostImports = ImportValues.builder()
.withFunctions(emscriptenFinalizer.emscriptenFunctions + wasiFunctions)
.build()

// Instantiate the WebAssembly module
val instance = Instance
.builder("helloworld.wasm")
.withImportValues(hostImports)
.withInitialize(true)
.withStart(false)
.build()

// Finalize initialization after module instantiation
val emscriptenRuntime = emscriptenFinalizer.finalize(instance)

// Initialize Emscripten runtime environment
emscriptenRuntime.initMainThread()

// Execute code
instance.export("main").apply(
/* argc */0,
/* argv */0,
)[0]

Types

Link copied to clipboard
class Builder
Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun setupWasiPreview1HostFunctions(moduleName: String = WASI_SNAPSHOT_PREVIEW1_MODULE_NAME): List<@JvmSuppressWildcards HostFunction>