diff --git a/go.mod b/go.mod index abd03d5d..273ef6f0 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/Jeffail/gabs v1.1.1 // indirect github.com/Philipp15b/go-steam v1.0.1-0.20180818081528-681bd9573329 github.com/bwmarrin/discordgo v0.19.0 - github.com/d5/tengo v1.9.2 + github.com/d5/tengo v1.12.1 github.com/dfordsoft/golib v0.0.0-20180902042739-76ee6ab99bec github.com/fsnotify/fsnotify v1.4.7 github.com/go-telegram-bot-api/telegram-bot-api v4.6.5-0.20181225215658-ec221ba9ea45+incompatible diff --git a/go.sum b/go.sum index 0bd156c5..57659c44 100644 --- a/go.sum +++ b/go.sum @@ -17,6 +17,8 @@ github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8Nz github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk= github.com/d5/tengo v1.9.2 h1:UE/X8PYl7bLS4Ww2zGeh91nq5PTnkhe8ncgNeA5PK7k= github.com/d5/tengo v1.9.2/go.mod h1:gsbjo7lBXzBIWBd6NQp1lRKqqiDDANqBOyhW8rTlFsY= +github.com/d5/tengo v1.12.1 h1:libKkDM95CsZgYs6E5eiEaM9sbcw2EzJRSkr9o5NO4s= +github.com/d5/tengo v1.12.1/go.mod h1:gsbjo7lBXzBIWBd6NQp1lRKqqiDDANqBOyhW8rTlFsY= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= diff --git a/vendor/github.com/d5/tengo/.gitignore b/vendor/github.com/d5/tengo/.gitignore new file mode 100644 index 00000000..77738287 --- /dev/null +++ b/vendor/github.com/d5/tengo/.gitignore @@ -0,0 +1 @@ +dist/ \ No newline at end of file diff --git a/vendor/github.com/d5/tengo/.goreleaser.yml b/vendor/github.com/d5/tengo/.goreleaser.yml new file mode 100644 index 00000000..f7e2bd0e --- /dev/null +++ b/vendor/github.com/d5/tengo/.goreleaser.yml @@ -0,0 +1,23 @@ +builds: + - env: + - CGO_ENABLED=0 + main: ./cmd/tengo/main.go + goos: + - darwin + - linux + - windows + - env: + - CGO_ENABLED=0 + main: ./cmd/tengomin/main.go + binary: tengomin + goos: + - darwin + - linux + - windows +archive: + files: + - none* +checksum: + name_template: 'checksums.txt' +changelog: + sort: asc diff --git a/vendor/github.com/d5/tengo/.travis.yml b/vendor/github.com/d5/tengo/.travis.yml new file mode 100644 index 00000000..d545268e --- /dev/null +++ b/vendor/github.com/d5/tengo/.travis.yml @@ -0,0 +1,17 @@ +language: go + +go: + - 1.9 + +install: + - go get -u golang.org/x/lint/golint + +script: + - make test + +deploy: + - provider: script + skip_cleanup: true + script: curl -sL https://git.io/goreleaser | bash + on: + tags: true \ No newline at end of file diff --git a/vendor/github.com/d5/tengo/Makefile b/vendor/github.com/d5/tengo/Makefile new file mode 100644 index 00000000..e4613390 --- /dev/null +++ b/vendor/github.com/d5/tengo/Makefile @@ -0,0 +1,11 @@ +vet: + go vet ./... + +lint: + golint -set_exit_status ./... + +test: vet lint + go test -race -cover ./... + +fmt: + go fmt ./... diff --git a/vendor/github.com/d5/tengo/README.md b/vendor/github.com/d5/tengo/README.md new file mode 100644 index 00000000..7aa05966 --- /dev/null +++ b/vendor/github.com/d5/tengo/README.md @@ -0,0 +1,74 @@ +

+ +

