-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm_exports.go
More file actions
84 lines (74 loc) · 2.31 KB
/
Copy pathwasm_exports.go
File metadata and controls
84 lines (74 loc) · 2.31 KB
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
//go:build wasip1
// Package plugin — WASM export layer.
//
// This file provides the four exported functions that the paca host runtime
// expects: Init, HandleRequest, HandleEvent, Shutdown, plus malloc/free for
// host-managed memory allocation.
package plugin
// ── Memory management ─────────────────────────────────────────────────────────
// nolint // exported function used by host runtime via WASM interface
//
//go:wasmexport malloc
func malloc(size int32) int32 {
return wasmMalloc(size)
}
// nolint // exported function used by host runtime via WASM interface
//
//go:wasmexport free
func free(_ int32) {}
// ── Exported WASM functions ───────────────────────────────────────────────────
//go:wasmexport Init
func Init() int32 {
if globalDispatcher == nil {
return 1
}
if err := globalDispatcher.init(); err != nil {
return 1
}
return 0
}
//go:wasmexport HandleRequest
func HandleRequest(ptr, length int32) int64 {
if globalDispatcher == nil {
return 0
}
payload := wasmSlice(ptr, length)
result := globalDispatcher.handleRequest(payload)
if len(result) == 0 {
return 0
}
// Allocate space in mallocBuffer for the response
outPtr := wasmMalloc(int32(len(result)))
if outPtr == 0 {
return 0
}
// Copy the result into allocated WASM memory.
out := wasmSlice(outPtr, int32(len(result)))
if len(out) != len(result) {
return 0
}
copy(out, result)
// Return offset and length combined into int64
// NOTE: Host MUST copy out the response before calling ResetAllocator,
// which is called after each HandleRequest completes.
return (int64(outPtr) << 32) | int64(len(result))
}
//go:wasmexport ResetAllocator
func ResetAllocator() {
wasmResetAllocator()
}
//go:wasmexport HandleEvent
func HandleEvent(topicPtr, topicLen, payloadPtr, payloadLen int32) {
if globalDispatcher == nil {
return
}
topic := string(wasmSlice(topicPtr, topicLen))
payload := wasmSlice(payloadPtr, payloadLen)
globalDispatcher.handleEvent(topic, payload)
}
//go:wasmexport Shutdown
func Shutdown() {
if globalDispatcher != nil && globalDispatcher.plugin != nil {
globalDispatcher.plugin.Shutdown()
}
}