-
Notifications
You must be signed in to change notification settings - Fork 1
/
call.go
65 lines (57 loc) · 1.57 KB
/
call.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
package starlet
import (
"context"
"github.com/1set/starlight/convert"
"go.starlark.net/starlark"
)
// Call executes a Starlark function or builtin saved in the thread and returns the result.
func (m *Machine) Call(name string, args ...interface{}) (out interface{}, err error) {
m.mu.Lock()
defer m.mu.Unlock()
defer func() {
if r := recover(); r != nil {
err = errorStarlarkPanic("call", r)
}
}()
// preconditions
if name == "" {
return nil, errorStarletErrorf("call", "no function name")
}
if m.predeclared == nil || m.thread == nil {
return nil, errorStarletErrorf("call", "no function loaded")
}
var callFunc starlark.Callable
if rf, ok := m.predeclared[name]; !ok {
return nil, errorStarletErrorf("call", "no such function: %s", name)
} else if sf, ok := rf.(*starlark.Function); ok {
callFunc = sf
} else if sb, ok := rf.(*starlark.Builtin); ok {
callFunc = sb
} else {
return nil, errorStarletErrorf("call", "mistyped function: %s", name)
}
// convert arguments
sl := starlark.Tuple{}
for _, arg := range args {
sv, err := convert.ToValueWithTag(arg, m.customTag)
if err != nil {
return nil, errorStarlightConvert("args", err)
}
sl = append(sl, sv)
}
// reset thread
m.thread.Uncancel()
m.thread.SetLocal("context", context.TODO())
// call and convert result
res, err := starlark.Call(m.thread, callFunc, sl, nil)
if m.enableOutConv { // convert to interface{} if enabled
out = convert.FromValue(res)
} else {
out = res
}
// handle error
if err != nil {
return out, errorStarlarkError("call", err)
}
return out, nil
}