-
Notifications
You must be signed in to change notification settings - Fork 0
/
capsule.dk.go
101 lines (87 loc) · 2.97 KB
/
capsule.dk.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
// Package capsule SDK for host applications
package capsule
import (
"context"
"errors"
"log"
//"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
func TestMe() string {
return "🦊"
}
const isFailure = rune('F')
const isSuccess = rune('S')
// success appends the isSuccess byte to the beginning of the input buffer and returns the result.
//
// buffer: byte slice to append isSuccess byte to.
// []byte: byte slice with the appended isSuccess byte.
func success(buffer []byte) []byte {
return append([]byte(string(isSuccess)), buffer...)
}
// failure appends a string "isFailure" to the given byte slice buffer and returns the new slice.
//
// buffer: the byte slice to which "isFailure" is appended.
// Returns the new byte slice with the string "isFailure" appended to it.
func failure(buffer []byte) []byte {
return append([]byte(string(isFailure)), buffer...)
}
// Result returns the data without the first byte if the first byte is isSuccess.
// Otherwise, it returns nil and an error with the data starting from the second byte.
//
// data: A byte slice containing the data to check.
// []byte: The data without the first byte if the first byte is isSuccess.
// error: If the first byte is not isSuccess, it returns an error with the data starting from the second byte.
func Result(data []byte,) ([]byte, error) {
if data[0] == byte(isSuccess) {
return data[1:], nil
}
return nil, errors.New(string(data[1:]))
}
// GetHandle returns an exported function named "callHandle" from the given module.
//
// mod: The module to retrieve the function from.
//
// Returns: An exported function with the name "callHandle".
func GetHandle(mod api.Module) api.Function {
return mod.ExportedFunction("callHandle")
}
// GetHandleJSON returns the exported "callHandleJSON" function from the given module.
//
// mod: the module to retrieve the function from.
//
// returns: the exported "callHandleJSON" function.
func GetHandleJSON(mod api.Module) api.Function {
return mod.ExportedFunction("callHandleJSON")
}
// GetHandleHTTP returns the exported 'callHandleHTTP' function from a given module.
//
// mod: The module containing the exported function.
//
// returns:
// - api.Function: the exported 'callHandleHTTP' function.
func GetHandleHTTP(mod api.Module) api.Function {
return mod.ExportedFunction("callHandleHTTP")
}
// CallOnStart calls the OnStart function (if it exists) from the given module.
func CallOnStart(ctx context.Context, mod api.Module , wasmFile []byte) {
onStart := mod.ExportedFunction("OnStart")
if onStart != nil {
_, err := onStart.Call(ctx)
if err != nil {
log.Println("❌ Error calling OnStart", err)
panic(err)
}
}
}
// CallOnStop calls the OnStop function (if it exists) from the given module.
func CallOnStop(ctx context.Context, mod api.Module, wasmFile []byte) {
onStop := mod.ExportedFunction("OnStop")
if onStop != nil {
_, err := onStop.Call(ctx)
if err != nil {
log.Println("❌ Error calling OnStop", err)
panic(err)
}
}
}