-
Notifications
You must be signed in to change notification settings - Fork 7
/
wazergo.go
126 lines (99 loc) · 3.88 KB
/
wazergo.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package wasi
import (
"encoding/binary"
"fmt"
"io"
"unsafe"
"github.com/stealthrocket/wazergo/types"
"github.com/stealthrocket/wazergo/wasm"
"github.com/tetratelabs/wazero/api"
)
func (f FDStat) ObjectSize() int { return int(unsafe.Sizeof(FDStat{})) }
func (f FDStat) LoadObject(_ api.Memory, b []byte) FDStat { return unsafeLoad[FDStat](b) }
func (f FDStat) StoreObject(_ api.Memory, b []byte) { unsafeStore(b, f) }
func (f FDStat) FormatObject(w io.Writer, _ api.Memory, b []byte) { formatObject(w, b, f) }
func (f FileStat) ObjectSize() int { return int(unsafe.Sizeof(FileStat{})) }
func (f FileStat) LoadObject(_ api.Memory, b []byte) FileStat { return unsafeLoad[FileStat](b) }
func (f FileStat) StoreObject(_ api.Memory, b []byte) { unsafeStore(b, f) }
func (f FileStat) FormatObject(w io.Writer, _ api.Memory, b []byte) { formatObject(w, b, f) }
func (p PreStat) ObjectSize() int { return int(unsafe.Sizeof(PreStat{})) }
func (p PreStat) LoadObject(_ api.Memory, b []byte) PreStat { return unsafeLoad[PreStat](b) }
func (p PreStat) StoreObject(_ api.Memory, b []byte) { unsafeStore(b, p) }
func (p PreStat) FormatObject(w io.Writer, _ api.Memory, b []byte) { formatObject(w, b, p) }
func (e Event) ObjectSize() int { return int(unsafe.Sizeof(Event{})) }
func (e Event) LoadObject(_ api.Memory, b []byte) Event { return unsafeLoad[Event](b) }
func (e Event) StoreObject(_ api.Memory, b []byte) { unsafeStore(b, e) }
func (e Event) FormatObject(w io.Writer, _ api.Memory, b []byte) { formatObject(w, b, e) }
func (s Subscription) ObjectSize() int {
return int(unsafe.Sizeof(Subscription{}))
}
func (s Subscription) LoadObject(_ api.Memory, b []byte) Subscription {
return unsafeLoad[Subscription](b)
}
func (s Subscription) StoreObject(_ api.Memory, b []byte) {
unsafeStore(b, s)
}
func (s Subscription) FormatObject(w io.Writer, m api.Memory, b []byte) {
s = s.LoadObject(m, b)
fmt.Fprintf(w, `{UserData:%#016x,EventType:%s,`, s.UserData, s.EventType)
switch s.EventType {
case ClockEvent:
io.WriteString(w, `SubscriptionClock:`)
s.GetClock().Format(w)
case FDReadEvent, FDWriteEvent:
io.WriteString(w, `SubscriptionFDReadWrite:`)
s.GetFDReadWrite().Format(w)
default:
fmt.Fprintf(w, `SubscriptionU:%x`, s.variant)
}
fmt.Fprintf(w, `}`)
}
func (arg IOVec) ObjectSize() int {
return 8
}
func (arg IOVec) LoadObject(memory api.Memory, object []byte) IOVec {
offset := binary.LittleEndian.Uint32(object[:4])
length := binary.LittleEndian.Uint32(object[4:])
return wasm.Read(memory, offset, length)
}
func (arg IOVec) StoreObject(memory api.Memory, object []byte) {
panic("BUG: i/o vectors cannot be stored back to wasm memory")
}
func (arg IOVec) FormatObject(w io.Writer, memory api.Memory, object []byte) {
types.Bytes(arg.LoadObject(memory, object)).Format(w)
}
func formatObject[T types.Object[T]](w io.Writer, object []byte, typ T) {
types.Format(w, typ.LoadObject(nil, object))
}
func unsafeStore[T types.Object[T]](b []byte, t T) {
types.UnsafeStoreObject(b, t)
}
func unsafeLoad[T types.Object[T]](b []byte) T {
return types.UnsafeLoadObject[T](b)
}
func (t Timestamp) Format(w io.Writer) {
io.WriteString(w, t.String())
}
func (c SubscriptionFDReadWrite) Format(w io.Writer) {
fmt.Fprintf(w, `{FD:%d}`, c.FD)
}
func (c SubscriptionClock) Format(w io.Writer) {
var formatTimeout func(Timestamp) string
switch c.Flags {
case Abstime:
formatTimeout = Timestamp.String
default:
formatTimeout = func(t Timestamp) string {
return t.Duration().String()
}
}
fmt.Fprintf(w, `{ID:%s,Timeout:%s,Precision:%s}`,
c.ID,
formatTimeout(c.Timeout),
c.Precision.Duration().String(),
)
}
var (
_ types.Formatter = Timestamp(0)
_ types.Formatter = SubscriptionClock{}
)