+ +# The Tengo Language + +[![GoDoc](https://godoc.org/github.com/d5/tengo?status.svg)](https://godoc.org/github.com/d5/tengo/script) +[![Go Report Card](https://goreportcard.com/badge/github.com/d5/tengo)](https://goreportcard.com/report/github.com/d5/tengo) +[![Build Status](https://travis-ci.org/d5/tengo.svg?branch=master)](https://travis-ci.org/d5/tengo) +[![](https://img.shields.io/badge/Support%20Tengo-%241-brightgreen.svg)](https://www.patreon.com/tengolang) + +**Tengo is a small, dynamic, fast, secure script language for Go.** + +Tengo is **[fast](#benchmark)** and secure because it's compiled/executed as bytecode on stack-based VM that's written in native Go. + +```golang +/* The Tengo Language */ + +each := func(seq, fn) { + for x in seq { fn(x) } +} + +sum := func(init, seq) { + each(seq, func(x) { init += x }) + return init +} + +n := sum(0, [1, 2, 3]) // == 6 +s := sum("", [1, 2, 3]) // == "123" +``` + +> Run this code in the [Playground](https://tengolang.com/?s=d01cf9ed81daba939e26618530eb171f7397d9c9) + +## Features + +- Simple and highly readable [Syntax](https://github.com/d5/tengo/blob/master/docs/tutorial.md) + - Dynamic typing with type coercion + - Higher-order functions and closures + - Immutable values + - Garbage collection +- [Securely Embeddable](https://github.com/d5/tengo/blob/master/docs/interoperability.md) and [Extensible](https://github.com/d5/tengo/blob/master/docs/objects.md) +- Compiler/runtime written in native Go _(no external deps or cgo)_ +- Executable as a [standalone](https://github.com/d5/tengo/blob/master/docs/tengo-cli.md) language / REPL + +## Benchmark + +| | fib(35) | fibt(35) | Type | +| :--- | ---: | ---: | :---: | +| Go | `58ms` | `4ms` | Go (native) | +| [**Tengo**](https://github.com/d5/tengo) | `4,180ms` | `5ms` | VM on Go | +| Lua | `1,695ms` | `3ms` | Lua (native) | +| [go-lua](https://github.com/Shopify/go-lua) | `5,163ms` | `5ms` | Lua VM on Go | +| [GopherLua](https://github.com/yuin/gopher-lua) | `5,525ms` | `5ms` | Lua VM on Go | +| Python | `3,097ms` | `27ms` | Python (native) | +| [starlark-go](https://github.com/google/starlark-go) | `15,307ms` | `5ms` | Python-like Interpreter on Go | +| [gpython](https://github.com/go-python/gpython) | `17,656ms` | `5ms` | Python Interpreter on Go | +| [goja](https://github.com/dop251/goja) | `6,876ms` | `5ms` | JS VM on Go | +| [otto](https://github.com/robertkrimen/otto) | `81,886ms` | `12ms` | JS Interpreter on Go | +| [Anko](https://github.com/mattn/anko) | `97,517ms` | `14ms` | Interpreter on Go | + +_* [fib(35)](https://github.com/d5/tengobench/blob/master/code/fib.tengo): Fibonacci(35)_ +_* [fibt(35)](https://github.com/d5/tengobench/blob/master/code/fibtc.tengo): [tail-call](https://en.wikipedia.org/wiki/Tail_call) version of Fibonacci(35)_ +_* **Go** does not read the source code from file, while all other cases do_ +_* See [here](https://github.com/d5/tengobench) for commands/codes used_ + +## References + +- [Language Syntax](https://github.com/d5/tengo/blob/master/docs/tutorial.md) +- [Object Types](https://github.com/d5/tengo/blob/master/docs/objects.md) +- [Runtime Types](https://github.com/d5/tengo/blob/master/docs/runtime-types.md) and [Operators](https://github.com/d5/tengo/blob/master/docs/operators.md) +- [Builtin Functions](https://github.com/d5/tengo/blob/master/docs/builtins.md) +- [Interoperability](https://github.com/d5/tengo/blob/master/docs/interoperability.md) +- [Tengo CLI](https://github.com/d5/tengo/blob/master/docs/tengo-cli.md) +- [Standard Library](https://github.com/d5/tengo/blob/master/docs/stdlib.md) diff --git a/vendor/github.com/d5/tengo/compiler/compiler.go b/vendor/github.com/d5/tengo/compiler/compiler.go index 141ea8fd..d8bc05fd 100644 --- a/vendor/github.com/d5/tengo/compiler/compiler.go +++ b/vendor/github.com/d5/tengo/compiler/compiler.go @@ -5,11 +5,11 @@ import ( "io" "reflect" + "github.com/d5/tengo" "github.com/d5/tengo/compiler/ast" "github.com/d5/tengo/compiler/source" "github.com/d5/tengo/compiler/token" "github.com/d5/tengo/objects" - "github.com/d5/tengo/stdlib" ) // Compiler compiles the AST into a bytecode. @@ -54,9 +54,6 @@ func NewCompiler(file *source.File, symbolTable *SymbolTable, constants []object // builtin modules if builtinModules == nil { builtinModules = make(map[string]bool) - for name := range stdlib.Modules { - builtinModules[name] = true - } } return &Compiler{ @@ -195,6 +192,10 @@ func (c *Compiler) Compile(node ast.Node) error { } case *ast.StringLit: + if len(node.Value) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } + c.emit(node, OpConstant, c.addConstant(&objects.String{Value: node.Value})) case *ast.CharLit: @@ -332,6 +333,9 @@ func (c *Compiler) Compile(node ast.Node) error { case *ast.MapLit: for _, elt := range node.Elements { // key + if len(elt.Key) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } c.emit(node, OpConstant, c.addConstant(&objects.String{Value: elt.Key})) // value @@ -507,6 +511,10 @@ func (c *Compiler) Compile(node ast.Node) error { case *ast.ImportExpr: if c.builtinModules[node.ModuleName] { + if len(node.ModuleName) > tengo.MaxStringLen { + return c.error(node, objects.ErrStringLimit) + } + c.emit(node, OpConstant, c.addConstant(&objects.String{Value: node.ModuleName})) c.emit(node, OpGetBuiltinModule) } else { @@ -610,6 +618,14 @@ func (c *Compiler) fork(file *source.File, moduleName string, symbolTable *Symbo return child } +func (c *Compiler) error(node ast.Node, err error) error { + return &Error{ + fileSet: c.file.Set(), + node: node, + error: err, + } +} + func (c *Compiler) errorf(node ast.Node, format string, args ...interface{}) error { return &Error{ fileSet: c.file.Set(), diff --git a/vendor/github.com/d5/tengo/compiler/compiler_module.go b/vendor/github.com/d5/tengo/compiler/compiler_module.go index 8f63abb3..d069bfab 100644 --- a/vendor/github.com/d5/tengo/compiler/compiler_module.go +++ b/vendor/github.com/d5/tengo/compiler/compiler_module.go @@ -77,11 +77,8 @@ func (c *Compiler) doCompileModule(moduleName string, src []byte) (*objects.Comp symbolTable := NewSymbolTable() // inherit builtin functions - for idx, fn := range objects.Builtins { - s, _, ok := c.symbolTable.Resolve(fn.Name) - if ok && s.Scope == ScopeBuiltin { - symbolTable.DefineBuiltin(idx, fn.Name) - } + for _, sym := range c.symbolTable.BuiltinSymbols() { + symbolTable.DefineBuiltin(sym.Index, sym.Name) } // no global scope for the module diff --git a/vendor/github.com/d5/tengo/compiler/parser/parse_file.go b/vendor/github.com/d5/tengo/compiler/parser/parse_file.go deleted file mode 100644 index 0482c775..00000000 --- a/vendor/github.com/d5/tengo/compiler/parser/parse_file.go +++ /dev/null @@ -1,28 +0,0 @@ -package parser - -import ( - "io" - - "github.com/d5/tengo/compiler/ast" - "github.com/d5/tengo/compiler/source" -) - -// ParseFile parses a file with a given src. -func ParseFile(file *source.File, src []byte, trace io.Writer) (res *ast.File, err error) { - p := NewParser(file, src, trace) - - defer func() { - if e := recover(); e != nil { - if _, ok := e.(bailout); !ok { - panic(e) - } - } - - p.errors.Sort() - err = p.errors.Err() - }() - - res, err = p.ParseFile() - - return -} diff --git a/vendor/github.com/d5/tengo/compiler/parser/parse_source.go b/vendor/github.com/d5/tengo/compiler/parser/parse_source.go index 5d242db4..5c71436d 100644 --- a/vendor/github.com/d5/tengo/compiler/parser/parse_source.go +++ b/vendor/github.com/d5/tengo/compiler/parser/parse_source.go @@ -12,5 +12,6 @@ func ParseSource(filename string, src []byte, trace io.Writer) (res *ast.File, e fileSet := source.NewFileSet() file := fileSet.AddFile(filename, -1, len(src)) - return ParseFile(file, src, trace) + p := NewParser(file, src, trace) + return p.ParseFile() } diff --git a/vendor/github.com/d5/tengo/compiler/parser/parser.go b/vendor/github.com/d5/tengo/compiler/parser/parser.go index 93f04f7c..1f609ab5 100644 --- a/vendor/github.com/d5/tengo/compiler/parser/parser.go +++ b/vendor/github.com/d5/tengo/compiler/parser/parser.go @@ -57,7 +57,18 @@ func NewParser(file *source.File, src []byte, trace io.Writer) *Parser { } // ParseFile parses the source and returns an AST file unit. -func (p *Parser) ParseFile() (*ast.File, error) { +func (p *Parser) ParseFile() (file *ast.File, err error) { + defer func() { + if e := recover(); e != nil { + if _, ok := e.(bailout); !ok { + panic(e) + } + } + + p.errors.Sort() + err = p.errors.Err() + }() + if p.trace { defer un(trace(p, "File")) } @@ -71,10 +82,12 @@ func (p *Parser) ParseFile() (*ast.File, error) { return nil, p.errors.Err() } - return &ast.File{ + file = &ast.File{ InputFile: p.file, Stmts: stmts, - }, nil + } + + return } func (p *Parser) parseExpr() ast.Expr { @@ -1002,16 +1015,26 @@ func (p *Parser) parseMapElementLit() *ast.MapElementLit { defer un(trace(p, "MapElementLit")) } - // key: read identifier token but it's not actually an identifier - ident := p.parseIdent() + pos := p.pos + name := "_" + + if p.token == token.Ident { + name = p.tokenLit + } else if p.token == token.String { + v, _ := strconv.Unquote(p.tokenLit) + name = v + } else { + p.errorExpected(pos, "map key") + } + + p.next() colonPos := p.expect(token.Colon) - valueExpr := p.parseExpr() return &ast.MapElementLit{ - Key: ident.Name, - KeyPos: ident.NamePos, + Key: name, + KeyPos: pos, ColonPos: colonPos, Value: valueExpr, } diff --git a/vendor/github.com/d5/tengo/compiler/symbol_table.go b/vendor/github.com/d5/tengo/compiler/symbol_table.go index da55a826..fb029b31 100644 --- a/vendor/github.com/d5/tengo/compiler/symbol_table.go +++ b/vendor/github.com/d5/tengo/compiler/symbol_table.go @@ -2,12 +2,13 @@ package compiler // SymbolTable represents a symbol table. type SymbolTable struct { - parent *SymbolTable - block bool - store map[string]*Symbol - numDefinition int - maxDefinition int - freeSymbols []*Symbol + parent *SymbolTable + block bool + store map[string]*Symbol + numDefinition int + maxDefinition int + freeSymbols []*Symbol + builtinSymbols []*Symbol } // NewSymbolTable creates a SymbolTable. @@ -37,6 +38,10 @@ func (t *SymbolTable) Define(name string) *Symbol { // DefineBuiltin adds a symbol for builtin function. func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol { + if t.parent != nil { + return t.parent.DefineBuiltin(index, name) + } + symbol := &Symbol{ Name: name, Index: index, @@ -45,6 +50,8 @@ func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol { t.store[name] = symbol + t.builtinSymbols = append(t.builtinSymbols, symbol) + return symbol } @@ -101,6 +108,15 @@ func (t *SymbolTable) FreeSymbols() []*Symbol { return t.freeSymbols } +// BuiltinSymbols returns builtin symbols for the scope. +func (t *SymbolTable) BuiltinSymbols() []*Symbol { + if t.parent != nil { + return t.parent.BuiltinSymbols() + } + + return t.builtinSymbols +} + // Names returns the name of all the symbols. func (t *SymbolTable) Names() []string { var names []string diff --git a/vendor/github.com/d5/tengo/objects/builtin_convert.go b/vendor/github.com/d5/tengo/objects/builtin_convert.go index 7d9a8733..b5f2d05d 100644 --- a/vendor/github.com/d5/tengo/objects/builtin_convert.go +++ b/vendor/github.com/d5/tengo/objects/builtin_convert.go @@ -1,5 +1,7 @@ package objects +import "github.com/d5/tengo" + func builtinString(args ...Object) (Object, error) { argsLen := len(args) if !(argsLen == 1 || argsLen == 2) { @@ -12,6 +14,10 @@ func builtinString(args ...Object) (Object, error) { v, ok := ToString(args[0]) if ok { + if len(v) > tengo.MaxStringLen { + return nil, ErrStringLimit + } + return &String{Value: v}, nil } @@ -117,11 +123,19 @@ func builtinBytes(args ...Object) (Object, error) { // 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 } diff --git a/vendor/github.com/d5/tengo/objects/builtin_json.go b/vendor/github.com/d5/tengo/objects/builtin_json.go index c0810f7d..b3413651 100644 --- a/vendor/github.com/d5/tengo/objects/builtin_json.go +++ b/vendor/github.com/d5/tengo/objects/builtin_json.go @@ -2,6 +2,8 @@ package objects import ( "encoding/json" + + "github.com/d5/tengo" ) // to_json(v object) => bytes @@ -15,6 +17,10 @@ func builtinToJSON(args ...Object) (Object, error) { return &Error{Value: &String{Value: err.Error()}}, nil } + if len(res) > tengo.MaxBytesLen { + return nil, ErrBytesLimit + } + return &Bytes{Value: res}, nil } diff --git a/vendor/github.com/d5/tengo/objects/builtin_print.go b/vendor/github.com/d5/tengo/objects/builtin_print.go index c5fe36db..58f22610 100644 --- a/vendor/github.com/d5/tengo/objects/builtin_print.go +++ b/vendor/github.com/d5/tengo/objects/builtin_print.go @@ -2,6 +2,8 @@ package objects import ( "fmt" + + "github.com/d5/tengo" ) // print(args...) @@ -71,5 +73,11 @@ func builtinSprintf(args ...Object) (Object, error) { formatArgs[idx] = objectToInterface(arg) } - return &String{Value: fmt.Sprintf(format.Value, formatArgs...)}, nil + s := fmt.Sprintf(format.Value, formatArgs...) + + if len(s) > tengo.MaxStringLen { + return nil, ErrStringLimit + } + + return &String{Value: s}, nil } diff --git a/vendor/github.com/d5/tengo/objects/builtins.go b/vendor/github.com/d5/tengo/objects/builtins.go index 67553932..42c1a759 100644 --- a/vendor/github.com/d5/tengo/objects/builtins.go +++ b/vendor/github.com/d5/tengo/objects/builtins.go @@ -1,135 +1,164 @@ package objects -// NamedBuiltinFunc is a named builtin function. -type NamedBuiltinFunc struct { - Name string - Func CallableFunc +// Builtins contains all default builtin functions. +// Use GetBuiltinFunctions instead of accessing Builtins directly. +var Builtins = []BuiltinFunction{ + { + Name: "print", + Value: builtinPrint, + }, + { + Name: "printf", + Value: builtinPrintf, + }, + { + Name: "sprintf", + Value: builtinSprintf, + }, + { + 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_time", + Value: builtinIsTime, + }, + { + Name: "is_error", + Value: builtinIsError, + }, + { + Name: "is_undefined", + Value: builtinIsUndefined, + }, + { + Name: "is_function", + Value: builtinIsFunction, + }, + { + Name: "is_callable", + Value: builtinIsCallable, + }, + { + Name: "to_json", + Value: builtinToJSON, + }, + { + Name: "from_json", + Value: builtinFromJSON, + }, + { + Name: "type_name", + Value: builtinTypeName, + }, } -// Builtins contains all default builtin functions. -var Builtins = []NamedBuiltinFunc{ - { - Name: "print", - Func: builtinPrint, - }, - { - Name: "printf", - Func: builtinPrintf, - }, - { - Name: "sprintf", - Func: builtinSprintf, - }, - { - Name: "len", - Func: builtinLen, - }, - { - Name: "copy", - Func: builtinCopy, - }, - { - Name: "append", - Func: builtinAppend, - }, - { - Name: "string", - Func: builtinString, - }, - { - Name: "int", - Func: builtinInt, - }, - { - Name: "bool", - Func: builtinBool, - }, - { - Name: "float", - Func: builtinFloat, - }, - { - Name: "char", - Func: builtinChar, - }, - { - Name: "bytes", - Func: builtinBytes, - }, - { - Name: "time", - Func: builtinTime, - }, - { - Name: "is_int", - Func: builtinIsInt, - }, - { - Name: "is_float", - Func: builtinIsFloat, - }, - { - Name: "is_string", - Func: builtinIsString, - }, - { - Name: "is_bool", - Func: builtinIsBool, - }, - { - Name: "is_char", - Func: builtinIsChar, - }, - { - Name: "is_bytes", - Func: builtinIsBytes, - }, - { - Name: "is_array", - Func: builtinIsArray, - }, - { - Name: "is_immutable_array", - Func: builtinIsImmutableArray, - }, - { - Name: "is_map", - Func: builtinIsMap, - }, - { - Name: "is_immutable_map", - Func: builtinIsImmutableMap, - }, - { - Name: "is_time", - Func: builtinIsTime, - }, - { - Name: "is_error", - Func: builtinIsError, - }, - { - Name: "is_undefined", - Func: builtinIsUndefined, - }, - { - Name: "is_function", - Func: builtinIsFunction, - }, - { - Name: "is_callable", - Func: builtinIsCallable, - }, - { - Name: "to_json", - Func: builtinToJSON, - }, - { - Name: "from_json", - Func: builtinFromJSON, - }, - { - Name: "type_name", - Func: builtinTypeName, - }, +// AllBuiltinFunctionNames returns a list of all default builtin function names. +func AllBuiltinFunctionNames() []string { + var names []string + for _, bf := range Builtins { + names = append(names, bf.Name) + } + return names +} + +// GetBuiltinFunctions returns a slice of builtin function objects. +// GetBuiltinFunctions removes the duplicate names, and, the returned builtin functions +// are not guaranteed to be in the same order as names. +func GetBuiltinFunctions(names ...string) []*BuiltinFunction { + include := make(map[string]bool) + for _, name := range names { + include[name] = true + } + + var builtinFuncs []*BuiltinFunction + for _, bf := range Builtins { + if include[bf.Name] { + bf := bf + builtinFuncs = append(builtinFuncs, &bf) + } + } + + return builtinFuncs +} + +// GetAllBuiltinFunctions returns all builtin functions. +func GetAllBuiltinFunctions() []*BuiltinFunction { + return GetBuiltinFunctions(AllBuiltinFunctionNames()...) } diff --git a/vendor/github.com/d5/tengo/objects/bytes.go b/vendor/github.com/d5/tengo/objects/bytes.go index 7d8d6694..16b61684 100644 --- a/vendor/github.com/d5/tengo/objects/bytes.go +++ b/vendor/github.com/d5/tengo/objects/bytes.go @@ -3,6 +3,7 @@ package objects import ( "bytes" + "github.com/d5/tengo" "github.com/d5/tengo/compiler/token" ) @@ -27,6 +28,10 @@ func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error) { 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 } } diff --git a/vendor/github.com/d5/tengo/objects/callable_func.go b/vendor/github.com/d5/tengo/objects/callable_func.go index cf9b43aa..ad25e65d 100644 --- a/vendor/github.com/d5/tengo/objects/callable_func.go +++ b/vendor/github.com/d5/tengo/objects/callable_func.go @@ -1,4 +1,4 @@ package objects // CallableFunc is a function signature for the callable functions. -type CallableFunc func(args ...Object) (ret Object, err error) +type CallableFunc = func(args ...Object) (ret Object, err error) diff --git a/vendor/github.com/d5/tengo/objects/conversion.go b/vendor/github.com/d5/tengo/objects/conversion.go index 3c17546f..714f2617 100644 --- a/vendor/github.com/d5/tengo/objects/conversion.go +++ b/vendor/github.com/d5/tengo/objects/conversion.go @@ -4,6 +4,8 @@ import ( "fmt" "strconv" "time" + + "github.com/d5/tengo" ) // ToString will try to convert object o to string value. @@ -194,6 +196,9 @@ func FromInterface(v interface{}) (Object, error) { 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 @@ -211,6 +216,9 @@ func FromInterface(v interface{}) (Object, error) { 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 @@ -243,6 +251,8 @@ func FromInterface(v interface{}) (Object, error) { 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/errors.go b/vendor/github.com/d5/tengo/objects/errors.go index e4012314..bcd480a1 100644 --- a/vendor/github.com/d5/tengo/objects/errors.go +++ b/vendor/github.com/d5/tengo/objects/errors.go @@ -20,6 +20,12 @@ 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 diff --git a/vendor/github.com/d5/tengo/objects/string.go b/vendor/github.com/d5/tengo/objects/string.go index 6a53b44d..c25b0502 100644 --- a/vendor/github.com/d5/tengo/objects/string.go +++ b/vendor/github.com/d5/tengo/objects/string.go @@ -3,6 +3,7 @@ package objects import ( "strconv" + "github.com/d5/tengo" "github.com/d5/tengo/compiler/token" ) @@ -28,9 +29,16 @@ func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error) { 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: - return &String{Value: o.Value + rhs.String()}, nil + rhsStr := rhs.String() + if len(o.Value)+len(rhsStr) > tengo.MaxStringLen { + return nil, ErrStringLimit + } + return &String{Value: o.Value + rhsStr}, nil } } diff --git a/vendor/github.com/d5/tengo/runtime/vm.go b/vendor/github.com/d5/tengo/runtime/vm.go index 2708fde7..9066bfea 100644 --- a/vendor/github.com/d5/tengo/runtime/vm.go +++ b/vendor/github.com/d5/tengo/runtime/vm.go @@ -8,7 +8,6 @@ import ( "github.com/d5/tengo/compiler/source" "github.com/d5/tengo/compiler/token" "github.com/d5/tengo/objects" - "github.com/d5/tengo/stdlib" ) const ( @@ -26,7 +25,6 @@ var ( truePtr = &objects.TrueValue falsePtr = &objects.FalseValue undefinedPtr = &objects.UndefinedValue - builtinFuncs []objects.Object ) // VM is a virtual machine that executes the bytecode compiled by Compiler. @@ -43,17 +41,30 @@ type VM struct { curIPLimit int ip int aborting int64 + builtinFuncs []objects.Object builtinModules map[string]*objects.Object + err error + errOffset int } // NewVM creates a VM. -func NewVM(bytecode *compiler.Bytecode, globals []*objects.Object, builtinModules map[string]*objects.Object) *VM { +func NewVM(bytecode *compiler.Bytecode, globals []*objects.Object, builtinFuncs []objects.Object, builtinModules map[string]*objects.Object) *VM { if globals == nil { globals = make([]*objects.Object, GlobalsSize) } if builtinModules == nil { - builtinModules = stdlib.Modules + builtinModules = make(map[string]*objects.Object) + } + + if builtinFuncs == nil { + builtinFuncs = make([]objects.Object, len(objects.Builtins)) + for idx, fn := range objects.Builtins { + builtinFuncs[idx] = &objects.BuiltinFunction{ + Name: fn.Name, + Value: fn.Value, + } + } } frames := make([]Frame, MaxFrames) @@ -74,6 +85,7 @@ func NewVM(bytecode *compiler.Bytecode, globals []*objects.Object, builtinModule curInsts: frames[0].fn.Instructions, curIPLimit: len(frames[0].fn.Instructions) - 1, ip: -1, + builtinFuncs: builtinFuncs, builtinModules: builtinModules, } } @@ -94,8 +106,31 @@ func (v *VM) Run() (err error) { v.ip = -1 atomic.StoreInt64(&v.aborting, 0) - var filePos source.FilePos + v.run() + err = v.err + if err != nil { + filePos := v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-v.errOffset]) + err = fmt.Errorf("Runtime Error: %s\n\tat %s", err.Error(), filePos) + for v.framesIndex > 1 { + v.framesIndex-- + v.curFrame = &v.frames[v.framesIndex-1] + + filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.curFrame.ip-1]) + err = fmt.Errorf("%s\n\tat %s", err.Error(), filePos) + } + return err + } + + // check if stack still has some objects left + if v.sp > 0 && atomic.LoadInt64(&v.aborting) == 0 { + panic(fmt.Errorf("non empty stack after execution: %d", v.sp)) + } + + return nil +} + +func (v *VM) run() { mainloop: for v.ip < v.curIPLimit && (atomic.LoadInt64(&v.aborting) == 0) { v.ip++ @@ -106,8 +141,8 @@ mainloop: v.ip += 2 if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &v.constants[cidx] @@ -115,298 +150,45 @@ mainloop: case compiler.OpNull: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = undefinedPtr v.sp++ case compiler.OpAdd: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Add, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s + %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Add) case compiler.OpSub: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Sub, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s - %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Sub) case compiler.OpMul: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Mul, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s * %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Mul) case compiler.OpDiv: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Quo, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s / %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Quo) case compiler.OpRem: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Rem, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s %% %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Rem) case compiler.OpBAnd: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.And, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s & %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.And) case compiler.OpBOr: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Or, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s | %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Or) case compiler.OpBXor: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Xor, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s ^ %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Xor) case compiler.OpBAndNot: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.AndNot, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s &^ %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.AndNot) case compiler.OpBShiftLeft: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Shl, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s << %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Shl) case compiler.OpBShiftRight: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Shr, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s >> %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Shr) case compiler.OpEqual: right := v.stack[v.sp-1] @@ -414,8 +196,8 @@ mainloop: v.sp -= 2 if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } if (*left).Equals(*right) { @@ -431,8 +213,8 @@ mainloop: v.sp -= 2 if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } if (*left).Equals(*right) { @@ -443,64 +225,18 @@ mainloop: v.sp++ case compiler.OpGreaterThan: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.Greater, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s > %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.Greater) case compiler.OpGreaterThanEqual: - right := v.stack[v.sp-1] - left := v.stack[v.sp-2] - v.sp -= 2 - - res, e := (*left).BinaryOp(token.GreaterEq, *right) - if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - if e == objects.ErrInvalidOperator { - err = fmt.Errorf("invalid operation: %s >= %s", - (*left).TypeName(), (*right).TypeName()) - break mainloop - } - - err = e - break mainloop - } - - if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop - } - - v.stack[v.sp] = &res - v.sp++ + v.binaryOp(token.GreaterEq) case compiler.OpPop: v.sp-- case compiler.OpTrue: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = truePtr @@ -508,8 +244,8 @@ mainloop: case compiler.OpFalse: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = falsePtr @@ -520,8 +256,8 @@ mainloop: v.sp-- if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } if (*operand).IsFalsy() { @@ -538,8 +274,8 @@ mainloop: switch x := (*operand).(type) { case *objects.Int: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var res objects.Object = &objects.Int{Value: ^x.Value} @@ -547,9 +283,8 @@ mainloop: v.stack[v.sp] = &res v.sp++ default: - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid operation: ^%s", (*operand).TypeName()) - break mainloop + v.err = fmt.Errorf("invalid operation: ^%s", (*operand).TypeName()) + return } case compiler.OpMinus: @@ -559,8 +294,8 @@ mainloop: switch x := (*operand).(type) { case *objects.Int: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var res objects.Object = &objects.Int{Value: -x.Value} @@ -569,8 +304,8 @@ mainloop: v.sp++ case *objects.Float: if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var res objects.Object = &objects.Float{Value: -x.Value} @@ -578,9 +313,8 @@ mainloop: v.stack[v.sp] = &res v.sp++ default: - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid operation: -%s", (*operand).TypeName()) - break mainloop + v.err = fmt.Errorf("invalid operation: -%s", (*operand).TypeName()) + return } case compiler.OpJumpFalsy: @@ -639,9 +373,9 @@ mainloop: v.sp -= numSelectors + 1 if e := indexAssign(v.globals[globalIndex], val, selectors); e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-3]) - err = e - break mainloop + v.errOffset = 3 + v.err = e + return } case compiler.OpGetGlobal: @@ -651,8 +385,8 @@ mainloop: val := v.globals[globalIndex] if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = val @@ -671,8 +405,8 @@ mainloop: var arr objects.Object = &objects.Array{Value: elements} if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &arr @@ -693,8 +427,8 @@ mainloop: var m objects.Object = &objects.Map{Value: kv} if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &m @@ -734,23 +468,22 @@ mainloop: case objects.Indexable: val, e := left.IndexGet(*index) if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) if e == objects.ErrInvalidIndexType { - err = fmt.Errorf("invalid index type: %s", (*index).TypeName()) - break mainloop + v.err = fmt.Errorf("invalid index type: %s", (*index).TypeName()) + return } - err = e - break mainloop + v.err = e + return } if val == nil { val = objects.UndefinedValue } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &val @@ -759,23 +492,21 @@ mainloop: case *objects.Error: // e.value key, ok := (*index).(*objects.String) if !ok || key.Value != "value" { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid index on error") - break mainloop + v.err = fmt.Errorf("invalid index on error") + return } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &left.Value v.sp++ default: - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("not indexable: %s", left.TypeName()) - break mainloop + v.err = fmt.Errorf("not indexable: %s", left.TypeName()) + return } case compiler.OpSliceIndex: @@ -789,9 +520,8 @@ mainloop: if low, ok := (*low).(*objects.Int); ok { lowIdx = low.Value } else { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index type: %s", low.TypeName()) - break mainloop + v.err = fmt.Errorf("invalid slice index type: %s", low.TypeName()) + return } } @@ -804,15 +534,13 @@ mainloop: } else if high, ok := (*high).(*objects.Int); ok { highIdx = high.Value } else { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) - break mainloop + v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) + return } if lowIdx > highIdx { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) - break mainloop + v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) + return } if lowIdx < 0 { @@ -828,8 +556,8 @@ mainloop: } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var val objects.Object = &objects.Array{Value: left.Value[lowIdx:highIdx]} @@ -844,15 +572,13 @@ mainloop: } else if high, ok := (*high).(*objects.Int); ok { highIdx = high.Value } else { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) - break mainloop + v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) + return } if lowIdx > highIdx { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) - break mainloop + v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) + return } if lowIdx < 0 { @@ -868,8 +594,8 @@ mainloop: } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var val objects.Object = &objects.Array{Value: left.Value[lowIdx:highIdx]} @@ -885,15 +611,13 @@ mainloop: } else if high, ok := (*high).(*objects.Int); ok { highIdx = high.Value } else { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) - break mainloop + v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) + return } if lowIdx > highIdx { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) - break mainloop + v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) + return } if lowIdx < 0 { @@ -909,8 +633,8 @@ mainloop: } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var val objects.Object = &objects.String{Value: left.Value[lowIdx:highIdx]} @@ -926,15 +650,13 @@ mainloop: } else if high, ok := (*high).(*objects.Int); ok { highIdx = high.Value } else { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) - break mainloop + v.err = fmt.Errorf("invalid slice index type: %s", high.TypeName()) + return } if lowIdx > highIdx { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) - break mainloop + v.err = fmt.Errorf("invalid slice index: %d > %d", lowIdx, highIdx) + return } if lowIdx < 0 { @@ -950,8 +672,8 @@ mainloop: } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var val objects.Object = &objects.Bytes{Value: left.Value[lowIdx:highIdx]} @@ -969,10 +691,10 @@ mainloop: switch callee := value.(type) { case *objects.Closure: if numArgs != callee.Fn.NumParameters { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-1]) - err = fmt.Errorf("wrong number of arguments: want=%d, got=%d", + v.errOffset = 1 + v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d", callee.Fn.NumParameters, numArgs) - break mainloop + return } // test if it's tail-call @@ -1003,10 +725,10 @@ mainloop: case *objects.CompiledFunction: if numArgs != callee.NumParameters { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-1]) - err = fmt.Errorf("wrong number of arguments: want=%d, got=%d", + v.errOffset = 1 + v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d", callee.NumParameters, numArgs) - break mainloop + return } // test if it's tail-call @@ -1046,22 +768,22 @@ mainloop: // runtime error if e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-1]) + v.errOffset = 1 if e == objects.ErrWrongNumArguments { - err = fmt.Errorf("wrong number of arguments in call to '%s'", + v.err = fmt.Errorf("wrong number of arguments in call to '%s'", value.TypeName()) - break mainloop + return } if e, ok := e.(objects.ErrInvalidArgumentType); ok { - err = fmt.Errorf("invalid type for argument '%s' in call to '%s': expected %s, found %s", + v.err = fmt.Errorf("invalid type for argument '%s' in call to '%s': expected %s, found %s", e.Name, value.TypeName(), e.Expected, e.Found) - break mainloop + return } - err = e - break mainloop + v.err = e + return } // nil return -> undefined @@ -1070,17 +792,17 @@ mainloop: } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &ret v.sp++ default: - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-1]) - err = fmt.Errorf("not callable: %s", callee.TypeName()) - break mainloop + v.errOffset = 1 + v.err = fmt.Errorf("not callable: %s", callee.TypeName()) + return } case compiler.OpReturnValue: @@ -1155,9 +877,9 @@ mainloop: sp := v.curFrame.basePointer + localIndex if e := indexAssign(v.stack[sp], val, selectors); e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-2]) - err = e - break mainloop + v.errOffset = 2 + v.err = e + return } case compiler.OpGetLocal: @@ -1167,8 +889,8 @@ mainloop: val := v.stack[v.curFrame.basePointer+localIndex] if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = val @@ -1179,11 +901,11 @@ mainloop: v.ip++ if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } - v.stack[v.sp] = &builtinFuncs[builtinIndex] + v.stack[v.sp] = &v.builtinFuncs[builtinIndex] v.sp++ case compiler.OpGetBuiltinModule: @@ -1194,14 +916,14 @@ mainloop: module, ok := v.builtinModules[moduleName] if !ok { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-3]) - err = fmt.Errorf("module '%s' not found", moduleName) - break mainloop + v.errOffset = 3 + v.err = fmt.Errorf("module '%s' not found", moduleName) + return } if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = module @@ -1214,9 +936,9 @@ mainloop: fn, ok := v.constants[constIndex].(*objects.CompiledFunction) if !ok { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-3]) - err = fmt.Errorf("not function: %s", fn.TypeName()) - break mainloop + v.errOffset = 3 + v.err = fmt.Errorf("not function: %s", fn.TypeName()) + return } free := make([]*objects.Object, numFree) @@ -1226,8 +948,8 @@ mainloop: v.sp -= numFree if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } var cl objects.Object = &objects.Closure{ @@ -1245,8 +967,8 @@ mainloop: val := v.curFrame.freeVars[freeIndex] if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = val @@ -1263,9 +985,9 @@ mainloop: v.sp -= numSelectors + 1 if e := indexAssign(v.curFrame.freeVars[freeIndex], val, selectors); e != nil { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip-2]) - err = e - break mainloop + v.errOffset = 2 + v.err = e + return } case compiler.OpSetFree: @@ -1285,16 +1007,15 @@ mainloop: iterable, ok := (*dst).(objects.Iterable) if !ok { - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.ip]) - err = fmt.Errorf("not iterable: %s", (*dst).TypeName()) - break mainloop + v.err = fmt.Errorf("not iterable: %s", (*dst).TypeName()) + return } iterator = iterable.Iterate() if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &iterator @@ -1307,8 +1028,8 @@ mainloop: hasMore := (*iterator).(objects.Iterator).Next() if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } if hasMore { @@ -1325,8 +1046,8 @@ mainloop: val := (*iterator).(objects.Iterator).Key() if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &val @@ -1339,8 +1060,8 @@ mainloop: val := (*iterator).(objects.Iterator).Value() if v.sp >= StackSize { - err = ErrStackOverflow - break mainloop + v.err = ErrStackOverflow + return } v.stack[v.sp] = &val @@ -1350,25 +1071,6 @@ mainloop: panic(fmt.Errorf("unknown opcode: %d", v.curInsts[v.ip])) } } - - if err != nil { - err = fmt.Errorf("Runtime Error: %s\n\tat %s", err.Error(), filePos) - for v.framesIndex > 1 { - v.framesIndex-- - v.curFrame = &v.frames[v.framesIndex-1] - - filePos = v.fileSet.Position(v.curFrame.fn.SourceMap[v.curFrame.ip-1]) - err = fmt.Errorf("%s\n\tat %s", err.Error(), filePos) - } - return err - } - - // check if stack still has some objects left - if v.sp > 0 && atomic.LoadInt64(&v.aborting) == 0 { - panic(fmt.Errorf("non empty stack after execution: %d", v.sp)) - } - - return nil } // Globals returns the global variables. @@ -1413,12 +1115,25 @@ func indexAssign(dst, src *objects.Object, selectors []*objects.Object) error { return nil } -func init() { - builtinFuncs = make([]objects.Object, len(objects.Builtins)) - for i, b := range objects.Builtins { - builtinFuncs[i] = &objects.BuiltinFunction{ - Name: b.Name, - Value: b.Func, +func (v *VM) binaryOp(tok token.Token) { + right := v.stack[v.sp-1] + left := v.stack[v.sp-2] + + res, e := (*left).BinaryOp(tok, *right) + if e != nil { + v.sp -= 2 + atomic.StoreInt64(&v.aborting, 1) + + if e == objects.ErrInvalidOperator { + v.err = fmt.Errorf("invalid operation: %s + %s", + (*left).TypeName(), (*right).TypeName()) + return } + + v.err = e + return } + + v.stack[v.sp-2] = &res + v.sp-- } diff --git a/vendor/github.com/d5/tengo/script/script.go b/vendor/github.com/d5/tengo/script/script.go index 0b810278..e8db52a3 100644 --- a/vendor/github.com/d5/tengo/script/script.go +++ b/vendor/github.com/d5/tengo/script/script.go @@ -9,16 +9,15 @@ import ( "github.com/d5/tengo/compiler/source" "github.com/d5/tengo/objects" "github.com/d5/tengo/runtime" - "github.com/d5/tengo/stdlib" ) // Script can simplify compilation and execution of embedded scripts. type Script struct { - variables map[string]*Variable - removedBuiltins map[string]bool - removedStdModules map[string]bool - userModuleLoader compiler.ModuleLoader - input []byte + variables map[string]*Variable + builtinFuncs []objects.Object + builtinModules map[string]*objects.Object + userModuleLoader compiler.ModuleLoader + input []byte } // New creates a Script instance with an input script. @@ -56,22 +55,28 @@ func (s *Script) Remove(name string) bool { return true } -// DisableBuiltinFunction disables a builtin function. -func (s *Script) DisableBuiltinFunction(name string) { - if s.removedBuiltins == nil { - s.removedBuiltins = make(map[string]bool) +// SetBuiltinFunctions allows to define builtin functions. +func (s *Script) SetBuiltinFunctions(funcs []*objects.BuiltinFunction) { + if funcs != nil { + s.builtinFuncs = make([]objects.Object, len(funcs)) + for idx, fn := range funcs { + s.builtinFuncs[idx] = fn + } + } else { + s.builtinFuncs = []objects.Object{} } - - s.removedBuiltins[name] = true } -// DisableStdModule disables a standard library module. -func (s *Script) DisableStdModule(name string) { - if s.removedStdModules == nil { - s.removedStdModules = make(map[string]bool) +// SetBuiltinModules allows to define builtin modules. +func (s *Script) SetBuiltinModules(modules map[string]*objects.ImmutableMap) { + if modules != nil { + s.builtinModules = make(map[string]*objects.Object, len(modules)) + for k, mod := range modules { + s.builtinModules[k] = objectPtr(mod) + } + } else { + s.builtinModules = map[string]*objects.Object{} } - - s.removedStdModules[name] = true } // SetUserModuleLoader sets the user module loader for the compiler. @@ -81,7 +86,7 @@ func (s *Script) SetUserModuleLoader(loader compiler.ModuleLoader) { // Compile compiles the script with all the defined variables, and, returns Compiled object. func (s *Script) Compile() (*Compiled, error) { - symbolTable, stdModules, globals, err := s.prepCompile() + symbolTable, builtinModules, globals, err := s.prepCompile() if err != nil { return nil, err } @@ -92,10 +97,10 @@ func (s *Script) Compile() (*Compiled, error) { p := parser.NewParser(srcFile, s.input, nil) file, err := p.ParseFile() if err != nil { - return nil, fmt.Errorf("parse error: %s", err.Error()) + return nil, err } - c := compiler.NewCompiler(srcFile, symbolTable, nil, stdModules, nil) + c := compiler.NewCompiler(srcFile, symbolTable, nil, builtinModules, nil) if s.userModuleLoader != nil { c.SetModuleLoader(s.userModuleLoader) @@ -107,7 +112,7 @@ func (s *Script) Compile() (*Compiled, error) { return &Compiled{ symbolTable: symbolTable, - machine: runtime.NewVM(c.Bytecode(), globals, nil), + machine: runtime.NewVM(c.Bytecode(), globals, s.builtinFuncs, s.builtinModules), }, nil } @@ -136,24 +141,36 @@ func (s *Script) RunContext(ctx context.Context) (compiled *Compiled, err error) return } -func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, stdModules map[string]bool, globals []*objects.Object, err error) { +func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, builtinModules map[string]bool, globals []*objects.Object, err error) { var names []string for name := range s.variables { names = append(names, name) } symbolTable = compiler.NewSymbolTable() - for idx, fn := range objects.Builtins { - if !s.removedBuiltins[fn.Name] { - symbolTable.DefineBuiltin(idx, fn.Name) + + if s.builtinFuncs == nil { + s.builtinFuncs = make([]objects.Object, len(objects.Builtins)) + for idx, fn := range objects.Builtins { + s.builtinFuncs[idx] = &objects.BuiltinFunction{ + Name: fn.Name, + Value: fn.Value, + } } } - stdModules = make(map[string]bool) - for name := range stdlib.Modules { - if !s.removedStdModules[name] { - stdModules[name] = true - } + if s.builtinModules == nil { + s.builtinModules = make(map[string]*objects.Object) + } + + for idx, fn := range s.builtinFuncs { + f := fn.(*objects.BuiltinFunction) + symbolTable.DefineBuiltin(idx, f.Name) + } + + builtinModules = make(map[string]bool) + for name := range s.builtinModules { + builtinModules[name] = true } globals = make([]*objects.Object, runtime.GlobalsSize, runtime.GlobalsSize) @@ -178,3 +195,7 @@ func (s *Script) copyVariables() map[string]*Variable { return vars } + +func objectPtr(o objects.Object) *objects.Object { + return &o +} diff --git a/vendor/github.com/d5/tengo/stdlib/errors.go b/vendor/github.com/d5/tengo/stdlib/errors.go deleted file mode 100644 index a2942bb0..00000000 --- a/vendor/github.com/d5/tengo/stdlib/errors.go +++ /dev/null @@ -1,11 +0,0 @@ -package stdlib - -import "github.com/d5/tengo/objects" - -func wrapError(err error) objects.Object { - if err == nil { - return objects.TrueValue - } - - return &objects.Error{Value: &objects.String{Value: err.Error()}} -} diff --git a/vendor/github.com/d5/tengo/stdlib/func_typedefs.go b/vendor/github.com/d5/tengo/stdlib/func_typedefs.go deleted file mode 100644 index a85619fa..00000000 --- a/vendor/github.com/d5/tengo/stdlib/func_typedefs.go +++ /dev/null @@ -1,1057 +0,0 @@ -package stdlib - -import ( - "fmt" - - "github.com/d5/tengo/objects" -) - -// FuncAR transform a function of 'func()' signature -// into CallableFunc type. -func FuncAR(fn func()) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - fn() - - return objects.UndefinedValue, nil - } -} - -// FuncARI transform a function of 'func() int' signature -// into CallableFunc type. -func FuncARI(fn func() int) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return &objects.Int{Value: int64(fn())}, nil - } -} - -// FuncARI64 transform a function of 'func() int64' signature -// into CallableFunc type. -func FuncARI64(fn func() int64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return &objects.Int{Value: fn()}, nil - } -} - -// FuncAI64RI64 transform a function of 'func(int64) int64' signature -// into CallableFunc type. -func FuncAI64RI64(fn func(int64) int64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.Int{Value: fn(i1)}, nil - } -} - -// FuncAI64R transform a function of 'func(int64)' signature -// into CallableFunc type. -func FuncAI64R(fn func(int64)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - fn(i1) - - return objects.UndefinedValue, nil - } -} - -// FuncARB transform a function of 'func() bool' signature -// into CallableFunc type. -func FuncARB(fn func() bool) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - if fn() { - return objects.TrueValue, nil - } - - return objects.FalseValue, nil - } -} - -// FuncARE transform a function of 'func() error' signature -// into CallableFunc type. -func FuncARE(fn func() error) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return wrapError(fn()), nil - } -} - -// FuncARS transform a function of 'func() string' signature -// into CallableFunc type. -func FuncARS(fn func() string) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return &objects.String{Value: fn()}, nil - } -} - -// FuncARSE transform a function of 'func() (string, error)' signature -// into CallableFunc type. -func FuncARSE(fn func() (string, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - res, err := fn() - if err != nil { - return wrapError(err), nil - } - - return &objects.String{Value: res}, nil - } -} - -// FuncARYE transform a function of 'func() ([]byte, error)' signature -// into CallableFunc type. -func FuncARYE(fn func() ([]byte, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - res, err := fn() - if err != nil { - return wrapError(err), nil - } - - return &objects.Bytes{Value: res}, nil - } -} - -// FuncARF transform a function of 'func() float64' signature -// into CallableFunc type. -func FuncARF(fn func() float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return &objects.Float{Value: fn()}, nil - } -} - -// FuncARSs transform a function of 'func() []string' signature -// into CallableFunc type. -func FuncARSs(fn func() []string) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - arr := &objects.Array{} - for _, osArg := range fn() { - arr.Value = append(arr.Value, &objects.String{Value: osArg}) - } - - return arr, nil - } -} - -// FuncARIsE transform a function of 'func() ([]int, error)' signature -// into CallableFunc type. -func FuncARIsE(fn func() ([]int, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - res, err := fn() - if err != nil { - return wrapError(err), nil - } - - arr := &objects.Array{} - for _, v := range res { - arr.Value = append(arr.Value, &objects.Int{Value: int64(v)}) - } - - return arr, nil - } -} - -// FuncAIRIs transform a function of 'func(int) []int' signature -// into CallableFunc type. -func FuncAIRIs(fn func(int) []int) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - res := fn(i1) - - arr := &objects.Array{} - for _, v := range res { - arr.Value = append(arr.Value, &objects.Int{Value: int64(v)}) - } - - return arr, nil - } -} - -// FuncAFRF transform a function of 'func(float64) float64' signature -// into CallableFunc type. -func FuncAFRF(fn func(float64) float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.Float{Value: fn(f1)}, nil - } -} - -// FuncAIR transform a function of 'func(int)' signature -// into CallableFunc type. -func FuncAIR(fn func(int)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - fn(i1) - - return objects.UndefinedValue, nil - } -} - -// FuncAIRF transform a function of 'func(int) float64' signature -// into CallableFunc type. -func FuncAIRF(fn func(int) float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.Float{Value: fn(i1)}, nil - } -} - -// FuncAFRI transform a function of 'func(float64) int' signature -// into CallableFunc type. -func FuncAFRI(fn func(float64) int) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.Int{Value: int64(fn(f1))}, nil - } -} - -// FuncAFFRF transform a function of 'func(float64, float64) float64' signature -// into CallableFunc type. -func FuncAFFRF(fn func(float64, float64) float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - f2, ok := objects.ToFloat64(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "float(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.Float{Value: fn(f1, f2)}, nil - } -} - -// FuncAIFRF transform a function of 'func(int, float64) float64' signature -// into CallableFunc type. -func FuncAIFRF(fn func(int, float64) float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - f2, ok := objects.ToFloat64(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "float(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.Float{Value: fn(i1, f2)}, nil - } -} - -// FuncAFIRF transform a function of 'func(float64, int) float64' signature -// into CallableFunc type. -func FuncAFIRF(fn func(float64, int) float64) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.Float{Value: fn(f1, i2)}, nil - } -} - -// FuncAFIRB transform a function of 'func(float64, int) bool' signature -// into CallableFunc type. -func FuncAFIRB(fn func(float64, int) bool) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - if fn(f1, i2) { - return objects.TrueValue, nil - } - - return objects.FalseValue, nil - } -} - -// FuncAFRB transform a function of 'func(float64) bool' signature -// into CallableFunc type. -func FuncAFRB(fn func(float64) bool) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - f1, ok := objects.ToFloat64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float(compatible)", - Found: args[0].TypeName(), - } - } - - if fn(f1) { - return objects.TrueValue, nil - } - - return objects.FalseValue, nil - } -} - -// FuncASRS transform a function of 'func(string) string' signature into CallableFunc type. -// User function will return 'true' if underlying native function returns nil. -func FuncASRS(fn func(string) string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.String{Value: fn(s1)}, nil - } -} - -// FuncASRSs transform a function of 'func(string) []string' signature into CallableFunc type. -func FuncASRSs(fn func(string) []string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res := fn(s1) - - arr := &objects.Array{} - for _, osArg := range res { - arr.Value = append(arr.Value, &objects.String{Value: osArg}) - } - - return arr, nil - } -} - -// FuncASRSE transform a function of 'func(string) (string, error)' signature into CallableFunc type. -// User function will return 'true' if underlying native function returns nil. -func FuncASRSE(fn func(string) (string, error)) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := fn(s1) - if err != nil { - return wrapError(err), nil - } - - return &objects.String{Value: res}, nil - } -} - -// FuncASRE transform a function of 'func(string) error' signature into CallableFunc type. -// User function will return 'true' if underlying native function returns nil. -func FuncASRE(fn func(string) error) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - return wrapError(fn(s1)), nil - } -} - -// FuncASSRE transform a function of 'func(string, string) error' signature into CallableFunc type. -// User function will return 'true' if underlying native function returns nil. -func FuncASSRE(fn func(string, string) error) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - return wrapError(fn(s1, s2)), nil - } -} - -// FuncASSRSs transform a function of 'func(string, string) []string' signature into CallableFunc type. -func FuncASSRSs(fn func(string, string) []string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - arr := &objects.Array{} - for _, res := range fn(s1, s2) { - arr.Value = append(arr.Value, &objects.String{Value: res}) - } - - return arr, nil - } -} - -// FuncASSIRSs transform a function of 'func(string, string, int) []string' signature into CallableFunc type. -func FuncASSIRSs(fn func(string, string, int) []string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 3 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - } - - arr := &objects.Array{} - for _, res := range fn(s1, s2, i3) { - arr.Value = append(arr.Value, &objects.String{Value: res}) - } - - return arr, nil - } -} - -// FuncASSRI transform a function of 'func(string, string) int' signature into CallableFunc type. -func FuncASSRI(fn func(string, string) int) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.Int{Value: int64(fn(s1, s2))}, nil - } -} - -// FuncASSRS transform a function of 'func(string, string) string' signature into CallableFunc type. -func FuncASSRS(fn func(string, string) string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.String{Value: fn(s1, s2)}, nil - } -} - -// FuncASSRB transform a function of 'func(string, string) bool' signature into CallableFunc type. -func FuncASSRB(fn func(string, string) bool) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - if fn(s1, s2) { - return objects.TrueValue, nil - } - - return objects.FalseValue, nil - } -} - -// FuncASsSRS transform a function of 'func([]string, string) string' signature into CallableFunc type. -func FuncASsSRS(fn func([]string, string) string) objects.CallableFunc { - return func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - var ss1 []string - switch arg0 := args[0].(type) { - case *objects.Array: - for idx, a := range arg0.Value { - as, ok := objects.ToString(a) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: fmt.Sprintf("first[%d]", idx), - Expected: "string(compatible)", - Found: a.TypeName(), - } - } - ss1 = append(ss1, as) - } - case *objects.ImmutableArray: - for idx, a := range arg0.Value { - as, ok := objects.ToString(a) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: fmt.Sprintf("first[%d]", idx), - Expected: "string(compatible)", - Found: a.TypeName(), - } - } - ss1 = append(ss1, as) - } - default: - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "array", - Found: args[0].TypeName(), - } - } - - s2, ok := objects.ToString(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.String{Value: fn(ss1, s2)}, nil - } -} - -// FuncASI64RE transform a function of 'func(string, int64) error' signature -// into CallableFunc type. -func FuncASI64RE(fn func(string, int64) error) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt64(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - return wrapError(fn(s1, i2)), nil - } -} - -// FuncAIIRE transform a function of 'func(int, int) error' signature -// into CallableFunc type. -func FuncAIIRE(fn func(int, int) error) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - return wrapError(fn(i1, i2)), nil - } -} - -// FuncASIRS transform a function of 'func(string, int) string' signature -// into CallableFunc type. -func FuncASIRS(fn func(string, int) string) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - return &objects.String{Value: fn(s1, i2)}, nil - } -} - -// FuncASIIRE transform a function of 'func(string, int, int) error' signature -// into CallableFunc type. -func FuncASIIRE(fn func(string, int, int) error) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 3 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - } - - return wrapError(fn(s1, i2, i3)), nil - } -} - -// FuncAYRIE transform a function of 'func([]byte) (int, error)' signature -// into CallableFunc type. -func FuncAYRIE(fn func([]byte) (int, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - y1, ok := objects.ToByteSlice(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "bytes(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := fn(y1) - if err != nil { - return wrapError(err), nil - } - - return &objects.Int{Value: int64(res)}, nil - } -} - -// FuncASRIE transform a function of 'func(string) (int, error)' signature -// into CallableFunc type. -func FuncASRIE(fn func(string) (int, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := fn(s1) - if err != nil { - return wrapError(err), nil - } - - return &objects.Int{Value: int64(res)}, nil - } -} - -// FuncAIRSsE transform a function of 'func(int) ([]string, error)' signature -// into CallableFunc type. -func FuncAIRSsE(fn func(int) ([]string, error)) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := fn(i1) - if err != nil { - return wrapError(err), nil - } - - arr := &objects.Array{} - for _, r := range res { - arr.Value = append(arr.Value, &objects.String{Value: r}) - } - - return arr, nil - } -} - -// FuncAIRS transform a function of 'func(int) string' signature -// into CallableFunc type. -func FuncAIRS(fn func(int) string) objects.CallableFunc { - return func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - return &objects.String{Value: fn(i1)}, nil - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/math.go b/vendor/github.com/d5/tengo/stdlib/math.go deleted file mode 100644 index 08d82bdf..00000000 --- a/vendor/github.com/d5/tengo/stdlib/math.go +++ /dev/null @@ -1,74 +0,0 @@ -package stdlib - -import ( - "math" - - "github.com/d5/tengo/objects" -) - -var mathModule = map[string]objects.Object{ - "e": &objects.Float{Value: math.E}, - "pi": &objects.Float{Value: math.Pi}, - "phi": &objects.Float{Value: math.Phi}, - "sqrt2": &objects.Float{Value: math.Sqrt2}, - "sqrtE": &objects.Float{Value: math.SqrtE}, - "sqrtPi": &objects.Float{Value: math.SqrtPi}, - "sqrtPhi": &objects.Float{Value: math.SqrtPhi}, - "ln2": &objects.Float{Value: math.Ln2}, - "log2E": &objects.Float{Value: math.Log2E}, - "ln10": &objects.Float{Value: math.Ln10}, - "log10E": &objects.Float{Value: math.Log10E}, - "abs": &objects.UserFunction{Name: "abs", Value: FuncAFRF(math.Abs)}, - "acos": &objects.UserFunction{Name: "acos", Value: FuncAFRF(math.Acos)}, - "acosh": &objects.UserFunction{Name: "acosh", Value: FuncAFRF(math.Acosh)}, - "asin": &objects.UserFunction{Name: "asin", Value: FuncAFRF(math.Asin)}, - "asinh": &objects.UserFunction{Name: "asinh", Value: FuncAFRF(math.Asinh)}, - "atan": &objects.UserFunction{Name: "atan", Value: FuncAFRF(math.Atan)}, - "atan2": &objects.UserFunction{Name: "atan2", Value: FuncAFFRF(math.Atan2)}, - "atanh": &objects.UserFunction{Name: "atanh", Value: FuncAFRF(math.Atanh)}, - "cbrt": &objects.UserFunction{Name: "cbrt", Value: FuncAFRF(math.Cbrt)}, - "ceil": &objects.UserFunction{Name: "ceil", Value: FuncAFRF(math.Ceil)}, - "copysign": &objects.UserFunction{Name: "copysign", Value: FuncAFFRF(math.Copysign)}, - "cos": &objects.UserFunction{Name: "cos", Value: FuncAFRF(math.Cos)}, - "cosh": &objects.UserFunction{Name: "cosh", Value: FuncAFRF(math.Cosh)}, - "dim": &objects.UserFunction{Name: "dim", Value: FuncAFFRF(math.Dim)}, - "erf": &objects.UserFunction{Name: "erf", Value: FuncAFRF(math.Erf)}, - "erfc": &objects.UserFunction{Name: "erfc", Value: FuncAFRF(math.Erfc)}, - "exp": &objects.UserFunction{Name: "exp", Value: FuncAFRF(math.Exp)}, - "exp2": &objects.UserFunction{Name: "exp2", Value: FuncAFRF(math.Exp2)}, - "expm1": &objects.UserFunction{Name: "expm1", Value: FuncAFRF(math.Expm1)}, - "floor": &objects.UserFunction{Name: "floor", Value: FuncAFRF(math.Floor)}, - "gamma": &objects.UserFunction{Name: "gamma", Value: FuncAFRF(math.Gamma)}, - "hypot": &objects.UserFunction{Name: "hypot", Value: FuncAFFRF(math.Hypot)}, - "ilogb": &objects.UserFunction{Name: "ilogb", Value: FuncAFRI(math.Ilogb)}, - "inf": &objects.UserFunction{Name: "inf", Value: FuncAIRF(math.Inf)}, - "is_inf": &objects.UserFunction{Name: "is_inf", Value: FuncAFIRB(math.IsInf)}, - "is_nan": &objects.UserFunction{Name: "is_nan", Value: FuncAFRB(math.IsNaN)}, - "j0": &objects.UserFunction{Name: "j0", Value: FuncAFRF(math.J0)}, - "j1": &objects.UserFunction{Name: "j1", Value: FuncAFRF(math.J1)}, - "jn": &objects.UserFunction{Name: "jn", Value: FuncAIFRF(math.Jn)}, - "ldexp": &objects.UserFunction{Name: "ldexp", Value: FuncAFIRF(math.Ldexp)}, - "log": &objects.UserFunction{Name: "log", Value: FuncAFRF(math.Log)}, - "log10": &objects.UserFunction{Name: "log10", Value: FuncAFRF(math.Log10)}, - "log1p": &objects.UserFunction{Name: "log1p", Value: FuncAFRF(math.Log1p)}, - "log2": &objects.UserFunction{Name: "log2", Value: FuncAFRF(math.Log2)}, - "logb": &objects.UserFunction{Name: "logb", Value: FuncAFRF(math.Logb)}, - "max": &objects.UserFunction{Name: "max", Value: FuncAFFRF(math.Max)}, - "min": &objects.UserFunction{Name: "min", Value: FuncAFFRF(math.Min)}, - "mod": &objects.UserFunction{Name: "mod", Value: FuncAFFRF(math.Mod)}, - "nan": &objects.UserFunction{Name: "nan", Value: FuncARF(math.NaN)}, - "nextafter": &objects.UserFunction{Name: "nextafter", Value: FuncAFFRF(math.Nextafter)}, - "pow": &objects.UserFunction{Name: "pow", Value: FuncAFFRF(math.Pow)}, - "pow10": &objects.UserFunction{Name: "pow10", Value: FuncAIRF(math.Pow10)}, - "remainder": &objects.UserFunction{Name: "remainder", Value: FuncAFFRF(math.Remainder)}, - "signbit": &objects.UserFunction{Name: "signbit", Value: FuncAFRB(math.Signbit)}, - "sin": &objects.UserFunction{Name: "sin", Value: FuncAFRF(math.Sin)}, - "sinh": &objects.UserFunction{Name: "sinh", Value: FuncAFRF(math.Sinh)}, - "sqrt": &objects.UserFunction{Name: "sqrt", Value: FuncAFRF(math.Sqrt)}, - "tan": &objects.UserFunction{Name: "tan", Value: FuncAFRF(math.Tan)}, - "tanh": &objects.UserFunction{Name: "tanh", Value: FuncAFRF(math.Tanh)}, - "trunc": &objects.UserFunction{Name: "trunc", Value: FuncAFRF(math.Trunc)}, - "y0": &objects.UserFunction{Name: "y0", Value: FuncAFRF(math.Y0)}, - "y1": &objects.UserFunction{Name: "y1", Value: FuncAFRF(math.Y1)}, - "yn": &objects.UserFunction{Name: "yn", Value: FuncAIFRF(math.Yn)}, -} diff --git a/vendor/github.com/d5/tengo/stdlib/os.go b/vendor/github.com/d5/tengo/stdlib/os.go deleted file mode 100644 index e68d5101..00000000 --- a/vendor/github.com/d5/tengo/stdlib/os.go +++ /dev/null @@ -1,437 +0,0 @@ -package stdlib - -import ( - "fmt" - "io" - "io/ioutil" - "os" - "os/exec" - - "github.com/d5/tengo/objects" -) - -var osModule = map[string]objects.Object{ - "o_rdonly": &objects.Int{Value: int64(os.O_RDONLY)}, - "o_wronly": &objects.Int{Value: int64(os.O_WRONLY)}, - "o_rdwr": &objects.Int{Value: int64(os.O_RDWR)}, - "o_append": &objects.Int{Value: int64(os.O_APPEND)}, - "o_create": &objects.Int{Value: int64(os.O_CREATE)}, - "o_excl": &objects.Int{Value: int64(os.O_EXCL)}, - "o_sync": &objects.Int{Value: int64(os.O_SYNC)}, - "o_trunc": &objects.Int{Value: int64(os.O_TRUNC)}, - "mode_dir": &objects.Int{Value: int64(os.ModeDir)}, - "mode_append": &objects.Int{Value: int64(os.ModeAppend)}, - "mode_exclusive": &objects.Int{Value: int64(os.ModeExclusive)}, - "mode_temporary": &objects.Int{Value: int64(os.ModeTemporary)}, - "mode_symlink": &objects.Int{Value: int64(os.ModeSymlink)}, - "mode_device": &objects.Int{Value: int64(os.ModeDevice)}, - "mode_named_pipe": &objects.Int{Value: int64(os.ModeNamedPipe)}, - "mode_socket": &objects.Int{Value: int64(os.ModeSocket)}, - "mode_setuid": &objects.Int{Value: int64(os.ModeSetuid)}, - "mode_setgui": &objects.Int{Value: int64(os.ModeSetgid)}, - "mode_char_device": &objects.Int{Value: int64(os.ModeCharDevice)}, - "mode_sticky": &objects.Int{Value: int64(os.ModeSticky)}, - "mode_type": &objects.Int{Value: int64(os.ModeType)}, - "mode_perm": &objects.Int{Value: int64(os.ModePerm)}, - "path_separator": &objects.Char{Value: os.PathSeparator}, - "path_list_separator": &objects.Char{Value: os.PathListSeparator}, - "dev_null": &objects.String{Value: os.DevNull}, - "seek_set": &objects.Int{Value: int64(io.SeekStart)}, - "seek_cur": &objects.Int{Value: int64(io.SeekCurrent)}, - "seek_end": &objects.Int{Value: int64(io.SeekEnd)}, - "args": &objects.UserFunction{Value: osArgs}, // args() => array(string) - "chdir": &objects.UserFunction{Name: "chdir", Value: FuncASRE(os.Chdir)}, // chdir(dir string) => error - "chmod": osFuncASFmRE(os.Chmod), // chmod(name string, mode int) => error - "chown": &objects.UserFunction{Name: "chown", Value: FuncASIIRE(os.Chown)}, // chown(name string, uid int, gid int) => error - "clearenv": &objects.UserFunction{Name: "clearenv", Value: FuncAR(os.Clearenv)}, // clearenv() - "environ": &objects.UserFunction{Name: "environ", Value: FuncARSs(os.Environ)}, // environ() => array(string) - "exit": &objects.UserFunction{Name: "exit", Value: FuncAIR(os.Exit)}, // exit(code int) - "expand_env": &objects.UserFunction{Name: "expand_env", Value: FuncASRS(os.ExpandEnv)}, // expand_env(s string) => string - "getegid": &objects.UserFunction{Name: "getegid", Value: FuncARI(os.Getegid)}, // getegid() => int - "getenv": &objects.UserFunction{Name: "getenv", Value: FuncASRS(os.Getenv)}, // getenv(s string) => string - "geteuid": &objects.UserFunction{Name: "geteuid", Value: FuncARI(os.Geteuid)}, // geteuid() => int - "getgid": &objects.UserFunction{Name: "getgid", Value: FuncARI(os.Getgid)}, // getgid() => int - "getgroups": &objects.UserFunction{Name: "getgroups", Value: FuncARIsE(os.Getgroups)}, // getgroups() => array(string)/error - "getpagesize": &objects.UserFunction{Name: "getpagesize", Value: FuncARI(os.Getpagesize)}, // getpagesize() => int - "getpid": &objects.UserFunction{Name: "getpid", Value: FuncARI(os.Getpid)}, // getpid() => int - "getppid": &objects.UserFunction{Name: "getppid", Value: FuncARI(os.Getppid)}, // getppid() => int - "getuid": &objects.UserFunction{Name: "getuid", Value: FuncARI(os.Getuid)}, // getuid() => int - "getwd": &objects.UserFunction{Name: "getwd", Value: FuncARSE(os.Getwd)}, // getwd() => string/error - "hostname": &objects.UserFunction{Name: "hostname", Value: FuncARSE(os.Hostname)}, // hostname() => string/error - "lchown": &objects.UserFunction{Name: "lchown", Value: FuncASIIRE(os.Lchown)}, // lchown(name string, uid int, gid int) => error - "link": &objects.UserFunction{Name: "link", Value: FuncASSRE(os.Link)}, // link(oldname string, newname string) => error - "lookup_env": &objects.UserFunction{Value: osLookupEnv}, // lookup_env(key string) => string/false - "mkdir": osFuncASFmRE(os.Mkdir), // mkdir(name string, perm int) => error - "mkdir_all": osFuncASFmRE(os.MkdirAll), // mkdir_all(name string, perm int) => error - "readlink": &objects.UserFunction{Name: "readlink", Value: FuncASRSE(os.Readlink)}, // readlink(name string) => string/error - "remove": &objects.UserFunction{Name: "remove", Value: FuncASRE(os.Remove)}, // remove(name string) => error - "remove_all": &objects.UserFunction{Name: "remove_all", Value: FuncASRE(os.RemoveAll)}, // remove_all(name string) => error - "rename": &objects.UserFunction{Name: "rename", Value: FuncASSRE(os.Rename)}, // rename(oldpath string, newpath string) => error - "setenv": &objects.UserFunction{Name: "setenv", Value: FuncASSRE(os.Setenv)}, // setenv(key string, value string) => error - "symlink": &objects.UserFunction{Name: "symlink", Value: FuncASSRE(os.Symlink)}, // symlink(oldname string newname string) => error - "temp_dir": &objects.UserFunction{Name: "temp_dir", Value: FuncARS(os.TempDir)}, // temp_dir() => string - "truncate": &objects.UserFunction{Name: "truncate", Value: FuncASI64RE(os.Truncate)}, // truncate(name string, size int) => error - "unsetenv": &objects.UserFunction{Name: "unsetenv", Value: FuncASRE(os.Unsetenv)}, // unsetenv(key string) => error - "create": &objects.UserFunction{Value: osCreate}, // create(name string) => imap(file)/error - "open": &objects.UserFunction{Value: osOpen}, // open(name string) => imap(file)/error - "open_file": &objects.UserFunction{Value: osOpenFile}, // open_file(name string, flag int, perm int) => imap(file)/error - "find_process": &objects.UserFunction{Value: osFindProcess}, // find_process(pid int) => imap(process)/error - "start_process": &objects.UserFunction{Value: osStartProcess}, // start_process(name string, argv array(string), dir string, env array(string)) => imap(process)/error - "exec_look_path": &objects.UserFunction{Name: "exec_look_path", Value: FuncASRSE(exec.LookPath)}, // exec_look_path(file) => string/error - "exec": &objects.UserFunction{Value: osExec}, // exec(name, args...) => command - "stat": &objects.UserFunction{Value: osStat}, // stat(name) => imap(fileinfo)/error - "read_file": &objects.UserFunction{Value: osReadFile}, // readfile(name) => array(byte)/error -} - -func osReadFile(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - fname, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - bytes, err := ioutil.ReadFile(fname) - if err != nil { - return wrapError(err), nil - } - - return &objects.Bytes{Value: bytes}, nil -} - -func osStat(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - fname, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - stat, err := os.Stat(fname) - if err != nil { - return wrapError(err), nil - } - - fstat := &objects.ImmutableMap{ - Value: map[string]objects.Object{ - "name": &objects.String{Value: stat.Name()}, - "mtime": &objects.Time{Value: stat.ModTime()}, - "size": &objects.Int{Value: stat.Size()}, - "mode": &objects.Int{Value: int64(stat.Mode())}, - }, - } - - if stat.IsDir() { - fstat.Value["directory"] = objects.TrueValue - } else { - fstat.Value["directory"] = objects.FalseValue - } - - return fstat, nil -} - -func osCreate(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := os.Create(s1) - if err != nil { - return wrapError(err), nil - } - - return makeOSFile(res), nil -} - -func osOpen(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res, err := os.Open(s1) - if err != nil { - return wrapError(err), nil - } - - return makeOSFile(res), nil -} - -func osOpenFile(args ...objects.Object) (objects.Object, error) { - if len(args) != 3 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - } - - res, err := os.OpenFile(s1, i2, os.FileMode(i3)) - if err != nil { - return wrapError(err), nil - } - - return makeOSFile(res), nil -} - -func osArgs(args ...objects.Object) (objects.Object, error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - arr := &objects.Array{} - for _, osArg := range os.Args { - arr.Value = append(arr.Value, &objects.String{Value: osArg}) - } - - return arr, nil -} - -func osFuncASFmRE(fn func(string, os.FileMode) error) *objects.UserFunction { - return &objects.UserFunction{ - Value: func(args ...objects.Object) (objects.Object, error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - i2, ok := objects.ToInt64(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - return wrapError(fn(s1, os.FileMode(i2))), nil - }, - } -} - -func osLookupEnv(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - res, ok := os.LookupEnv(s1) - if !ok { - return objects.FalseValue, nil - } - - return &objects.String{Value: res}, nil -} - -func osExec(args ...objects.Object) (objects.Object, error) { - if len(args) == 0 { - return nil, objects.ErrWrongNumArguments - } - - name, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - var execArgs []string - for idx, arg := range args[1:] { - execArg, ok := objects.ToString(arg) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: fmt.Sprintf("args[%d]", idx), - Expected: "string(compatible)", - Found: args[1+idx].TypeName(), - } - } - - execArgs = append(execArgs, execArg) - } - - return makeOSExecCommand(exec.Command(name, execArgs...)), nil -} - -func osFindProcess(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - proc, err := os.FindProcess(i1) - if err != nil { - return wrapError(err), nil - } - - return makeOSProcess(proc), nil -} - -func osStartProcess(args ...objects.Object) (objects.Object, error) { - if len(args) != 4 { - return nil, objects.ErrWrongNumArguments - } - - name, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - var argv []string - var err error - switch arg1 := args[1].(type) { - case *objects.Array: - argv, err = stringArray(arg1.Value, "second") - if err != nil { - return nil, err - } - case *objects.ImmutableArray: - argv, err = stringArray(arg1.Value, "second") - if err != nil { - return nil, err - } - default: - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "array", - Found: arg1.TypeName(), - } - } - - dir, ok := objects.ToString(args[2]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "string(compatible)", - Found: args[2].TypeName(), - } - } - - var env []string - switch arg3 := args[3].(type) { - case *objects.Array: - env, err = stringArray(arg3.Value, "fourth") - if err != nil { - return nil, err - } - case *objects.ImmutableArray: - env, err = stringArray(arg3.Value, "fourth") - if err != nil { - return nil, err - } - default: - return nil, objects.ErrInvalidArgumentType{ - Name: "fourth", - Expected: "array", - Found: arg3.TypeName(), - } - } - - proc, err := os.StartProcess(name, argv, &os.ProcAttr{ - Dir: dir, - Env: env, - }) - if err != nil { - return wrapError(err), nil - } - - return makeOSProcess(proc), nil -} - -func stringArray(arr []objects.Object, argName string) ([]string, error) { - var sarr []string - for idx, elem := range arr { - str, ok := elem.(*objects.String) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: fmt.Sprintf("%s[%d]", argName, idx), - Expected: "string", - Found: elem.TypeName(), - } - } - - sarr = append(sarr, str.Value) - } - - return sarr, nil -} diff --git a/vendor/github.com/d5/tengo/stdlib/os_exec.go b/vendor/github.com/d5/tengo/stdlib/os_exec.go deleted file mode 100644 index 809c5810..00000000 --- a/vendor/github.com/d5/tengo/stdlib/os_exec.go +++ /dev/null @@ -1,109 +0,0 @@ -package stdlib - -import ( - "os/exec" - - "github.com/d5/tengo/objects" -) - -func makeOSExecCommand(cmd *exec.Cmd) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - // combined_output() => bytes/error - "combined_output": &objects.UserFunction{Name: "combined_output", Value: FuncARYE(cmd.CombinedOutput)}, // - // output() => bytes/error - "output": &objects.UserFunction{Name: "output", Value: FuncARYE(cmd.Output)}, // - // run() => error - "run": &objects.UserFunction{Name: "run", Value: FuncARE(cmd.Run)}, // - // start() => error - "start": &objects.UserFunction{Name: "start", Value: FuncARE(cmd.Start)}, // - // wait() => error - "wait": &objects.UserFunction{Name: "wait", Value: FuncARE(cmd.Wait)}, // - // set_path(path string) - "set_path": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - cmd.Path = s1 - - return objects.UndefinedValue, nil - }, - }, - // set_dir(dir string) - "set_dir": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - s1, ok := objects.ToString(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - } - - cmd.Dir = s1 - - return objects.UndefinedValue, nil - }, - }, - // set_env(env array(string)) - "set_env": &objects.UserFunction{ - Value: func(args ...objects.Object) (objects.Object, error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - var env []string - var err error - switch arg0 := args[0].(type) { - case *objects.Array: - env, err = stringArray(arg0.Value, "first") - if err != nil { - return nil, err - } - case *objects.ImmutableArray: - env, err = stringArray(arg0.Value, "first") - if err != nil { - return nil, err - } - default: - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "array", - Found: arg0.TypeName(), - } - } - - cmd.Env = env - - return objects.UndefinedValue, nil - }, - }, - // process() => imap(process) - "process": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return makeOSProcess(cmd.Process), nil - }, - }, - }, - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/os_file.go b/vendor/github.com/d5/tengo/stdlib/os_file.go deleted file mode 100644 index 4fc41dd8..00000000 --- a/vendor/github.com/d5/tengo/stdlib/os_file.go +++ /dev/null @@ -1,93 +0,0 @@ -package stdlib - -import ( - "os" - - "github.com/d5/tengo/objects" -) - -func makeOSFile(file *os.File) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - // chdir() => true/error - "chdir": &objects.UserFunction{Name: "chdir", Value: FuncARE(file.Chdir)}, // - // chown(uid int, gid int) => true/error - "chown": &objects.UserFunction{Name: "chown", Value: FuncAIIRE(file.Chown)}, // - // close() => error - "close": &objects.UserFunction{Name: "close", Value: FuncARE(file.Close)}, // - // name() => string - "name": &objects.UserFunction{Name: "name", Value: FuncARS(file.Name)}, // - // readdirnames(n int) => array(string)/error - "readdirnames": &objects.UserFunction{Name: "readdirnames", Value: FuncAIRSsE(file.Readdirnames)}, // - // sync() => error - "sync": &objects.UserFunction{Name: "sync", Value: FuncARE(file.Sync)}, // - // write(bytes) => int/error - "write": &objects.UserFunction{Name: "write", Value: FuncAYRIE(file.Write)}, // - // write(string) => int/error - "write_string": &objects.UserFunction{Name: "write_string", Value: FuncASRIE(file.WriteString)}, // - // read(bytes) => int/error - "read": &objects.UserFunction{Name: "read", Value: FuncAYRIE(file.Read)}, // - // chmod(mode int) => error - "chmod": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - return wrapError(file.Chmod(os.FileMode(i1))), nil - }, - }, - // seek(offset int, whence int) => int/error - "seek": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - i2, ok := objects.ToInt(args[1]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - } - - res, err := file.Seek(i1, i2) - if err != nil { - return wrapError(err), nil - } - - return &objects.Int{Value: res}, nil - }, - }, - // stat() => imap(fileinfo)/error - "stat": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - return osStat(&objects.String{Value: file.Name()}) - }, - }, - }, - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/os_process.go b/vendor/github.com/d5/tengo/stdlib/os_process.go deleted file mode 100644 index 0f4a9f77..00000000 --- a/vendor/github.com/d5/tengo/stdlib/os_process.go +++ /dev/null @@ -1,60 +0,0 @@ -package stdlib - -import ( - "os" - "syscall" - - "github.com/d5/tengo/objects" -) - -func makeOSProcessState(state *os.ProcessState) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - "exited": &objects.UserFunction{Name: "exited", Value: FuncARB(state.Exited)}, // - "pid": &objects.UserFunction{Name: "pid", Value: FuncARI(state.Pid)}, // - "string": &objects.UserFunction{Name: "string", Value: FuncARS(state.String)}, // - "success": &objects.UserFunction{Name: "success", Value: FuncARB(state.Success)}, // - }, - } -} - -func makeOSProcess(proc *os.Process) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - "kill": &objects.UserFunction{Name: "kill", Value: FuncARE(proc.Kill)}, // - "release": &objects.UserFunction{Name: "release", Value: FuncARE(proc.Release)}, // - "signal": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - return wrapError(proc.Signal(syscall.Signal(i1))), nil - }, - }, - "wait": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - return nil, objects.ErrWrongNumArguments - } - - state, err := proc.Wait() - if err != nil { - return wrapError(err), nil - } - - return makeOSProcessState(state), nil - }, - }, - }, - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/rand.go b/vendor/github.com/d5/tengo/stdlib/rand.go deleted file mode 100644 index 248d8e79..00000000 --- a/vendor/github.com/d5/tengo/stdlib/rand.go +++ /dev/null @@ -1,99 +0,0 @@ -package stdlib - -import ( - "math/rand" - - "github.com/d5/tengo/objects" -) - -var randModule = map[string]objects.Object{ - "int": &objects.UserFunction{Name: "int", Value: FuncARI64(rand.Int63)}, - "float": &objects.UserFunction{Name: "float", Value: FuncARF(rand.Float64)}, - "intn": &objects.UserFunction{Name: "intn", Value: FuncAI64RI64(rand.Int63n)}, - "exp_float": &objects.UserFunction{Name: "exp_float", Value: FuncARF(rand.ExpFloat64)}, - "norm_float": &objects.UserFunction{Name: "norm_float", Value: FuncARF(rand.NormFloat64)}, - "perm": &objects.UserFunction{Name: "perm", Value: FuncAIRIs(rand.Perm)}, - "seed": &objects.UserFunction{Name: "seed", Value: FuncAI64R(rand.Seed)}, - "read": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - y1, ok := args[0].(*objects.Bytes) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "bytes", - Found: args[0].TypeName(), - } - } - - res, err := rand.Read(y1.Value) - if err != nil { - ret = wrapError(err) - return - } - - return &objects.Int{Value: int64(res)}, nil - }, - }, - "rand": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - } - - src := rand.NewSource(i1) - - return randRand(rand.New(src)), nil - }, - }, -} - -func randRand(r *rand.Rand) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - "int": &objects.UserFunction{Name: "int", Value: FuncARI64(r.Int63)}, - "float": &objects.UserFunction{Name: "float", Value: FuncARF(r.Float64)}, - "intn": &objects.UserFunction{Name: "intn", Value: FuncAI64RI64(r.Int63n)}, - "exp_float": &objects.UserFunction{Name: "exp_float", Value: FuncARF(r.ExpFloat64)}, - "norm_float": &objects.UserFunction{Name: "norm_float", Value: FuncARF(r.NormFloat64)}, - "perm": &objects.UserFunction{Name: "perm", Value: FuncAIRIs(r.Perm)}, - "seed": &objects.UserFunction{Name: "seed", Value: FuncAI64R(r.Seed)}, - "read": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - return nil, objects.ErrWrongNumArguments - } - - y1, ok := args[0].(*objects.Bytes) - if !ok { - return nil, objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "bytes", - Found: args[0].TypeName(), - } - } - - res, err := r.Read(y1.Value) - if err != nil { - ret = wrapError(err) - return - } - - return &objects.Int{Value: int64(res)}, nil - }, - }, - }, - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/stdlib.go b/vendor/github.com/d5/tengo/stdlib/stdlib.go deleted file mode 100644 index d34fbc82..00000000 --- a/vendor/github.com/d5/tengo/stdlib/stdlib.go +++ /dev/null @@ -1,16 +0,0 @@ -package stdlib - -import "github.com/d5/tengo/objects" - -// Modules contain the standard modules. -var Modules = map[string]*objects.Object{ - "math": objectPtr(&objects.ImmutableMap{Value: mathModule}), - "os": objectPtr(&objects.ImmutableMap{Value: osModule}), - "text": objectPtr(&objects.ImmutableMap{Value: textModule}), - "times": objectPtr(&objects.ImmutableMap{Value: timesModule}), - "rand": objectPtr(&objects.ImmutableMap{Value: randModule}), -} - -func objectPtr(o objects.Object) *objects.Object { - return &o -} diff --git a/vendor/github.com/d5/tengo/stdlib/text.go b/vendor/github.com/d5/tengo/stdlib/text.go deleted file mode 100644 index 053bebf5..00000000 --- a/vendor/github.com/d5/tengo/stdlib/text.go +++ /dev/null @@ -1,585 +0,0 @@ -package stdlib - -import ( - "regexp" - "strconv" - "strings" - - "github.com/d5/tengo/objects" -) - -var textModule = map[string]objects.Object{ - "re_match": &objects.UserFunction{Value: textREMatch}, // re_match(pattern, text) => bool/error - "re_find": &objects.UserFunction{Value: textREFind}, // re_find(pattern, text, count) => [[{text:,begin:,end:}]]/undefined - "re_replace": &objects.UserFunction{Value: textREReplace}, // re_replace(pattern, text, repl) => string/error - "re_split": &objects.UserFunction{Value: textRESplit}, // re_split(pattern, text, count) => [string]/error - "re_compile": &objects.UserFunction{Value: textRECompile}, // re_compile(pattern) => Regexp/error - "compare": &objects.UserFunction{Name: "compare", Value: FuncASSRI(strings.Compare)}, // compare(a, b) => int - "contains": &objects.UserFunction{Name: "contains", Value: FuncASSRB(strings.Contains)}, // contains(s, substr) => bool - "contains_any": &objects.UserFunction{Name: "contains_any", Value: FuncASSRB(strings.ContainsAny)}, // contains_any(s, chars) => bool - "count": &objects.UserFunction{Name: "count", Value: FuncASSRI(strings.Count)}, // count(s, substr) => int - "equal_fold": &objects.UserFunction{Name: "equal_fold", Value: FuncASSRB(strings.EqualFold)}, // "equal_fold(s, t) => bool - "fields": &objects.UserFunction{Name: "fields", Value: FuncASRSs(strings.Fields)}, // fields(s) => [string] - "has_prefix": &objects.UserFunction{Name: "has_prefix", Value: FuncASSRB(strings.HasPrefix)}, // has_prefix(s, prefix) => bool - "has_suffix": &objects.UserFunction{Name: "has_suffix", Value: FuncASSRB(strings.HasSuffix)}, // has_suffix(s, suffix) => bool - "index": &objects.UserFunction{Name: "index", Value: FuncASSRI(strings.Index)}, // index(s, substr) => int - "index_any": &objects.UserFunction{Name: "index_any", Value: FuncASSRI(strings.IndexAny)}, // index_any(s, chars) => int - "join": &objects.UserFunction{Name: "join", Value: FuncASsSRS(strings.Join)}, // join(arr, sep) => string - "last_index": &objects.UserFunction{Name: "last_index", Value: FuncASSRI(strings.LastIndex)}, // last_index(s, substr) => int - "last_index_any": &objects.UserFunction{Name: "last_index_any", Value: FuncASSRI(strings.LastIndexAny)}, // last_index_any(s, chars) => int - "repeat": &objects.UserFunction{Name: "repeat", Value: FuncASIRS(strings.Repeat)}, // repeat(s, count) => string - "replace": &objects.UserFunction{Value: textReplace}, // replace(s, old, new, n) => string - "split": &objects.UserFunction{Name: "split", Value: FuncASSRSs(strings.Split)}, // split(s, sep) => [string] - "split_after": &objects.UserFunction{Name: "split_after", Value: FuncASSRSs(strings.SplitAfter)}, // split_after(s, sep) => [string] - "split_after_n": &objects.UserFunction{Name: "split_after_n", Value: FuncASSIRSs(strings.SplitAfterN)}, // split_after_n(s, sep, n) => [string] - "split_n": &objects.UserFunction{Name: "split_n", Value: FuncASSIRSs(strings.SplitN)}, // split_n(s, sep, n) => [string] - "title": &objects.UserFunction{Name: "title", Value: FuncASRS(strings.Title)}, // title(s) => string - "to_lower": &objects.UserFunction{Name: "to_lower", Value: FuncASRS(strings.ToLower)}, // to_lower(s) => string - "to_title": &objects.UserFunction{Name: "to_title", Value: FuncASRS(strings.ToTitle)}, // to_title(s) => string - "to_upper": &objects.UserFunction{Name: "to_upper", Value: FuncASRS(strings.ToUpper)}, // to_upper(s) => string - "trim_left": &objects.UserFunction{Name: "trim_left", Value: FuncASSRS(strings.TrimLeft)}, // trim_left(s, cutset) => string - "trim_prefix": &objects.UserFunction{Name: "trim_prefix", Value: FuncASSRS(strings.TrimPrefix)}, // trim_prefix(s, prefix) => string - "trim_right": &objects.UserFunction{Name: "trim_right", Value: FuncASSRS(strings.TrimRight)}, // trim_right(s, cutset) => string - "trim_space": &objects.UserFunction{Name: "trim_space", Value: FuncASRS(strings.TrimSpace)}, // trim_space(s) => string - "trim_suffix": &objects.UserFunction{Name: "trim_suffix", Value: FuncASSRS(strings.TrimSuffix)}, // trim_suffix(s, suffix) => string - "atoi": &objects.UserFunction{Name: "atoi", Value: FuncASRIE(strconv.Atoi)}, // atoi(str) => int/error - "format_bool": &objects.UserFunction{Value: textFormatBool}, // format_bool(b) => string - "format_float": &objects.UserFunction{Value: textFormatFloat}, // format_float(f, fmt, prec, bits) => string - "format_int": &objects.UserFunction{Value: textFormatInt}, // format_int(i, base) => string - "itoa": &objects.UserFunction{Name: "itoa", Value: FuncAIRS(strconv.Itoa)}, // itoa(i) => string - "parse_bool": &objects.UserFunction{Value: textParseBool}, // parse_bool(str) => bool/error - "parse_float": &objects.UserFunction{Value: textParseFloat}, // parse_float(str, bits) => float/error - "parse_int": &objects.UserFunction{Value: textParseInt}, // parse_int(str, base, bits) => int/error - "quote": &objects.UserFunction{Name: "quote", Value: FuncASRS(strconv.Quote)}, // quote(str) => string - "unquote": &objects.UserFunction{Name: "unquote", Value: FuncASRSE(strconv.Unquote)}, // unquote(str) => string/error -} - -func textREMatch(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - matched, err := regexp.MatchString(s1, s2) - if err != nil { - ret = wrapError(err) - return - } - - if matched { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return -} - -func textREFind(args ...objects.Object) (ret objects.Object, err error) { - numArgs := len(args) - if numArgs != 2 && numArgs != 3 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - re, err := regexp.Compile(s1) - if err != nil { - ret = wrapError(err) - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - if numArgs < 3 { - m := re.FindStringSubmatchIndex(s2) - if m == nil { - ret = objects.UndefinedValue - return - } - - arr := &objects.Array{} - for i := 0; i < len(m); i += 2 { - arr.Value = append(arr.Value, &objects.ImmutableMap{Value: map[string]objects.Object{ - "text": &objects.String{Value: s2[m[i]:m[i+1]]}, - "begin": &objects.Int{Value: int64(m[i])}, - "end": &objects.Int{Value: int64(m[i+1])}, - }}) - } - - ret = &objects.Array{Value: []objects.Object{arr}} - - return - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - m := re.FindAllStringSubmatchIndex(s2, i3) - if m == nil { - ret = objects.UndefinedValue - return - } - - arr := &objects.Array{} - for _, m := range m { - subMatch := &objects.Array{} - for i := 0; i < len(m); i += 2 { - subMatch.Value = append(subMatch.Value, &objects.ImmutableMap{Value: map[string]objects.Object{ - "text": &objects.String{Value: s2[m[i]:m[i+1]]}, - "begin": &objects.Int{Value: int64(m[i])}, - "end": &objects.Int{Value: int64(m[i+1])}, - }}) - } - - arr.Value = append(arr.Value, subMatch) - } - - ret = arr - - return -} - -func textREReplace(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 3 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - s3, ok := objects.ToString(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "string(compatible)", - Found: args[2].TypeName(), - } - return - } - - re, err := regexp.Compile(s1) - if err != nil { - ret = wrapError(err) - } else { - ret = &objects.String{Value: re.ReplaceAllString(s2, s3)} - } - - return -} - -func textRESplit(args ...objects.Object) (ret objects.Object, err error) { - numArgs := len(args) - if numArgs != 2 && numArgs != 3 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - var i3 = -1 - if numArgs > 2 { - i3, ok = objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - } - - re, err := regexp.Compile(s1) - if err != nil { - ret = wrapError(err) - return - } - - arr := &objects.Array{} - for _, s := range re.Split(s2, i3) { - arr.Value = append(arr.Value, &objects.String{Value: s}) - } - - ret = arr - - return -} - -func textRECompile(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - re, err := regexp.Compile(s1) - if err != nil { - ret = wrapError(err) - } else { - ret = makeTextRegexp(re) - } - - return -} - -func textReplace(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 4 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - s3, ok := objects.ToString(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "string(compatible)", - Found: args[2].TypeName(), - } - return - } - - i4, ok := objects.ToInt(args[3]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "fourth", - Expected: "int(compatible)", - Found: args[3].TypeName(), - } - return - } - - ret = &objects.String{Value: strings.Replace(s1, s2, s3, i4)} - - return -} - -func textFormatBool(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - b1, ok := args[0].(*objects.Bool) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "bool", - Found: args[0].TypeName(), - } - return - } - - if b1 == objects.TrueValue { - ret = &objects.String{Value: "true"} - } else { - ret = &objects.String{Value: "false"} - } - - return -} - -func textFormatFloat(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 4 { - err = objects.ErrWrongNumArguments - return - } - - f1, ok := args[0].(*objects.Float) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "float", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - - i4, ok := objects.ToInt(args[3]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "fourth", - Expected: "int(compatible)", - Found: args[3].TypeName(), - } - return - } - - ret = &objects.String{Value: strconv.FormatFloat(f1.Value, s2[0], i3, i4)} - - return -} - -func textFormatInt(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := args[0].(*objects.Int) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.String{Value: strconv.FormatInt(i1.Value, i2)} - - return -} - -func textParseBool(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := args[0].(*objects.String) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string", - Found: args[0].TypeName(), - } - return - } - - parsed, err := strconv.ParseBool(s1.Value) - if err != nil { - ret = wrapError(err) - return - } - - if parsed { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return -} - -func textParseFloat(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := args[0].(*objects.String) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - parsed, err := strconv.ParseFloat(s1.Value, i2) - if err != nil { - ret = wrapError(err) - return - } - - ret = &objects.Float{Value: parsed} - - return -} - -func textParseInt(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 3 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := args[0].(*objects.String) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - - parsed, err := strconv.ParseInt(s1.Value, i2, i3) - if err != nil { - ret = wrapError(err) - return - } - - ret = &objects.Int{Value: parsed} - - return -} diff --git a/vendor/github.com/d5/tengo/stdlib/text_regexp.go b/vendor/github.com/d5/tengo/stdlib/text_regexp.go deleted file mode 100644 index 3fb8b3b3..00000000 --- a/vendor/github.com/d5/tengo/stdlib/text_regexp.go +++ /dev/null @@ -1,195 +0,0 @@ -package stdlib - -import ( - "regexp" - - "github.com/d5/tengo/objects" -) - -func makeTextRegexp(re *regexp.Regexp) *objects.ImmutableMap { - return &objects.ImmutableMap{ - Value: map[string]objects.Object{ - // match(text) => bool - "match": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - if re.MatchString(s1) { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return - }, - }, - - // find(text) => array(array({text:,begin:,end:}))/undefined - // find(text, maxCount) => array(array({text:,begin:,end:}))/undefined - "find": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - numArgs := len(args) - if numArgs != 1 && numArgs != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - if numArgs == 1 { - m := re.FindStringSubmatchIndex(s1) - if m == nil { - ret = objects.UndefinedValue - return - } - - arr := &objects.Array{} - for i := 0; i < len(m); i += 2 { - arr.Value = append(arr.Value, &objects.ImmutableMap{Value: map[string]objects.Object{ - "text": &objects.String{Value: s1[m[i]:m[i+1]]}, - "begin": &objects.Int{Value: int64(m[i])}, - "end": &objects.Int{Value: int64(m[i+1])}, - }}) - } - - ret = &objects.Array{Value: []objects.Object{arr}} - - return - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - m := re.FindAllStringSubmatchIndex(s1, i2) - if m == nil { - ret = objects.UndefinedValue - return - } - - arr := &objects.Array{} - for _, m := range m { - subMatch := &objects.Array{} - for i := 0; i < len(m); i += 2 { - subMatch.Value = append(subMatch.Value, &objects.ImmutableMap{Value: map[string]objects.Object{ - "text": &objects.String{Value: s1[m[i]:m[i+1]]}, - "begin": &objects.Int{Value: int64(m[i])}, - "end": &objects.Int{Value: int64(m[i+1])}, - }}) - } - - arr.Value = append(arr.Value, subMatch) - } - - ret = arr - - return - }, - }, - - // replace(src, repl) => string - "replace": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.String{Value: re.ReplaceAllString(s1, s2)} - - return - }, - }, - - // split(text) => array(string) - // split(text, maxCount) => array(string) - "split": &objects.UserFunction{ - Value: func(args ...objects.Object) (ret objects.Object, err error) { - numArgs := len(args) - if numArgs != 1 && numArgs != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - var i2 = -1 - if numArgs > 1 { - i2, ok = objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - } - - arr := &objects.Array{} - for _, s := range re.Split(s1, i2) { - arr.Value = append(arr.Value, &objects.String{Value: s}) - } - - ret = arr - - return - }, - }, - }, - } -} diff --git a/vendor/github.com/d5/tengo/stdlib/times.go b/vendor/github.com/d5/tengo/stdlib/times.go deleted file mode 100644 index 16d6d146..00000000 --- a/vendor/github.com/d5/tengo/stdlib/times.go +++ /dev/null @@ -1,982 +0,0 @@ -package stdlib - -import ( - "time" - - "github.com/d5/tengo/objects" -) - -var timesModule = map[string]objects.Object{ - "format_ansic": &objects.String{Value: time.ANSIC}, - "format_unix_date": &objects.String{Value: time.UnixDate}, - "format_ruby_date": &objects.String{Value: time.RubyDate}, - "format_rfc822": &objects.String{Value: time.RFC822}, - "format_rfc822z": &objects.String{Value: time.RFC822Z}, - "format_rfc850": &objects.String{Value: time.RFC850}, - "format_rfc1123": &objects.String{Value: time.RFC1123}, - "format_rfc1123z": &objects.String{Value: time.RFC1123Z}, - "format_rfc3339": &objects.String{Value: time.RFC3339}, - "format_rfc3339_nano": &objects.String{Value: time.RFC3339Nano}, - "format_kitchen": &objects.String{Value: time.Kitchen}, - "format_stamp": &objects.String{Value: time.Stamp}, - "format_stamp_milli": &objects.String{Value: time.StampMilli}, - "format_stamp_micro": &objects.String{Value: time.StampMicro}, - "format_stamp_nano": &objects.String{Value: time.StampNano}, - "nanosecond": &objects.Int{Value: int64(time.Nanosecond)}, - "microsecond": &objects.Int{Value: int64(time.Microsecond)}, - "millisecond": &objects.Int{Value: int64(time.Millisecond)}, - "second": &objects.Int{Value: int64(time.Second)}, - "minute": &objects.Int{Value: int64(time.Minute)}, - "hour": &objects.Int{Value: int64(time.Hour)}, - "january": &objects.Int{Value: int64(time.January)}, - "february": &objects.Int{Value: int64(time.February)}, - "march": &objects.Int{Value: int64(time.March)}, - "april": &objects.Int{Value: int64(time.April)}, - "may": &objects.Int{Value: int64(time.May)}, - "june": &objects.Int{Value: int64(time.June)}, - "july": &objects.Int{Value: int64(time.July)}, - "august": &objects.Int{Value: int64(time.August)}, - "september": &objects.Int{Value: int64(time.September)}, - "october": &objects.Int{Value: int64(time.October)}, - "november": &objects.Int{Value: int64(time.November)}, - "december": &objects.Int{Value: int64(time.December)}, - "sleep": &objects.UserFunction{Name: "sleep", Value: timesSleep}, // sleep(int) - "parse_duration": &objects.UserFunction{Name: "parse_duration", Value: timesParseDuration}, // parse_duration(str) => int - "since": &objects.UserFunction{Name: "since", Value: timesSince}, // since(time) => int - "until": &objects.UserFunction{Name: "until", Value: timesUntil}, // until(time) => int - "duration_hours": &objects.UserFunction{Name: "duration_hours", Value: timesDurationHours}, // duration_hours(int) => float - "duration_minutes": &objects.UserFunction{Name: "duration_minutes", Value: timesDurationMinutes}, // duration_minutes(int) => float - "duration_nanoseconds": &objects.UserFunction{Name: "duration_nanoseconds", Value: timesDurationNanoseconds}, // duration_nanoseconds(int) => int - "duration_seconds": &objects.UserFunction{Name: "duration_seconds", Value: timesDurationSeconds}, // duration_seconds(int) => float - "duration_string": &objects.UserFunction{Name: "duration_string", Value: timesDurationString}, // duration_string(int) => string - "month_string": &objects.UserFunction{Name: "month_string", Value: timesMonthString}, // month_string(int) => string - "date": &objects.UserFunction{Name: "date", Value: timesDate}, // date(year, month, day, hour, min, sec, nsec) => time - "now": &objects.UserFunction{Name: "now", Value: timesNow}, // now() => time - "parse": &objects.UserFunction{Name: "parse", Value: timesParse}, // parse(format, str) => time - "unix": &objects.UserFunction{Name: "unix", Value: timesUnix}, // unix(sec, nsec) => time - "add": &objects.UserFunction{Name: "add", Value: timesAdd}, // add(time, int) => time - "add_date": &objects.UserFunction{Name: "add_date", Value: timesAddDate}, // add_date(time, years, months, days) => time - "sub": &objects.UserFunction{Name: "sub", Value: timesSub}, // sub(t time, u time) => int - "after": &objects.UserFunction{Name: "after", Value: timesAfter}, // after(t time, u time) => bool - "before": &objects.UserFunction{Name: "before", Value: timesBefore}, // before(t time, u time) => bool - "time_year": &objects.UserFunction{Name: "time_year", Value: timesTimeYear}, // time_year(time) => int - "time_month": &objects.UserFunction{Name: "time_month", Value: timesTimeMonth}, // time_month(time) => int - "time_day": &objects.UserFunction{Name: "time_day", Value: timesTimeDay}, // time_day(time) => int - "time_weekday": &objects.UserFunction{Name: "time_weekday", Value: timesTimeWeekday}, // time_weekday(time) => int - "time_hour": &objects.UserFunction{Name: "time_hour", Value: timesTimeHour}, // time_hour(time) => int - "time_minute": &objects.UserFunction{Name: "time_minute", Value: timesTimeMinute}, // time_minute(time) => int - "time_second": &objects.UserFunction{Name: "time_second", Value: timesTimeSecond}, // time_second(time) => int - "time_nanosecond": &objects.UserFunction{Name: "time_nanosecond", Value: timesTimeNanosecond}, // time_nanosecond(time) => int - "time_unix": &objects.UserFunction{Name: "time_unix", Value: timesTimeUnix}, // time_unix(time) => int - "time_unix_nano": &objects.UserFunction{Name: "time_unix_nano", Value: timesTimeUnixNano}, // time_unix_nano(time) => int - "time_format": &objects.UserFunction{Name: "time_format", Value: timesTimeFormat}, // time_format(time, format) => string - "time_location": &objects.UserFunction{Name: "time_location", Value: timesTimeLocation}, // time_location(time) => string - "time_string": &objects.UserFunction{Name: "time_string", Value: timesTimeString}, // time_string(time) => string - "is_zero": &objects.UserFunction{Name: "is_zero", Value: timesIsZero}, // is_zero(time) => bool - "to_local": &objects.UserFunction{Name: "to_local", Value: timesToLocal}, // to_local(time) => time - "to_utc": &objects.UserFunction{Name: "to_utc", Value: timesToUTC}, // to_utc(time) => time -} - -func timesSleep(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - time.Sleep(time.Duration(i1)) - ret = objects.UndefinedValue - - return -} - -func timesParseDuration(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - dur, err := time.ParseDuration(s1) - if err != nil { - ret = wrapError(err) - return - } - - ret = &objects.Int{Value: int64(dur)} - - return -} - -func timesSince(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(time.Since(t1))} - - return -} - -func timesUntil(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(time.Until(t1))} - - return -} - -func timesDurationHours(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Float{Value: time.Duration(i1).Hours()} - - return -} - -func timesDurationMinutes(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Float{Value: time.Duration(i1).Minutes()} - - return -} - -func timesDurationNanoseconds(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: time.Duration(i1).Nanoseconds()} - - return -} - -func timesDurationSeconds(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Float{Value: time.Duration(i1).Seconds()} - - return -} - -func timesDurationString(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.String{Value: time.Duration(i1).String()} - - return -} - -func timesMonthString(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.String{Value: time.Month(i1).String()} - - return -} - -func timesDate(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 7 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - i3, ok := objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - i4, ok := objects.ToInt(args[3]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "fourth", - Expected: "int(compatible)", - Found: args[3].TypeName(), - } - return - } - i5, ok := objects.ToInt(args[4]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "fifth", - Expected: "int(compatible)", - Found: args[4].TypeName(), - } - return - } - i6, ok := objects.ToInt(args[5]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "sixth", - Expected: "int(compatible)", - Found: args[5].TypeName(), - } - return - } - i7, ok := objects.ToInt(args[6]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "seventh", - Expected: "int(compatible)", - Found: args[6].TypeName(), - } - return - } - - ret = &objects.Time{Value: time.Date(i1, time.Month(i2), i3, i4, i5, i6, i7, time.Now().Location())} - - return -} - -func timesNow(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 0 { - err = objects.ErrWrongNumArguments - return - } - - ret = &objects.Time{Value: time.Now()} - - return -} - -func timesParse(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - s1, ok := objects.ToString(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "string(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - parsed, err := time.Parse(s1, s2) - if err != nil { - ret = wrapError(err) - return - } - - ret = &objects.Time{Value: parsed} - - return -} - -func timesUnix(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - i1, ok := objects.ToInt64(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "int(compatible)", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt64(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.Time{Value: time.Unix(i1, i2)} - - return -} - -func timesAdd(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt64(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.Time{Value: t1.Add(time.Duration(i2))} - - return -} - -func timesSub(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - t2, ok := objects.ToTime(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "time(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Sub(t2))} - - return -} - -func timesAddDate(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 4 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - i2, ok := objects.ToInt(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "int(compatible)", - Found: args[1].TypeName(), - } - return - } - - i3, ok := objects.ToInt(args[2]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "third", - Expected: "int(compatible)", - Found: args[2].TypeName(), - } - return - } - - i4, ok := objects.ToInt(args[3]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "fourth", - Expected: "int(compatible)", - Found: args[3].TypeName(), - } - return - } - - ret = &objects.Time{Value: t1.AddDate(i2, i3, i4)} - - return -} - -func timesAfter(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - t2, ok := objects.ToTime(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "time(compatible)", - Found: args[1].TypeName(), - } - return - } - - if t1.After(t2) { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return -} - -func timesBefore(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - t2, ok := objects.ToTime(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - if t1.Before(t2) { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return -} - -func timesTimeYear(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Year())} - - return -} - -func timesTimeMonth(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Month())} - - return -} - -func timesTimeDay(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Day())} - - return -} - -func timesTimeWeekday(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Weekday())} - - return -} - -func timesTimeHour(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Hour())} - - return -} - -func timesTimeMinute(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Minute())} - - return -} - -func timesTimeSecond(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Second())} - - return -} - -func timesTimeNanosecond(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Nanosecond())} - - return -} - -func timesTimeUnix(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.Unix())} - - return -} - -func timesTimeUnixNano(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Int{Value: int64(t1.UnixNano())} - - return -} - -func timesTimeFormat(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 2 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - s2, ok := objects.ToString(args[1]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "second", - Expected: "string(compatible)", - Found: args[1].TypeName(), - } - return - } - - ret = &objects.String{Value: t1.Format(s2)} - - return -} - -func timesIsZero(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - if t1.IsZero() { - ret = objects.TrueValue - } else { - ret = objects.FalseValue - } - - return -} - -func timesToLocal(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Time{Value: t1.Local()} - - return -} - -func timesToUTC(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.Time{Value: t1.UTC()} - - return -} - -func timesTimeLocation(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.String{Value: t1.Location().String()} - - return -} - -func timesTimeString(args ...objects.Object) (ret objects.Object, err error) { - if len(args) != 1 { - err = objects.ErrWrongNumArguments - return - } - - t1, ok := objects.ToTime(args[0]) - if !ok { - err = objects.ErrInvalidArgumentType{ - Name: "first", - Expected: "time(compatible)", - Found: args[0].TypeName(), - } - return - } - - ret = &objects.String{Value: t1.String()} - - return -} diff --git a/vendor/github.com/d5/tengo/tengo.go b/vendor/github.com/d5/tengo/tengo.go new file mode 100644 index 00000000..a883bbd7 --- /dev/null +++ b/vendor/github.com/d5/tengo/tengo.go @@ -0,0 +1,11 @@ +package tengo + +var ( + // MaxStringLen is the maximum byte-length for string value. + // Note this limit applies to all compiler/VM instances in the process. + MaxStringLen = 2147483647 + + // MaxBytesLen is the maximum length for bytes value. + // Note this limit applies to all compiler/VM instances in the process. + MaxBytesLen = 2147483647 +) diff --git a/vendor/modules.txt b/vendor/modules.txt index 53d1d268..dfa18372 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -17,14 +17,14 @@ github.com/Philipp15b/go-steam/rwu github.com/Philipp15b/go-steam/socialcache # github.com/bwmarrin/discordgo v0.19.0 github.com/bwmarrin/discordgo -# github.com/d5/tengo v1.9.2 +# github.com/d5/tengo v1.12.1 github.com/d5/tengo/script github.com/d5/tengo/compiler github.com/d5/tengo/compiler/parser github.com/d5/tengo/compiler/source github.com/d5/tengo/objects github.com/d5/tengo/runtime -github.com/d5/tengo/stdlib +github.com/d5/tengo github.com/d5/tengo/compiler/ast github.com/d5/tengo/compiler/token github.com/d5/tengo/compiler/scanner