bindings-graalvm241-wasip1

Implementation of WASI Preview 1 host functions for GraalWasm WebAssembly runtime.

Maven Central Version

  • Targets: JVM

Use GraalvmWasiPreview1Builder to install host functions into the GraalVM context.

Usage

Usage example:

const val HELLO_WORLD_MODULE_NAME: String = "helloworld"

// Create Host and run code
EmbedderHost {
fileSystem {
addPreopenedDirectory(".", "/data")
}
}.use(::executeCode)

private fun executeCode(embedderHost: EmbedderHost) {
// Prepare Source
val source = Source.newBuilder("wasm", App::class.java.getResource("helloworld.wasm"))
.name(HELLO_WORLD_MODULE_NAME)
.build()

// Setup Polyglot Context
val context: Context = Context.newBuilder().build()
context.use {
// Context must be initialized before installing modules
context.initialize("wasm")

// Setup WASI Preview 1 module
GraalvmWasiPreview1Builder {
host = embedderHost
}.build(context)

// Evaluate the WebAssembly module
context.eval(source)

// Run code
val startFunction = context.getBindings("wasm").getMember(HELLO_WORLD_MODULE_NAME).getMember("_start")

try {
startFunction.execute()
} catch (re: PolyglotException) {
if (re.message?.startsWith("Program exited with status code") == false) {
throw re
}
Unit
}
}
}

Packages

Link copied to clipboard