Skip to content

Instantly share code, notes, and snippets.

@illarionov
Created March 8, 2025 05:12
Show Gist options
  • Select an option

  • Save illarionov/789a8fa685a105da71f1f0b2ce6f8e96 to your computer and use it in GitHub Desktop.

Select an option

Save illarionov/789a8fa685a105da71f1f0b2ce6f8e96 to your computer and use it in GitHub Desktop.
Chicory + Emscripten + own imports
/*
* Copyright 2024-2025, the wasi-emscripten-host project authors and contributors. Please see the AUTHORS file
* for details. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
* SPDX-License-Identifier: Apache-2.0
*/
@file:Suppress("CommentWrapping")
package at.released.weh.sample.chicory.gradle.app
import at.released.weh.bindings.chicory.ChicoryEmscriptenHostInstaller
import at.released.weh.bindings.chicory.ChicoryEmscriptenHostInstaller.ChicoryEmscriptenSetupFinalizer
import at.released.weh.host.EmbedderHost
import com.dylibso.chicory.runtime.HostFunction
import com.dylibso.chicory.runtime.ImportFunction
import com.dylibso.chicory.runtime.ImportValues
import com.dylibso.chicory.runtime.Instance
import com.dylibso.chicory.wasm.Parser
import com.dylibso.chicory.wasm.types.ValueType
fun main() {
// Create Host and run code
EmbedderHost {
fileSystem {
unrestricted = true
}
}.use(::executeCode)
}
private fun executeCode(embedderHost: EmbedderHost) {
// 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 = ImportValues.builder()
.withFunctions(emscriptenFinalizer.emscriptenFunctions + wasiFunctions)
.addFunction(
ImportFunction(
"env",
"invoke_i",
listOf(ValueType.I32),
listOf(ValueType.I32)
) { instance, args ->
longArrayOf(0L)
}
)
.build()
// Load WebAssembly binary
val wasmModule = Thread.currentThread().contextClassLoader.getResourceAsStream("postgres.wasm")
.use(Parser::parse)
// Instantiate the WebAssembly module
val instance = Instance
.builder(wasmModule)
.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]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment