diff options
Diffstat (limited to 'vendor/github.com/d5/tengo/script')
-rw-r--r-- | vendor/github.com/d5/tengo/script/compiled.go | 159 | ||||
-rw-r--r-- | vendor/github.com/d5/tengo/script/script.go | 185 | ||||
-rw-r--r-- | vendor/github.com/d5/tengo/script/variable.go | 149 |
3 files changed, 0 insertions, 493 deletions
diff --git a/vendor/github.com/d5/tengo/script/compiled.go b/vendor/github.com/d5/tengo/script/compiled.go deleted file mode 100644 index ce50f498..00000000 --- a/vendor/github.com/d5/tengo/script/compiled.go +++ /dev/null @@ -1,159 +0,0 @@ -package script - -import ( - "context" - "fmt" - "sync" - - "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 { - globalIndexes map[string]int // global symbol name to index - bytecode *compiler.Bytecode - globals []objects.Object - maxAllocs int64 - lock sync.RWMutex -} - -// Run executes the compiled script in the virtual machine. -func (c *Compiled) Run() error { - c.lock.Lock() - defer c.lock.Unlock() - - v := runtime.NewVM(c.bytecode, c.globals, c.maxAllocs) - - return v.Run() -} - -// RunContext is like Run but includes a context. -func (c *Compiled) RunContext(ctx context.Context) (err error) { - c.lock.Lock() - defer c.lock.Unlock() - - v := runtime.NewVM(c.bytecode, c.globals, c.maxAllocs) - - ch := make(chan error, 1) - - go func() { - ch <- v.Run() - }() - - select { - case <-ctx.Done(): - v.Abort() - <-ch - err = ctx.Err() - case err = <-ch: - } - - return -} - -// 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 -} - -// IsDefined returns true if the variable name is defined (has value) before or after the execution. -func (c *Compiled) IsDefined(name string) bool { - c.lock.RLock() - defer c.lock.RUnlock() - - idx, ok := c.globalIndexes[name] - if !ok { - return false - } - - v := c.globals[idx] - if v == nil { - return false - } - - return v != objects.UndefinedValue -} - -// Get returns a variable identified by the name. -func (c *Compiled) Get(name string) *Variable { - c.lock.RLock() - defer c.lock.RUnlock() - - value := objects.UndefinedValue - - if idx, ok := c.globalIndexes[name]; ok { - value = c.globals[idx] - if value == nil { - value = objects.UndefinedValue - } - } - - return &Variable{ - name: name, - value: value, - } -} - -// GetAll returns all the variables that are defined by the compiled script. -func (c *Compiled) GetAll() []*Variable { - c.lock.RLock() - defer c.lock.RUnlock() - - var vars []*Variable - - for name, idx := range c.globalIndexes { - value := c.globals[idx] - if value == nil { - value = objects.UndefinedValue - } - - vars = append(vars, &Variable{ - name: name, - value: value, - }) - } - - 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 { - c.lock.Lock() - defer c.lock.Unlock() - - obj, err := objects.FromInterface(value) - if err != nil { - return err - } - - idx, ok := c.globalIndexes[name] - if !ok { - return fmt.Errorf("'%s' is not defined", name) - } - - c.globals[idx] = obj - - return nil -} diff --git a/vendor/github.com/d5/tengo/script/script.go b/vendor/github.com/d5/tengo/script/script.go deleted file mode 100644 index 2ee67b61..00000000 --- a/vendor/github.com/d5/tengo/script/script.go +++ /dev/null @@ -1,185 +0,0 @@ -package script - -import ( - "context" - "fmt" - - "github.com/d5/tengo/compiler" - "github.com/d5/tengo/compiler/parser" - "github.com/d5/tengo/compiler/source" - "github.com/d5/tengo/objects" - "github.com/d5/tengo/runtime" -) - -// Script can simplify compilation and execution of embedded scripts. -type Script struct { - variables map[string]*Variable - modules *objects.ModuleMap - input []byte - maxAllocs int64 - maxConstObjects int - enableFileImport bool -} - -// New creates a Script instance with an input script. -func New(input []byte) *Script { - return &Script{ - variables: make(map[string]*Variable), - input: input, - maxAllocs: -1, - maxConstObjects: -1, - } -} - -// Add adds a new variable or updates an existing variable to the script. -func (s *Script) Add(name string, value interface{}) error { - obj, err := objects.FromInterface(value) - if err != nil { - return err - } - - s.variables[name] = &Variable{ - name: name, - value: obj, - } - - 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 _, ok := s.variables[name]; !ok { - return false - } - - delete(s.variables, name) - - return true -} - -// SetImports sets import modules. -func (s *Script) SetImports(modules *objects.ModuleMap) { - s.modules = modules -} - -// SetMaxAllocs sets the maximum number of objects allocations during the run time. -// Compiled script will return runtime.ErrObjectAllocLimit error if it exceeds this limit. -func (s *Script) SetMaxAllocs(n int64) { - s.maxAllocs = n -} - -// SetMaxConstObjects sets the maximum number of objects in the compiled constants. -func (s *Script) SetMaxConstObjects(n int) { - s.maxConstObjects = n -} - -// EnableFileImport enables or disables module loading from local files. -// Local file modules are disabled by default. -func (s *Script) EnableFileImport(enable bool) { - s.enableFileImport = enable -} - -// Compile compiles the script with all the defined variables, and, returns Compiled object. -func (s *Script) Compile() (*Compiled, error) { - symbolTable, globals, err := s.prepCompile() - if err != nil { - return nil, err - } - - fileSet := source.NewFileSet() - srcFile := fileSet.AddFile("(main)", -1, len(s.input)) - - p := parser.NewParser(srcFile, s.input, nil) - file, err := p.ParseFile() - if err != nil { - return nil, err - } - - c := compiler.NewCompiler(srcFile, symbolTable, nil, s.modules, nil) - c.EnableFileImport(s.enableFileImport) - if err := c.Compile(file); err != nil { - return nil, err - } - - // reduce globals size - 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 == compiler.ScopeGlobal { - globalIndexes[name] = symbol.Index - } - } - - // remove duplicates from constants - bytecode := c.Bytecode() - bytecode.RemoveDuplicates() - - // check the constant objects limit - if s.maxConstObjects >= 0 { - cnt := bytecode.CountObjects() - if cnt > s.maxConstObjects { - return nil, fmt.Errorf("exceeding constant objects limit: %d", cnt) - } - } - - return &Compiled{ - globalIndexes: globalIndexes, - bytecode: bytecode, - globals: globals, - maxAllocs: s.maxAllocs, - }, nil -} - -// Run compiles and runs the scripts. -// Use returned compiled object to access global variables. -func (s *Script) Run() (compiled *Compiled, err error) { - compiled, err = s.Compile() - if err != nil { - return - } - - err = compiled.Run() - - return -} - -// RunContext is like Run but includes a context. -func (s *Script) RunContext(ctx context.Context) (compiled *Compiled, err error) { - compiled, err = s.Compile() - if err != nil { - return - } - - err = compiled.RunContext(ctx) - - return -} - -func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, globals []objects.Object, err error) { - var names []string - for name := range s.variables { - names = append(names, name) - } - - symbolTable = compiler.NewSymbolTable() - for idx, fn := range objects.Builtins { - symbolTable.DefineBuiltin(idx, fn.Name) - } - - globals = make([]objects.Object, runtime.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].value - } - - return -} diff --git a/vendor/github.com/d5/tengo/script/variable.go b/vendor/github.com/d5/tengo/script/variable.go deleted file mode 100644 index df345115..00000000 --- a/vendor/github.com/d5/tengo/script/variable.go +++ /dev/null @@ -1,149 +0,0 @@ -package script - -import ( - "errors" - - "github.com/d5/tengo/objects" -) - -// Variable is a user-defined variable for the script. -type Variable struct { - name string - value objects.Object -} - -// NewVariable creates a Variable. -func NewVariable(name string, value interface{}) (*Variable, error) { - obj, err := objects.FromInterface(value) - if err != nil { - return nil, err - } - - return &Variable{ - name: name, - value: obj, - }, nil -} - -// Name returns the name of the variable. -func (v *Variable) Name() string { - return v.name -} - -// Value returns an empty interface of the variable value. -func (v *Variable) Value() interface{} { - return objects.ToInterface(v.value) -} - -// ValueType returns the name of the value type. -func (v *Variable) ValueType() string { - return v.value.TypeName() -} - -// Int returns int value of the variable value. -// It returns 0 if the value is not convertible to int. -func (v *Variable) Int() int { - c, _ := objects.ToInt(v.value) - - return c -} - -// Int64 returns int64 value of the variable value. -// It returns 0 if the value is not convertible to int64. -func (v *Variable) Int64() int64 { - c, _ := objects.ToInt64(v.value) - - return c -} - -// Float returns float64 value of the variable value. -// It returns 0.0 if the value is not convertible to float64. -func (v *Variable) Float() float64 { - c, _ := objects.ToFloat64(v.value) - - return c -} - -// Char returns rune value of the variable value. -// It returns 0 if the value is not convertible to rune. -func (v *Variable) Char() rune { - c, _ := objects.ToRune(v.value) - - return c -} - -// Bool returns bool value of the variable value. -// It returns 0 if the value is not convertible to bool. -func (v *Variable) Bool() bool { - c, _ := objects.ToBool(v.value) - - return c -} - -// Array returns []interface value of the variable value. -// It returns 0 if the value is not convertible to []interface. -func (v *Variable) Array() []interface{} { - switch val := v.value.(type) { - case *objects.Array: - var arr []interface{} - for _, e := range val.Value { - arr = append(arr, objects.ToInterface(e)) - } - return arr - } - - return nil -} - -// Map returns map[string]interface{} value of the variable value. -// It returns 0 if the value is not convertible to map[string]interface{}. -func (v *Variable) Map() map[string]interface{} { - switch val := v.value.(type) { - case *objects.Map: - kv := make(map[string]interface{}) - for mk, mv := range val.Value { - kv[mk] = objects.ToInterface(mv) - } - return kv - } - - return nil -} - -// String returns string value of the variable value. -// It returns 0 if the value is not convertible to string. -func (v *Variable) String() string { - c, _ := objects.ToString(v.value) - - return c -} - -// Bytes returns a byte slice of the variable value. -// It returns nil if the value is not convertible to byte slice. -func (v *Variable) Bytes() []byte { - c, _ := objects.ToByteSlice(v.value) - - return c -} - -// Error returns an error if the underlying value is error object. -// If not, this returns nil. -func (v *Variable) Error() error { - err, ok := v.value.(*objects.Error) - if ok { - return errors.New(err.String()) - } - - return nil -} - -// Object returns an underlying Object of the variable value. -// Note that returned Object is a copy of an actual Object used in the script. -func (v *Variable) Object() objects.Object { - return v.value -} - -// IsUndefined returns true if the underlying value is undefined. -func (v *Variable) IsUndefined() bool { - return v.value == objects.UndefinedValue -} |