forked from bytecodealliance/wasmtime-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
73 lines (63 loc) · 1.73 KB
/
global.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
package wasmtime
// #include "shims.h"
import "C"
import "runtime"
// Global is a global instance, which is the runtime representation of a global variable.
// It holds an individual value and a flag indicating whether it is mutable.
// Read more in [spec](https://webassembly.github.io/spec/core/exec/runtime.html#global-instances)
type Global struct {
val C.wasmtime_global_t
}
// NewGlobal creates a new `Global` in the given `Store` with the specified `ty` and
// initial value `val`.
func NewGlobal(
store Storelike,
ty *GlobalType,
val Val,
) (*Global, error) {
var ret C.wasmtime_global_t
err := C.wasmtime_global_new(
store.Context(),
ty.ptr(),
val.ptr(),
&ret,
)
runtime.KeepAlive(store)
runtime.KeepAlive(ty)
runtime.KeepAlive(val)
if err != nil {
return nil, mkError(err)
}
return mkGlobal(ret), nil
}
func mkGlobal(val C.wasmtime_global_t) *Global {
return &Global{val}
}
// Type returns the type of this global
func (g *Global) Type(store Storelike) *GlobalType {
ptr := C.wasmtime_global_type(store.Context(), &g.val)
runtime.KeepAlive(store)
return mkGlobalType(ptr, nil)
}
// Get gets the value of this global
func (g *Global) Get(store Storelike) Val {
ret := C.wasmtime_val_t{}
C.wasmtime_global_get(store.Context(), &g.val, &ret)
runtime.KeepAlive(store)
return takeVal(&ret)
}
// Set sets the value of this global
func (g *Global) Set(store Storelike, val Val) error {
err := C.wasmtime_global_set(store.Context(), &g.val, val.ptr())
runtime.KeepAlive(store)
runtime.KeepAlive(val)
if err == nil {
return nil
}
return mkError(err)
}
func (g *Global) AsExtern() C.wasmtime_extern_t {
ret := C.wasmtime_extern_t{kind: C.WASMTIME_EXTERN_GLOBAL}
C.go_wasmtime_extern_global_set(&ret, g.val)
return ret
}