4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-07-01 15:56:19 +00:00

Update vendor (#852)

This commit is contained in:
Wim
2019-06-16 23:33:25 +02:00
committed by GitHub
parent f4ae610448
commit cb712ff37d
294 changed files with 71447 additions and 6861 deletions

View File

@ -7,6 +7,7 @@
[![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)
[![Sourcegraph](https://sourcegraph.com/github.com/d5/tengo/-/badge.svg)](https://sourcegraph.com/github.com/d5/tengo?badge)
**Tengo is a small, dynamic, fast, secure script language for Go.**

View File

@ -8,9 +8,10 @@ import (
// IdentList represents a list of identifiers.
type IdentList struct {
LParen source.Pos
List []*Ident
RParen source.Pos
LParen source.Pos
VarArgs bool
List []*Ident
RParen source.Pos
}
// Pos returns the position of first character belonging to the node.
@ -50,8 +51,12 @@ func (n *IdentList) NumFields() int {
func (n *IdentList) String() string {
var list []string
for _, e := range n.List {
list = append(list, e.String())
for i, e := range n.List {
if n.VarArgs && i == len(n.List)-1 {
list = append(list, "..."+e.String())
} else {
list = append(list, e.String())
}
}
return "(" + strings.Join(list, ", ") + ")"

View File

@ -477,6 +477,7 @@ func (c *Compiler) Compile(node ast.Node) error {
Instructions: instructions,
NumLocals: numLocals,
NumParameters: len(node.Type.Params.List),
VarArgs: node.Type.Params.VarArgs,
SourceMap: sourceMap,
}

View File

@ -13,7 +13,7 @@ func MakeInstruction(opcode Opcode, operands ...int) []byte {
totalLen += w
}
instruction := make([]byte, totalLen, totalLen)
instruction := make([]byte, totalLen)
instruction[0] = byte(opcode)
offset := 1

View File

@ -610,19 +610,31 @@ func (p *Parser) parseIdentList() *ast.IdentList {
var params []*ast.Ident
lparen := p.expect(token.LParen)
isVarArgs := false
if p.token != token.RParen {
params = append(params, p.parseIdent())
for p.token == token.Comma {
if p.token == token.Ellipsis {
isVarArgs = true
p.next()
}
params = append(params, p.parseIdent())
for !isVarArgs && p.token == token.Comma {
p.next()
if p.token == token.Ellipsis {
isVarArgs = true
p.next()
}
params = append(params, p.parseIdent())
}
}
rparen := p.expect(token.RParen)
return &ast.IdentList{
LParen: lparen,
RParen: rparen,
List: params,
LParen: lparen,
RParen: rparen,
VarArgs: isVarArgs,
List: params,
}
}

View File

@ -6,7 +6,7 @@ type SymbolScope string
// List of symbol scopes
const (
ScopeGlobal SymbolScope = "GLOBAL"
ScopeLocal = "LOCAL"
ScopeBuiltin = "BUILTIN"
ScopeFree = "FREE"
ScopeLocal SymbolScope = "LOCAL"
ScopeBuiltin SymbolScope = "BUILTIN"
ScopeFree SymbolScope = "FREE"
)

27
vendor/github.com/d5/tengo/objects/builtin_format.go generated vendored Normal file
View File

@ -0,0 +1,27 @@
package objects
func builtinFormat(args ...Object) (Object, error) {
numArgs := len(args)
if numArgs == 0 {
return nil, ErrWrongNumArguments
}
format, ok := args[0].(*String)
if !ok {
return nil, ErrInvalidArgumentType{
Name: "format",
Expected: "string",
Found: args[0].TypeName(),
}
}
if numArgs == 1 {
return format, nil // okay to return 'format' directly as String is immutable
}
s, err := Format(format.Value, args[1:]...)
if err != nil {
return nil, err
}
return &String{Value: s}, nil
}

View File

@ -111,4 +111,8 @@ var Builtins = []*BuiltinFunction{
Name: "type_name",
Value: builtinTypeName,
},
{
Name: "format",
Value: builtinFormat,
},
}

View File

@ -57,7 +57,7 @@ func (o *Bytes) Equals(x Object) bool {
return false
}
return bytes.Compare(o.Value, t.Value) == 0
return bytes.Equal(o.Value, t.Value)
}
// IndexGet returns an element (as Int) at a given index.

View File

@ -10,6 +10,7 @@ type CompiledFunction struct {
Instructions []byte
NumLocals int // number of local variables (including function parameters)
NumParameters int
VarArgs bool
SourceMap map[int]source.Pos
}
@ -34,6 +35,7 @@ func (o *CompiledFunction) Copy() Object {
Instructions: append([]byte{}, o.Instructions...),
NumLocals: o.NumLocals,
NumParameters: o.NumParameters,
VarArgs: o.VarArgs,
}
}

View File

@ -254,7 +254,7 @@ func FromInterface(v interface{}) (Object, error) {
case []Object:
return &Array{Value: v}, nil
case []interface{}:
arr := make([]Object, len(v), len(v))
arr := make([]Object, len(v))
for i, e := range v {
vo, err := FromInterface(e)
if err != nil {

1212
vendor/github.com/d5/tengo/objects/formatter.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -76,13 +76,13 @@ func (o *Map) Equals(x Object) bool {
// IndexGet returns the value for the given key.
func (o *Map) IndexGet(index Object) (res Object, err error) {
strIdx, ok := index.(*String)
strIdx, ok := ToString(index)
if !ok {
err = ErrInvalidIndexType
return
}
val, ok := o.Value[strIdx.Value]
val, ok := o.Value[strIdx]
if !ok {
val = UndefinedValue
}

View File

@ -40,3 +40,23 @@ func (o *Undefined) Equals(x Object) bool {
func (o *Undefined) IndexGet(index Object) (Object, error) {
return UndefinedValue, nil
}
// Iterate creates a map iterator.
func (o *Undefined) Iterate() Iterator {
return o
}
// Next returns true if there are more elements to iterate.
func (o *Undefined) Next() bool {
return false
}
// Key returns the key or index value of the current element.
func (o *Undefined) Key() Object {
return o
}
// Value returns the value of the current element.
func (o *Undefined) Value() Object {
return o
}

View File

@ -642,9 +642,31 @@ func (v *VM) run() {
switch callee := value.(type) {
case *objects.Closure:
if callee.Fn.VarArgs {
// if the closure is variadic,
// roll up all variadic parameters into an array
realArgs := callee.Fn.NumParameters - 1
varArgs := numArgs - realArgs
if varArgs >= 0 {
numArgs = realArgs + 1
args := make([]objects.Object, varArgs)
spStart := v.sp - varArgs
for i := spStart; i < v.sp; i++ {
args[i-spStart] = v.stack[i]
}
v.stack[spStart] = &objects.Array{Value: args}
v.sp = spStart + 1
}
}
if numArgs != callee.Fn.NumParameters {
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
callee.Fn.NumParameters, numArgs)
if callee.Fn.VarArgs {
v.err = fmt.Errorf("wrong number of arguments: want>=%d, got=%d",
callee.Fn.NumParameters-1, numArgs)
} else {
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
callee.Fn.NumParameters, numArgs)
}
return
}
@ -674,9 +696,31 @@ func (v *VM) run() {
v.sp = v.sp - numArgs + callee.Fn.NumLocals
case *objects.CompiledFunction:
if callee.VarArgs {
// if the closure is variadic,
// roll up all variadic parameters into an array
realArgs := callee.NumParameters - 1
varArgs := numArgs - realArgs
if varArgs >= 0 {
numArgs = realArgs + 1
args := make([]objects.Object, varArgs)
spStart := v.sp - varArgs
for i := spStart; i < v.sp; i++ {
args[i-spStart] = v.stack[i]
}
v.stack[spStart] = &objects.Array{Value: args}
v.sp = spStart + 1
}
}
if numArgs != callee.NumParameters {
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
callee.NumParameters, numArgs)
if callee.VarArgs {
v.err = fmt.Errorf("wrong number of arguments: want>=%d, got=%d",
callee.NumParameters-1, numArgs)
} else {
v.err = fmt.Errorf("wrong number of arguments: want=%d, got=%d",
callee.NumParameters, numArgs)
}
return
}
@ -707,9 +751,7 @@ func (v *VM) run() {
case objects.Callable:
var args []objects.Object
for _, arg := range v.stack[v.sp-numArgs : v.sp] {
args = append(args, arg)
}
args = append(args, v.stack[v.sp-numArgs:v.sp]...)
ret, e := callee.Call(args...)
v.sp -= numArgs + 1
@ -817,9 +859,12 @@ func (v *VM) run() {
val := v.stack[v.sp-numSelectors-1]
v.sp -= numSelectors + 1
sp := v.curFrame.basePointer + localIndex
dst := v.stack[v.curFrame.basePointer+localIndex]
if obj, ok := dst.(*objects.ObjectPtr); ok {
dst = *obj.Value
}
if e := indexAssign(v.stack[sp], val, selectors); e != nil {
if e := indexAssign(dst, val, selectors); e != nil {
v.err = e
return
}

View File

@ -183,12 +183,3 @@ func (s *Script) prepCompile() (symbolTable *compiler.SymbolTable, globals []obj
return
}
func (s *Script) copyVariables() map[string]*Variable {
vars := make(map[string]*Variable)
for n, v := range s.variables {
vars[n] = v
}
return vars
}

View File

@ -44,12 +44,12 @@ func fmtPrintf(args ...objects.Object) (ret objects.Object, err error) {
return nil, nil
}
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
formatArgs[idx] = objects.ToInterface(arg)
s, err := objects.Format(format.Value, args[1:]...)
if err != nil {
return nil, err
}
fmt.Printf(format.Value, formatArgs...)
fmt.Print(s)
return nil, nil
}
@ -84,15 +84,9 @@ func fmtSprintf(args ...objects.Object) (ret objects.Object, err error) {
return format, nil // okay to return 'format' directly as String is immutable
}
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
formatArgs[idx] = objects.ToInterface(arg)
}
s := fmt.Sprintf(format.Value, formatArgs...)
if len(s) > tengo.MaxStringLen {
return nil, objects.ErrStringLimit
s, err := objects.Format(format.Value, args[1:]...)
if err != nil {
return nil, err
}
return &objects.String{Value: s}, nil

View File

@ -32,6 +32,7 @@ var textModule = map[string]objects.Object{
"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: textRepeat}, // repeat(s, count) => string
"replace": &objects.UserFunction{Name: "replace", Value: textReplace}, // replace(s, old, new, n) => string
"substr": &objects.UserFunction{Name: "substr", Value: textSubstring}, // substr(s, lower, upper) => 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]
@ -40,6 +41,9 @@ var textModule = map[string]objects.Object{
"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
"pad_left": &objects.UserFunction{Name: "pad_left", Value: textPadLeft}, // pad_left(s, pad_len, pad_with) => string
"pad_right": &objects.UserFunction{Name: "pad_right", Value: textPadRight}, // pad_right(s, pad_len, pad_with) => string
"trim": &objects.UserFunction{Name: "trim", Value: FuncASSRS(strings.Trim)}, // trim(s, cutset) => 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
@ -376,6 +380,195 @@ func textReplace(args ...objects.Object) (ret objects.Object, err error) {
return
}
func textSubstring(args ...objects.Object) (ret objects.Object, err error) {
argslen := len(args)
if argslen != 2 && argslen != 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
}
i2, ok := objects.ToInt(args[1])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
return
}
strlen := len(s1)
i3 := strlen
if argslen == 3 {
i3, ok = objects.ToInt(args[2])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "third",
Expected: "int(compatible)",
Found: args[2].TypeName(),
}
return
}
}
if i2 > i3 {
err = objects.ErrInvalidIndexType
return
}
if i2 < 0 {
i2 = 0
} else if i2 > strlen {
i2 = strlen
}
if i3 < 0 {
i3 = 0
} else if i3 > strlen {
i3 = strlen
}
ret = &objects.String{Value: s1[i2:i3]}
return
}
func textPadLeft(args ...objects.Object) (ret objects.Object, err error) {
argslen := len(args)
if argslen != 2 && argslen != 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
}
i2, ok := objects.ToInt(args[1])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
return
}
if i2 > tengo.MaxStringLen {
return nil, objects.ErrStringLimit
}
sLen := len(s1)
if sLen >= i2 {
ret = &objects.String{Value: s1}
return
}
s3 := " "
if argslen == 3 {
s3, ok = objects.ToString(args[2])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "third",
Expected: "string(compatible)",
Found: args[2].TypeName(),
}
return
}
}
padStrLen := len(s3)
if padStrLen == 0 {
ret = &objects.String{Value: s1}
return
}
padCount := ((i2 - padStrLen) / padStrLen) + 1
retStr := strings.Repeat(s3, int(padCount)) + s1
ret = &objects.String{Value: retStr[len(retStr)-i2:]}
return
}
func textPadRight(args ...objects.Object) (ret objects.Object, err error) {
argslen := len(args)
if argslen != 2 && argslen != 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
}
i2, ok := objects.ToInt(args[1])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "second",
Expected: "int(compatible)",
Found: args[1].TypeName(),
}
return
}
if i2 > tengo.MaxStringLen {
return nil, objects.ErrStringLimit
}
sLen := len(s1)
if sLen >= i2 {
ret = &objects.String{Value: s1}
return
}
s3 := " "
if argslen == 3 {
s3, ok = objects.ToString(args[2])
if !ok {
err = objects.ErrInvalidArgumentType{
Name: "third",
Expected: "string(compatible)",
Found: args[2].TypeName(),
}
return
}
}
padStrLen := len(s3)
if padStrLen == 0 {
ret = &objects.String{Value: s1}
return
}
padCount := ((i2 - padStrLen) / padStrLen) + 1
retStr := s1 + strings.Repeat(s3, int(padCount))
ret = &objects.String{Value: retStr[:i2]}
return
}
func textRepeat(args ...objects.Object) (ret objects.Object, err error) {
if len(args) != 2 {
return nil, objects.ErrWrongNumArguments