-
Notifications
You must be signed in to change notification settings - Fork 30
/
func.go
172 lines (121 loc) · 2.91 KB
/
func.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
//go:build js && wasm
// +build js,wasm
package jsbridge
import (
"fmt"
"log"
"reflect"
"syscall/js"
)
// BindFunc bind go func to js func in global
// only support
// - func(...)
// - func(...) error
// - func(...) T
// - func(...) (T,error)
func BindFunc(global js.Value, jsFuncName string, fn interface{}) error {
jsFunc, err := promise(fn)
if err != nil {
return err
}
global.Set(jsFuncName, jsFunc)
return nil
}
func BindAsyncFuncs(global js.Value, fnList map[string]interface{}) {
for jsFuncName, fn := range fnList {
if jsFuncName == "registerAuthorizer" || jsFuncName == "callAuth" {
global.Set(jsFuncName, fn)
} else {
jsFunc, err := promise(fn)
if err != nil {
log.Println("bridge promise failed:", jsFuncName, err)
}
global.Set(jsFuncName, jsFunc)
}
}
}
func BindFuncs(global js.Value, fnList map[string]interface{}) {
for jsFuncName, fn := range fnList {
jsFunc, err := invoke(fn)
if err != nil {
log.Println("[", jsFuncName, "]", err)
continue
}
global.Set(jsFuncName, jsFunc)
}
}
func invoke(fn interface{}) (js.Func, error) {
funcType := reflect.TypeOf(fn)
if funcType.Kind() != reflect.Func {
return js.Func{}, ErrIsNotFunc
}
numOut := funcType.NumOut()
if numOut > 2 {
return js.Func{}, ErrFuncNotSupported
}
syncInvoker, err := Sync(funcType)
if err != nil {
return js.Func{}, err
}
invoker := reflect.ValueOf(fn)
if err != nil {
return js.Func{}, err
}
inputBuilder, err := NewInputBuilder(funcType).Build()
if err != nil {
return js.Func{}, err
}
jsFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer func() {
if r := recover(); r != nil {
fmt.Println("[recover]", r)
}
}()
in, err := inputBuilder(args)
if err != nil {
return NewJsError(err.Error())
}
result := syncInvoker(invoker, in)
return result
})
jsFuncList = append(jsFuncList, jsFunc)
return jsFunc, nil
}
func promise(fn interface{}) (js.Func, error) {
funcType := reflect.TypeOf(fn)
if funcType.Kind() != reflect.Func {
return js.Func{}, ErrIsNotFunc
}
numOut := funcType.NumOut()
if numOut > 2 {
return js.Func{}, ErrFuncNotSupported
}
awaiter, err := Async(funcType)
if err != nil {
return js.Func{}, err
}
inputBuilder, err := NewInputBuilder(funcType).Build()
if err != nil {
return js.Func{}, err
}
invoker := reflect.ValueOf(fn)
jsFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
defer func() {
if r := recover(); r != nil {
fmt.Println("[recover]", r)
}
}()
in, err := inputBuilder(args)
handler := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
resolve := args[0]
reject := args[1]
go awaiter(resolve, reject, invoker, in, err)
return nil
})
jsFuncList = append(jsFuncList, handler)
promise := js.Global().Get("Promise")
return promise.New(handler)
})
jsFuncList = append(jsFuncList, jsFunc)
return jsFunc, nil
}