This repository was archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathscript.go
More file actions
468 lines (402 loc) · 11.8 KB
/
Copy pathscript.go
File metadata and controls
468 lines (402 loc) · 11.8 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// A modified version of Tengo's script compilation and run implementation.
// https://github.com/d5/tengo
package tengox
import (
"context"
"errors"
"fmt"
"io"
"sync"
"github.com/d5/tengo/v2"
"github.com/d5/tengo/v2/parser"
)
const reservedVar = "$out"
// Script is used to call callable Tengo objects from Go.
// Methods are derived from Tengo's Script type. Unlike Tengo.
type Script struct {
trace io.Writer
modules *tengo.ModuleMap
variables map[string]*tengo.Variable
src []byte
maxAllocs int64
}
// NewScript creates a Script object.
func NewScript(src []byte) *Script {
return &Script{
src: src,
variables: make(map[string]*tengo.Variable),
maxAllocs: -1,
}
}
// CompileRun compiles the source script to bytecode and run it once to fill
// global objects.
func (s *Script) CompileRun() (*Compiled, error) {
return s.compile(nil)
}
// CompileRunContext compiles the source script to bytecode and run it once to
// fill global objects.
func (s *Script) CompileRunContext(ctx context.Context) (*Compiled, error) {
return s.compile(ctx)
}
func (s *Script) compile(ctx context.Context) (*Compiled, error) {
symbolTable, globals, err := s.prepCompile()
if err != nil {
return nil, err
}
fileSet := parser.NewFileSet()
srcFile := fileSet.AddFile("(main)", -1, len(s.src))
p := parser.NewParser(srcFile, s.src, nil)
file, err := p.ParseFile()
if err != nil {
return nil, err
}
out := symbolTable.Define(reservedVar)
globals[out.Index] = tengo.UndefinedValue
cc := tengo.NewCompiler(srcFile, symbolTable, nil, s.modules, s.trace)
if err := cc.Compile(file); err != nil {
return nil, err
}
globals = globals[:symbolTable.MaxSymbols()+1]
// global symbol names to indexes
globalIndexes := make(map[string]int, len(globals))
for _, name := range symbolTable.Names() {
symbol, _, _ := symbolTable.Resolve(name)
if symbol.Scope == tengo.ScopeGlobal {
globalIndexes[name] = symbol.Index
}
}
// run VM once and fill globals
if ctx == nil {
vm := tengo.NewVM(cc.Bytecode(), globals, s.maxAllocs)
if err := vm.Run(); err != nil {
return nil, err
}
} else {
vm := tengo.NewVM(cc.Bytecode(), globals, s.maxAllocs)
if err := runVMContext(ctx, vm); err != nil {
return nil, err
}
}
bc := cc.Bytecode()
bc.RemoveDuplicates()
return &Compiled{
bytecode: bc,
globals: globals,
indexes: globalIndexes,
maxAllocs: s.maxAllocs,
outIdx: out.Index,
}, nil
}
func (s *Script) prepCompile() (
symbolTable *tengo.SymbolTable,
globals []tengo.Object,
err error,
) {
var names []string
for name := range s.variables {
names = append(names, name)
}
symbolTable = tengo.NewSymbolTable()
globals = make([]tengo.Object, tengo.GlobalsSize)
for idx, name := range names {
symbol := symbolTable.Define(name)
if symbol.Index != idx {
panic(fmt.Errorf("wrong symbol index: %d != %d",
idx, symbol.Index))
}
globals[symbol.Index] = s.variables[name].Object()
}
return
}
// Add adds a new variable or updates an existing variable to the script.
func (s *Script) Add(name string, value interface{}) error {
if name == reservedVar {
return errors.New("variable name must be different")
}
v, err := tengo.NewVariable(name, value)
if err != nil {
return err
}
s.variables[name] = v
return nil
}
// Remove removes (undefines) an existing variable for the script. It returns
// false if the variable name is not defined.
func (s *Script) Remove(name string) bool {
if name == reservedVar {
return false
}
if _, ok := s.variables[name]; !ok {
return false
}
delete(s.variables, name)
return true
}
// SetImports sets import modules.
func (s *Script) SetImports(modules *tengo.ModuleMap) {
s.modules = modules
}
// SetMaxAllocs sets the maximum number of objects allocations during the run
// time. Compiled script will return tengo.ErrObjectAllocLimit error if it
// exceeds this limit.
func (s *Script) SetMaxAllocs(n int64) {
s.maxAllocs = n
}
// Trace set a tracer for compiler and VM for debugging purposes.
func (s *Script) Trace(w io.Writer) {
s.trace = w
}
// Compiled is a compiled instance of the user script. Use Script.CompileRun()
// to create Compiled object.
type Compiled struct {
mu sync.RWMutex
bytecode *tengo.Bytecode
indexes map[string]int
globals []tengo.Object
maxAllocs int64
outIdx int
}
// Clone creates a new copy of Compiled. Cloned copies are safe for concurrent
// use. Clones occupy less memory..
func (c *Compiled) Clone() *Compiled {
c.mu.Lock()
defer c.mu.Unlock()
clone := &Compiled{
indexes: c.indexes,
bytecode: c.bytecode,
globals: make([]tengo.Object, len(c.globals)),
maxAllocs: c.maxAllocs,
outIdx: c.outIdx,
}
// copy global objects
for idx, g := range c.globals {
if g != nil {
clone.globals[idx] = g
}
}
return clone
}
// CallByName calls callable tengo.Object by its name and with given
// arguments, and returns result.
// args must be convertible to supported Tengo types.
func (c *Compiled) CallByName(fn string, args ...interface{}) (interface{}, error) {
return c.CallByNameContext(nil, fn, args...)
}
// CallByNameContext calls callable tengo.Object by its name and with given
// arguments, and returns result.
// args must be convertible to supported Tengo types.
func (c *Compiled) CallByNameContext(ctx context.Context,
fn string, args ...interface{}) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
idx, ok := c.indexes[fn]
if !ok {
return nil, fmt.Errorf("'%s' not found", fn)
}
cfn := c.globals[idx]
if cfn == nil {
return nil, errors.New("callable expected, got nil")
}
if !cfn.CanCall() {
return nil, errors.New("not a callable")
}
return c.call(ctx, cfn, args...)
}
// Call calls callable tengo.Object with given arguments, and returns result.
// args must be convertible to supported Tengo types.
func (c *Compiled) Call(fn tengo.Object,
args ...interface{}) (interface{}, error) {
return c.CallContext(nil, fn, args...)
}
// CallContext calls callable tengo.Object with given arguments, and returns result.
// args must be convertible to supported Tengo types.
func (c *Compiled) CallContext(ctx context.Context, fn tengo.Object,
args ...interface{}) (interface{}, error) {
c.mu.Lock()
defer c.mu.Unlock()
if fn == nil {
return nil, errors.New("callable expected, got nil")
}
if !fn.CanCall() {
return nil, errors.New("not a callable")
}
return c.call(ctx, fn, args...)
}
func (c *Compiled) call(ctx context.Context, cfn tengo.Object,
args ...interface{}) (interface{}, error) {
targs := make([]tengo.Object, 0, len(args))
for i := range args {
v, err := tengo.FromInterface(args[i])
if err != nil {
return nil, err
}
targs = append(targs, v)
}
v, err := c.callCompiled(ctx, cfn, targs...)
if err != nil {
return nil, err
}
return tengo.ToInterface(v), nil
}
func (c *Compiled) callCompiled(ctx context.Context, fn tengo.Object,
args ...tengo.Object) (tengo.Object, error) {
constsOffset := len(c.bytecode.Constants)
// Load fn
inst := tengo.MakeInstruction(parser.OpConstant, constsOffset)
// Load args
for i := range args {
inst = append(inst,
tengo.MakeInstruction(parser.OpConstant, constsOffset+i+1)...)
}
// Call, set value to a global, stop
inst = append(inst, tengo.MakeInstruction(parser.OpCall, len(args))...)
inst = append(inst, tengo.MakeInstruction(parser.OpSetGlobal, c.outIdx)...)
inst = append(inst, tengo.MakeInstruction(parser.OpSuspend)...)
c.bytecode.Constants = append(c.bytecode.Constants, fn)
c.bytecode.Constants = append(c.bytecode.Constants, args...)
// orig := s.bytecode.MainFunction
c.bytecode.MainFunction = &tengo.CompiledFunction{
Instructions: inst,
}
var err error
if ctx == nil {
vm := tengo.NewVM(c.bytecode, c.globals, c.maxAllocs)
err = vm.Run()
} else {
vm := tengo.NewVM(c.bytecode, c.globals, c.maxAllocs)
err = runVMContext(ctx, vm)
}
// TODO: go back to normal if required
// s.bytecode.MainFunction = orig
// avoid memory leak.
for i := constsOffset; i < len(c.bytecode.Constants); i++ {
c.bytecode.Constants[i] = nil
}
c.bytecode.Constants = c.bytecode.Constants[:constsOffset]
// get symbol using index and return it
return c.globals[c.outIdx], err
}
// Get returns a variable identified by the name.
func (c *Compiled) Get(name string) *tengo.Variable {
c.mu.RLock()
defer c.mu.RUnlock()
value := tengo.UndefinedValue
if idx, ok := c.indexes[name]; ok {
value = c.globals[idx]
if value == nil {
value = tengo.UndefinedValue
}
}
v, err := tengo.NewVariable(name, value)
if err != nil {
// This should never happen, since the value will already be a tengo object
// tengo's (*Script).Get avoids this because it constructs the object directly.
panic(fmt.Errorf("unable to create new variable from global value, type was %T", value))
}
return v
}
// GetAll returns all the variables that are defined by the compiled script.
func (c *Compiled) GetAll() []*tengo.Variable {
vars := make([]*tengo.Variable, 0, len(c.indexes))
for name, idx := range c.indexes {
value := c.globals[idx]
if value == nil {
value = tengo.UndefinedValue
}
tv, err := tengo.NewVariable(name, value)
if err != nil {
// This should never happen, since the value will already be a tengo object
panic(fmt.Errorf("unable to create new variable from global value, type was %T", value))
}
vars = append(vars, tv)
}
return vars
}
// Set replaces the value of a global variable identified by the name. An error
// will be returned if the name was not defined during compilation.
func (c *Compiled) Set(name string, value interface{}) error {
obj, err := tengo.FromInterface(value)
if err != nil {
return err
}
idx, ok := c.indexes[name]
if !ok {
return fmt.Errorf("'%s' is not defined", name)
}
c.globals[idx] = obj
return nil
}
// IsDefined returns true if the variable name is defined (has value)
// after the execution.
func (c *Compiled) IsDefined(name string) bool {
c.mu.RLock()
defer c.mu.RUnlock()
idx, ok := c.indexes[name]
if !ok {
return false
}
v := c.globals[idx]
if v == nil {
return false
}
return v != tengo.UndefinedValue
}
func runVMContext(ctx context.Context, vm *tengo.VM) (err error) {
errch := make(chan error)
go func() {
errch <- vm.Run()
}()
select {
case err = <-errch:
case <-ctx.Done():
vm.Abort()
<-errch
err = ctx.Err()
}
if err != nil {
return
}
return
}
// Callback is a wrapper to call a callable tengo.Object from Go with Call() and
// CallContext() methods. *Compiled object must be set before Call() and
// CallContext() calls, otherwise an error is thrown.
// Callback is intended to be created in a Go function that is invoked by tengo
// script to capture callable tengo.Object and additional arguments if required
// later. Args is deliberately exposed to use it as arguments to CallXXX methods
// but it is optional.
// Note: Do not call CallXXX methods while script is running, it locks the VM.
type Callback struct {
Args []interface{}
compiled *Compiled
fn tengo.Object
}
// NewCallback creates Callback object. See Callback type.
func NewCallback(fn tengo.Object, args ...interface{}) *Callback {
return &Callback{
fn: fn,
Args: args,
}
}
// Set sets compiled object, which is required to call callables.
func (cb *Callback) Set(c *Compiled) *Callback {
cb.compiled = c
return cb
}
// Call calls tengo.Object and returns result. Set *Compiled object before
// Call().
func (cb *Callback) Call(args ...interface{}) (interface{}, error) {
if cb.compiled == nil {
return nil, errors.New("compiled code not set")
}
return cb.compiled.Call(cb.fn, args...)
}
// CallContext calls tengo.Object and returns result. Set *Compiled object
// before CallContext().
func (cb *Callback) CallContext(ctx context.Context,
args ...interface{}) (interface{}, error) {
if cb.compiled == nil {
return nil, errors.New("compiled code not set")
}
return cb.compiled.CallContext(ctx, cb.fn, args...)
}