-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgo-func.go
46 lines (40 loc) · 1.11 KB
/
go-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
package epy
import (
elutils "github.com/rosbit/go-embedding-utils"
"go.starlark.net/starlark"
)
func bindGoFunc(name string, funcVar interface{}) (goFunc *starlark.Builtin, err error) {
helper, e := elutils.NewGolangFuncHelper(funcVar, name)
if e != nil {
err = e
return
}
goFunc = starlark.NewBuiltin(helper.GetRealName(), wrapGoFunc(helper))
return
}
func wrapGoFunc(helper *elutils.GolangFuncHelper) func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
return func(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (val starlark.Value, err error) {
getArgs := func(i int) interface{} {
return fromValue(args.Index(i))
}
v, e := helper.CallGolangFunc(args.Len(), b.Name(), getArgs)
if e != nil {
err = e
return
}
if v == nil {
val = starlark.None
return
}
if vv, ok := v.([]interface{}); ok {
retV := make([]starlark.Value, len(vv))
for i, rv := range vv {
retV[i] = toValue(rv)
}
val = starlark.Tuple(retV)
} else {
val = toValue(v)
}
return
}
}