bindings-graalvm241-emscripten
Implementation of Emscripten host functions for the GraalWasm JVM WebAssembly runtime integration.
Targets: JVM
Use GraalvmHostFunctionInstaller to install host functions into the GraalVM context.
Usage
Usage example:
// Create Host and run code
EmbedderHost {
fileSystem {
unrestricted = true
}
}.use(::executeCode)
private fun executeCode(embedderHost: EmbedderHost) {
val HELLO_WORLD_MODULE_NAME: String = "helloworld"
// 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 modules
val installer = GraalvmHostFunctionInstaller(context) {
host = embedderHost
}
installer.setupWasiPreview1Module()
val emscriptenInstaller = installer.setupEmscriptenFunctions()
// Evaluate the WebAssembly module
context.eval(source)
// Finish initialization after module instantiation
emscriptenInstaller.finalize(HELLO_WORLD_MODULE_NAME).use { emscriptenEnv ->
// Initialize Emscripten runtime environment
emscriptenEnv.emscriptenRuntime.initMainThread()
// Execute code
val mainFunction = context.getBindings("wasm").getMember(HELLO_WORLD_MODULE_NAME).getMember("main")
mainFunction.execute(
/* argc */ 0,
/* argv */0,
).asInt()
}
}
}
Content copied to clipboard