4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-03 11:57:45 +00:00

Update dependencies/vendor (#1659)

This commit is contained in:
Wim
2021-12-12 00:05:15 +01:00
committed by GitHub
parent 658bdd9faa
commit 3893a035be
214 changed files with 8892 additions and 5650 deletions

View File

@ -1,7 +1,3 @@
<p align="center">
<img src="https://raw.githubusercontent.com/d5/tengolang-share/master/logo_400.png" width="200" height="200">
</p>
# The Tengo Language
[![GoDoc](https://godoc.org/github.com/d5/tengo/v2?status.svg)](https://godoc.org/github.com/d5/tengo/v2)

View File

@ -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) {

View File

@ -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 {

View File

@ -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()
}()

View File

@ -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.

View File

@ -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]