-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bindings.go
380 lines (326 loc) · 9.89 KB
/
bindings.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package application
import (
"context"
"encoding/json"
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"github.com/wailsapp/wails/v3/internal/hash"
"github.com/samber/lo"
)
type CallOptions struct {
MethodID uint32 `json:"methodID"`
MethodName string `json:"methodName"`
Args []json.RawMessage `json:"args"`
}
type PluginCallOptions struct {
Name string `json:"name"`
Args []json.RawMessage `json:"args"`
}
var reservedPluginMethods = []string{
"Name",
"Init",
"Shutdown",
"Exported",
}
// Parameter defines a Go method parameter
type Parameter struct {
Name string `json:"name,omitempty"`
TypeName string `json:"type"`
ReflectType reflect.Type
}
func newParameter(Name string, Type reflect.Type) *Parameter {
return &Parameter{
Name: Name,
TypeName: Type.String(),
ReflectType: Type,
}
}
// IsType returns true if the given
func (p *Parameter) IsType(typename string) bool {
return p.TypeName == typename
}
// IsError returns true if the parameter type is an error
func (p *Parameter) IsError() bool {
return p.IsType("error")
}
// BoundMethod defines all the data related to a Go method that is
// bound to the Wails application
type BoundMethod struct {
ID uint32 `json:"id"`
Name string `json:"name"`
Inputs []*Parameter `json:"inputs,omitempty"`
Outputs []*Parameter `json:"outputs,omitempty"`
Comments string `json:"comments,omitempty"`
Method reflect.Value `json:"-"`
TypeName string
PackagePath string
needsContext bool
}
type Bindings struct {
boundMethods map[string]*BoundMethod
boundByID map[uint32]*BoundMethod
methodAliases map[uint32]uint32
}
func NewBindings(instances []Service, aliases map[uint32]uint32) (*Bindings, error) {
b := &Bindings{
boundMethods: make(map[string]*BoundMethod),
boundByID: make(map[uint32]*BoundMethod),
methodAliases: aliases,
}
for _, binding := range instances {
err := b.Add(binding.Instance())
if err != nil {
return nil, err
}
}
return b, nil
}
// Add the given named type pointer methods to the Bindings
func (b *Bindings) Add(namedPtr interface{}) error {
methods, err := b.getMethods(namedPtr, false)
if err != nil {
return fmt.Errorf("cannot bind value to app: %s", err.Error())
}
for _, method := range methods {
// Add it as a regular method
b.boundMethods[method.String()] = method
b.boundByID[method.ID] = method
}
return nil
}
func (b *Bindings) AddPlugins(plugins map[string]Plugin) error {
for pluginID, plugin := range plugins {
methods, err := b.getMethods(plugin, true)
if err != nil {
return fmt.Errorf("cannot add plugin '%s' to app: %s", pluginID, err.Error())
}
exportedMethods := plugin.CallableByJS()
for _, method := range methods {
// Do not expose reserved methods
if lo.Contains(reservedPluginMethods, method.Name) {
continue
}
// Do not expose methods that are not in the exported list
if !lo.Contains(exportedMethods, method.Name) {
continue
}
// Add it as a regular method
b.boundMethods[fmt.Sprintf("wails-plugins.%s.%s", pluginID, method.Name)] = method
b.boundByID[method.ID] = method
globalApplication.debug("Added plugin method: "+pluginID+"."+method.Name, "id", method.ID)
}
}
return nil
}
// Get returns the bound method with the given name
func (b *Bindings) Get(options *CallOptions) *BoundMethod {
method, ok := b.boundMethods[options.MethodName]
if !ok {
return nil
}
return method
}
// GetByID returns the bound method with the given ID
func (b *Bindings) GetByID(id uint32) *BoundMethod {
// Check method aliases
if b.methodAliases != nil {
if alias, ok := b.methodAliases[id]; ok {
id = alias
}
}
result := b.boundByID[id]
return result
}
// GenerateID generates a unique ID for a binding
func (b *Bindings) GenerateID(name string) (uint32, error) {
id, err := hash.Fnv(name)
if err != nil {
return 0, err
}
// Check if we already have it
boundMethod, ok := b.boundByID[id]
if ok {
return 0, fmt.Errorf("oh wow, we're sorry about this! Amazingly, a hash collision was detected for method '%s' (it generates the same hash as '%s'). To continue, please rename it. Sorry :(", name, boundMethod.String())
}
return id, nil
}
func (b *BoundMethod) String() string {
return fmt.Sprintf("%s.%s.%s", b.PackagePath, b.TypeName, b.Name)
}
func (b *Bindings) getMethods(value interface{}, isPlugin bool) ([]*BoundMethod, error) {
// Create result placeholder
var result []*BoundMethod
// Check type
if !isNamed(value) {
if isFunction(value) {
name := runtime.FuncForPC(reflect.ValueOf(value).Pointer()).Name()
return nil, fmt.Errorf("%s is a function, not a pointer to named type. Wails v2 has deprecated the binding of functions. Please define your functions as methods on a struct and bind a pointer to that struct", name)
}
return nil, fmt.Errorf("%s is not a pointer to named type", reflect.ValueOf(value).Type().String())
} else if !isPtr(value) {
return nil, fmt.Errorf("%s is a named type, not a pointer to named type", reflect.ValueOf(value).Type().String())
}
// Process Named Type
namedValue := reflect.ValueOf(value)
ptrType := namedValue.Type()
namedType := ptrType.Elem()
typeName := namedType.Name()
packagePath := namedType.PkgPath()
if strings.Contains(namedType.String(), "[") {
return nil, fmt.Errorf("%s.%s is a generic type. Generic bound types are not supported", packagePath, namedType.String())
}
ctxType := reflect.TypeFor[context.Context]()
// Process Methods
for i := 0; i < ptrType.NumMethod(); i++ {
methodDef := ptrType.Method(i)
methodName := methodDef.Name
method := namedValue.MethodByName(methodName)
// Create new method
boundMethod := &BoundMethod{
Name: methodName,
PackagePath: packagePath,
TypeName: typeName,
Inputs: nil,
Outputs: nil,
Comments: "",
Method: method,
}
var err error
boundMethod.ID, err = b.GenerateID(boundMethod.String())
if err != nil {
return nil, err
}
if !isPlugin {
args := []any{"name", boundMethod, "id", boundMethod.ID}
if b.methodAliases != nil {
alias, found := lo.FindKey(b.methodAliases, boundMethod.ID)
if found {
args = append(args, "alias", alias)
}
}
globalApplication.debug("Adding method:", args...)
}
// Iterate inputs
methodType := method.Type()
inputParamCount := methodType.NumIn()
var inputs []*Parameter
for inputIndex := 0; inputIndex < inputParamCount; inputIndex++ {
input := methodType.In(inputIndex)
if inputIndex == 0 && input.AssignableTo(ctxType) {
boundMethod.needsContext = true
}
thisParam := newParameter("", input)
inputs = append(inputs, thisParam)
}
boundMethod.Inputs = inputs
outputParamCount := methodType.NumOut()
var outputs []*Parameter
for outputIndex := 0; outputIndex < outputParamCount; outputIndex++ {
output := methodType.Out(outputIndex)
thisParam := newParameter("", output)
outputs = append(outputs, thisParam)
}
boundMethod.Outputs = outputs
// Save method in result
result = append(result, boundMethod)
}
return result, nil
}
var errorType = reflect.TypeFor[error]()
// Call will attempt to call this bound method with the given args
func (b *BoundMethod) Call(ctx context.Context, args []json.RawMessage) (returnValue interface{}, err error) {
// Use a defer statement to capture panics
defer func() {
if r := recover(); r != nil {
if str, ok := r.(string); ok {
if strings.HasPrefix(str, "reflect: Call using") {
// Remove prefix
str = strings.Replace(str, "reflect: Call using ", "", 1)
// Split on "as"
parts := strings.Split(str, " as type ")
if len(parts) == 2 {
err = fmt.Errorf("invalid argument type: got '%s', expected '%s'", parts[0], parts[1])
return
}
}
}
err = fmt.Errorf("%v", r)
}
}()
argCount := len(args)
if b.needsContext {
argCount++
}
if argCount != len(b.Inputs) {
err = fmt.Errorf("%s expects %d arguments, received %d", b.Name, len(b.Inputs), argCount)
return
}
// Convert inputs to values of appropriate type
callArgs := make([]reflect.Value, argCount)
base := 0
if b.needsContext {
callArgs[0] = reflect.ValueOf(ctx)
base++
}
// Iterate over given arguments
for index, arg := range args {
value := reflect.New(b.Inputs[base+index].ReflectType)
err = json.Unmarshal(arg, value.Interface())
if err != nil {
err = fmt.Errorf("could not parse argument #%d: %w", index, err)
return
}
callArgs[base+index] = value.Elem()
}
// Do the call
var callResults []reflect.Value
if b.Method.Type().IsVariadic() {
callResults = b.Method.CallSlice(callArgs)
} else {
callResults = b.Method.Call(callArgs)
}
var nonErrorOutputs = make([]any, 0, len(callResults))
var errorOutputs []error
for _, result := range callResults {
if result.Type() == errorType {
if result.IsNil() {
continue
}
if errorOutputs == nil {
errorOutputs = make([]error, 0, len(callResults)-len(nonErrorOutputs))
nonErrorOutputs = nil
}
errorOutputs = append(errorOutputs, result.Interface().(error))
} else if nonErrorOutputs != nil {
nonErrorOutputs = append(nonErrorOutputs, result.Interface())
}
}
if errorOutputs != nil {
err = errors.Join(errorOutputs...)
} else if len(nonErrorOutputs) == 1 {
returnValue = nonErrorOutputs[0]
} else if len(nonErrorOutputs) > 1 {
returnValue = nonErrorOutputs
}
return
}
// isPtr returns true if the value given is a pointer.
func isPtr(value interface{}) bool {
return reflect.ValueOf(value).Kind() == reflect.Ptr
}
// isFunction returns true if the given value is a function
func isFunction(value interface{}) bool {
return reflect.ValueOf(value).Kind() == reflect.Func
}
// isNamed returns true if the given value is of named type
// or pointer to named type.
func isNamed(value interface{}) bool {
rv := reflect.ValueOf(value)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
return rv.Type().Name() != ""
}