You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I wonder if Otto could have a little wrapper function that would convert a function() to a go func(), regardless of parameters. I think this could be a problem with go's static nature and that there is nothing like generics, though (Reflect's call should still make this possible, I presume.)... Also probably doesn't help that JS has no function parameter and return value typing. But couldn't one just assume that it will be alright and panic if it doesn't fit?
Go:
func someevent(f func(e EvArgs)) { // code }
// give to JavaScript in some way, like set()
js:
someevent(function(e) { // do }})
EDIT: I forgot Otto does part of this, it does call arbitrary go functions with arbitrary Parameters, the function "bridge" is the only part missing.
The text was updated successfully, but these errors were encountered:
Though destfunc comes out as an empty map, not as the actual go function... Anyone know something? in.Argument(0) is function () { [native code] } from the JS perspective.
I think my problem is that I want the original go value back...
EDIT: Going the "long" way by always having a type with the methods on won't make the methods unusable... I managed to create a function wrapper (error prone, no error checking in this preliminary version!)
func cf(in otto.FunctionCall) otto.Value {
destobj, _ := in.Argument(0).Export()
destobjrefl := reflect.ValueOf(destobj)
destfunc, _ := destobjrefl.Type().MethodByName(in.Argument(1).String())
destfuncrefl := destfunc.Func
destfuncreflt := destfunc.Type
arglist := make([]reflect.Value, 0, 0)
arglist = append(arglist, destobjrefl)
for x := 1; x < destfuncreflt.NumIn() ; x++ {
if destfuncreflt.In(x).Kind() == reflect.Func {
y := x
f := reflect.MakeFunc(destfuncreflt.In(x), func (args []reflect.Value) []reflect.Value {
funcargs := make([]interface{}, 0, 0)
for _, v := range args {
val, _ := js.ToValue(v.Interface())
funcargs = append(funcargs, val)
}
outargs := make([]reflect.Value, 0)
outarg, err := in.Argument(1+y).Call(otto.UndefinedValue(), funcargs...)
fmt.Println(in.Argument(1+y), err)
outarggo, _ := outarg.Export()
if !outarg.IsUndefined(){
outargs = append(outargs, reflect.ValueOf(outarggo))
}
return outargs
})
arglist = append(arglist, f)
} else {
i, _ := in.Argument(1+x).Export()
y := reflect.ValueOf(i)
arglist = append(arglist, y)
}
}
fmt.Println(len(arglist))
ret := destfuncrefl.Call(arglist)
retnorm := make([]interface{}, 0, 0)
for _, val := range ret {
retnorm = append(retnorm, val.Interface())
}
ret2, _ := js.ToValue(retnorm)
return ret2
}
First parameter is the value the function is on, second parameter is the Name of the function, third parameter and on are all parameters the function expects. If any parameter has the wrong type as it arrives in Go-land, it will panic since I didn't put any error checking in... It will also panic if a parameter was omitted. More like a proof of concept that needs work, I know, but might be a cool start.
I wonder if Otto could have a little wrapper function that would convert a function() to a go func(), regardless of parameters. I think this could be a problem with go's static nature and that there is nothing like generics, though (Reflect's call should still make this possible, I presume.)... Also probably doesn't help that JS has no function parameter and return value typing. But couldn't one just assume that it will be alright and panic if it doesn't fit?
Go:
js:
EDIT: I forgot Otto does part of this, it does call arbitrary go functions with arbitrary Parameters, the function "bridge" is the only part missing.
The text was updated successfully, but these errors were encountered: