From 9d84d6dd643c4017074e81465671cd9b25f9539a Mon Sep 17 00:00:00 2001 From: Wim Date: Thu, 9 Jan 2020 21:52:19 +0100 Subject: Update to tengo v2 (#976) --- vendor/github.com/d5/tengo/objects/array.go | 130 --- .../github.com/d5/tengo/objects/array_iterator.go | 57 - vendor/github.com/d5/tengo/objects/bool.go | 64 -- .../github.com/d5/tengo/objects/builtin_append.go | 21 - .../github.com/d5/tengo/objects/builtin_convert.go | 169 --- vendor/github.com/d5/tengo/objects/builtin_copy.go | 9 - .../github.com/d5/tengo/objects/builtin_format.go | 27 - .../d5/tengo/objects/builtin_function.go | 47 - vendor/github.com/d5/tengo/objects/builtin_len.go | 29 - .../github.com/d5/tengo/objects/builtin_module.go | 23 - vendor/github.com/d5/tengo/objects/builtin_type.go | 9 - .../d5/tengo/objects/builtin_type_checks.go | 195 ---- vendor/github.com/d5/tengo/objects/builtins.go | 118 -- vendor/github.com/d5/tengo/objects/bytes.go | 89 -- .../github.com/d5/tengo/objects/bytes_iterator.go | 57 - vendor/github.com/d5/tengo/objects/callable.go | 9 - .../github.com/d5/tengo/objects/callable_func.go | 4 - vendor/github.com/d5/tengo/objects/char.go | 119 -- vendor/github.com/d5/tengo/objects/closure.go | 45 - .../d5/tengo/objects/compiled_function.go | 62 - vendor/github.com/d5/tengo/objects/conversion.go | 276 ----- .../github.com/d5/tengo/objects/count_objects.go | 31 - vendor/github.com/d5/tengo/objects/error.go | 47 - vendor/github.com/d5/tengo/objects/errors.go | 38 - vendor/github.com/d5/tengo/objects/float.go | 146 --- vendor/github.com/d5/tengo/objects/formatter.go | 1212 -------------------- .../github.com/d5/tengo/objects/immutable_array.go | 109 -- .../github.com/d5/tengo/objects/immutable_map.go | 105 -- vendor/github.com/d5/tengo/objects/importable.go | 7 - .../d5/tengo/objects/index_assignable.go | 9 - vendor/github.com/d5/tengo/objects/indexable.go | 9 - vendor/github.com/d5/tengo/objects/int.go | 198 ---- vendor/github.com/d5/tengo/objects/iterable.go | 7 - vendor/github.com/d5/tengo/objects/iterator.go | 15 - vendor/github.com/d5/tengo/objects/map.go | 118 -- vendor/github.com/d5/tengo/objects/map_iterator.go | 62 - vendor/github.com/d5/tengo/objects/module_map.go | 77 -- vendor/github.com/d5/tengo/objects/object.go | 30 - vendor/github.com/d5/tengo/objects/object_ptr.go | 41 - vendor/github.com/d5/tengo/objects/objects.go | 12 - .../github.com/d5/tengo/objects/source_module.go | 11 - vendor/github.com/d5/tengo/objects/string.go | 103 -- .../github.com/d5/tengo/objects/string_iterator.go | 57 - vendor/github.com/d5/tengo/objects/time.go | 89 -- vendor/github.com/d5/tengo/objects/undefined.go | 62 - .../github.com/d5/tengo/objects/user_function.go | 48 - 46 files changed, 4202 deletions(-) delete mode 100644 vendor/github.com/d5/tengo/objects/array.go delete mode 100644 vendor/github.com/d5/tengo/objects/array_iterator.go delete mode 100644 vendor/github.com/d5/tengo/objects/bool.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_append.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_convert.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_copy.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_format.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_function.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_len.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_module.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_type.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtin_type_checks.go delete mode 100644 vendor/github.com/d5/tengo/objects/builtins.go delete mode 100644 vendor/github.com/d5/tengo/objects/bytes.go delete mode 100644 vendor/github.com/d5/tengo/objects/bytes_iterator.go delete mode 100644 vendor/github.com/d5/tengo/objects/callable.go delete mode 100644 vendor/github.com/d5/tengo/objects/callable_func.go delete mode 100644 vendor/github.com/d5/tengo/objects/char.go delete mode 100644 vendor/github.com/d5/tengo/objects/closure.go delete mode 100644 vendor/github.com/d5/tengo/objects/compiled_function.go delete mode 100644 vendor/github.com/d5/tengo/objects/conversion.go delete mode 100644 vendor/github.com/d5/tengo/objects/count_objects.go delete mode 100644 vendor/github.com/d5/tengo/objects/error.go delete mode 100644 vendor/github.com/d5/tengo/objects/errors.go delete mode 100644 vendor/github.com/d5/tengo/objects/float.go delete mode 100644 vendor/github.com/d5/tengo/objects/formatter.go delete mode 100644 vendor/github.com/d5/tengo/objects/immutable_array.go delete mode 100644 vendor/github.com/d5/tengo/objects/immutable_map.go delete mode 100644 vendor/github.com/d5/tengo/objects/importable.go delete mode 100644 vendor/github.com/d5/tengo/objects/index_assignable.go delete mode 100644 vendor/github.com/d5/tengo/objects/indexable.go delete mode 100644 vendor/github.com/d5/tengo/objects/int.go delete mode 100644 vendor/github.com/d5/tengo/objects/iterable.go delete mode 100644 vendor/github.com/d5/tengo/objects/iterator.go delete mode 100644 vendor/github.com/d5/tengo/objects/map.go delete mode 100644 vendor/github.com/d5/tengo/objects/map_iterator.go delete mode 100644 vendor/github.com/d5/tengo/objects/module_map.go delete mode 100644 vendor/github.com/d5/tengo/objects/object.go delete mode 100644 vendor/github.com/d5/tengo/objects/object_ptr.go delete mode 100644 vendor/github.com/d5/tengo/objects/objects.go delete mode 100644 vendor/github.com/d5/tengo/objects/source_module.go delete mode 100644 vendor/github.com/d5/tengo/objects/string.go delete mode 100644 vendor/github.com/d5/tengo/objects/string_iterator.go delete mode 100644 vendor/github.com/d5/tengo/objects/time.go delete mode 100644 vendor/github.com/d5/tengo/objects/undefined.go delete mode 100644 vendor/github.com/d5/tengo/objects/user_function.go (limited to 'vendor/github.com/d5/tengo/objects') diff --git a/vendor/github.com/d5/tengo/objects/array.go b/vendor/github.com/d5/tengo/objects/array.go deleted file mode 100644 index 1e917c59..00000000 --- a/vendor/github.com/d5/tengo/objects/array.go +++ /dev/null @@ -1,130 +0,0 @@ -package objects - -import ( - "fmt" - "strings" - - "github.com/d5/tengo/compiler/token" -) - -// Array represents an array of objects. -type Array struct { - Value []Object -} - -// TypeName returns the name of the type. -func (o *Array) TypeName() string { - return "array" -} - -func (o *Array) String() string { - var elements []string - for _, e := range o.Value { - elements = append(elements, e.String()) - } - - return fmt.Sprintf("[%s]", strings.Join(elements, ", ")) -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error) { - if rhs, ok := rhs.(*Array); ok { - switch op { - case token.Add: - if len(rhs.Value) == 0 { - return o, nil - } - return &Array{Value: append(o.Value, rhs.Value...)}, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Array) Copy() Object { - var c []Object - for _, elem := range o.Value { - c = append(c, elem.Copy()) - } - - return &Array{Value: c} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Array) IsFalsy() bool { - return len(o.Value) == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Array) Equals(x Object) bool { - var xVal []Object - switch x := x.(type) { - case *Array: - xVal = x.Value - case *ImmutableArray: - xVal = x.Value - default: - return false - } - - if len(o.Value) != len(xVal) { - return false - } - - for i, e := range o.Value { - if !e.Equals(xVal[i]) { - return false - } - } - - return true -} - -// IndexGet returns an element at a given index. -func (o *Array) IndexGet(index Object) (res Object, err error) { - intIdx, ok := index.(*Int) - if !ok { - err = ErrInvalidIndexType - return - } - - idxVal := int(intIdx.Value) - - if idxVal < 0 || idxVal >= len(o.Value) { - res = UndefinedValue - return - } - - res = o.Value[idxVal] - - return -} - -// IndexSet sets an element at a given index. -func (o *Array) IndexSet(index, value Object) (err error) { - intIdx, ok := ToInt(index) - if !ok { - err = ErrInvalidIndexType - return - } - - if intIdx < 0 || intIdx >= len(o.Value) { - err = ErrIndexOutOfBounds - return - } - - o.Value[intIdx] = value - - return nil -} - -// Iterate creates an array iterator. -func (o *Array) Iterate() Iterator { - return &ArrayIterator{ - v: o.Value, - l: len(o.Value), - } -} diff --git a/vendor/github.com/d5/tengo/objects/array_iterator.go b/vendor/github.com/d5/tengo/objects/array_iterator.go deleted file mode 100644 index 204faa41..00000000 --- a/vendor/github.com/d5/tengo/objects/array_iterator.go +++ /dev/null @@ -1,57 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// ArrayIterator is an iterator for an array. -type ArrayIterator struct { - v []Object - i int - l int -} - -// TypeName returns the name of the type. -func (i *ArrayIterator) TypeName() string { - return "array-iterator" -} - -func (i *ArrayIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *ArrayIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *ArrayIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *ArrayIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *ArrayIterator) Copy() Object { - return &ArrayIterator{v: i.v, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *ArrayIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *ArrayIterator) Key() Object { - return &Int{Value: int64(i.i - 1)} -} - -// Value returns the value of the current element. -func (i *ArrayIterator) Value() Object { - return i.v[i.i-1] -} diff --git a/vendor/github.com/d5/tengo/objects/bool.go b/vendor/github.com/d5/tengo/objects/bool.go deleted file mode 100644 index ac9949e4..00000000 --- a/vendor/github.com/d5/tengo/objects/bool.go +++ /dev/null @@ -1,64 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/token" -) - -// Bool represents a boolean value. -type Bool struct { - // this is intentionally non-public to force using objects.TrueValue and FalseValue always - value bool -} - -func (o *Bool) String() string { - if o.value { - return "true" - } - - return "false" -} - -// TypeName returns the name of the type. -func (o *Bool) TypeName() string { - return "bool" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Bool) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Bool) Copy() Object { - return o -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Bool) IsFalsy() bool { - return !o.value -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Bool) Equals(x Object) bool { - return o == x -} - -// GobDecode decodes bool value from input bytes. -func (o *Bool) GobDecode(b []byte) (err error) { - o.value = b[0] == 1 - - return -} - -// GobEncode encodes bool values into bytes. -func (o *Bool) GobEncode() (b []byte, err error) { - if o.value { - b = []byte{1} - } else { - b = []byte{0} - } - - return -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_append.go b/vendor/github.com/d5/tengo/objects/builtin_append.go deleted file mode 100644 index 9fb14b82..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_append.go +++ /dev/null @@ -1,21 +0,0 @@ -package objects - -// append(arr, items...) -func builtinAppend(args ...Object) (Object, error) { - if len(args) < 2 { - return nil, ErrWrongNumArguments - } - - switch arg := args[0].(type) { - case *Array: - return &Array{Value: append(arg.Value, args[1:]...)}, nil - case *ImmutableArray: - return &Array{Value: append(arg.Value, args[1:]...)}, nil - default: - return nil, ErrInvalidArgumentType{ - Name: "first", - Expected: "array", - Found: arg.TypeName(), - } - } -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_convert.go b/vendor/github.com/d5/tengo/objects/builtin_convert.go deleted file mode 100644 index b5f2d05d..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_convert.go +++ /dev/null @@ -1,169 +0,0 @@ -package objects - -import "github.com/d5/tengo" - -func builtinString(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*String); ok { - return args[0], nil - } - - v, ok := ToString(args[0]) - if ok { - if len(v) > tengo.MaxStringLen { - return nil, ErrStringLimit - } - - return &String{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} - -func builtinInt(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Int); ok { - return args[0], nil - } - - v, ok := ToInt64(args[0]) - if ok { - return &Int{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} - -func builtinFloat(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Float); ok { - return args[0], nil - } - - v, ok := ToFloat64(args[0]) - if ok { - return &Float{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} - -func builtinBool(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Bool); ok { - return args[0], nil - } - - v, ok := ToBool(args[0]) - if ok { - if v { - return TrueValue, nil - } - - return FalseValue, nil - } - - return UndefinedValue, nil -} - -func builtinChar(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Char); ok { - return args[0], nil - } - - v, ok := ToRune(args[0]) - if ok { - return &Char{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} - -func builtinBytes(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - // bytes(N) => create a new bytes with given size N - if n, ok := args[0].(*Int); ok { - if n.Value > int64(tengo.MaxBytesLen) { - return nil, ErrBytesLimit - } - - return &Bytes{Value: make([]byte, int(n.Value))}, nil - } - - v, ok := ToByteSlice(args[0]) - if ok { - if len(v) > tengo.MaxBytesLen { - return nil, ErrBytesLimit - } - - return &Bytes{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} - -func builtinTime(args ...Object) (Object, error) { - argsLen := len(args) - if !(argsLen == 1 || argsLen == 2) { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Time); ok { - return args[0], nil - } - - v, ok := ToTime(args[0]) - if ok { - return &Time{Value: v}, nil - } - - if argsLen == 2 { - return args[1], nil - } - - return UndefinedValue, nil -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_copy.go b/vendor/github.com/d5/tengo/objects/builtin_copy.go deleted file mode 100644 index 4b254b2b..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_copy.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -func builtinCopy(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - return args[0].Copy(), nil -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_format.go b/vendor/github.com/d5/tengo/objects/builtin_format.go deleted file mode 100644 index 1f0e75ed..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_format.go +++ /dev/null @@ -1,27 +0,0 @@ -package objects - -func builtinFormat(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 - } - - s, err := Format(format.Value, args[1:]...) - if err != nil { - return nil, err - } - - return &String{Value: s}, nil -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_function.go b/vendor/github.com/d5/tengo/objects/builtin_function.go deleted file mode 100644 index 1d021617..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_function.go +++ /dev/null @@ -1,47 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/token" -) - -// BuiltinFunction represents a builtin function. -type BuiltinFunction struct { - Name string - Value CallableFunc -} - -// TypeName returns the name of the type. -func (o *BuiltinFunction) TypeName() string { - return "builtin-function:" + o.Name -} - -func (o *BuiltinFunction) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *BuiltinFunction) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *BuiltinFunction) Copy() Object { - return &BuiltinFunction{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *BuiltinFunction) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *BuiltinFunction) Equals(x Object) bool { - return false -} - -// Call executes a builtin function. -func (o *BuiltinFunction) Call(args ...Object) (Object, error) { - return o.Value(args...) -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_len.go b/vendor/github.com/d5/tengo/objects/builtin_len.go deleted file mode 100644 index 39fbedd8..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_len.go +++ /dev/null @@ -1,29 +0,0 @@ -package objects - -// len(obj object) => int -func builtinLen(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - switch arg := args[0].(type) { - case *Array: - return &Int{Value: int64(len(arg.Value))}, nil - case *ImmutableArray: - return &Int{Value: int64(len(arg.Value))}, nil - case *String: - return &Int{Value: int64(len(arg.Value))}, nil - case *Bytes: - return &Int{Value: int64(len(arg.Value))}, nil - case *Map: - return &Int{Value: int64(len(arg.Value))}, nil - case *ImmutableMap: - return &Int{Value: int64(len(arg.Value))}, nil - default: - return nil, ErrInvalidArgumentType{ - Name: "first", - Expected: "array/string/bytes/map", - Found: arg.TypeName(), - } - } -} diff --git a/vendor/github.com/d5/tengo/objects/builtin_module.go b/vendor/github.com/d5/tengo/objects/builtin_module.go deleted file mode 100644 index 0ad1d99d..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_module.go +++ /dev/null @@ -1,23 +0,0 @@ -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_type.go b/vendor/github.com/d5/tengo/objects/builtin_type.go deleted file mode 100644 index 376c26bb..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_type.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -func builtinTypeName(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - return &String{Value: args[0].TypeName()}, 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 deleted file mode 100644 index d1e8471d..00000000 --- a/vendor/github.com/d5/tengo/objects/builtin_type_checks.go +++ /dev/null @@ -1,195 +0,0 @@ -package objects - -func builtinIsString(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*String); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsInt(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Int); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsFloat(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Float); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsBool(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Bool); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsChar(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Char); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsBytes(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Bytes); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsArray(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Array); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsImmutableArray(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*ImmutableArray); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsMap(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Map); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsImmutableMap(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*ImmutableMap); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsTime(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Time); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsError(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if _, ok := args[0].(*Error); ok { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsUndefined(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - if args[0] == UndefinedValue { - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsFunction(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - switch args[0].(type) { - case *CompiledFunction, *Closure: - return TrueValue, nil - } - - return FalseValue, nil -} - -func builtinIsCallable(args ...Object) (Object, error) { - if len(args) != 1 { - return nil, ErrWrongNumArguments - } - - switch args[0].(type) { - case *CompiledFunction, *Closure, Callable: // BuiltinFunction is Callable - return TrueValue, nil - } - - 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 deleted file mode 100644 index 773636ec..00000000 --- a/vendor/github.com/d5/tengo/objects/builtins.go +++ /dev/null @@ -1,118 +0,0 @@ -package objects - -// Builtins contains all default builtin functions. -// Use GetBuiltinFunctions instead of accessing Builtins directly. -var Builtins = []*BuiltinFunction{ - { - Name: "len", - Value: builtinLen, - }, - { - Name: "copy", - Value: builtinCopy, - }, - { - Name: "append", - Value: builtinAppend, - }, - { - Name: "string", - Value: builtinString, - }, - { - Name: "int", - Value: builtinInt, - }, - { - Name: "bool", - Value: builtinBool, - }, - { - Name: "float", - Value: builtinFloat, - }, - { - Name: "char", - Value: builtinChar, - }, - { - Name: "bytes", - Value: builtinBytes, - }, - { - Name: "time", - Value: builtinTime, - }, - { - Name: "is_int", - Value: builtinIsInt, - }, - { - Name: "is_float", - Value: builtinIsFloat, - }, - { - Name: "is_string", - Value: builtinIsString, - }, - { - Name: "is_bool", - Value: builtinIsBool, - }, - { - Name: "is_char", - Value: builtinIsChar, - }, - { - Name: "is_bytes", - Value: builtinIsBytes, - }, - { - Name: "is_array", - Value: builtinIsArray, - }, - { - Name: "is_immutable_array", - Value: builtinIsImmutableArray, - }, - { - Name: "is_map", - Value: builtinIsMap, - }, - { - Name: "is_immutable_map", - Value: builtinIsImmutableMap, - }, - { - Name: "is_iterable", - Value: builtinIsIterable, - }, - { - Name: "is_time", - Value: builtinIsTime, - }, - { - Name: "is_error", - Value: builtinIsError, - }, - { - Name: "is_undefined", - Value: builtinIsUndefined, - }, - { - Name: "is_function", - Value: builtinIsFunction, - }, - { - Name: "is_callable", - Value: builtinIsCallable, - }, - { - Name: "type_name", - Value: builtinTypeName, - }, - { - Name: "format", - Value: builtinFormat, - }, -} diff --git a/vendor/github.com/d5/tengo/objects/bytes.go b/vendor/github.com/d5/tengo/objects/bytes.go deleted file mode 100644 index 5159c22f..00000000 --- a/vendor/github.com/d5/tengo/objects/bytes.go +++ /dev/null @@ -1,89 +0,0 @@ -package objects - -import ( - "bytes" - - "github.com/d5/tengo" - "github.com/d5/tengo/compiler/token" -) - -// Bytes represents a byte array. -type Bytes struct { - Value []byte -} - -func (o *Bytes) String() string { - return string(o.Value) -} - -// TypeName returns the name of the type. -func (o *Bytes) TypeName() string { - return "bytes" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch op { - case token.Add: - switch rhs := rhs.(type) { - case *Bytes: - if len(o.Value)+len(rhs.Value) > tengo.MaxBytesLen { - return nil, ErrBytesLimit - } - - return &Bytes{Value: append(o.Value, rhs.Value...)}, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Bytes) Copy() Object { - return &Bytes{Value: append([]byte{}, o.Value...)} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Bytes) IsFalsy() bool { - return len(o.Value) == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Bytes) Equals(x Object) bool { - t, ok := x.(*Bytes) - if !ok { - return false - } - - return bytes.Equal(o.Value, t.Value) -} - -// IndexGet returns an element (as Int) at a given index. -func (o *Bytes) IndexGet(index Object) (res Object, err error) { - intIdx, ok := index.(*Int) - if !ok { - err = ErrInvalidIndexType - return - } - - idxVal := int(intIdx.Value) - - if idxVal < 0 || idxVal >= len(o.Value) { - res = UndefinedValue - return - } - - res = &Int{Value: int64(o.Value[idxVal])} - - 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 deleted file mode 100644 index 18a36e17..00000000 --- a/vendor/github.com/d5/tengo/objects/bytes_iterator.go +++ /dev/null @@ -1,57 +0,0 @@ -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 "" -} - -// 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/callable.go b/vendor/github.com/d5/tengo/objects/callable.go deleted file mode 100644 index a066e1b9..00000000 --- a/vendor/github.com/d5/tengo/objects/callable.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -// Callable represents an object that can be called like a function. -type Callable interface { - // Call should take an arbitrary number of arguments - // and returns a return value and/or an error, - // which the VM will consider as a run-time error. - Call(args ...Object) (ret Object, err error) -} diff --git a/vendor/github.com/d5/tengo/objects/callable_func.go b/vendor/github.com/d5/tengo/objects/callable_func.go deleted file mode 100644 index ad25e65d..00000000 --- a/vendor/github.com/d5/tengo/objects/callable_func.go +++ /dev/null @@ -1,4 +0,0 @@ -package objects - -// CallableFunc is a function signature for the callable functions. -type CallableFunc = func(args ...Object) (ret Object, err error) diff --git a/vendor/github.com/d5/tengo/objects/char.go b/vendor/github.com/d5/tengo/objects/char.go deleted file mode 100644 index 4458bd12..00000000 --- a/vendor/github.com/d5/tengo/objects/char.go +++ /dev/null @@ -1,119 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/token" -) - -// Char represents a character value. -type Char struct { - Value rune -} - -func (o *Char) String() string { - return string(o.Value) -} - -// TypeName returns the name of the type. -func (o *Char) TypeName() string { - return "char" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Char) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch rhs := rhs.(type) { - case *Char: - switch op { - case token.Add: - r := o.Value + rhs.Value - if r == o.Value { - return o, nil - } - return &Char{Value: r}, nil - case token.Sub: - r := o.Value - rhs.Value - if r == o.Value { - return o, nil - } - return &Char{Value: r}, nil - case token.Less: - if o.Value < rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value > rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value <= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value >= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - } - case *Int: - switch op { - case token.Add: - r := o.Value + rune(rhs.Value) - if r == o.Value { - return o, nil - } - return &Char{Value: r}, nil - case token.Sub: - r := o.Value - rune(rhs.Value) - if r == o.Value { - return o, nil - } - return &Char{Value: r}, nil - case token.Less: - if int64(o.Value) < rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if int64(o.Value) > rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if int64(o.Value) <= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if int64(o.Value) >= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Char) Copy() Object { - return &Char{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Char) IsFalsy() bool { - return o.Value == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Char) Equals(x Object) bool { - t, ok := x.(*Char) - if !ok { - return false - } - - return o.Value == t.Value -} diff --git a/vendor/github.com/d5/tengo/objects/closure.go b/vendor/github.com/d5/tengo/objects/closure.go deleted file mode 100644 index 06058b23..00000000 --- a/vendor/github.com/d5/tengo/objects/closure.go +++ /dev/null @@ -1,45 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/token" -) - -// Closure represents a function closure. -type Closure struct { - Fn *CompiledFunction - Free []*ObjectPtr -} - -// TypeName returns the name of the type. -func (o *Closure) TypeName() string { - return "closure" -} - -func (o *Closure) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Closure) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Closure) Copy() Object { - return &Closure{ - Fn: o.Fn.Copy().(*CompiledFunction), - Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers - } -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Closure) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Closure) Equals(x Object) bool { - return false -} diff --git a/vendor/github.com/d5/tengo/objects/compiled_function.go b/vendor/github.com/d5/tengo/objects/compiled_function.go deleted file mode 100644 index d42e69ec..00000000 --- a/vendor/github.com/d5/tengo/objects/compiled_function.go +++ /dev/null @@ -1,62 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/source" - "github.com/d5/tengo/compiler/token" -) - -// CompiledFunction represents a compiled function. -type CompiledFunction struct { - Instructions []byte - NumLocals int // number of local variables (including function parameters) - NumParameters int - VarArgs bool - SourceMap map[int]source.Pos -} - -// TypeName returns the name of the type. -func (o *CompiledFunction) TypeName() string { - return "compiled-function" -} - -func (o *CompiledFunction) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *CompiledFunction) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *CompiledFunction) Copy() Object { - return &CompiledFunction{ - Instructions: append([]byte{}, o.Instructions...), - NumLocals: o.NumLocals, - NumParameters: o.NumParameters, - VarArgs: o.VarArgs, - } -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *CompiledFunction) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -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/conversion.go b/vendor/github.com/d5/tengo/objects/conversion.go deleted file mode 100644 index 27514132..00000000 --- a/vendor/github.com/d5/tengo/objects/conversion.go +++ /dev/null @@ -1,276 +0,0 @@ -package objects - -import ( - "errors" - "fmt" - "strconv" - "time" - - "github.com/d5/tengo" -) - -// ToString will try to convert object o to string value. -func ToString(o Object) (v string, ok bool) { - if o == UndefinedValue { - //ok = false - return - } - - ok = true - - if str, isStr := o.(*String); isStr { - v = str.Value - } else { - v = o.String() - } - - return -} - -// ToInt will try to convert object o to int value. -func ToInt(o Object) (v int, ok bool) { - switch o := o.(type) { - case *Int: - v = int(o.Value) - ok = true - case *Float: - v = int(o.Value) - ok = true - case *Char: - v = int(o.Value) - ok = true - case *Bool: - if o == TrueValue { - v = 1 - } - ok = true - case *String: - c, err := strconv.ParseInt(o.Value, 10, 64) - if err == nil { - v = int(c) - ok = true - } - } - - //ok = false - return -} - -// ToInt64 will try to convert object o to int64 value. -func ToInt64(o Object) (v int64, ok bool) { - switch o := o.(type) { - case *Int: - v = o.Value - ok = true - case *Float: - v = int64(o.Value) - ok = true - case *Char: - v = int64(o.Value) - ok = true - case *Bool: - if o == TrueValue { - v = 1 - } - ok = true - case *String: - c, err := strconv.ParseInt(o.Value, 10, 64) - if err == nil { - v = c - ok = true - } - } - - //ok = false - return -} - -// ToFloat64 will try to convert object o to float64 value. -func ToFloat64(o Object) (v float64, ok bool) { - switch o := o.(type) { - case *Int: - v = float64(o.Value) - ok = true - case *Float: - v = o.Value - ok = true - case *String: - c, err := strconv.ParseFloat(o.Value, 64) - if err == nil { - v = c - ok = true - } - } - - //ok = false - return -} - -// ToBool will try to convert object o to bool value. -func ToBool(o Object) (v bool, ok bool) { - ok = true - v = !o.IsFalsy() - - return -} - -// ToRune will try to convert object o to rune value. -func ToRune(o Object) (v rune, ok bool) { - switch o := o.(type) { - case *Int: - v = rune(o.Value) - ok = true - case *Char: - v = rune(o.Value) - ok = true - } - - //ok = false - return -} - -// ToByteSlice will try to convert object o to []byte value. -func ToByteSlice(o Object) (v []byte, ok bool) { - switch o := o.(type) { - case *Bytes: - v = o.Value - ok = true - case *String: - v = []byte(o.Value) - ok = true - } - - //ok = false - return -} - -// ToTime will try to convert object o to time.Time value. -func ToTime(o Object) (v time.Time, ok bool) { - switch o := o.(type) { - case *Time: - v = o.Value - ok = true - case *Int: - v = time.Unix(o.Value, 0) - ok = true - } - - //ok = false - return -} - -// 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 - case *String: - res = o.Value - case *Float: - res = o.Value - case *Bool: - res = o == TrueValue - case *Char: - res = o.Value - case *Bytes: - res = o.Value - case *Array: - res = make([]interface{}, len(o.Value)) - for i, val := range o.Value { - 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] = 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 - } - - return -} - -// FromInterface will attempt to convert an interface{} v to a Tengo Object -func FromInterface(v interface{}) (Object, error) { - switch v := v.(type) { - case nil: - return UndefinedValue, nil - case string: - if len(v) > tengo.MaxStringLen { - return nil, ErrStringLimit - } - return &String{Value: v}, nil - case int64: - return &Int{Value: v}, nil - case int: - return &Int{Value: int64(v)}, nil - case bool: - if v { - return TrueValue, nil - } - return FalseValue, nil - case rune: - return &Char{Value: v}, nil - case byte: - return &Char{Value: rune(v)}, nil - case float64: - return &Float{Value: v}, nil - case []byte: - if len(v) > tengo.MaxBytesLen { - return nil, ErrBytesLimit - } - return &Bytes{Value: v}, nil - case error: - return &Error{Value: &String{Value: v.Error()}}, nil - case map[string]Object: - return &Map{Value: v}, nil - case map[string]interface{}: - kv := make(map[string]Object) - for vk, vv := range v { - vo, err := FromInterface(vv) - if err != nil { - return nil, err - } - kv[vk] = vo - } - return &Map{Value: kv}, nil - case []Object: - return &Array{Value: v}, nil - case []interface{}: - arr := make([]Object, len(v)) - for i, e := range v { - vo, err := FromInterface(e) - if err != nil { - return nil, err - } - - arr[i] = vo - } - return &Array{Value: arr}, nil - case time.Time: - return &Time{Value: v}, nil - case Object: - return v, nil - case CallableFunc: - return &UserFunction{Value: v}, nil - } - - return nil, fmt.Errorf("cannot convert to object: %T", v) -} diff --git a/vendor/github.com/d5/tengo/objects/count_objects.go b/vendor/github.com/d5/tengo/objects/count_objects.go deleted file mode 100644 index 8c482eb3..00000000 --- a/vendor/github.com/d5/tengo/objects/count_objects.go +++ /dev/null @@ -1,31 +0,0 @@ -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/error.go b/vendor/github.com/d5/tengo/objects/error.go deleted file mode 100644 index be21de03..00000000 --- a/vendor/github.com/d5/tengo/objects/error.go +++ /dev/null @@ -1,47 +0,0 @@ -package objects - -import ( - "fmt" - - "github.com/d5/tengo/compiler/token" -) - -// Error represents a string value. -type Error struct { - Value Object -} - -// TypeName returns the name of the type. -func (o *Error) TypeName() string { - return "error" -} - -func (o *Error) String() string { - if o.Value != nil { - return fmt.Sprintf("error: %s", o.Value.String()) - } - - return "error" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Error) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Error) IsFalsy() bool { - return true // error is always false. -} - -// Copy returns a copy of the type. -func (o *Error) Copy() Object { - return &Error{Value: o.Value.Copy()} -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Error) Equals(x Object) bool { - return o == x // pointer equality -} diff --git a/vendor/github.com/d5/tengo/objects/errors.go b/vendor/github.com/d5/tengo/objects/errors.go deleted file mode 100644 index bcd480a1..00000000 --- a/vendor/github.com/d5/tengo/objects/errors.go +++ /dev/null @@ -1,38 +0,0 @@ -package objects - -import ( - "errors" - "fmt" -) - -// ErrIndexOutOfBounds is an error where a given index is out of the bounds. -var ErrIndexOutOfBounds = errors.New("index out of bounds") - -// ErrInvalidIndexType represents an invalid index type. -var ErrInvalidIndexType = errors.New("invalid index type") - -// ErrInvalidIndexValueType represents an invalid index value type. -var ErrInvalidIndexValueType = errors.New("invalid index value type") - -// ErrInvalidOperator represents an error for invalid operator usage. -var ErrInvalidOperator = errors.New("invalid operator") - -// ErrWrongNumArguments represents a wrong number of arguments error. -var ErrWrongNumArguments = errors.New("wrong number of arguments") - -// ErrBytesLimit represents an error where the size of bytes value exceeds the limit. -var ErrBytesLimit = errors.New("exceeding bytes size limit") - -// ErrStringLimit represents an error where the size of string value exceeds the limit. -var ErrStringLimit = errors.New("exceeding string size limit") - -// ErrInvalidArgumentType represents an invalid argument value type error. -type ErrInvalidArgumentType struct { - Name string - Expected string - Found string -} - -func (e ErrInvalidArgumentType) Error() string { - return fmt.Sprintf("invalid type for argument '%s': expected %s, found %s", e.Name, e.Expected, e.Found) -} diff --git a/vendor/github.com/d5/tengo/objects/float.go b/vendor/github.com/d5/tengo/objects/float.go deleted file mode 100644 index 65997303..00000000 --- a/vendor/github.com/d5/tengo/objects/float.go +++ /dev/null @@ -1,146 +0,0 @@ -package objects - -import ( - "math" - "strconv" - - "github.com/d5/tengo/compiler/token" -) - -// Float represents a floating point number value. -type Float struct { - Value float64 -} - -func (o *Float) String() string { - return strconv.FormatFloat(o.Value, 'f', -1, 64) -} - -// TypeName returns the name of the type. -func (o *Float) TypeName() string { - return "float" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Float) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch rhs := rhs.(type) { - case *Float: - switch op { - case token.Add: - r := o.Value + rhs.Value - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Sub: - r := o.Value - rhs.Value - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Mul: - r := o.Value * rhs.Value - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Quo: - r := o.Value / rhs.Value - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Less: - if o.Value < rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value > rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value <= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value >= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - } - case *Int: - switch op { - case token.Add: - r := o.Value + float64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Sub: - r := o.Value - float64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Mul: - r := o.Value * float64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Quo: - r := o.Value / float64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Float{Value: r}, nil - case token.Less: - if o.Value < float64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value > float64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value <= float64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value >= float64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Float) Copy() Object { - return &Float{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Float) IsFalsy() bool { - return math.IsNaN(o.Value) -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Float) Equals(x Object) bool { - t, ok := x.(*Float) - if !ok { - return false - } - - return o.Value == t.Value -} diff --git a/vendor/github.com/d5/tengo/objects/formatter.go b/vendor/github.com/d5/tengo/objects/formatter.go deleted file mode 100644 index 95d7f6b1..00000000 --- a/vendor/github.com/d5/tengo/objects/formatter.go +++ /dev/null @@ -1,1212 +0,0 @@ -package objects - -import ( - "strconv" - "sync" - "unicode/utf8" - - "github.com/d5/tengo" -) - -// Strings for use with buffer.WriteString. -// This is less overhead than using buffer.Write with byte arrays. -const ( - commaSpaceString = ", " - nilParenString = "(nil)" - percentBangString = "%!" - missingString = "(MISSING)" - badIndexString = "(BADINDEX)" - extraString = "%!(EXTRA " - badWidthString = "%!(BADWIDTH)" - badPrecString = "%!(BADPREC)" - noVerbString = "%!(NOVERB)" -) - -const ( - ldigits = "0123456789abcdefx" - udigits = "0123456789ABCDEFX" -) - -const ( - signed = true - unsigned = false -) - -// flags placed in a separate struct for easy clearing. -type fmtFlags struct { - widPresent bool - precPresent bool - minus bool - plus bool - sharp bool - space bool - zero bool - - // For the formats %+v %#v, we set the plusV/sharpV flags - // and clear the plus/sharp flags since %+v and %#v are in effect - // different, flagless formats set at the top level. - plusV bool - sharpV bool - - // error-related flags. - inDetail bool - needNewline bool - needColon bool -} - -// A formatter is the raw formatter used by Printf etc. -// It prints into a buffer that must be set up separately. -type formatter struct { - buf *buffer - - fmtFlags - - wid int // width - prec int // precision - - // intbuf is large enough to store %b of an int64 with a sign and - // avoids padding at the end of the struct on 32 bit architectures. - intbuf [68]byte -} - -func (f *formatter) clearflags() { - f.fmtFlags = fmtFlags{} -} - -func (f *formatter) init(buf *buffer) { - f.buf = buf - f.clearflags() -} - -// writePadding generates n bytes of padding. -func (f *formatter) writePadding(n int) { - if n <= 0 { // No padding bytes needed. - return - } - buf := *f.buf - oldLen := len(buf) - newLen := oldLen + n - - if newLen > tengo.MaxStringLen { - panic(ErrStringLimit) - } - - // Make enough room for padding. - if newLen > cap(buf) { - buf = make(buffer, cap(buf)*2+n) - copy(buf, *f.buf) - } - // Decide which byte the padding should be filled with. - padByte := byte(' ') - if f.zero { - padByte = byte('0') - } - // Fill padding with padByte. - padding := buf[oldLen:newLen] - for i := range padding { - padding[i] = padByte - } - *f.buf = buf[:newLen] -} - -// pad appends b to f.buf, padded on left (!f.minus) or right (f.minus). -func (f *formatter) pad(b []byte) { - if !f.widPresent || f.wid == 0 { - f.buf.Write(b) - return - } - width := f.wid - utf8.RuneCount(b) - if !f.minus { - // left padding - f.writePadding(width) - f.buf.Write(b) - } else { - // right padding - f.buf.Write(b) - f.writePadding(width) - } -} - -// padString appends s to f.buf, padded on left (!f.minus) or right (f.minus). -func (f *formatter) padString(s string) { - if !f.widPresent || f.wid == 0 { - f.buf.WriteString(s) - return - } - width := f.wid - utf8.RuneCountInString(s) - if !f.minus { - // left padding - f.writePadding(width) - f.buf.WriteString(s) - } else { - // right padding - f.buf.WriteString(s) - f.writePadding(width) - } -} - -// fmtBoolean formats a boolean. -func (f *formatter) fmtBoolean(v bool) { - if v { - f.padString("true") - } else { - f.padString("false") - } -} - -// fmtUnicode formats a uint64 as "U+0078" or with f.sharp set as "U+0078 'x'". -func (f *formatter) fmtUnicode(u uint64) { - buf := f.intbuf[0:] - - // With default precision set the maximum needed buf length is 18 - // for formatting -1 with %#U ("U+FFFFFFFFFFFFFFFF") which fits - // into the already allocated intbuf with a capacity of 68 bytes. - prec := 4 - if f.precPresent && f.prec > 4 { - prec = f.prec - // Compute space needed for "U+" , number, " '", character, "'". - width := 2 + prec + 2 + utf8.UTFMax + 1 - if width > len(buf) { - buf = make([]byte, width) - } - } - - // Format into buf, ending at buf[i]. Formatting numbers is easier right-to-left. - i := len(buf) - - // For %#U we want to add a space and a quoted character at the end of the buffer. - if f.sharp && u <= utf8.MaxRune && strconv.IsPrint(rune(u)) { - i-- - buf[i] = '\'' - i -= utf8.RuneLen(rune(u)) - utf8.EncodeRune(buf[i:], rune(u)) - i-- - buf[i] = '\'' - i-- - buf[i] = ' ' - } - // Format the Unicode code point u as a hexadecimal number. - for u >= 16 { - i-- - buf[i] = udigits[u&0xF] - prec-- - u >>= 4 - } - i-- - buf[i] = udigits[u] - prec-- - // Add zeros in front of the number until requested precision is reached. - for prec > 0 { - i-- - buf[i] = '0' - prec-- - } - // Add a leading "U+". - i-- - buf[i] = '+' - i-- - buf[i] = 'U' - - oldZero := f.zero - f.zero = false - f.pad(buf[i:]) - f.zero = oldZero -} - -// fmtInteger formats signed and unsigned integers. -func (f *formatter) fmtInteger(u uint64, base int, isSigned bool, verb rune, digits string) { - negative := isSigned && int64(u) < 0 - if negative { - u = -u - } - - buf := f.intbuf[0:] - // The already allocated f.intbuf with a capacity of 68 bytes - // is large enough for integer formatting when no precision or width is set. - if f.widPresent || f.precPresent { - // Account 3 extra bytes for possible addition of a sign and "0x". - width := 3 + f.wid + f.prec // wid and prec are always positive. - if width > len(buf) { - // We're going to need a bigger boat. - buf = make([]byte, width) - } - } - - // Two ways to ask for extra leading zero digits: %.3d or %03d. - // If both are specified the f.zero flag is ignored and - // padding with spaces is used instead. - prec := 0 - if f.precPresent { - prec = f.prec - // Precision of 0 and value of 0 means "print nothing" but padding. - if prec == 0 && u == 0 { - oldZero := f.zero - f.zero = false - f.writePadding(f.wid) - f.zero = oldZero - return - } - } else if f.zero && f.widPresent { - prec = f.wid - if negative || f.plus || f.space { - prec-- // leave room for sign - } - } - - // Because printing is easier right-to-left: format u into buf, ending at buf[i]. - // We could make things marginally faster by splitting the 32-bit case out - // into a separate block but it's not worth the duplication, so u has 64 bits. - i := len(buf) - // Use constants for the division and modulo for more efficient code. - // Switch cases ordered by popularity. - switch base { - case 10: - for u >= 10 { - i-- - next := u / 10 - buf[i] = byte('0' + u - next*10) - u = next - } - case 16: - for u >= 16 { - i-- - buf[i] = digits[u&0xF] - u >>= 4 - } - case 8: - for u >= 8 { - i-- - buf[i] = byte('0' + u&7) - u >>= 3 - } - case 2: - for u >= 2 { - i-- - buf[i] = byte('0' + u&1) - u >>= 1 - } - default: - panic("fmt: unknown base; can't happen") - } - i-- - buf[i] = digits[u] - for i > 0 && prec > len(buf)-i { - i-- - buf[i] = '0' - } - - // Various prefixes: 0x, -, etc. - if f.sharp { - switch base { - case 2: - // Add a leading 0b. - i-- - buf[i] = 'b' - i-- - buf[i] = '0' - case 8: - if buf[i] != '0' { - i-- - buf[i] = '0' - } - case 16: - // Add a leading 0x or 0X. - i-- - buf[i] = digits[16] - i-- - buf[i] = '0' - } - } - if verb == 'O' { - i-- - buf[i] = 'o' - i-- - buf[i] = '0' - } - - if negative { - i-- - buf[i] = '-' - } else if f.plus { - i-- - buf[i] = '+' - } else if f.space { - i-- - buf[i] = ' ' - } - - // Left padding with zeros has already been handled like precision earlier - // or the f.zero flag is ignored due to an explicitly set precision. - oldZero := f.zero - f.zero = false - f.pad(buf[i:]) - f.zero = oldZero -} - -// truncate truncates the string s to the specified precision, if present. -func (f *formatter) truncateString(s string) string { - if f.precPresent { - n := f.prec - for i := range s { - n-- - if n < 0 { - return s[:i] - } - } - } - return s -} - -// truncate truncates the byte slice b as a string of the specified precision, if present. -func (f *formatter) truncate(b []byte) []byte { - if f.precPresent { - n := f.prec - for i := 0; i < len(b); { - n-- - if n < 0 { - return b[:i] - } - wid := 1 - if b[i] >= utf8.RuneSelf { - _, wid = utf8.DecodeRune(b[i:]) - } - i += wid - } - } - return b -} - -// fmtS formats a string. -func (f *formatter) fmtS(s string) { - s = f.truncateString(s) - f.padString(s) -} - -// fmtBs formats the byte slice b as if it was formatted as string with fmtS. -func (f *formatter) fmtBs(b []byte) { - b = f.truncate(b) - f.pad(b) -} - -// fmtSbx formats a string or byte slice as a hexadecimal encoding of its bytes. -func (f *formatter) fmtSbx(s string, b []byte, digits string) { - length := len(b) - if b == nil { - // No byte slice present. Assume string s should be encoded. - length = len(s) - } - // Set length to not process more bytes than the precision demands. - if f.precPresent && f.prec < length { - length = f.prec - } - // Compute width of the encoding taking into account the f.sharp and f.space flag. - width := 2 * length - if width > 0 { - if f.space { - // Each element encoded by two hexadecimals will get a leading 0x or 0X. - if f.sharp { - width *= 2 - } - // Elements will be separated by a space. - width += length - 1 - } else if f.sharp { - // Only a leading 0x or 0X will be added for the whole string. - width += 2 - } - } else { // The byte slice or string that should be encoded is empty. - if f.widPresent { - f.writePadding(f.wid) - } - return - } - // Handle padding to the left. - if f.widPresent && f.wid > width && !f.minus { - f.writePadding(f.wid - width) - } - // Write the encoding directly into the output buffer. - buf := *f.buf - if f.sharp { - // Add leading 0x or 0X. - buf = append(buf, '0', digits[16]) - } - var c byte - for i := 0; i < length; i++ { - if f.space && i > 0 { - // Separate elements with a space. - buf = append(buf, ' ') - if f.sharp { - // Add leading 0x or 0X for each element. - buf = append(buf, '0', digits[16]) - } - } - if b != nil { - c = b[i] // Take a byte from the input byte slice. - } else { - c = s[i] // Take a byte from the input string. - } - // Encode each byte as two hexadecimal digits. - buf = append(buf, digits[c>>4], digits[c&0xF]) - } - *f.buf = buf - // Handle padding to the right. - if f.widPresent && f.wid > width && f.minus { - f.writePadding(f.wid - width) - } -} - -// fmtSx formats a string as a hexadecimal encoding of its bytes. -func (f *formatter) fmtSx(s, digits string) { - f.fmtSbx(s, nil, digits) -} - -// fmtBx formats a byte slice as a hexadecimal encoding of its bytes. -func (f *formatter) fmtBx(b []byte, digits string) { - f.fmtSbx("", b, digits) -} - -// fmtQ formats a string as a double-quoted, escaped Go string constant. -// If f.sharp is set a raw (backquoted) string may be returned instead -// if the string does not contain any control characters other than tab. -func (f *formatter) fmtQ(s string) { - s = f.truncateString(s) - if f.sharp && strconv.CanBackquote(s) { - f.padString("`" + s + "`") - return - } - buf := f.intbuf[:0] - if f.plus { - f.pad(strconv.AppendQuoteToASCII(buf, s)) - } else { - f.pad(strconv.AppendQuote(buf, s)) - } -} - -// fmtC formats an integer as a Unicode character. -// If the character is not valid Unicode, it will print '\ufffd'. -func (f *formatter) fmtC(c uint64) { - r := rune(c) - if c > utf8.MaxRune { - r = utf8.RuneError - } - buf := f.intbuf[:0] - w := utf8.EncodeRune(buf[:utf8.UTFMax], r) - f.pad(buf[:w]) -} - -// fmtQc formats an integer as a single-quoted, escaped Go character constant. -// If the character is not valid Unicode, it will print '\ufffd'. -func (f *formatter) fmtQc(c uint64) { - r := rune(c) - if c > utf8.MaxRune { - r = utf8.RuneError - } - buf := f.intbuf[:0] - if f.plus { - f.pad(strconv.AppendQuoteRuneToASCII(buf, r)) - } else { - f.pad(strconv.AppendQuoteRune(buf, r)) - } -} - -// fmtFloat formats a float64. It assumes that verb is a valid format specifier -// for strconv.AppendFloat and therefore fits into a byte. -func (f *formatter) fmtFloat(v float64, size int, verb rune, prec int) { - // Explicit precision in format specifier overrules default precision. - if f.precPresent { - prec = f.prec - } - // Format number, reserving space for leading + sign if needed. - num := strconv.AppendFloat(f.intbuf[:1], v, byte(verb), prec, size) - if num[1] == '-' || num[1] == '+' { - num = num[1:] - } else { - num[0] = '+' - } - // f.space means to add a leading space instead of a "+" sign unless - // the sign is explicitly asked for by f.plus. - if f.space && num[0] == '+' && !f.plus { - num[0] = ' ' - } - // Special handling for infinities and NaN, - // which don't look like a number so shouldn't be padded with zeros. - if num[1] == 'I' || num[1] == 'N' { - oldZero := f.zero - f.zero = false - // Remove sign before NaN if not asked for. - if num[1] == 'N' && !f.space && !f.plus { - num = num[1:] - } - f.pad(num) - f.zero = oldZero - return - } - // The sharp flag forces printing a decimal point for non-binary formats - // and retains trailing zeros, which we may need to restore. - if f.sharp && verb != 'b' { - digits := 0 - switch verb { - case 'v', 'g', 'G', 'x': - digits = prec - // If no precision is set explicitly use a precision of 6. - if digits == -1 { - digits = 6 - } - } - - // Buffer pre-allocated with enough room for - // exponent notations of the form "e+123" or "p-1023". - var tailBuf [6]byte - tail := tailBuf[:0] - - hasDecimalPoint := false - // Starting from i = 1 to skip sign at num[0]. - for i := 1; i < len(num); i++ { - switch num[i] { - case '.': - hasDecimalPoint = true - case 'p', 'P': - tail = append(tail, num[i:]...) - num = num[:i] - case 'e', 'E': - if verb != 'x' && verb != 'X' { - tail = append(tail, num[i:]...) - num = num[:i] - break - } - fallthrough - default: - digits-- - } - } - if !hasDecimalPoint { - num = append(num, '.') - } - for digits > 0 { - num = append(num, '0') - digits-- - } - num = append(num, tail...) - } - // We want a sign if asked for and if the sign is not positive. - if f.plus || num[0] != '+' { - // If we're zero padding to the left we want the sign before the leading zeros. - // Achieve this by writing the sign out and then padding the unsigned number. - if f.zero && f.widPresent && f.wid > len(num) { - f.buf.WriteSingleByte(num[0]) - f.writePadding(f.wid - len(num)) - f.buf.Write(num[1:]) - return - } - f.pad(num) - return - } - // No sign to show and the number is positive; just print the unsigned number. - f.pad(num[1:]) -} - -// Use simple []byte instead of bytes.Buffer to avoid large dependency. -type buffer []byte - -func (b *buffer) Write(p []byte) { - if len(*b)+len(p) > tengo.MaxStringLen { - panic(ErrStringLimit) - } - - *b = append(*b, p...) -} - -func (b *buffer) WriteString(s string) { - if len(*b)+len(s) > tengo.MaxStringLen { - panic(ErrStringLimit) - } - - *b = append(*b, s...) -} - -func (b *buffer) WriteSingleByte(c byte) { - if len(*b) >= tengo.MaxStringLen { - panic(ErrStringLimit) - } - - *b = append(*b, c) -} - -func (b *buffer) WriteRune(r rune) { - if len(*b)+utf8.RuneLen(r) > tengo.MaxStringLen { - panic(ErrStringLimit) - } - - if r < utf8.RuneSelf { - *b = append(*b, byte(r)) - return - } - - b2 := *b - n := len(b2) - for n+utf8.UTFMax > cap(b2) { - b2 = append(b2, 0) - } - w := utf8.EncodeRune(b2[n:n+utf8.UTFMax], r) - *b = b2[:n+w] -} - -// pp is used to store a printer's state and is reused with sync.Pool to avoid allocations. -type pp struct { - buf buffer - - // arg holds the current item. - arg Object - - // fmt is used to format basic items such as integers or strings. - fmt formatter - - // reordered records whether the format string used argument reordering. - reordered bool - - // goodArgNum records whether the most recent reordering directive was valid. - goodArgNum bool - - // erroring is set when printing an error string to guard against calling handleMethods. - erroring bool -} - -var ppFree = sync.Pool{ - New: func() interface{} { return new(pp) }, -} - -// newPrinter allocates a new pp struct or grabs a cached one. -func newPrinter() *pp { - p := ppFree.Get().(*pp) - p.erroring = false - p.fmt.init(&p.buf) - return p -} - -// free saves used pp structs in ppFree; avoids an allocation per invocation. -func (p *pp) free() { - // Proper usage of a sync.Pool requires each entry to have approximately - // the same memory cost. To obtain this property when the stored type - // contains a variably-sized buffer, we add a hard limit on the maximum buffer - // to place back in the pool. - // - // See https://golang.org/issue/23199 - if cap(p.buf) > 64<<10 { - return - } - - p.buf = p.buf[:0] - p.arg = nil - ppFree.Put(p) -} - -func (p *pp) Width() (wid int, ok bool) { return p.fmt.wid, p.fmt.widPresent } - -func (p *pp) Precision() (prec int, ok bool) { return p.fmt.prec, p.fmt.precPresent } - -func (p *pp) Flag(b int) bool { - switch b { - case '-': - return p.fmt.minus - case '+': - return p.fmt.plus || p.fmt.plusV - case '#': - return p.fmt.sharp || p.fmt.sharpV - case ' ': - return p.fmt.space - case '0': - return p.fmt.zero - } - return false -} - -// Implement Write so we can call Fprintf on a pp (through State), for -// recursive use in custom verbs. -func (p *pp) Write(b []byte) (ret int, err error) { - p.buf.Write(b) - return len(b), nil -} - -// Implement WriteString so that we can call io.WriteString -// on a pp (through state), for efficiency. -func (p *pp) WriteString(s string) (ret int, err error) { - p.buf.WriteString(s) - return len(s), nil -} - -func (p *pp) WriteRune(r rune) (ret int, err error) { - p.buf.WriteRune(r) - return utf8.RuneLen(r), nil -} - -func (p *pp) WriteSingleByte(c byte) (ret int, err error) { - p.buf.WriteSingleByte(c) - return 1, nil -} - -// tooLarge reports whether the magnitude of the integer is -// too large to be used as a formatting width or precision. -func tooLarge(x int) bool { - const max int = 1e6 - return x > max || x < -max -} - -// parsenum converts ASCII to integer. num is 0 (and isnum is false) if no number present. -func parsenum(s string, start, end int) (num int, isnum bool, newi int) { - if start >= end { - return 0, false, end - } - for newi = start; newi < end && '0' <= s[newi] && s[newi] <= '9'; newi++ { - if tooLarge(num) { - return 0, false, end // Overflow; crazy long number most likely. - } - num = num*10 + int(s[newi]-'0') - isnum = true - } - return -} - -func (p *pp) badVerb(verb rune) { - p.erroring = true - _, _ = p.WriteString(percentBangString) - _, _ = p.WriteRune(verb) - _, _ = p.WriteSingleByte('(') - switch { - case p.arg != nil: - _, _ = p.WriteString(p.arg.String()) - _, _ = p.WriteSingleByte('=') - p.printArg(p.arg, 'v') - default: - _, _ = p.WriteString(UndefinedValue.String()) - } - _, _ = p.WriteSingleByte(')') - p.erroring = false -} - -func (p *pp) fmtBool(v bool, verb rune) { - switch verb { - case 't', 'v': - p.fmt.fmtBoolean(v) - default: - p.badVerb(verb) - } -} - -// fmt0x64 formats a uint64 in hexadecimal and prefixes it with 0x or -// not, as requested, by temporarily setting the sharp flag. -func (p *pp) fmt0x64(v uint64, leading0x bool) { - sharp := p.fmt.sharp - p.fmt.sharp = leading0x - p.fmt.fmtInteger(v, 16, unsigned, 'v', ldigits) - p.fmt.sharp = sharp -} - -// fmtInteger formats a signed or unsigned integer. -func (p *pp) fmtInteger(v uint64, isSigned bool, verb rune) { - switch verb { - case 'v': - if p.fmt.sharpV && !isSigned { - p.fmt0x64(v, true) - } else { - p.fmt.fmtInteger(v, 10, isSigned, verb, ldigits) - } - case 'd': - p.fmt.fmtInteger(v, 10, isSigned, verb, ldigits) - case 'b': - p.fmt.fmtInteger(v, 2, isSigned, verb, ldigits) - case 'o', 'O': - p.fmt.fmtInteger(v, 8, isSigned, verb, ldigits) - case 'x': - p.fmt.fmtInteger(v, 16, isSigned, verb, ldigits) - case 'X': - p.fmt.fmtInteger(v, 16, isSigned, verb, udigits) - case 'c': - p.fmt.fmtC(v) - case 'q': - if v <= utf8.MaxRune { - p.fmt.fmtQc(v) - } else { - p.badVerb(verb) - } - case 'U': - p.fmt.fmtUnicode(v) - default: - p.badVerb(verb) - } -} - -// fmtFloat formats a float. The default precision for each verb -// is specified as last argument in the call to fmt_float. -func (p *pp) fmtFloat(v float64, size int, verb rune) { - switch verb { - case 'v': - p.fmt.fmtFloat(v, size, 'g', -1) - case 'b', 'g', 'G', 'x', 'X': - p.fmt.fmtFloat(v, size, verb, -1) - case 'f', 'e', 'E': - p.fmt.fmtFloat(v, size, verb, 6) - case 'F': - p.fmt.fmtFloat(v, size, 'f', 6) - default: - p.badVerb(verb) - } -} - -func (p *pp) fmtString(v string, verb rune) { - switch verb { - case 'v': - if p.fmt.sharpV { - p.fmt.fmtQ(v) - } else { - p.fmt.fmtS(v) - } - case 's': - p.fmt.fmtS(v) - case 'x': - p.fmt.fmtSx(v, ldigits) - case 'X': - p.fmt.fmtSx(v, udigits) - case 'q': - p.fmt.fmtQ(v) - default: - p.badVerb(verb) - } -} - -func (p *pp) fmtBytes(v []byte, verb rune, typeString string) { - switch verb { - case 'v', 'd': - if p.fmt.sharpV { - _, _ = p.WriteString(typeString) - if v == nil { - _, _ = p.WriteString(nilParenString) - return - } - _, _ = p.WriteSingleByte('{') - for i, c := range v { - if i > 0 { - _, _ = p.WriteString(commaSpaceString) - } - p.fmt0x64(uint64(c), true) - } - _, _ = p.WriteSingleByte('}') - } else { - _, _ = p.WriteSingleByte('[') - for i, c := range v { - if i > 0 { - _, _ = p.WriteSingleByte(' ') - } - p.fmt.fmtInteger(uint64(c), 10, unsigned, verb, ldigits) - } - _, _ = p.WriteSingleByte(']') - } - case 's': - p.fmt.fmtBs(v) - case 'x': - p.fmt.fmtBx(v, ldigits) - case 'X': - p.fmt.fmtBx(v, udigits) - case 'q': - p.fmt.fmtQ(string(v)) - } -} - -func (p *pp) printArg(arg Object, verb rune) { - p.arg = arg - - if arg == nil { - arg = UndefinedValue - } - - // Special processing considerations. - // %T (the value's type) and %p (its address) are special; we always do them first. - switch verb { - case 'T': - p.fmt.fmtS(arg.TypeName()) - return - case 'v': - p.fmt.fmtS(arg.String()) - return - } - - // Some types can be done without reflection. - switch f := arg.(type) { - case *Bool: - p.fmtBool(!f.IsFalsy(), verb) - case *Float: - p.fmtFloat(f.Value, 64, verb) - case *Int: - p.fmtInteger(uint64(f.Value), signed, verb) - case *String: - p.fmtString(f.Value, verb) - case *Bytes: - p.fmtBytes(f.Value, verb, "[]byte") - default: - p.fmtString(f.String(), verb) - } -} - -// intFromArg gets the argNumth element of a. On return, isInt reports whether the argument has integer type. -func intFromArg(a []Object, argNum int) (num int, isInt bool, newArgNum int) { - newArgNum = argNum - if argNum < len(a) { - var num64 int64 - num64, isInt = ToInt64(a[argNum]) - num = int(num64) - newArgNum = argNum + 1 - if tooLarge(num) { - num = 0 - isInt = false - } - } - return -} - -// parseArgNumber returns the value of the bracketed number, minus 1 -// (explicit argument numbers are one-indexed but we want zero-indexed). -// The opening bracket is known to be present at format[0]. -// The returned values are the index, the number of bytes to consume -// up to the closing paren, if present, and whether the number parsed -// ok. The bytes to consume will be 1 if no closing paren is present. -func parseArgNumber(format string) (index int, wid int, ok bool) { - // There must be at least 3 bytes: [n]. - if len(format) < 3 { - return 0, 1, false - } - - // Find closing bracket. - for i := 1; i < len(format); i++ { - if format[i] == ']' { - width, ok, newi := parsenum(format, 1, i) - if !ok || newi != i { - return 0, i + 1, false - } - return width - 1, i + 1, true // arg numbers are one-indexed and skip paren. - } - } - return 0, 1, false -} - -// argNumber returns the next argument to evaluate, which is either the value of the passed-in -// argNum or the value of the bracketed integer that begins format[i:]. It also returns -// the new value of i, that is, the index of the next byte of the format to process. -func (p *pp) argNumber(argNum int, format string, i int, numArgs int) (newArgNum, newi int, found bool) { - if len(format) <= i || format[i] != '[' { - return argNum, i, false - } - p.reordered = true - index, wid, ok := parseArgNumber(format[i:]) - if ok && 0 <= index && index < numArgs { - return index, i + wid, true - } - p.goodArgNum = false - return argNum, i + wid, ok -} - -func (p *pp) badArgNum(verb rune) { - _, _ = p.WriteString(percentBangString) - _, _ = p.WriteRune(verb) - _, _ = p.WriteString(badIndexString) -} - -func (p *pp) missingArg(verb rune) { - _, _ = p.WriteString(percentBangString) - _, _ = p.WriteRune(verb) - _, _ = p.WriteString(missingString) -} - -func (p *pp) doFormat(format string, a []Object) (err error) { - defer func() { - if r := recover(); r != nil { - if e, ok := r.(error); ok && e == ErrStringLimit { - err = e - return - } - panic(r) - } - }() - - end := len(format) - argNum := 0 // we process one argument per non-trivial format - afterIndex := false // previous item in format was an index like [3]. - p.reordered = false -formatLoop: - for i := 0; i < end; { - p.goodArgNum = true - lasti := i - for i < end && format[i] != '%' { - i++ - } - if i > lasti { - _, _ = p.WriteString(format[lasti:i]) - } - if i >= end { - // done processing format string - break - } - - // Process one verb - i++ - - // Do we have flags? - p.fmt.clearflags() - simpleFormat: - for ; i < end; i++ { - c := format[i] - switch c { - case '#': - p.fmt.sharp = true - case '0': - p.fmt.zero = !p.fmt.minus // Only allow zero padding to the left. - case '+': - p.fmt.plus = true - case '-': - p.fmt.minus = true - p.fmt.zero = false // Do not pad with zeros to the right. - case ' ': - p.fmt.space = true - default: - // Fast path for common case of ascii lower case simple verbs - // without precision or width or argument indices. - if 'a' <= c && c <= 'z' && argNum < len(a) { - if c == 'v' { - // Go syntax - p.fmt.sharpV = p.fmt.sharp - p.fmt.sharp = false - // Struct-field syntax - p.fmt.plusV = p.fmt.plus - p.fmt.plus = false - } - p.printArg(a[argNum], rune(c)) - argNum++ - i++ - continue formatLoop - } - // Format is more complex than simple flags and a verb or is malformed. - break simpleFormat - } - } - - // Do we have an explicit argument index? - argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a)) - - // Do we have width? - if i < end && format[i] == '*' { - i++ - p.fmt.wid, p.fmt.widPresent, argNum = intFromArg(a, argNum) - - if !p.fmt.widPresent { - _, _ = p.WriteString(badWidthString) - } - - // We have a negative width, so take its value and ensure - // that the minus flag is set - if p.fmt.wid < 0 { - p.fmt.wid = -p.fmt.wid - p.fmt.minus = true - p.fmt.zero = false // Do not pad with zeros to the right. - } - afterIndex = false - } else { - p.fmt.wid, p.fmt.widPresent, i = parsenum(format, i, end) - if afterIndex && p.fmt.widPresent { // "%[3]2d" - p.goodArgNum = false - } - } - - // Do we have precision? - if i+1 < end && format[i] == '.' { - i++ - if afterIndex { // "%[3].2d" - p.goodArgNum = false - } - argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a)) - if i < end && format[i] == '*' { - i++ - p.fmt.prec, p.fmt.precPresent, argNum = intFromArg(a, argNum) - // Negative precision arguments don't make sense - if p.fmt.prec < 0 { - p.fmt.prec = 0 - p.fmt.precPresent = false - } - if !p.fmt.precPresent { - _, _ = p.WriteString(badPrecString) - } - afterIndex = false - } else { - p.fmt.prec, p.fmt.precPresent, i = parsenum(format, i, end) - if !p.fmt.precPresent { - p.fmt.prec = 0 - p.fmt.precPresent = true - } - } - } - - if !afterIndex { - argNum, i, afterIndex = p.argNumber(argNum, format, i, len(a)) - } - - if i >= end { - _, _ = p.WriteString(noVerbString) - break - } - - verb, size := rune(format[i]), 1 - if verb >= utf8.RuneSelf { - verb, size = utf8.DecodeRuneInString(format[i:]) - } - i += size - - switch { - case verb == '%': // Percent does not absorb operands and ignores f.wid and f.prec. - _, _ = p.WriteSingleByte('%') - case !p.goodArgNum: - p.badArgNum(verb) - case argNum >= len(a): // No argument left over to print for the current verb. - p.missingArg(verb) - case verb == 'v': - // Go syntax - p.fmt.sharpV = p.fmt.sharp - p.fmt.sharp = false - // Struct-field syntax - p.fmt.plusV = p.fmt.plus - p.fmt.plus = false - fallthrough - default: - p.printArg(a[argNum], verb) - argNum++ - } - } - - // Check for extra arguments unless the call accessed the arguments - // out of order, in which case it's too expensive to detect if they've all - // been used and arguably OK if they're not. - if !p.reordered && argNum < len(a) { - p.fmt.clearflags() - _, _ = p.WriteString(extraString) - for i, arg := range a[argNum:] { - if i > 0 { - _, _ = p.WriteString(commaSpaceString) - } - if arg == nil { - _, _ = p.WriteString(UndefinedValue.String()) - } else { - _, _ = p.WriteString(arg.TypeName()) - _, _ = p.WriteSingleByte('=') - p.printArg(arg, 'v') - } - } - _, _ = p.WriteSingleByte(')') - } - - return nil -} - -// Format formats according to a format specifier and returns the resulting string. -func Format(format string, a ...Object) (string, error) { - p := newPrinter() - err := p.doFormat(format, a) - s := string(p.buf) - p.free() - - return s, err -} diff --git a/vendor/github.com/d5/tengo/objects/immutable_array.go b/vendor/github.com/d5/tengo/objects/immutable_array.go deleted file mode 100644 index f3621e29..00000000 --- a/vendor/github.com/d5/tengo/objects/immutable_array.go +++ /dev/null @@ -1,109 +0,0 @@ -package objects - -import ( - "fmt" - "strings" - - "github.com/d5/tengo/compiler/token" -) - -// ImmutableArray represents an immutable array of objects. -type ImmutableArray struct { - Value []Object -} - -// TypeName returns the name of the type. -func (o *ImmutableArray) TypeName() string { - return "immutable-array" -} - -func (o *ImmutableArray) String() string { - var elements []string - for _, e := range o.Value { - elements = append(elements, e.String()) - } - - return fmt.Sprintf("[%s]", strings.Join(elements, ", ")) -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error) { - if rhs, ok := rhs.(*ImmutableArray); ok { - switch op { - case token.Add: - return &Array{Value: append(o.Value, rhs.Value...)}, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *ImmutableArray) Copy() Object { - var c []Object - for _, elem := range o.Value { - c = append(c, elem.Copy()) - } - - return &Array{Value: c} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *ImmutableArray) IsFalsy() bool { - return len(o.Value) == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *ImmutableArray) Equals(x Object) bool { - var xVal []Object - switch x := x.(type) { - case *Array: - xVal = x.Value - case *ImmutableArray: - xVal = x.Value - default: - return false - } - - if len(o.Value) != len(xVal) { - return false - } - - for i, e := range o.Value { - if !e.Equals(xVal[i]) { - return false - } - } - - return true -} - -// IndexGet returns an element at a given index. -func (o *ImmutableArray) IndexGet(index Object) (res Object, err error) { - intIdx, ok := index.(*Int) - if !ok { - err = ErrInvalidIndexType - return - } - - idxVal := int(intIdx.Value) - - if idxVal < 0 || idxVal >= len(o.Value) { - res = UndefinedValue - return - } - - res = o.Value[idxVal] - - return -} - -// Iterate creates an array iterator. -func (o *ImmutableArray) Iterate() Iterator { - return &ArrayIterator{ - v: o.Value, - l: len(o.Value), - } -} diff --git a/vendor/github.com/d5/tengo/objects/immutable_map.go b/vendor/github.com/d5/tengo/objects/immutable_map.go deleted file mode 100644 index 8f58701b..00000000 --- a/vendor/github.com/d5/tengo/objects/immutable_map.go +++ /dev/null @@ -1,105 +0,0 @@ -package objects - -import ( - "fmt" - "strings" - - "github.com/d5/tengo/compiler/token" -) - -// ImmutableMap represents an immutable map object. -type ImmutableMap struct { - Value map[string]Object -} - -// TypeName returns the name of the type. -func (o *ImmutableMap) TypeName() string { - return "immutable-map" -} - -func (o *ImmutableMap) String() string { - var pairs []string - for k, v := range o.Value { - pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String())) - } - - return fmt.Sprintf("{%s}", strings.Join(pairs, ", ")) -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *ImmutableMap) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *ImmutableMap) Copy() Object { - c := make(map[string]Object) - for k, v := range o.Value { - c[k] = v.Copy() - } - - return &Map{Value: c} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *ImmutableMap) IsFalsy() bool { - return len(o.Value) == 0 -} - -// IndexGet returns the value for the given key. -func (o *ImmutableMap) IndexGet(index Object) (res Object, err error) { - strIdx, ok := ToString(index) - if !ok { - err = ErrInvalidIndexType - return - } - - val, ok := o.Value[strIdx] - if !ok { - val = UndefinedValue - } - - return val, nil -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *ImmutableMap) Equals(x Object) bool { - var xVal map[string]Object - switch x := x.(type) { - case *Map: - xVal = x.Value - case *ImmutableMap: - xVal = x.Value - default: - return false - } - - if len(o.Value) != len(xVal) { - return false - } - - for k, v := range o.Value { - tv := xVal[k] - if !v.Equals(tv) { - return false - } - } - - return true -} - -// Iterate creates an immutable map iterator. -func (o *ImmutableMap) Iterate() Iterator { - var keys []string - for k := range o.Value { - keys = append(keys, k) - } - - return &MapIterator{ - v: o.Value, - k: keys, - l: len(keys), - } -} diff --git a/vendor/github.com/d5/tengo/objects/importable.go b/vendor/github.com/d5/tengo/objects/importable.go deleted file mode 100644 index 9fd86ae8..00000000 --- a/vendor/github.com/d5/tengo/objects/importable.go +++ /dev/null @@ -1,7 +0,0 @@ -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/index_assignable.go b/vendor/github.com/d5/tengo/objects/index_assignable.go deleted file mode 100644 index a1c6cbff..00000000 --- a/vendor/github.com/d5/tengo/objects/index_assignable.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -// IndexAssignable is an object that can take an index and a value -// on the left-hand side of the assignment statement. -type IndexAssignable interface { - // IndexSet should take an index Object and a value Object. - // If an error is returned, it will be treated as a run-time error. - IndexSet(index, value Object) error -} diff --git a/vendor/github.com/d5/tengo/objects/indexable.go b/vendor/github.com/d5/tengo/objects/indexable.go deleted file mode 100644 index bbc81633..00000000 --- a/vendor/github.com/d5/tengo/objects/indexable.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -// Indexable is an object that can take an index and return an object. -type Indexable interface { - // IndexGet should take an index Object and return a result Object or an error. - // If error is returned, the runtime will treat it as a run-time error and ignore returned value. - // If nil is returned as value, it will be converted to Undefined value by the runtime. - IndexGet(index Object) (value Object, err error) -} diff --git a/vendor/github.com/d5/tengo/objects/int.go b/vendor/github.com/d5/tengo/objects/int.go deleted file mode 100644 index e902c93a..00000000 --- a/vendor/github.com/d5/tengo/objects/int.go +++ /dev/null @@ -1,198 +0,0 @@ -package objects - -import ( - "strconv" - - "github.com/d5/tengo/compiler/token" -) - -// Int represents an integer value. -type Int struct { - Value int64 -} - -func (o *Int) String() string { - return strconv.FormatInt(o.Value, 10) -} - -// TypeName returns the name of the type. -func (o *Int) TypeName() string { - return "int" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Int) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch rhs := rhs.(type) { - case *Int: - switch op { - case token.Add: - r := o.Value + rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Sub: - r := o.Value - rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Mul: - r := o.Value * rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Quo: - r := o.Value / rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Rem: - r := o.Value % rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.And: - r := o.Value & rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Or: - r := o.Value | rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Xor: - r := o.Value ^ rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.AndNot: - r := o.Value &^ rhs.Value - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Shl: - r := o.Value << uint64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Shr: - r := o.Value >> uint64(rhs.Value) - if r == o.Value { - return o, nil - } - return &Int{Value: r}, nil - case token.Less: - if o.Value < rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value > rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value <= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value >= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - } - case *Float: - switch op { - case token.Add: - return &Float{float64(o.Value) + rhs.Value}, nil - case token.Sub: - return &Float{float64(o.Value) - rhs.Value}, nil - case token.Mul: - return &Float{float64(o.Value) * rhs.Value}, nil - case token.Quo: - return &Float{float64(o.Value) / rhs.Value}, nil - case token.Less: - if float64(o.Value) < rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if float64(o.Value) > rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if float64(o.Value) <= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if float64(o.Value) >= rhs.Value { - return TrueValue, nil - } - return FalseValue, nil - } - case *Char: - switch op { - case token.Add: - return &Char{rune(o.Value) + rhs.Value}, nil - case token.Sub: - return &Char{rune(o.Value) - rhs.Value}, nil - case token.Less: - if o.Value < int64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value > int64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value <= int64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value >= int64(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Int) Copy() Object { - return &Int{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Int) IsFalsy() bool { - return o.Value == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Int) Equals(x Object) bool { - t, ok := x.(*Int) - if !ok { - return false - } - - return o.Value == t.Value -} diff --git a/vendor/github.com/d5/tengo/objects/iterable.go b/vendor/github.com/d5/tengo/objects/iterable.go deleted file mode 100644 index e431d3d7..00000000 --- a/vendor/github.com/d5/tengo/objects/iterable.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -// Iterable represents an object that has iterator. -type Iterable interface { - // Iterate should return an Iterator for the type. - Iterate() Iterator -} diff --git a/vendor/github.com/d5/tengo/objects/iterator.go b/vendor/github.com/d5/tengo/objects/iterator.go deleted file mode 100644 index 01522ba5..00000000 --- a/vendor/github.com/d5/tengo/objects/iterator.go +++ /dev/null @@ -1,15 +0,0 @@ -package objects - -// Iterator represents an iterator for underlying data type. -type Iterator interface { - Object - - // Next returns true if there are more elements to iterate. - Next() bool - - // Key returns the key or index value of the current element. - Key() Object - - // Value returns the value of the current element. - Value() Object -} diff --git a/vendor/github.com/d5/tengo/objects/map.go b/vendor/github.com/d5/tengo/objects/map.go deleted file mode 100644 index 9208872c..00000000 --- a/vendor/github.com/d5/tengo/objects/map.go +++ /dev/null @@ -1,118 +0,0 @@ -package objects - -import ( - "fmt" - "strings" - - "github.com/d5/tengo/compiler/token" -) - -// Map represents a map of objects. -type Map struct { - Value map[string]Object -} - -// TypeName returns the name of the type. -func (o *Map) TypeName() string { - return "map" -} - -func (o *Map) String() string { - var pairs []string - for k, v := range o.Value { - pairs = append(pairs, fmt.Sprintf("%s: %s", k, v.String())) - } - - return fmt.Sprintf("{%s}", strings.Join(pairs, ", ")) -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Map) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Map) Copy() Object { - c := make(map[string]Object) - for k, v := range o.Value { - c[k] = v.Copy() - } - - return &Map{Value: c} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Map) IsFalsy() bool { - return len(o.Value) == 0 -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Map) Equals(x Object) bool { - var xVal map[string]Object - switch x := x.(type) { - case *Map: - xVal = x.Value - case *ImmutableMap: - xVal = x.Value - default: - return false - } - - if len(o.Value) != len(xVal) { - return false - } - - for k, v := range o.Value { - tv := xVal[k] - if !v.Equals(tv) { - return false - } - } - - return true -} - -// IndexGet returns the value for the given key. -func (o *Map) IndexGet(index Object) (res Object, err error) { - strIdx, ok := ToString(index) - if !ok { - err = ErrInvalidIndexType - return - } - - val, ok := o.Value[strIdx] - if !ok { - val = UndefinedValue - } - - return val, nil -} - -// IndexSet sets the value for the given key. -func (o *Map) IndexSet(index, value Object) (err error) { - strIdx, ok := ToString(index) - if !ok { - err = ErrInvalidIndexType - return - } - - o.Value[strIdx] = value - - return nil -} - -// Iterate creates a map iterator. -func (o *Map) Iterate() Iterator { - var keys []string - for k := range o.Value { - keys = append(keys, k) - } - - return &MapIterator{ - v: o.Value, - k: keys, - l: len(keys), - } -} diff --git a/vendor/github.com/d5/tengo/objects/map_iterator.go b/vendor/github.com/d5/tengo/objects/map_iterator.go deleted file mode 100644 index d60dd0e1..00000000 --- a/vendor/github.com/d5/tengo/objects/map_iterator.go +++ /dev/null @@ -1,62 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// MapIterator represents an iterator for the map. -type MapIterator struct { - v map[string]Object - k []string - i int - l int -} - -// TypeName returns the name of the type. -func (i *MapIterator) TypeName() string { - return "map-iterator" -} - -func (i *MapIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *MapIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *MapIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *MapIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *MapIterator) Copy() Object { - return &MapIterator{v: i.v, k: i.k, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *MapIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *MapIterator) Key() Object { - k := i.k[i.i-1] - - return &String{Value: k} -} - -// Value returns the value of the current element. -func (i *MapIterator) Value() Object { - k := i.k[i.i-1] - - return i.v[k] -} diff --git a/vendor/github.com/d5/tengo/objects/module_map.go b/vendor/github.com/d5/tengo/objects/module_map.go deleted file mode 100644 index 874b8a2b..00000000 --- a/vendor/github.com/d5/tengo/objects/module_map.go +++ /dev/null @@ -1,77 +0,0 @@ -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.go b/vendor/github.com/d5/tengo/objects/object.go deleted file mode 100644 index 4c5aa7ae..00000000 --- a/vendor/github.com/d5/tengo/objects/object.go +++ /dev/null @@ -1,30 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// Object represents an object in the VM. -type Object interface { - // TypeName should return the name of the type. - TypeName() string - - // String should return a string representation of the type's value. - String() string - - // BinaryOp should return another object that is the result of - // a given binary operator and a right-hand side object. - // If BinaryOp returns an error, the VM will treat it as a run-time error. - BinaryOp(op token.Token, rhs Object) (Object, error) - - // IsFalsy should return true if the value of the type - // should be considered as falsy. - IsFalsy() bool - - // Equals should return true if the value of the type - // should be considered as equal to the value of another object. - Equals(another Object) bool - - // Copy should return a copy of the type (and its value). - // Copy function will be used for copy() builtin function - // which is expected to deep-copy the values generally. - Copy() Object -} diff --git a/vendor/github.com/d5/tengo/objects/object_ptr.go b/vendor/github.com/d5/tengo/objects/object_ptr.go deleted file mode 100644 index 2c87c561..00000000 --- a/vendor/github.com/d5/tengo/objects/object_ptr.go +++ /dev/null @@ -1,41 +0,0 @@ -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 "" -} - -// 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/objects.go b/vendor/github.com/d5/tengo/objects/objects.go deleted file mode 100644 index f3878b11..00000000 --- a/vendor/github.com/d5/tengo/objects/objects.go +++ /dev/null @@ -1,12 +0,0 @@ -package objects - -var ( - // TrueValue represents a true value. - TrueValue Object = &Bool{value: true} - - // FalseValue represents a false value. - FalseValue Object = &Bool{value: false} - - // UndefinedValue represents an undefined value. - UndefinedValue Object = &Undefined{} -) diff --git a/vendor/github.com/d5/tengo/objects/source_module.go b/vendor/github.com/d5/tengo/objects/source_module.go deleted file mode 100644 index 577fddf2..00000000 --- a/vendor/github.com/d5/tengo/objects/source_module.go +++ /dev/null @@ -1,11 +0,0 @@ -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/string.go b/vendor/github.com/d5/tengo/objects/string.go deleted file mode 100644 index c25b0502..00000000 --- a/vendor/github.com/d5/tengo/objects/string.go +++ /dev/null @@ -1,103 +0,0 @@ -package objects - -import ( - "strconv" - - "github.com/d5/tengo" - "github.com/d5/tengo/compiler/token" -) - -// String represents a string value. -type String struct { - Value string - runeStr []rune -} - -// TypeName returns the name of the type. -func (o *String) TypeName() string { - return "string" -} - -func (o *String) String() string { - return strconv.Quote(o.Value) -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch op { - case token.Add: - switch rhs := rhs.(type) { - case *String: - if len(o.Value)+len(rhs.Value) > tengo.MaxStringLen { - return nil, ErrStringLimit - } - return &String{Value: o.Value + rhs.Value}, nil - default: - rhsStr := rhs.String() - if len(o.Value)+len(rhsStr) > tengo.MaxStringLen { - return nil, ErrStringLimit - } - return &String{Value: o.Value + rhsStr}, nil - } - } - - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *String) IsFalsy() bool { - return len(o.Value) == 0 -} - -// Copy returns a copy of the type. -func (o *String) Copy() Object { - return &String{Value: o.Value} -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *String) Equals(x Object) bool { - t, ok := x.(*String) - if !ok { - return false - } - - return o.Value == t.Value -} - -// IndexGet returns a character at a given index. -func (o *String) IndexGet(index Object) (res Object, err error) { - intIdx, ok := index.(*Int) - if !ok { - err = ErrInvalidIndexType - return - } - - idxVal := int(intIdx.Value) - - if o.runeStr == nil { - o.runeStr = []rune(o.Value) - } - - if idxVal < 0 || idxVal >= len(o.runeStr) { - res = UndefinedValue - return - } - - res = &Char{Value: o.runeStr[idxVal]} - - return -} - -// Iterate creates a string iterator. -func (o *String) Iterate() Iterator { - if o.runeStr == nil { - o.runeStr = []rune(o.Value) - } - - return &StringIterator{ - v: o.runeStr, - l: len(o.runeStr), - } -} diff --git a/vendor/github.com/d5/tengo/objects/string_iterator.go b/vendor/github.com/d5/tengo/objects/string_iterator.go deleted file mode 100644 index 8bc95eb5..00000000 --- a/vendor/github.com/d5/tengo/objects/string_iterator.go +++ /dev/null @@ -1,57 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// StringIterator represents an iterator for a string. -type StringIterator struct { - v []rune - i int - l int -} - -// TypeName returns the name of the type. -func (i *StringIterator) TypeName() string { - return "string-iterator" -} - -func (i *StringIterator) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (i *StringIterator) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// IsFalsy returns true if the value of the type is falsy. -func (i *StringIterator) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (i *StringIterator) Equals(Object) bool { - return false -} - -// Copy returns a copy of the type. -func (i *StringIterator) Copy() Object { - return &StringIterator{v: i.v, i: i.i, l: i.l} -} - -// Next returns true if there are more elements to iterate. -func (i *StringIterator) Next() bool { - i.i++ - return i.i <= i.l -} - -// Key returns the key or index value of the current element. -func (i *StringIterator) Key() Object { - return &Int{Value: int64(i.i - 1)} -} - -// Value returns the value of the current element. -func (i *StringIterator) Value() Object { - return &Char{Value: i.v[i.i-1]} -} diff --git a/vendor/github.com/d5/tengo/objects/time.go b/vendor/github.com/d5/tengo/objects/time.go deleted file mode 100644 index 4e783cc8..00000000 --- a/vendor/github.com/d5/tengo/objects/time.go +++ /dev/null @@ -1,89 +0,0 @@ -package objects - -import ( - "time" - - "github.com/d5/tengo/compiler/token" -) - -// Time represents a time value. -type Time struct { - Value time.Time -} - -func (o *Time) String() string { - return o.Value.String() -} - -// TypeName returns the name of the type. -func (o *Time) TypeName() string { - return "time" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Time) BinaryOp(op token.Token, rhs Object) (Object, error) { - switch rhs := rhs.(type) { - case *Int: - switch op { - case token.Add: // time + int => time - if rhs.Value == 0 { - return o, nil - } - return &Time{Value: o.Value.Add(time.Duration(rhs.Value))}, nil - case token.Sub: // time - int => time - if rhs.Value == 0 { - return o, nil - } - return &Time{Value: o.Value.Add(time.Duration(-rhs.Value))}, nil - } - case *Time: - switch op { - case token.Sub: // time - time => int (duration) - return &Int{Value: int64(o.Value.Sub(rhs.Value))}, nil - case token.Less: // time < time => bool - if o.Value.Before(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.Greater: - if o.Value.After(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.LessEq: - if o.Value.Equal(rhs.Value) || o.Value.Before(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - case token.GreaterEq: - if o.Value.Equal(rhs.Value) || o.Value.After(rhs.Value) { - return TrueValue, nil - } - return FalseValue, nil - } - } - - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Time) Copy() Object { - return &Time{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Time) IsFalsy() bool { - return o.Value.IsZero() -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Time) Equals(x Object) bool { - t, ok := x.(*Time) - if !ok { - return false - } - - return o.Value.Equal(t.Value) -} diff --git a/vendor/github.com/d5/tengo/objects/undefined.go b/vendor/github.com/d5/tengo/objects/undefined.go deleted file mode 100644 index 0fdbc084..00000000 --- a/vendor/github.com/d5/tengo/objects/undefined.go +++ /dev/null @@ -1,62 +0,0 @@ -package objects - -import "github.com/d5/tengo/compiler/token" - -// Undefined represents an undefined value. -type Undefined struct{} - -// TypeName returns the name of the type. -func (o *Undefined) TypeName() string { - return "undefined" -} - -func (o *Undefined) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *Undefined) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *Undefined) Copy() Object { - return o -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *Undefined) IsFalsy() bool { - return true -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *Undefined) Equals(x Object) bool { - return o == x -} - -// IndexGet returns an element at a given index. -func (o *Undefined) IndexGet(index Object) (Object, error) { - return UndefinedValue, nil -} - -// Iterate creates a map iterator. -func (o *Undefined) Iterate() Iterator { - return o -} - -// Next returns true if there are more elements to iterate. -func (o *Undefined) Next() bool { - return false -} - -// Key returns the key or index value of the current element. -func (o *Undefined) Key() Object { - return o -} - -// Value returns the value of the current element. -func (o *Undefined) Value() Object { - return o -} diff --git a/vendor/github.com/d5/tengo/objects/user_function.go b/vendor/github.com/d5/tengo/objects/user_function.go deleted file mode 100644 index a896788b..00000000 --- a/vendor/github.com/d5/tengo/objects/user_function.go +++ /dev/null @@ -1,48 +0,0 @@ -package objects - -import ( - "github.com/d5/tengo/compiler/token" -) - -// UserFunction represents a user function. -type UserFunction struct { - Name string - Value CallableFunc - EncodingID string -} - -// TypeName returns the name of the type. -func (o *UserFunction) TypeName() string { - return "user-function:" + o.Name -} - -func (o *UserFunction) String() string { - return "" -} - -// BinaryOp returns another object that is the result of -// a given binary operator and a right-hand side object. -func (o *UserFunction) BinaryOp(op token.Token, rhs Object) (Object, error) { - return nil, ErrInvalidOperator -} - -// Copy returns a copy of the type. -func (o *UserFunction) Copy() Object { - return &UserFunction{Value: o.Value} -} - -// IsFalsy returns true if the value of the type is falsy. -func (o *UserFunction) IsFalsy() bool { - return false -} - -// Equals returns true if the value of the type -// is equal to the value of another object. -func (o *UserFunction) Equals(x Object) bool { - return false -} - -// Call invokes a user function. -func (o *UserFunction) Call(args ...Object) (Object, error) { - return o.Value(args...) -} -- cgit v1.2.3