-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun.go
340 lines (299 loc) · 9.66 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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package starlet
import (
"context"
"fmt"
"io/fs"
"sync"
"time"
"github.com/1set/starlet/lib/goidiomatic"
"github.com/1set/starlight/convert"
"go.starlark.net/repl"
"go.starlark.net/starlark"
"go.starlark.net/syntax"
)
// REPL is a Read-Eval-Print-Loop for Starlark.
// It loads the predeclared symbols and modules into the global environment,
func (m *Machine) REPL() {
if err := m.prepareThread(nil); err != nil {
repl.PrintError(err)
return
}
repl.REPLOptions(m.getFileOptions(), m.thread, m.predeclared)
}
// RunScript initiates a Machine, executes a script with extra variables, and returns the Machine and the execution result.
func RunScript(content []byte, extras StringAnyMap) (*Machine, StringAnyMap, error) {
m := NewDefault()
res, err := m.RunScript(content, extras)
return m, res, err
}
// RunFile initiates a Machine, executes a script from a file with extra variables, and returns the Machine and the execution result.
func RunFile(name string, fileSys fs.FS, extras StringAnyMap) (*Machine, StringAnyMap, error) {
m := NewDefault()
res, err := m.RunFile(name, fileSys, extras)
return m, res, err
}
// RunTrustedScript initiates a Machine, executes a script with all builtin modules loaded and extra variables, returns the Machine and the result.
// Use with caution as it allows script access to file system and network.
func RunTrustedScript(content []byte, globals, extras StringAnyMap) (*Machine, StringAnyMap, error) {
m := NewWithBuiltins(globals, nil, nil)
res, err := m.RunScript(content, extras)
return m, res, err
}
// RunTrustedFile initiates a Machine, executes a script from a file with all builtin modules loaded and extra variables, returns the Machine and the result.
// Use with caution as it allows script access to file system and network.
func RunTrustedFile(name string, fileSys fs.FS, globals, extras StringAnyMap) (*Machine, StringAnyMap, error) {
m := NewWithBuiltins(globals, nil, nil)
res, err := m.RunFile(name, fileSys, extras)
return m, res, err
}
// Run executes a preset script and returns the output.
func (m *Machine) Run() (StringAnyMap, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.runInternal(context.Background(), nil, true)
}
// RunScript executes a script with additional variables, which take precedence over global variables and modules, returns the result.
func (m *Machine) RunScript(content []byte, extras StringAnyMap) (StringAnyMap, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.scriptName = "direct.star"
m.scriptContent = content
m.scriptFS = nil
return m.runInternal(context.Background(), extras, false)
}
// RunFile executes a script from a file with additional variables, which take precedence over global variables and modules, returns the result.
func (m *Machine) RunFile(name string, fileSys fs.FS, extras StringAnyMap) (StringAnyMap, error) {
m.mu.Lock()
defer m.mu.Unlock()
m.scriptName = name
m.scriptContent = nil
m.scriptFS = fileSys
return m.runInternal(context.Background(), extras, true)
}
// RunWithTimeout executes a preset script with a timeout and additional variables, which take precedence over global variables and modules, returns the result.
func (m *Machine) RunWithTimeout(timeout time.Duration, extras StringAnyMap) (StringAnyMap, error) {
m.mu.Lock()
defer m.mu.Unlock()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return m.runInternal(ctx, extras, true)
}
// RunWithContext executes a preset script within a specified context and additional variables, which take precedence over global variables and modules, returns the result.
func (m *Machine) RunWithContext(ctx context.Context, extras StringAnyMap) (StringAnyMap, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.runInternal(ctx, extras, true)
}
func (m *Machine) runInternal(ctx context.Context, extras StringAnyMap, allowCache bool) (out StringAnyMap, err error) {
defer func() {
if r := recover(); r != nil {
err = errorStarlarkPanic("exec", r)
}
}()
// either script content or name and FS must be set
var (
scriptName = m.scriptName
source interface{}
)
if m.scriptContent != nil {
if scriptName == "" {
// for default name, and disable cache to avoid conflict
scriptName = "eval.star"
allowCache = false
}
source = m.scriptContent
} else if m.scriptFS != nil {
if scriptName == "" {
// if no name, cannot load
return nil, errorStarletErrorf("run", "no script name")
}
// load script from FS
rd, e := m.scriptFS.Open(scriptName)
if e != nil {
return nil, errorStarletError("run", e)
}
source = rd
} else {
return nil, errorStarletErrorf("run", "no script to execute")
}
// prepare thread
if err = m.prepareThread(extras); err != nil {
return nil, err
}
// cancel thread when context cancelled
if ctx == nil || ctx.Err() != nil {
// for nil context, or context already cancelled, use a new one
ctx = context.TODO()
}
m.thread.SetLocal("context", ctx)
// wait for the routine to finish, or cancel it when context cancelled
var wg sync.WaitGroup
defer wg.Wait()
wg.Add(1)
// if context is not cancelled, cancel the routine when execution is done, or panic
done := make(chan struct{}, 1)
defer close(done)
go func() {
defer wg.Done()
select {
case <-ctx.Done():
m.thread.Cancel("context cancelled")
case <-done:
// No action if the script has finished
}
}()
// run with everything prepared
m.runTimes++
res, err := m.execStarlarkFile(scriptName, source, allowCache)
done <- struct{}{}
// merge result as predeclared for next run
for k, v := range res {
m.predeclared[k] = v
}
// handle result and convert
out = m.convertOutput(res)
if err != nil {
// for exit code
if err.Error() == goidiomatic.ErrSystemExit.Error() {
var exitCode uint8
if c := m.thread.Local("exit_code"); c != nil {
if co, ok := c.(uint8); ok {
exitCode = co
}
}
// exit code 0 means success
if exitCode == 0 {
err = nil
} else {
err = errorStarletErrorf("run", "exit code: %d", exitCode)
}
} else {
// wrap starlark errors
err = errorStarlarkError("exec", err)
}
return out, err
}
return out, nil
}
// prepareThread prepares the thread for execution, including preset globals, preload modules and extras.
func (m *Machine) prepareThread(extras StringAnyMap) (err error) {
mergeExtra := func() error {
// no extras
if extras == nil {
return nil
}
// convert extras if needed
esd, err := m.convertInput(extras)
if err != nil {
return errorStarlightConvert("extras", err)
}
// merge extras
for k, v := range esd {
m.predeclared[k] = v
}
return nil
}
// initialize thread or reset for each run
if m.thread == nil {
// -- for the first run
// preset globals + preload modules + extras -> predeclared
if m.predeclared, err = m.convertInput(m.globals); err != nil {
return errorStarlightConvert("globals", err)
}
if err = m.preloadMods.LoadAll(m.predeclared); err != nil {
return errorStarletError("preload", err)
}
// merge extras into predeclared
if err = mergeExtra(); err != nil {
return err
}
// cache load&read + printf -> thread
m.loadCache = &cache{
cache: make(map[string]*entry),
execOpts: m.getFileOptions(),
loadMod: m.lazyloadMods.GetLazyLoader(),
readFile: func(name string) ([]byte, error) {
return readScriptFile(name, m.scriptFS)
},
globals: m.predeclared,
}
m.thread = &starlark.Thread{
Name: "starlet",
Print: m.printFunc,
Load: func(thread *starlark.Thread, module string) (starlark.StringDict, error) {
return m.loadCache.Load(module)
},
}
} else {
// -- for the second and following runs
// merge extras into predeclared
if err = mergeExtra(); err != nil {
return err
}
// set globals for cache
m.loadCache.loadMod = m.lazyloadMods.GetLazyLoader()
m.loadCache.globals = m.predeclared
// reset for each run
m.thread.Print = m.printFunc
m.thread.Uncancel()
}
return nil
}
// Reset resets the machine to initial state before the first run.
// Attention: It does not reset the compiled program cache.
func (m *Machine) Reset() {
m.runTimes = 0
m.thread = nil
m.loadCache = nil
m.predeclared = nil
}
// convertInput converts a StringAnyMap to a starlark.StringDict, usually for output variable.
func (m *Machine) convertInput(a StringAnyMap) (starlark.StringDict, error) {
if m.enableInConv {
return convert.MakeStringDictWithTag(a, m.customTag)
}
return castStringAnyMapToStringDict(a)
}
// convertOutput converts a starlark.StringDict to a StringAnyMap, usually for output variable.
func (m *Machine) convertOutput(d starlark.StringDict) StringAnyMap {
if m.enableOutConv {
return convert.FromStringDict(d)
}
return castStringDictToAnyMap(d)
}
// getFileOptions gets the exec options from the config.
func (m *Machine) getFileOptions() *syntax.FileOptions {
opt := syntax.FileOptions{
Set: true,
}
if m.allowRecursion {
opt.Recursion = true
}
if m.allowGlobalReassign {
opt.GlobalReassign = true
opt.TopLevelControl = true
opt.While = true
}
return &opt
}
// castStringDictToAnyMap converts a starlark.StringDict to a StringAnyMap without any Starlight conversion.
func castStringDictToAnyMap(m starlark.StringDict) StringAnyMap {
ret := make(StringAnyMap, len(m))
for k, v := range m {
ret[k] = v
}
return ret
}
// castStringAnyMapToStringDict converts a StringAnyMap to a starlark.StringDict without any Starlight conversion.
// It fails if any values are not starlark.Value.
func castStringAnyMapToStringDict(m StringAnyMap) (starlark.StringDict, error) {
ret := make(starlark.StringDict, len(m))
for k, v := range m {
sv, ok := v.(starlark.Value)
if !ok {
return nil, fmt.Errorf("value of key %q is not a starlark.Value", k)
}
ret[k] = sv
}
return ret, nil
}