2019-02-23 15:39:44 +00:00
|
|
|
package script
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-04-06 20:18:25 +00:00
|
|
|
"sync"
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
"github.com/d5/tengo/compiler"
|
|
|
|
"github.com/d5/tengo/objects"
|
|
|
|
"github.com/d5/tengo/runtime"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Compiled is a compiled instance of the user script.
|
|
|
|
// Use Script.Compile() to create Compiled object.
|
|
|
|
type Compiled struct {
|
2019-04-06 20:18:25 +00:00
|
|
|
globalIndexes map[string]int // global symbol name to index
|
|
|
|
bytecode *compiler.Bytecode
|
|
|
|
globals []objects.Object
|
|
|
|
maxAllocs int64
|
|
|
|
lock sync.RWMutex
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run executes the compiled script in the virtual machine.
|
|
|
|
func (c *Compiled) Run() error {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
v := runtime.NewVM(c.bytecode, c.globals, c.maxAllocs)
|
|
|
|
|
|
|
|
return v.Run()
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// RunContext is like Run but includes a context.
|
|
|
|
func (c *Compiled) RunContext(ctx context.Context) (err error) {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
v := runtime.NewVM(c.bytecode, c.globals, c.maxAllocs)
|
|
|
|
|
2019-02-23 15:39:44 +00:00
|
|
|
ch := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
2019-04-06 20:18:25 +00:00
|
|
|
ch <- v.Run()
|
2019-02-23 15:39:44 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2019-04-06 20:18:25 +00:00
|
|
|
v.Abort()
|
2019-02-23 15:39:44 +00:00
|
|
|
<-ch
|
|
|
|
err = ctx.Err()
|
|
|
|
case err = <-ch:
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
// Clone creates a new copy of Compiled.
|
|
|
|
// Cloned copies are safe for concurrent use by multiple goroutines.
|
|
|
|
func (c *Compiled) Clone() *Compiled {
|
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
|
|
|
clone := &Compiled{
|
|
|
|
globalIndexes: c.globalIndexes,
|
|
|
|
bytecode: c.bytecode,
|
|
|
|
globals: make([]objects.Object, len(c.globals)),
|
|
|
|
maxAllocs: c.maxAllocs,
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy global objects
|
|
|
|
for idx, g := range c.globals {
|
|
|
|
if g != nil {
|
|
|
|
clone.globals[idx] = g
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone
|
|
|
|
}
|
|
|
|
|
2019-02-23 15:39:44 +00:00
|
|
|
// IsDefined returns true if the variable name is defined (has value) before or after the execution.
|
|
|
|
func (c *Compiled) IsDefined(name string) bool {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
|
|
|
|
idx, ok := c.globalIndexes[name]
|
2019-02-23 15:39:44 +00:00
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
v := c.globals[idx]
|
2019-02-23 15:39:44 +00:00
|
|
|
if v == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
return v != objects.UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns a variable identified by the name.
|
|
|
|
func (c *Compiled) Get(name string) *Variable {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
|
|
|
|
value := objects.UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
if idx, ok := c.globalIndexes[name]; ok {
|
|
|
|
value = c.globals[idx]
|
2019-02-23 15:39:44 +00:00
|
|
|
if value == nil {
|
2019-04-06 20:18:25 +00:00
|
|
|
value = objects.UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Variable{
|
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll returns all the variables that are defined by the compiled script.
|
|
|
|
func (c *Compiled) GetAll() []*Variable {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.RLock()
|
|
|
|
defer c.lock.RUnlock()
|
|
|
|
|
2019-02-23 15:39:44 +00:00
|
|
|
var vars []*Variable
|
2019-04-06 20:18:25 +00:00
|
|
|
|
|
|
|
for name, idx := range c.globalIndexes {
|
|
|
|
value := c.globals[idx]
|
|
|
|
if value == nil {
|
|
|
|
value = objects.UndefinedValue
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
2019-04-06 20:18:25 +00:00
|
|
|
|
|
|
|
vars = append(vars, &Variable{
|
|
|
|
name: name,
|
|
|
|
value: value,
|
|
|
|
})
|
2019-02-23 15:39:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-04-06 20:18:25 +00:00
|
|
|
c.lock.Lock()
|
|
|
|
defer c.lock.Unlock()
|
|
|
|
|
2019-02-23 15:39:44 +00:00
|
|
|
obj, err := objects.FromInterface(value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
idx, ok := c.globalIndexes[name]
|
|
|
|
if !ok {
|
2019-02-23 15:39:44 +00:00
|
|
|
return fmt.Errorf("'%s' is not defined", name)
|
|
|
|
}
|
|
|
|
|
2019-04-06 20:18:25 +00:00
|
|
|
c.globals[idx] = obj
|
2019-02-23 15:39:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|