blob: e0dd0a74d2345e4bd7a2626b1eca7bd9c30efbc7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
package main
import (
"context"
"fmt"
"os"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
const wasmLocation = "../hello.wasm"
func main() {
var ctx = context.Background()
var runtime = wazero.NewRuntime(ctx)
defer runtime.Close(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, runtime)
var wasm, wasmErr = LoadWasm()
if wasmErr != nil {
panic(fmt.Sprintf("Failed to load WASM module: %s", wasmErr.Error()))
}
var mod, modErr = runtime.Instantiate(ctx, wasm)
if modErr != nil {
panic(fmt.Sprintf("Failed to instantiate WASM module: %s", modErr.Error()))
}
var add = mod.ExportedFunction("Add")
var result, callErr = add.Call(ctx, 25, 25)
if callErr != nil {
panic(fmt.Sprintf("Failed to call WASM finction: %s", callErr.Error()))
}
fmt.Printf("Addition result: %v\n", result[0])
}
func LoadWasm() ([]byte, error) {
return os.ReadFile(wasmLocation)
}
|