From 3893a035be347a7687a41d2054dd1b274d3a0504 Mon Sep 17 00:00:00 2001 From: Wim Date: Sun, 12 Dec 2021 00:05:15 +0100 Subject: Update dependencies/vendor (#1659) --- vendor/github.com/d5/tengo/v2/README.md | 4 -- vendor/github.com/d5/tengo/v2/compiler.go | 70 +++++++++++++++++++++++++++---- vendor/github.com/d5/tengo/v2/modules.go | 5 +++ vendor/github.com/d5/tengo/v2/script.go | 16 ++++++- vendor/github.com/d5/tengo/v2/tengo.go | 3 ++ vendor/github.com/d5/tengo/v2/vm.go | 2 +- 6 files changed, 86 insertions(+), 14 deletions(-) (limited to 'vendor/github.com/d5/tengo') diff --git a/vendor/github.com/d5/tengo/v2/README.md b/vendor/github.com/d5/tengo/v2/README.md index fd214cd9..c19c5699 100644 --- a/vendor/github.com/d5/tengo/v2/README.md +++ b/vendor/github.com/d5/tengo/v2/README.md @@ -1,7 +1,3 @@ -

- -

- # The Tengo Language [![GoDoc](https://godoc.org/github.com/d5/tengo/v2?status.svg)](https://godoc.org/github.com/d5/tengo/v2) diff --git a/vendor/github.com/d5/tengo/v2/compiler.go b/vendor/github.com/d5/tengo/v2/compiler.go index 53cc7d38..e4e04303 100644 --- a/vendor/github.com/d5/tengo/v2/compiler.go +++ b/vendor/github.com/d5/tengo/v2/compiler.go @@ -1,9 +1,11 @@ package tengo import ( + "errors" "fmt" "io" "io/ioutil" + "os" "path/filepath" "reflect" "strings" @@ -45,11 +47,12 @@ type Compiler struct { parent *Compiler modulePath string importDir string + importFileExt []string constants []Object symbolTable *SymbolTable scopes []compilationScope scopeIndex int - modules *ModuleMap + modules ModuleGetter compiledModules map[string]*CompiledFunction allowFileImport bool loops []*loop @@ -63,7 +66,7 @@ func NewCompiler( file *parser.SourceFile, symbolTable *SymbolTable, constants []Object, - modules *ModuleMap, + modules ModuleGetter, trace io.Writer, ) *Compiler { mainScope := compilationScope{ @@ -96,6 +99,7 @@ func NewCompiler( trace: trace, modules: modules, compiledModules: make(map[string]*CompiledFunction), + importFileExt: []string{SourceFileExtDefault}, } } @@ -538,12 +542,8 @@ func (c *Compiler) Compile(node parser.Node) error { } } else if c.allowFileImport { moduleName := node.ModuleName - if !strings.HasSuffix(moduleName, ".tengo") { - moduleName += ".tengo" - } - modulePath, err := filepath.Abs( - filepath.Join(c.importDir, moduleName)) + modulePath, err := c.getPathModule(moduleName) if err != nil { return c.errorf(node, "module file path error: %s", err.Error()) @@ -640,6 +640,39 @@ func (c *Compiler) SetImportDir(dir string) { c.importDir = dir } +// SetImportFileExt sets the extension name of the source file for loading +// local module files. +// +// Use this method if you want other source file extension than ".tengo". +// +// // this will search for *.tengo, *.foo, *.bar +// err := c.SetImportFileExt(".tengo", ".foo", ".bar") +// +// This function requires at least one argument, since it will replace the +// current list of extension name. +func (c *Compiler) SetImportFileExt(exts ...string) error { + if len(exts) == 0 { + return fmt.Errorf("missing arg: at least one argument is required") + } + + for _, ext := range exts { + if ext != filepath.Ext(ext) || ext == "" { + return fmt.Errorf("invalid file extension: %s", ext) + } + } + + c.importFileExt = exts // Replace the hole current extension list + + return nil +} + +// GetImportFileExt returns the current list of extension name. +// Thease are the complementary suffix of the source file to search and load +// local module files. +func (c *Compiler) GetImportFileExt() []string { + return c.importFileExt +} + func (c *Compiler) compileAssign( node parser.Node, lhs, rhs []parser.Expr, @@ -1098,6 +1131,7 @@ func (c *Compiler) fork( child.parent = c // parent to set to current compiler child.allowFileImport = c.allowFileImport child.importDir = c.importDir + child.importFileExt = c.importFileExt if isFile && c.importDir != "" { child.importDir = filepath.Dir(modulePath) } @@ -1287,6 +1321,28 @@ func (c *Compiler) printTrace(a ...interface{}) { _, _ = fmt.Fprintln(c.trace, a...) } +func (c *Compiler) getPathModule(moduleName string) (pathFile string, err error) { + for _, ext := range c.importFileExt { + nameFile := moduleName + + if !strings.HasSuffix(nameFile, ext) { + nameFile += ext + } + + pathFile, err = filepath.Abs(filepath.Join(c.importDir, nameFile)) + if err != nil { + continue + } + + // Check if file exists + if _, err := os.Stat(pathFile); !errors.Is(err, os.ErrNotExist) { + return pathFile, nil + } + } + + return "", fmt.Errorf("module '%s' not found at: %s", moduleName, pathFile) +} + func resolveAssignLHS( expr parser.Expr, ) (name string, selectors []parser.Expr) { diff --git a/vendor/github.com/d5/tengo/v2/modules.go b/vendor/github.com/d5/tengo/v2/modules.go index c8fcde7f..dadd5a3b 100644 --- a/vendor/github.com/d5/tengo/v2/modules.go +++ b/vendor/github.com/d5/tengo/v2/modules.go @@ -6,6 +6,11 @@ type Importable interface { Import(moduleName string) (interface{}, error) } +// ModuleGetter enables implementing dynamic module loading. +type ModuleGetter interface { + Get(name string) Importable +} + // ModuleMap represents a set of named modules. Use NewModuleMap to create a // new module map. type ModuleMap struct { diff --git a/vendor/github.com/d5/tengo/v2/script.go b/vendor/github.com/d5/tengo/v2/script.go index 46e48029..82b02f52 100644 --- a/vendor/github.com/d5/tengo/v2/script.go +++ b/vendor/github.com/d5/tengo/v2/script.go @@ -12,7 +12,7 @@ import ( // Script can simplify compilation and execution of embedded scripts. type Script struct { variables map[string]*Variable - modules *ModuleMap + modules ModuleGetter input []byte maxAllocs int64 maxConstObjects int @@ -54,7 +54,7 @@ func (s *Script) Remove(name string) bool { } // SetImports sets import modules. -func (s *Script) SetImports(modules *ModuleMap) { +func (s *Script) SetImports(modules ModuleGetter) { s.modules = modules } @@ -219,6 +219,18 @@ func (c *Compiled) RunContext(ctx context.Context) (err error) { v := NewVM(c.bytecode, c.globals, c.maxAllocs) ch := make(chan error, 1) go func() { + defer func() { + if r := recover(); r != nil { + switch e := r.(type) { + case string: + ch <- fmt.Errorf(e) + case error: + ch <- e + default: + ch <- fmt.Errorf("unknown panic: %v", e) + } + } + }() ch <- v.Run() }() diff --git a/vendor/github.com/d5/tengo/v2/tengo.go b/vendor/github.com/d5/tengo/v2/tengo.go index 098a1970..490e9aed 100644 --- a/vendor/github.com/d5/tengo/v2/tengo.go +++ b/vendor/github.com/d5/tengo/v2/tengo.go @@ -26,6 +26,9 @@ const ( // MaxFrames is the maximum number of function frames for a VM. MaxFrames = 1024 + + // SourceFileExtDefault is the default extension for source files. + SourceFileExtDefault = ".tengo" ) // CallableFunc is a function signature for the callable functions. diff --git a/vendor/github.com/d5/tengo/v2/vm.go b/vendor/github.com/d5/tengo/v2/vm.go index 811ecef9..c8365252 100644 --- a/vendor/github.com/d5/tengo/v2/vm.go +++ b/vendor/github.com/d5/tengo/v2/vm.go @@ -293,7 +293,7 @@ func (v *VM) run() { case parser.OpMap: v.ip += 2 numElements := int(v.curInsts[v.ip]) | int(v.curInsts[v.ip-1])<<8 - kv := make(map[string]Object) + kv := make(map[string]Object, numElements) for i := v.sp - numElements; i < v.sp; i += 2 { key := v.stack[i] value := v.stack[i+1] -- cgit v1.2.3