-
Notifications
You must be signed in to change notification settings - Fork 2
/
run.go
233 lines (185 loc) · 6.15 KB
/
run.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
package runtime
import (
"bytes"
"context"
"fmt"
"maps"
"path/filepath"
"strings"
"go.starlark.net/starlark"
"go.starlark.net/starlarktest"
"go.autokitteh.dev/autokitteh/internal/kittehs"
"go.autokitteh.dev/autokitteh/runtimes/starlarkrt/internal/bootstrap"
"go.autokitteh.dev/autokitteh/runtimes/starlarkrt/internal/libs"
"go.autokitteh.dev/autokitteh/runtimes/starlarkrt/internal/tls"
"go.autokitteh.dev/autokitteh/runtimes/starlarkrt/internal/values"
"go.autokitteh.dev/autokitteh/sdk/sdkerrors"
"go.autokitteh.dev/autokitteh/sdk/sdkservices"
"go.autokitteh.dev/autokitteh/sdk/sdktypes"
)
// TODO: https://linear.app/autokitteh/issue/ENG-241/decide-how-to-fix-pr-250
// const testsGlobalName = "tests"
type run struct {
runID sdktypes.RunID
compiled map[string][]byte
vctx *values.Context
cbs *sdkservices.RunCallbacks
// globals returns from initial evaluation of the script.
globals starlark.StringDict
// same as globals, but as autokitteh Values.
exports map[string]sdktypes.Value
}
func (r *run) ID() sdktypes.RunID { return r.runID }
func Run(
ctx context.Context,
runID sdktypes.RunID,
mainPath string,
compiled map[string][]byte,
givenValues map[string]sdktypes.Value,
cbs *sdkservices.RunCallbacks,
) (sdkservices.Run, error) {
prog, err := getProgram(compiled, mainPath)
if err != nil {
return nil, fmt.Errorf("invalid compiled program: %w", err)
}
if prog == nil {
return nil, fmt.Errorf("not found: %q", mainPath)
}
vctx := &values.Context{Call: cbs.SafeCall, RunID: runID}
givens, err := kittehs.TransformMapValuesError(givenValues, vctx.ToStarlarkValue)
if err != nil {
return nil, fmt.Errorf("converting values to starlark: %w", err)
}
// preload all modules
libs := libs.LoadModules(int64(kittehs.HashString64(runID.String())))
// order matters here - builtins are the least important. givens can override them.
// then we have the starlib libs, which can override both.
predeclared := maps.Clone(builtins)
maps.Copy(predeclared, givens)
maps.Copy(predeclared, libs)
th := &starlark.Thread{
Name: mainPath,
Print: func(_ *starlark.Thread, text string) { cbs.SafePrint(ctx, runID, text) },
Load: func(th *starlark.Thread, path string) (starlark.StringDict, error) {
if len(th.CallStack()) > 0 {
// Path is relative to the file that called load.
pos := th.CallFrame(0).Pos.Filename()
path = filepath.Join(filepath.Dir(pos), path)
}
prog, err := getProgram(compiled, path)
if err != nil {
return nil, err
}
if prog == nil {
globals, err := cbs.SafeLoad(ctx, runID, path)
if err != nil {
return nil, err
}
if globals == nil {
return nil, sdkerrors.ErrNotFound
}
return kittehs.TransformMapValuesError(globals, vctx.ToStarlarkValue)
}
return prog.Init(th, predeclared)
},
}
vctx.SetTLS(th)
tls.Set(th, &tls.Context{
GoCtx: ctx,
RunID: runID,
Callbacks: cbs,
})
var errorReporter errorReporter
starlarktest.SetReporter(th, &errorReporter)
bootstrappedValues, err := bootstrap.Run(th, predeclared)
if err != nil {
return nil, translateError(err, nil)
}
bootstrappedValues = kittehs.FilterMapKeys(bootstrappedValues, func(s string) bool {
return s[0] != '_'
})
// We treat the bootstrap exported values as predeclared values in the actual program.
maps.Copy(predeclared, bootstrappedValues)
globals, err := prog.Init(th, predeclared)
if err != nil {
// TODO: multierror.
return nil, translateError(err, nil)
}
if len(errorReporter.errs) > 0 {
// TODO(ENG-196): make program errors.
return nil, fmt.Errorf("%s", errorReporter.Report())
}
globals = kittehs.FilterMapKeys(globals, func(s string) bool {
return !strings.HasPrefix(s, "_")
})
// TODO: https://linear.app/autokitteh/issue/ENG-241/decide-how-to-fix-pr-250
// if _, ok := globals[testsGlobalName]; !ok {
// globals[testsGlobalName] = predeclared[testsGlobalName]
// }
exports, err := kittehs.TransformMapValuesError(globals, vctx.FromStarlarkValue)
if err != nil {
return nil, fmt.Errorf("converting values from starlark: %w", err)
}
return &run{runID: runID, compiled: compiled, exports: exports, globals: globals, vctx: vctx, cbs: cbs}, nil
}
func (r *run) Call(ctx context.Context, v sdktypes.Value, args []sdktypes.Value, kwargs map[string]sdktypes.Value) (sdktypes.Value, error) {
fv, err := r.vctx.ToStarlarkValue(v)
if err != nil {
return sdktypes.InvalidValue, err
}
th := &starlark.Thread{
Name: v.GetFunction().UniqueID(),
Print: func(_ *starlark.Thread, text string) { r.cbs.SafePrint(ctx, r.runID, text) },
}
r.vctx.SetTLS(th)
tls.Set(th, &tls.Context{
GoCtx: ctx,
RunID: r.runID,
Globals: r.globals,
Callbacks: r.cbs,
})
var errorReporter errorReporter
starlarktest.SetReporter(th, &errorReporter)
slargs, err := kittehs.TransformError(args, r.vctx.ToStarlarkValue)
if err != nil {
return sdktypes.InvalidValue, fmt.Errorf("args transform: %w", err)
}
slkwargs := make([]starlark.Tuple, 0, len(kwargs))
for k, v := range kwargs {
slv, err := r.vctx.ToStarlarkValue(v)
if err != nil {
return sdktypes.InvalidValue, fmt.Errorf("kwarg with key %q transform: %w", k, err)
}
slkwargs = append(slkwargs, starlark.Tuple([]starlark.Value{
starlark.String(k),
slv,
}))
}
slretv, err := starlark.Call(th, fv, slargs, slkwargs)
if err != nil {
return sdktypes.InvalidValue, translateError(err, nil)
}
if len(errorReporter.errs) > 0 {
// TODO(ENG-196): make program errors.
return sdktypes.InvalidValue, fmt.Errorf("%s", errorReporter.Report())
}
retv, err := r.vctx.FromStarlarkValue(slretv)
if err != nil {
return sdktypes.InvalidValue, fmt.Errorf("return value transform: %w", err)
}
return retv, nil
}
func (r *run) Values() map[string]sdktypes.Value { return r.exports }
func (r *run) ExecutorID() sdktypes.ExecutorID { return sdktypes.NewExecutorID(r.runID) }
func (r *run) Close() {}
func getProgram(compiled map[string][]byte, path string) (*starlark.Program, error) {
data := compiled[path]
if data == nil {
return nil, nil
}
prog, err := starlark.CompiledProgram(bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("invalid compiled program: %w", err)
}
return prog, nil
}