diff options
Diffstat (limited to 'vendor/github.com/d5/tengo/objects')
19 files changed, 309 insertions, 320 deletions
diff --git a/vendor/github.com/d5/tengo/objects/break.go b/vendor/github.com/d5/tengo/objects/break.go deleted file mode 100644 index cd473a87..00000000 --- a/vendor/github.com/d5/tengo/objects/break.go +++ /dev/null @@ -1,37 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// Break represents a break statement. -type Break struct{} - -// TypeName returns the name of the type. -func (o *Break) TypeName() string { - return "break" -} - -func (o *Break) String() string { - return "<break>" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Break) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Break) Copy() Object { - return &Break{} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Break) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Break) Equals(x Object) bool { - return false -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_json.go b/vendor/github.com/d5/tengo/objects/builtin_json.go deleted file mode 100644 index b3413651..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_json.go +++ /dev/null @@ -1,60 +0,0 @@ -package objects - -import ( - "encoding/json" - - "github.com/d5/tengo" -) - -// to_json(v object) => bytes -func builtinToJSON(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - res, err := json.Marshal(objectToInterface(args[0])) - if err != nil { - return &Error{Value: &String{Value: err.Error()}}, nil - } - - if len(res) > tengo.MaxBytesLen { - return nil, ErrBytesLimit - } - - return &Bytes{Value: res}, nil -} - -// from_json(data string/bytes) => object -func builtinFromJSON(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - var target interface{} - - switch o := args[0].(type) { - case *Bytes: - err := json.Unmarshal(o.Value, &target) - if err != nil { - return &Error{Value: &String{Value: err.Error()}}, nil - } - case *String: - err := json.Unmarshal([]byte(o.Value), &target) - if err != nil { - return &Error{Value: &String{Value: err.Error()}}, nil - } - default: - return nil, ErrInvalidArgumentType{ - Name: "first", - Expected: "bytes/string", - Found: args[0].TypeName(), - } - } - - res, err := FromInterface(target) - if err != nil { - return nil, err - } - - return res, nil -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_module.go b/vendor/github.com/d5/tengo/objects/builtin_module.go new file mode 100644 index 00000000..0ad1d99d --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/builtin_module.go @@ -0,0 +1,23 @@ +package objects + +// BuiltinModule is an importable module that's written in Go. +type BuiltinModule struct { + Attrs map[string]Object +} + +// Import returns an immutable map for the module. +func (m *BuiltinModule) Import(moduleName string) (interface{}, error) { + return m.AsImmutableMap(moduleName), nil +} + +// AsImmutableMap converts builtin module into an immutable map. +func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap { + attrs := make(map[string]Object, len(m.Attrs)) + for k, v := range m.Attrs { + attrs[k] = v.Copy() + } + + attrs["__module_name__"] = &String{Value: moduleName} + + return &ImmutableMap{Value: attrs} +} diff --git a/vendor/github.com/d5/tengo/objects/builtin_print.go b/vendor/github.com/d5/tengo/objects/builtin_print.go deleted file mode 100644 index 58f22610..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_print.go +++ /dev/null @@ -1,83 +0,0 @@ -package objects - -import ( - "fmt" - - "github.com/d5/tengo" -) - -// print(args...) -func builtinPrint(args ...Object) (Object, error) { - for _, arg := range args { - if str, ok := arg.(*String); ok { - fmt.Println(str.Value) - } else { - fmt.Println(arg.String()) - } - } - - return nil, nil -} - -// printf("format", args...) -func builtinPrintf(args ...Object) (Object, error) { - numArgs := len(args) - if numArgs == 0 { - return nil, ErrWrongNumArguments - } - - format, ok := args[0].(*String) - if !ok { - return nil, ErrInvalidArgumentType{ - Name: "format", - Expected: "string", - Found: args[0].TypeName(), - } - } - if numArgs == 1 { - fmt.Print(format) - return nil, nil - } - - formatArgs := make([]interface{}, numArgs-1, numArgs-1) - for idx, arg := range args[1:] { - formatArgs[idx] = objectToInterface(arg) - } - - fmt.Printf(format.Value, formatArgs...) - - return nil, nil -} - -// sprintf("format", args...) -func builtinSprintf(args ...Object) (Object, error) { - numArgs := len(args) - if numArgs == 0 { - return nil, ErrWrongNumArguments - } - - format, ok := args[0].(*String) - if !ok { - return nil, ErrInvalidArgumentType{ - Name: "format", - Expected: "string", - Found: args[0].TypeName(), - } - } - if numArgs == 1 { - return format, nil // okay to return 'format' directly as String is immutable - } - - formatArgs := make([]interface{}, numArgs-1, numArgs-1) - for idx, arg := range args[1:] { - formatArgs[idx] = objectToInterface(arg) - } - - s := fmt.Sprintf(format.Value, formatArgs...) - - if len(s) > tengo.MaxStringLen { - return nil, ErrStringLimit - } - - return &String{Value: s}, nil -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_type_checks.go b/vendor/github.com/d5/tengo/objects/builtin_type_checks.go index 960f7828..d1e8471d 100644 --- a/vendor/github.com/d5/tengo/objects/builtin_type_checks.go +++ b/vendor/github.com/d5/tengo/objects/builtin_type_checks.go @@ -181,3 +181,15 @@ func builtinIsCallable(args ...Object) (Object, error) { return FalseValue, nil } + +func builtinIsIterable(args ...Object) (Object, error) { + if len(args) != 1 { + return nil, ErrWrongNumArguments + } + + if _, ok := args[0].(Iterable); ok { + return TrueValue, nil + } + + return FalseValue, nil +} diff --git a/vendor/github.com/d5/tengo/objects/builtins.go b/vendor/github.com/d5/tengo/objects/builtins.go index 42c1a759..bfd004dd 100644 --- a/vendor/github.com/d5/tengo/objects/builtins.go +++ b/vendor/github.com/d5/tengo/objects/builtins.go @@ -2,19 +2,7 @@ package objects // Builtins contains all default builtin functions. // Use GetBuiltinFunctions instead of accessing Builtins directly. -var Builtins = []BuiltinFunction{ - { - Name: "print", - Value: builtinPrint, - }, - { - Name: "printf", - Value: builtinPrintf, - }, - { - Name: "sprintf", - Value: builtinSprintf, - }, +var Builtins = []*BuiltinFunction{ { Name: "len", Value: builtinLen, @@ -96,6 +84,10 @@ var Builtins = []BuiltinFunction{ Value: builtinIsImmutableMap, }, { + Name: "is_iterable", + Value: builtinIsIterable, + }, + { Name: "is_time", Value: builtinIsTime, }, @@ -116,49 +108,7 @@ var Builtins = []BuiltinFunction{ Value: builtinIsCallable, }, { - Name: "to_json", - Value: builtinToJSON, - }, - { - Name: "from_json", - Value: builtinFromJSON, - }, - { Name: "type_name", Value: builtinTypeName, }, } - -// AllBuiltinFunctionNames returns a list of all default builtin function names. -func AllBuiltinFunctionNames() []string { - var names []string - for _, bf := range Builtins { - names = append(names, bf.Name) - } - return names -} - -// GetBuiltinFunctions returns a slice of builtin function objects. -// GetBuiltinFunctions removes the duplicate names, and, the returned builtin functions -// are not guaranteed to be in the same order as names. -func GetBuiltinFunctions(names ...string) []*BuiltinFunction { - include := make(map[string]bool) - for _, name := range names { - include[name] = true - } - - var builtinFuncs []*BuiltinFunction - for _, bf := range Builtins { - if include[bf.Name] { - bf := bf - builtinFuncs = append(builtinFuncs, &bf) - } - } - - return builtinFuncs -} - -// GetAllBuiltinFunctions returns all builtin functions. -func GetAllBuiltinFunctions() []*BuiltinFunction { - return GetBuiltinFunctions(AllBuiltinFunctionNames()...) -} diff --git a/vendor/github.com/d5/tengo/objects/bytes.go b/vendor/github.com/d5/tengo/objects/bytes.go index 16b61684..6710c7c1 100644 --- a/vendor/github.com/d5/tengo/objects/bytes.go +++ b/vendor/github.com/d5/tengo/objects/bytes.go @@ -79,3 +79,11 @@ func (o *Bytes) IndexGet(index Object) (res Object, err error) { return } + +// Iterate creates a bytes iterator. +func (o *Bytes) Iterate() Iterator { + return &BytesIterator{ + v: o.Value, + l: len(o.Value), + } +} diff --git a/vendor/github.com/d5/tengo/objects/bytes_iterator.go b/vendor/github.com/d5/tengo/objects/bytes_iterator.go new file mode 100644 index 00000000..18a36e17 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/bytes_iterator.go @@ -0,0 +1,57 @@ +package objects + +import "github.com/d5/tengo/compiler/token" + +// BytesIterator represents an iterator for a string. +type BytesIterator struct { + v []byte + i int + l int +} + +// TypeName returns the name of the type. +func (i *BytesIterator) TypeName() string { + return "bytes-iterator" +} + +func (i *BytesIterator) String() string { + return "<bytes-iterator>" +} + +// BinaryOp returns another object that is the result of +// a given binary operator and a right-hand side object. +func (i *BytesIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { + return nil, ErrInvalidOperator +} + +// IsFalsy returns true if the value of the type is falsy. +func (i *BytesIterator) IsFalsy() bool { + return true +} + +// Equals returns true if the value of the type +// is equal to the value of another object. +func (i *BytesIterator) Equals(Object) bool { + return false +} + +// Copy returns a copy of the type. +func (i *BytesIterator) Copy() Object { + return &BytesIterator{v: i.v, i: i.i, l: i.l} +} + +// Next returns true if there are more elements to iterate. +func (i *BytesIterator) Next() bool { + i.i++ + return i.i <= i.l +} + +// Key returns the key or index value of the current element. +func (i *BytesIterator) Key() Object { + return &Int{Value: int64(i.i - 1)} +} + +// Value returns the value of the current element. +func (i *BytesIterator) Value() Object { + return &Int{Value: int64(i.v[i.i-1])} +} diff --git a/vendor/github.com/d5/tengo/objects/closure.go b/vendor/github.com/d5/tengo/objects/closure.go index d4915a52..06058b23 100644 --- a/vendor/github.com/d5/tengo/objects/closure.go +++ b/vendor/github.com/d5/tengo/objects/closure.go @@ -7,7 +7,7 @@ import ( // Closure represents a function closure. type Closure struct { Fn *CompiledFunction - Free []*Object + Free []*ObjectPtr } // TypeName returns the name of the type. @@ -29,7 +29,7 @@ func (o *Closure) BinaryOp(op token.Token, rhs Object) (Object, error) { func (o *Closure) Copy() Object { return &Closure{ Fn: o.Fn.Copy().(*CompiledFunction), - Free: append([]*Object{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers + Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers } } diff --git a/vendor/github.com/d5/tengo/objects/compiled_function.go b/vendor/github.com/d5/tengo/objects/compiled_function.go index d20f2375..606e3d90 100644 --- a/vendor/github.com/d5/tengo/objects/compiled_function.go +++ b/vendor/github.com/d5/tengo/objects/compiled_function.go @@ -47,3 +47,14 @@ func (o *CompiledFunction) IsFalsy() bool { func (o *CompiledFunction) Equals(x Object) bool { return false } + +// SourcePos returns the source position of the instruction at ip. +func (o *CompiledFunction) SourcePos(ip int) source.Pos { + for ip >= 0 { + if p, ok := o.SourceMap[ip]; ok { + return p + } + ip-- + } + return source.NoPos +} diff --git a/vendor/github.com/d5/tengo/objects/continue.go b/vendor/github.com/d5/tengo/objects/continue.go deleted file mode 100644 index 8094e686..00000000 --- a/vendor/github.com/d5/tengo/objects/continue.go +++ /dev/null @@ -1,38 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// Continue represents a continue statement. -type Continue struct { -} - -// TypeName returns the name of the type. -func (o *Continue) TypeName() string { - return "continue" -} - -func (o *Continue) String() string { - return "<continue>" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Continue) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Continue) Copy() Object { - return &Continue{} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Continue) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Continue) Equals(x Object) bool { - return false -} diff --git a/vendor/github.com/d5/tengo/objects/conversion.go b/vendor/github.com/d5/tengo/objects/conversion.go index 714f2617..d7cb3a03 100644 --- a/vendor/github.com/d5/tengo/objects/conversion.go +++ b/vendor/github.com/d5/tengo/objects/conversion.go @@ -1,6 +1,7 @@ package objects import ( + "errors" "fmt" "strconv" "time" @@ -158,8 +159,8 @@ func ToTime(o Object) (v time.Time, ok bool) { return } -// objectToInterface attempts to convert an object o to an interface{} value -func objectToInterface(o Object) (res interface{}) { +// ToInterface attempts to convert an object o to an interface{} value +func ToInterface(o Object) (res interface{}) { switch o := o.(type) { case *Int: res = o.Value @@ -176,13 +177,29 @@ func objectToInterface(o Object) (res interface{}) { case *Array: res = make([]interface{}, len(o.Value)) for i, val := range o.Value { - res.([]interface{})[i] = objectToInterface(val) + res.([]interface{})[i] = ToInterface(val) + } + case *ImmutableArray: + res = make([]interface{}, len(o.Value)) + for i, val := range o.Value { + res.([]interface{})[i] = ToInterface(val) } case *Map: res = make(map[string]interface{}) for key, v := range o.Value { - res.(map[string]interface{})[key] = objectToInterface(v) + res.(map[string]interface{})[key] = ToInterface(v) + } + case *ImmutableMap: + res = make(map[string]interface{}) + for key, v := range o.Value { + res.(map[string]interface{})[key] = ToInterface(v) } + case *Time: + res = o.Value + case *Error: + res = errors.New(o.String()) + case *Undefined: + res = nil case Object: return o } diff --git a/vendor/github.com/d5/tengo/objects/count_objects.go b/vendor/github.com/d5/tengo/objects/count_objects.go new file mode 100644 index 00000000..8c482eb3 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/count_objects.go @@ -0,0 +1,31 @@ +package objects + +// CountObjects returns the number of objects that a given object o contains. +// For scalar value types, it will always be 1. For compound value types, +// this will include its elements and all of their elements recursively. +func CountObjects(o Object) (c int) { + c = 1 + + switch o := o.(type) { + case *Array: + for _, v := range o.Value { + c += CountObjects(v) + } + case *ImmutableArray: + for _, v := range o.Value { + c += CountObjects(v) + } + case *Map: + for _, v := range o.Value { + c += CountObjects(v) + } + case *ImmutableMap: + for _, v := range o.Value { + c += CountObjects(v) + } + case *Error: + c += CountObjects(o.Value) + } + + return +} diff --git a/vendor/github.com/d5/tengo/objects/importable.go b/vendor/github.com/d5/tengo/objects/importable.go new file mode 100644 index 00000000..9fd86ae8 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/importable.go @@ -0,0 +1,7 @@ +package objects + +// Importable interface represents importable module instance. +type Importable interface { + // Import should return either an Object or module source code ([]byte). + Import(moduleName string) (interface{}, error) +} diff --git a/vendor/github.com/d5/tengo/objects/module_map.go b/vendor/github.com/d5/tengo/objects/module_map.go new file mode 100644 index 00000000..874b8a2b --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/module_map.go @@ -0,0 +1,77 @@ +package objects + +// ModuleMap represents a set of named modules. +// Use NewModuleMap to create a new module map. +type ModuleMap struct { + m map[string]Importable +} + +// NewModuleMap creates a new module map. +func NewModuleMap() *ModuleMap { + return &ModuleMap{ + m: make(map[string]Importable), + } +} + +// Add adds an import module. +func (m *ModuleMap) Add(name string, module Importable) { + m.m[name] = module +} + +// AddBuiltinModule adds a builtin module. +func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]Object) { + m.m[name] = &BuiltinModule{Attrs: attrs} +} + +// AddSourceModule adds a source module. +func (m *ModuleMap) AddSourceModule(name string, src []byte) { + m.m[name] = &SourceModule{Src: src} +} + +// Remove removes a named module. +func (m *ModuleMap) Remove(name string) { + delete(m.m, name) +} + +// Get returns an import module identified by name. +// It returns if the name is not found. +func (m *ModuleMap) Get(name string) Importable { + return m.m[name] +} + +// GetBuiltinModule returns a builtin module identified by name. +// It returns if the name is not found or the module is not a builtin module. +func (m *ModuleMap) GetBuiltinModule(name string) *BuiltinModule { + mod, _ := m.m[name].(*BuiltinModule) + return mod +} + +// GetSourceModule returns a source module identified by name. +// It returns if the name is not found or the module is not a source module. +func (m *ModuleMap) GetSourceModule(name string) *SourceModule { + mod, _ := m.m[name].(*SourceModule) + return mod +} + +// Copy creates a copy of the module map. +func (m *ModuleMap) Copy() *ModuleMap { + c := &ModuleMap{ + m: make(map[string]Importable), + } + for name, mod := range m.m { + c.m[name] = mod + } + return c +} + +// Len returns the number of named modules. +func (m *ModuleMap) Len() int { + return len(m.m) +} + +// AddMap adds named modules from another module map. +func (m *ModuleMap) AddMap(o *ModuleMap) { + for name, mod := range o.m { + m.m[name] = mod + } +} diff --git a/vendor/github.com/d5/tengo/objects/object_ptr.go b/vendor/github.com/d5/tengo/objects/object_ptr.go new file mode 100644 index 00000000..2c87c561 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/object_ptr.go @@ -0,0 +1,41 @@ +package objects + +import ( + "github.com/d5/tengo/compiler/token" +) + +// ObjectPtr represents a free variable. +type ObjectPtr struct { + Value *Object +} + +func (o *ObjectPtr) String() string { + return "free-var" +} + +// TypeName returns the name of the type. +func (o *ObjectPtr) TypeName() string { + return "<free-var>" +} + +// BinaryOp returns another object that is the result of +// a given binary operator and a right-hand side object. +func (o *ObjectPtr) BinaryOp(op token.Token, rhs Object) (Object, error) { + return nil, ErrInvalidOperator +} + +// Copy returns a copy of the type. +func (o *ObjectPtr) Copy() Object { + return o +} + +// IsFalsy returns true if the value of the type is falsy. +func (o *ObjectPtr) IsFalsy() bool { + return o.Value == nil +} + +// Equals returns true if the value of the type +// is equal to the value of another object. +func (o *ObjectPtr) Equals(x Object) bool { + return o == x +} diff --git a/vendor/github.com/d5/tengo/objects/return_value.go b/vendor/github.com/d5/tengo/objects/return_value.go deleted file mode 100644 index f7ef1dc4..00000000 --- a/vendor/github.com/d5/tengo/objects/return_value.go +++ /dev/null @@ -1,39 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// ReturnValue represents a value that is being returned. -type ReturnValue struct { - Value Object -} - -// TypeName returns the name of the type. -func (o *ReturnValue) TypeName() string { - return "return-value" -} - -func (o *ReturnValue) String() string { - return "<return-value>" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *ReturnValue) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *ReturnValue) Copy() Object { - return &ReturnValue{Value: o.Copy()} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *ReturnValue) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *ReturnValue) Equals(x Object) bool { - return false -} diff --git a/vendor/github.com/d5/tengo/objects/source_module.go b/vendor/github.com/d5/tengo/objects/source_module.go new file mode 100644 index 00000000..577fddf2 --- /dev/null +++ b/vendor/github.com/d5/tengo/objects/source_module.go @@ -0,0 +1,11 @@ +package objects + +// SourceModule is an importable module that's written in Tengo. +type SourceModule struct { + Src []byte +} + +// Import returns a module source code. +func (m *SourceModule) Import(_ string) (interface{}, error) { + return m.Src, nil +} diff --git a/vendor/github.com/d5/tengo/objects/user_function.go b/vendor/github.com/d5/tengo/objects/user_function.go index 1d9bb4f7..a896788b 100644 --- a/vendor/github.com/d5/tengo/objects/user_function.go +++ b/vendor/github.com/d5/tengo/objects/user_function.go @@ -6,8 +6,9 @@ import ( // UserFunction represents a user function. type UserFunction struct { - Name string - Value CallableFunc + Name string + Value CallableFunc + EncodingID string } // TypeName returns the name of the type. |