mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 04:59:38 +08:00
refactored processCall
This commit is contained in:
parent
2c28a8f550
commit
d8c591e64c
@ -163,8 +163,71 @@ func (b *bindingManager) bind(object interface{}) {
|
|||||||
b.objectsToBind = append(b.objectsToBind, object)
|
b.objectsToBind = append(b.objectsToBind, object)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *bindingManager) processFunctionCall(callData *callData) (interface{}, error) {
|
||||||
|
// Return values
|
||||||
|
var result []reflect.Value
|
||||||
|
var err error
|
||||||
|
|
||||||
|
function := b.functions[callData.BindingName]
|
||||||
|
if function == nil {
|
||||||
|
return nil, fmt.Errorf("Invalid function name '%s'", callData.BindingName)
|
||||||
|
}
|
||||||
|
result, err = function.call(callData.Data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do we have an error return type?
|
||||||
|
if function.hasErrorReturnType {
|
||||||
|
// We do - last result is an error type
|
||||||
|
// Check if the last result was nil
|
||||||
|
b.log.Debugf("# of return types: %d", len(function.returnTypes))
|
||||||
|
b.log.Debugf("# of results: %d", len(result))
|
||||||
|
errorResult := result[len(function.returnTypes)-1]
|
||||||
|
if !errorResult.IsNil() {
|
||||||
|
// It wasn't - we have an error
|
||||||
|
return nil, errorResult.Interface().(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result[0].Interface(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *bindingManager) processMethodCall(callData *callData) (interface{}, error) {
|
||||||
|
// Return values
|
||||||
|
var result []reflect.Value
|
||||||
|
var err error
|
||||||
|
|
||||||
|
// do we have this method?
|
||||||
|
method := b.methods[callData.BindingName]
|
||||||
|
if method == nil {
|
||||||
|
return nil, fmt.Errorf("Invalid method name '%s'", callData.BindingName)
|
||||||
|
}
|
||||||
|
|
||||||
|
result, err = method.call(callData.Data)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Do we have an error return type?
|
||||||
|
if method.hasErrorReturnType {
|
||||||
|
// We do - last result is an error type
|
||||||
|
// Check if the last result was nil
|
||||||
|
b.log.Debugf("# of return types: %d", len(method.returnTypes))
|
||||||
|
b.log.Debugf("# of results: %d", len(result))
|
||||||
|
errorResult := result[len(method.returnTypes)-1]
|
||||||
|
if !errorResult.IsNil() {
|
||||||
|
// It wasn't - we have an error
|
||||||
|
return nil, errorResult.Interface().(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if result != nil {
|
||||||
|
return result[0].Interface(), nil
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
// process an incoming call request
|
// process an incoming call request
|
||||||
func (b *bindingManager) processCall(callData *callData) (interface{}, error) {
|
func (b *bindingManager) processCall(callData *callData) (result interface{}, err error) {
|
||||||
b.log.Debugf("Wanting to call %s", callData.BindingName)
|
b.log.Debugf("Wanting to call %s", callData.BindingName)
|
||||||
|
|
||||||
// Determine if this is function call or method call by the number of
|
// Determine if this is function call or method call by the number of
|
||||||
@ -176,13 +239,10 @@ func (b *bindingManager) processCall(callData *callData) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return values
|
|
||||||
var result []reflect.Value
|
|
||||||
var err error
|
|
||||||
|
|
||||||
// We need to catch reflect related panics and return
|
// We need to catch reflect related panics and return
|
||||||
// a decent error message
|
// a decent error message
|
||||||
// TODO: DEBUG THIS!
|
// TODO: DEBUG THIS!
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
err = fmt.Errorf("%s", r.(string))
|
err = fmt.Errorf("%s", r.(string))
|
||||||
@ -191,59 +251,14 @@ func (b *bindingManager) processCall(callData *callData) (interface{}, error) {
|
|||||||
|
|
||||||
switch dotCount {
|
switch dotCount {
|
||||||
case 1:
|
case 1:
|
||||||
function := b.functions[callData.BindingName]
|
result, err = b.processFunctionCall(callData)
|
||||||
if function == nil {
|
|
||||||
return nil, fmt.Errorf("Invalid function name '%s'", callData.BindingName)
|
|
||||||
}
|
|
||||||
result, err = function.call(callData.Data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do we have an error return type?
|
|
||||||
if function.hasErrorReturnType {
|
|
||||||
// We do - last result is an error type
|
|
||||||
// Check if the last result was nil
|
|
||||||
b.log.Debugf("# of return types: %d", len(function.returnTypes))
|
|
||||||
b.log.Debugf("# of results: %d", len(result))
|
|
||||||
errorResult := result[len(function.returnTypes)-1]
|
|
||||||
if !errorResult.IsNil() {
|
|
||||||
// It wasn't - we have an error
|
|
||||||
return nil, errorResult.Interface().(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return result[0].Interface(), nil
|
|
||||||
case 2:
|
case 2:
|
||||||
// do we have this method?
|
result, err = b.processMethodCall(callData)
|
||||||
method := b.methods[callData.BindingName]
|
|
||||||
if method == nil {
|
|
||||||
return nil, fmt.Errorf("Invalid method name '%s'", callData.BindingName)
|
|
||||||
}
|
|
||||||
result, err = method.call(callData.Data)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do we have an error return type?
|
|
||||||
if method.hasErrorReturnType {
|
|
||||||
// We do - last result is an error type
|
|
||||||
// Check if the last result was nil
|
|
||||||
b.log.Debugf("# of return types: %d", len(method.returnTypes))
|
|
||||||
b.log.Debugf("# of results: %d", len(result))
|
|
||||||
errorResult := result[len(method.returnTypes)-1]
|
|
||||||
if !errorResult.IsNil() {
|
|
||||||
// It wasn't - we have an error
|
|
||||||
return nil, errorResult.Interface().(error)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if result != nil {
|
|
||||||
return result[0].Interface(), nil
|
|
||||||
}
|
|
||||||
return nil, nil
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("Invalid binding name '%s'", callData.BindingName)
|
result = nil
|
||||||
|
err = fmt.Errorf("Invalid binding name '%s'", callData.BindingName)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// callWailsInitMethods calls all of the WailsInit methods that were
|
// callWailsInitMethods calls all of the WailsInit methods that were
|
||||||
|
Loading…
Reference in New Issue
Block a user