-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
evalfilter.go
354 lines (298 loc) · 8.2 KB
/
evalfilter.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// Package evalfilter allows running a user-supplied script against an object.
//
// We're constructed with a program, and internally we parse that to an
// abstract syntax-tree, then we walk that tree to generate a series of
// bytecodes.
//
// The bytecode is then executed via the VM-package.
package evalfilter
import (
"context"
"fmt"
"strings"
"sync"
"github.com/skx/evalfilter/v2/code"
"github.com/skx/evalfilter/v2/environment"
"github.com/skx/evalfilter/v2/lexer"
"github.com/skx/evalfilter/v2/object"
"github.com/skx/evalfilter/v2/parser"
"github.com/skx/evalfilter/v2/vm"
)
// Flags which can be optionally passed to Prepare.
const (
// Don't run the optimizer when generating bytecode.
NoOptimize byte = iota
)
// Eval is our public-facing structure which stores our state.
type Eval struct {
// Script holds the script the user submitted in our constructor.
Script string
// Environment
environment *environment.Environment
// constants compiled
constants []object.Object
// bytecode we generate
instructions code.Instructions
// the machine we drive
machine *vm.VM
// context for handling timeout
context context.Context
// user-defined functions
functions map[string]environment.UserFunction
// Mutex to allow concurrent runs
mutex sync.Mutex
}
// New creates a new instance of the evaluator.
func New(script string) *Eval {
//
// Create our object.
//
e := &Eval{
environment: environment.New(),
Script: script,
context: context.Background(),
functions: make(map[string]environment.UserFunction),
mutex: sync.Mutex{},
}
//
// Return it.
//
return e
}
// SetContext allows a context to be passed to the evaluator.
//
// The context is passed down to the virtual machine, which allows you to
// setup a timeout/deadline for the execution of user-supplied scripts.
func (e *Eval) SetContext(ctx context.Context) {
e.context = ctx
}
// Prepare is the second function the caller must invoke, it compiles
// the user-supplied program to its final-form.
//
// Internally this compilation process walks through the usual steps,
// lexing, parsing, and bytecode-compilation.
func (e *Eval) Prepare(flags ...[]byte) error {
e.mutex.Lock()
defer e.mutex.Unlock()
//
// Default to optimizing the bytecode.
//
optimize := true
//
// But let flags change our behaviour.
//
for _, arg := range flags {
for _, val := range arg {
if val == NoOptimize {
optimize = false
}
}
}
//
// Create a lexer.
//
l := lexer.New(e.Script)
//
// Create a parser using the lexer.
//
p := parser.New(l)
//
// Parse the program into an AST.
//
program, err := p.Parse()
if err != nil {
return err
}
//
// Compile the program to bytecode
//
err = e.compile(program)
//
// If there were errors then return them.
//
if err != nil {
return err
}
//
// If we've got the optimizer enabled then set the environment
// variable, so that the virtual machine knows it should
// run a series of optimizations.
//
if optimize {
e.environment.Set("OPTIMIZE", &object.Boolean{Value: true})
}
//
// Now we're done, construct a VM with the bytecode and constants
// we've created - as well as any function pointers and variables
// which we were given.
//
// The optimization will happen at this step, so that it is complete
// before Execute/Run are invoked - and we only take the speed hit
// once.
e.machine = vm.New(e.constants, e.instructions, e.functions, e.environment)
//
// Setup our context
//
e.machine.SetContext(e.context)
//
// All done; no errors.
//
return nil
}
// dumper is the callback function which is invoked for dumping bytecode
func (e *Eval) dumper(offset int, opCode code.Opcode, opArg interface{}) (bool, error) {
// Show the offset + instruction.
fmt.Printf(" %04d\t%14s", offset, code.String(opCode))
// Show the optional argument, if present.
if opArg != nil {
fmt.Printf("\t% 4d", opArg.(int))
}
// Some opcodes benefit from inline comments
if code.Opcode(opCode) == code.OpConstant {
v := e.constants[opArg.(int)]
s := strings.ReplaceAll(v.Inspect(), "\n", "\\n")
s = strings.ReplaceAll(s, "\r", "\\r")
s = strings.ReplaceAll(s, "\t", "\\t")
fmt.Printf("\t// push constant onto stack: \"%s\"", s)
}
if code.Opcode(opCode) == code.OpLookup {
v := e.constants[opArg.(int)]
s := strings.ReplaceAll(v.Inspect(), "\n", "\\n")
s = strings.ReplaceAll(s, "\r", "\\r")
s = strings.ReplaceAll(s, "\t", "\\t")
fmt.Printf("\t// lookup field/variable: %s", s)
}
if code.Opcode(opCode) == code.OpCall {
fmt.Printf("\t// call function with %d arg(s)", opArg.(int))
}
if code.Opcode(opCode) == code.OpPush {
fmt.Printf("\t// Push %d to stack", opArg.(int))
}
fmt.Printf("\n")
// Keep walking, no error.
return true, nil
}
// Dump causes our bytecode to be dumped, along with the contents
// of the constant-pool
func (e *Eval) Dump() error {
fmt.Printf("Bytecode:\n")
// Use the walker to dump the bytecode.
err := e.machine.WalkBytecode(e.dumper)
if err != nil {
return err
}
// Show constants, if any are present.
consts := e.constants
if len(consts) > 0 {
fmt.Printf("\n\nConstant Pool:\n")
for i, n := range consts {
s := strings.ReplaceAll(n.Inspect(), "\n", "\\n")
fmt.Printf(" %04d Type:%s Value:\"%s\"\n", i, n.Type(), s)
}
}
// Do we have user-defined functions?
funs := e.functions
if len(funs) > 0 {
fmt.Printf("\nUser-defined functions:\n")
}
// For each function
count := 0
for name, obj := range funs {
// Show brief information
fmt.Printf(" function %s(%s)\n", name, strings.Join(obj.Arguments, ","))
// Then dump the body.
err := e.machine.WalkFunctionBytecode(name, e.dumper)
if err != nil {
return err
}
// Put a newline between functions.
if count < len(e.functions)-1 {
fmt.Printf("\n")
}
count++
}
return nil
}
// Execute executes the program which the user passed in the constructor,
// and returns the object that the script finished with.
//
// This function is very similar to the `Run` method, however the Run
// method only returns a binary/boolean result, and this method returns
// the actual object your script returned with.
//
// Use of this method allows you to receive the `3` that a script
// such as `return 1 + 2;` would return.
func (e *Eval) Execute(obj interface{}) (out object.Object, error error) {
// Catch errors when we're executing.
defer func() {
if r := recover(); r != nil {
out = &object.Null{}
error = fmt.Errorf("error during Run: %s", r)
}
}()
//
// Launch the program in the VM.
//
out, err := e.machine.Run(obj)
//
// Error executing? Report that.
//
if err != nil {
return &object.Null{}, err
}
//
// Return the resulting object.
//
return out, nil
}
// Run executes the program which the user passed in the constructor.
//
// The return value, assuming no error, is a binary/boolean result which
// suits the use of this package as a filter.
//
// If you wish to return the actual value the script returned then you can
// use the `Execute` method instead. That doesn't attempt to determine whether
// the result of the script was "true" or not.
func (e *Eval) Run(obj interface{}) (bool, error) {
e.mutex.Lock()
//
// Execute the script, getting the resulting error
// and return object.
//
out, err := e.Execute(obj)
e.mutex.Unlock()
//
// Error? Then return that.
//
if err != nil {
return false, err
}
//
// Otherwise case the resulting object into
// a boolean and pass that back to the caller.
//
return out.True(), nil
}
// AddFunction exposes a golang function from your host application
// to the scripting environment.
//
// Once a function has been added it may be used by the filter script.
func (e *Eval) AddFunction(name string, fun interface{}) {
e.environment.SetFunction(name, fun)
}
// SetVariable adds, or updates a variable which will be available
// to the filter script.
func (e *Eval) SetVariable(name string, value object.Object) {
e.environment.Set(name, value)
}
// GetVariable retrieves the contents of a variable which has been
// set within a user-script.
//
// If the variable hasn't been set then the null-value will be returned.
func (e *Eval) GetVariable(name string) object.Object {
value, ok := e.environment.Get(name)
if ok {
return value
}
return &object.Null{}
}