dispatch.mx raw
1 //:build wasm
2
3 package callback
4
5 import (
6 "runtime"
7 "unsafe"
8 )
9
10 // drainCompactions runs at every JS->wasm entry point. On wasm there is no
11 // event loop of our own - each browser callback is a separate wasm export
12 // invocation, and returning to JS is the only point at which no Moxie frame is
13 // live. That makes this the wasm counterpart of the relay's epoll yield: the
14 // one safe place to compact sovereign arenas that have reached their pressure
15 // ratchet. One group per entry, so a compaction cannot stall a callback.
16 func drainCompactions() {
17 runtime.SovDrainCompactions(1)
18 }
19
20 //:wasmexport __cb0
21 func Dispatch0(id int32) {
22 drainCompactions()
23 f, ok := table.Lookup0(id)
24 if !ok {
25 return
26 }
27 f()
28 }
29
30 //:wasmexport __cbs
31 func DispatchS(id int32, ptr *byte, length int32) {
32 drainCompactions()
33 s := ptrToString(ptr, length)
34 f, ok := table.LookupS(id)
35 if !ok {
36 return
37 }
38 f(s)
39 }
40
41 //:wasmexport __cbb
42 func DispatchB(id int32, val int32) {
43 drainCompactions()
44 f, ok := table.LookupB(id)
45 if !ok {
46 return
47 }
48 f(val != 0)
49 }
50
51 //:wasmexport __cbi
52 func DispatchI(id int32, val int32) {
53 drainCompactions()
54 f, ok := table.LookupI(id)
55 if !ok {
56 return
57 }
58 f(val)
59 }
60
61 //:wasmexport __cbis
62 func DispatchIS(id int32, ival int32, ptr *byte, length int32) {
63 drainCompactions()
64 s := ptrToString(ptr, length)
65 f, ok := table.LookupIS(id)
66 if !ok {
67 return
68 }
69 f(ival, s)
70 }
71
72 //:wasmexport __cbiis
73 func DispatchIIS(id int32, a int32, b int32, ptr *byte, length int32) {
74 drainCompactions()
75 s := ptrToString(ptr, length)
76 f, ok := table.LookupIIS(id)
77 if !ok {
78 return
79 }
80 f(a, b, s)
81 }
82
83 //:wasmexport __cbss
84 func DispatchSS(id int32, ptr1 *byte, len1 int32, ptr2 *byte, len2 int32) {
85 drainCompactions()
86 s1 := ptrToString(ptr1, len1)
87 s2 := ptrToString(ptr2, len2)
88 f, ok := table.LookupSS(id)
89 if !ok {
90 return
91 }
92 f(s1, s2)
93 }
94
95 //:wasmexport __cb6i
96 func Dispatch6I(id int32, a int32, b int32, c int32, d int32, e int32, f int32) {
97 drainCompactions()
98 cb, ok := table.Lookup6I(id)
99 if !ok {
100 return
101 }
102 cb(a, b, c, d, e, f)
103 }
104
105 //:wasmexport __cbii
106 func DispatchII(id int32, a int32, b int32) {
107 drainCompactions()
108 f, ok := table.LookupII(id)
109 if !ok {
110 return
111 }
112 f(a, b)
113 }
114
115 //:wasmexport __cbiii
116 func DispatchIII(id int32, a int32, b int32, c int32) {
117 drainCompactions()
118 f, ok := table.LookupIII(id)
119 if !ok {
120 return
121 }
122 f(a, b, c)
123 }
124
125 //:wasmexport __cbdata
126 func DispatchBytes(id int32, ptr *byte, length int32) {
127 drainCompactions()
128 if length <= 0 {
129 f, ok := table.LookupBytes(id)
130 if !ok {
131 return
132 }
133 f(nil)
134 return
135 }
136 b := ptrToBytes(ptr, length)
137 f2, ok2 := table.LookupBytes(id)
138 if !ok2 {
139 return
140 }
141 f2(b)
142 }
143
144 //:wasmexport __cb_release
145 func DispatchRelease(id int32) {
146 drainCompactions()
147 table.Release(id)
148 }
149
150 func ptrToBytes(ptr *byte, length int32) (buf []byte) {
151 if ptr == nil || length <= 0 {
152 return nil
153 }
154 return unsafe.Slice(ptr, length)
155 }
156
157 func ptrToString(ptr *byte, length int32) (s string) {
158 if ptr == nil || length <= 0 {
159 return ""
160 }
161 return unsafe.String(ptr, length)
162 }
163