-
Notifications
You must be signed in to change notification settings - Fork 2
/
host_functions.go
60 lines (46 loc) · 1.18 KB
/
host_functions.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
package wasify
import (
"context"
)
const WASIFY_NAMESPACE = "wasify"
// hostFunctions is a list of pre-defined host functions
type hostFunctions struct {
moduleConfig *ModuleConfig
}
func newHostFunctions(moduleConfig *ModuleConfig) *hostFunctions {
return &hostFunctions{moduleConfig}
}
// newLog logs data from the guest module to the host machine,
// to avoid stdin/stdout calls and ensure sandboxing.
func (hf *hostFunctions) newLog() *HostFunction {
log := &HostFunction{
Name: "log",
Callback: func(ctx context.Context, m *ModuleProxy, params []PackedData) MultiPackedData {
msg, err := m.Memory.ReadStringPack(params[0])
if err != nil {
panic(err)
}
lvl, err := m.Memory.ReadBytePack(params[0])
if err != nil {
panic(err)
}
severity := LogSeverity(lvl)
switch severity {
case LogDebug:
hf.moduleConfig.log.Debug(msg)
case LogInfo:
hf.moduleConfig.log.Info(msg)
case LogWarning:
hf.moduleConfig.log.Warn(msg)
case LogError:
hf.moduleConfig.log.Error(msg)
}
return 0
},
Params: []ValueType{ValueTypeBytes, ValueTypeBytes},
Results: nil,
// required fields
moduleConfig: hf.moduleConfig,
}
return log
}