-
Notifications
You must be signed in to change notification settings - Fork 7
/
proxy.go
57 lines (48 loc) · 921 Bytes
/
proxy.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
package main
import "C"
import (
"sync"
"unsafe"
)
type (
index uint8
store map[index]interface{}
callback struct {
callback interface{}
arg unsafe.Pointer
}
)
var (
storage = make(store)
mutex sync.Mutex
)
//export evenNumberCallbackProxy
func evenNumberCallbackProxy(i C.uint, num int) {
c := getCallback(index(i))
c.callback.(func(int))(num)
}
//export userCallbackProxy
func userCallbackProxy(i C.uint) {
c := getCallback(index(i))
c.callback.(func(unsafe.Pointer))(c.arg)
}
func registerCallback(c interface{}, arg unsafe.Pointer) C.uint {
mutex.Lock()
i := index(len(storage))
storage[i] = &callback{
callback: c,
arg: arg,
}
mutex.Unlock()
return C.uint(i)
}
func getCallback(i index) *callback {
mutex.Lock()
defer mutex.Unlock()
return storage[i].(*callback)
}
func unregisterCallback(i C.uint) {
mutex.Lock()
delete(storage, index(i))
mutex.Unlock()
}