This repository has been archived by the owner on Nov 19, 2022. It is now read-only.
forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample_serialize_test.go
103 lines (88 loc) · 2.46 KB
/
example_serialize_test.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Small example of how to serialize a compiled wasm module, and then
// instantiate it from the compilation artifacts.
package wasmtime_test
import (
"fmt"
"github.com/bytecodealliance/wasmtime-go"
)
func serialize() []byte {
// Configure the initial compilation environment.
fmt.Println("Initializing...")
engine := wasmtime.NewEngine()
// Compile the wasm module into an in-memory instance of a `Module`.
fmt.Println("Compiling module...")
wasm, err := wasmtime.Wat2Wasm(`
(module
(func $hello (import "" "hello"))
(func (export "run") (call $hello))
)`)
if err != nil {
panic(err)
}
module, err := wasmtime.NewModule(engine, wasm)
if err != nil {
panic(err)
}
serialized, err := module.Serialize()
if err != nil {
panic(err)
}
fmt.Println("Serialized.")
return serialized
}
func deserialize(encoded []byte) {
// Configure the initial compilation environment.
fmt.Println("Initializing...")
store := wasmtime.NewStore(wasmtime.NewEngine())
// Deserialize the compiled module.
fmt.Println("Deserialize module...")
module, err := wasmtime.NewModuleDeserialize(store.Engine, encoded)
if err != nil {
panic(err)
}
// Here we handle the imports of the module, which in this case is our
// `helloFunc` callback.
fmt.Println("Creating callback...")
helloFunc := wasmtime.WrapFunc(store, func() {
fmt.Println("Calling back...")
fmt.Println("> Hello World!")
})
// Once we've got that all set up we can then move to the instantiation
// phase, pairing together a compiled module as well as a set of imports.
// Note that this is where the wasm `start` function, if any, would run.
fmt.Println("Instantiating module...")
instance, err := wasmtime.NewInstance(store, module, []wasmtime.AsExtern{helloFunc})
if err != nil {
panic(err)
}
// Next we poke around a bit to extract the `run` function from the module.
fmt.Println("Extracting export...")
run := instance.GetFunc(store, "run")
if run == nil {
panic("Failed to find function export `run`")
}
// And last but not least we can call it!
fmt.Println("Calling export...")
_, err = run.Call(store)
if err != nil {
panic(err)
}
fmt.Println("Done.")
}
func Example_serialize() {
bytes := serialize()
deserialize(bytes)
// Output:
// Initializing...
// Compiling module...
// Serialized.
// Initializing...
// Deserialize module...
// Creating callback...
// Instantiating module...
// Extracting export...
// Calling export...
// Calling back...
// > Hello World!
// Done.
}