I am new to this space so bear with me. I am experimenting with WASM/WIT a bit and want to build a host program in go and use wasmtime and components in various languages starting with python.
So i created a WIT.like this.
package example:reconciler@0.1.0;
world reconciler {
import reconcile: func(input: string) -> string;
}
create a python component
import reconciler
import json
class Reconciler(reconciler.reconciler):
def reconcile(self, input_json: str) -> str:
# Parse the input JSON
data = json.loads(input_json)
# Perform reconciliation logic
result = {
"status": "success",
"processed_items": len(data.get("items", []))
}
# Return the result as a JSON string
return json.dumps(result)
and generated a wasm file using the following command
componentize-py --wit-path wit --world reconciler bindings .
componentize-py --wit-path wit --world reconciler componentize reconciler -o reconciler.wasm
now the part that fails is the go program when i try to run the wasm file i get the error here.
package main
import (
"encoding/json"
"fmt"
"github.com/bytecodealliance/wasmtime-go"
)
func main() {
// Load the WebAssembly module
engine := wasmtime.NewEngine()
store := wasmtime.NewStore(engine)
wasiConfig := wasmtime.NewWasiConfig()
store.SetWasi(wasiConfig)
module, err := wasmtime.NewModuleFromFile(engine, "reconciler.wasm")
if err != nil {
panic(err)
}
}
error:
(.venv) (base) wasm/reconciler > go run main.go
panic: failed to parse WebAssembly module
Caused by:
Invalid input WebAssembly code at offset 4: unknown binary version
goroutine 1 [running]:
main.main()
/Users/henderiw/code/wasm/reconciler/main.go:20 +0x2b0
exit status 2
when i inspect the reconciler.wasm file i wasm-tools validate seems ok
(.venv) (base) wasm/reconciler > wasm-tools validate reconciler.wasm
(.venv) (base) wasm/reconciler > xxd reconciler.wasm | head -n 1
00000000: 0061 736d 0d00 0100 0756 0142 0a01 6f02 .asm.....V.B..o.
(.venv) (base) wasm/reconciler > wasm-tools component wit reconciler.wasm
package root:component;
world root {
import wasi:cli/environment@0.2.0;
import wasi:cli/exit@0.2.0;
import wasi:io/error@0.2.0;
import wasi:io/poll@0.2.0;
import wasi:io/streams@0.2.0;
import wasi:cli/stdin@0.2.0;
import wasi:cli/stdout@0.2.0;
import wasi:cli/stderr@0.2.0;
import wasi:cli/terminal-input@0.2.0;
import wasi:cli/terminal-output@0.2.0;
import wasi:cli/terminal-stdin@0.2.0;
import wasi:cli/terminal-stdout@0.2.0;
import wasi:cli/terminal-stderr@0.2.0;
import wasi:clocks/monotonic-clock@0.2.0;
import wasi:clocks/wall-clock@0.2.0;
import wasi:filesystem/types@0.2.0;
import wasi:filesystem/preopens@0.2.0;
import wasi:sockets/network@0.2.0;
import wasi:sockets/instance-network@0.2.0;
import wasi:sockets/udp@0.2.0;
import wasi:sockets/udp-create-socket@0.2.0;
import wasi:sockets/tcp@0.2.0;
import wasi:sockets/tcp-create-socket@0.2.0;
import wasi:sockets/ip-name-lookup@0.2.0;
import wasi:random/random@0.2.0;
import wasi:random/insecure@0.2.0;
import wasi:random/insecure-seed@0.2.0;
import reconcile: func(input: string) -> string;
export exports: interface {
record bundled {
module: string,
protocol: string,
name: string,
}
...
Not sure what i am doing wrong here. So looking for advise
WebAssembly "modules" are different from "components". componentize-py
produces a component but your Go code is attempting to load it as a module (wasmtime.NewModuleFromFile
). I am not very familiar with wasmtime-go but I believe it does not yet support loading components.
ok that would explain. I was trying to find something for components but did not find anything hence i used the Module.
Is something in the working for components in wasmtime? or should we use another host runtime for this?
Wasmtime is currently the best host runtime option for components, its just that the non-Rust language bindings lag behind feature-wise.
The only other relevant component implementation I am aware of is jco
which adapts components to run in JS environments (e.g. Node, browsers) but it comes with a number of caveats compared to Wasmtime.
Alright thx for the pointer.
Last updated: Dec 23 2024 at 13:07 UTC