-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.go
122 lines (98 loc) · 3.39 KB
/
module.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
package wasify
import (
"context"
"log/slog"
)
type Module interface {
Close(ctx context.Context) error
GuestFunction(ctx context.Context, functionName string) GuestFunction
Memory() Memory
}
type ModuleProxy struct {
Memory Memory
}
type GuestFunction interface {
Invoke(args ...any) (*GuestFunctionResult, error)
call(args ...uint64) (uint64, error)
}
type Memory interface {
ReadBytes(offset uint32, size uint32) ([]byte, error)
ReadByte(offset uint32) (byte, error)
ReadUint32(offset uint32) (uint32, error)
ReadUint64(offset uint32) (uint64, error)
ReadFloat32(offset uint32) (float32, error)
ReadFloat64(offset uint32) (float64, error)
ReadString(offset uint32, size uint32) (string, error)
ReadAnyPack(pd PackedData) (any, uint32, uint32, error)
ReadBytesPack(pd PackedData) ([]byte, error)
ReadBytePack(pd PackedData) (byte, error)
ReadUint32Pack(pd PackedData) (uint32, error)
ReadUint64Pack(pd PackedData) (uint64, error)
ReadFloat32Pack(pd PackedData) (float32, error)
ReadFloat64Pack(pd PackedData) (float64, error)
ReadStringPack(pd PackedData) (string, error)
WriteAny(offset uint32, v any) error
WriteBytes(offset uint32, v []byte) error
WriteByte(offset uint32, v byte) error
WriteUint32(offset uint32, v uint32) error
WriteUint64(offset uint32, v uint64) error
WriteFloat32(offset uint32, v float32) error
WriteFloat64(offset uint32, v float64) error
WriteString(offset uint32, v string) error
WriteBytesPack(v []byte) PackedData
WriteBytePack(v byte) PackedData
WriteUint32Pack(v uint32) PackedData
WriteUint64Pack(v uint64) PackedData
WriteFloat32Pack(v float32) PackedData
WriteFloat64Pack(v float64) PackedData
WriteStringPack(v string) PackedData
WriteMultiPack(...PackedData) MultiPackedData
FreePack(...PackedData) error
Free(...uint32) error
Size() uint32
Malloc(size uint32) (uint32, error)
}
type ModuleConfig struct {
// Module Namespace. Required.
Namespace string
// FSConfig configures a directory to be pre-opened for access by the WASI module if Enabled is set to true.
// If GuestDir is not provided, the default guest directory will be "/".
// Note: If FSConfig is not provided or Enabled is false, the directory will not be attached to WASI.
FSConfig FSConfig
// WASM configuration. Required.
Wasm Wasm
// List of host functions to be registered.
HostFunctions []HostFunction
// Set the severity level for a particular module's logs.
// Note: If LogSeverity isn't specified, the severity is inherited from the parent, like the runtime log severity.
LogSeverity LogSeverity
// Struct members for internal use.
ctx context.Context
log *slog.Logger
}
// Wasm configures a new wasm file.
// Binay is required.
// Hash is optional.
type Wasm struct {
Binary []byte
Hash string
}
// FSConfig configures a directory to be pre-opened for access by the WASI module if Enabled is set to true.
// If GuestDir is not provided, the default guest directory will be "/".
// Note: If FSConfig is not provided or Enabled is false, the directory will not be attached to WASI.
type FSConfig struct {
// Whether to Enabled the directory for WASI access.
Enabled bool
// The directory on the host system.
// Default: "/"
HostDir string
// The directory accessible to the WASI module.
GuestDir string
}
// getGuestDir gets the default path for guest module.
func (fs *FSConfig) getGuestDir() string {
if fs.GuestDir == "" {
return "/"
}
return fs.GuestDir
}