4
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2025-06-28 00:59:24 +00:00

Update tengo vendor and load the stdlib. Fixes #789 (#792)

This commit is contained in:
Wim
2019-04-06 22:18:25 +02:00
committed by GitHub
parent cdf33e5748
commit 115d20373c
63 changed files with 7020 additions and 1304 deletions

View File

@ -1,37 +0,0 @@
package objects
import "github.com/d5/tengo/compiler/token"
// Break represents a break statement.
type Break struct{}
// TypeName returns the name of the type.
func (o *Break) TypeName() string {
return "break"
}
func (o *Break) String() string {
return "<break>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
func (o *Break) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Break) Copy() Object {
return &Break{}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Break) IsFalsy() bool {
return false
}
// Equals returns true if the value of the type
// is equal to the value of another object.
func (o *Break) Equals(x Object) bool {
return false
}

View File

@ -1,60 +0,0 @@
package objects
import (
"encoding/json"
"github.com/d5/tengo"
)
// to_json(v object) => bytes
func builtinToJSON(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
res, err := json.Marshal(objectToInterface(args[0]))
if err != nil {
return &Error{Value: &String{Value: err.Error()}}, nil
}
if len(res) > tengo.MaxBytesLen {
return nil, ErrBytesLimit
}
return &Bytes{Value: res}, nil
}
// from_json(data string/bytes) => object
func builtinFromJSON(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
var target interface{}
switch o := args[0].(type) {
case *Bytes:
err := json.Unmarshal(o.Value, &target)
if err != nil {
return &Error{Value: &String{Value: err.Error()}}, nil
}
case *String:
err := json.Unmarshal([]byte(o.Value), &target)
if err != nil {
return &Error{Value: &String{Value: err.Error()}}, nil
}
default:
return nil, ErrInvalidArgumentType{
Name: "first",
Expected: "bytes/string",
Found: args[0].TypeName(),
}
}
res, err := FromInterface(target)
if err != nil {
return nil, err
}
return res, nil
}

23
vendor/github.com/d5/tengo/objects/builtin_module.go generated vendored Normal file
View File

@ -0,0 +1,23 @@
package objects
// BuiltinModule is an importable module that's written in Go.
type BuiltinModule struct {
Attrs map[string]Object
}
// Import returns an immutable map for the module.
func (m *BuiltinModule) Import(moduleName string) (interface{}, error) {
return m.AsImmutableMap(moduleName), nil
}
// AsImmutableMap converts builtin module into an immutable map.
func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap {
attrs := make(map[string]Object, len(m.Attrs))
for k, v := range m.Attrs {
attrs[k] = v.Copy()
}
attrs["__module_name__"] = &String{Value: moduleName}
return &ImmutableMap{Value: attrs}
}

View File

@ -1,83 +0,0 @@
package objects
import (
"fmt"
"github.com/d5/tengo"
)
// print(args...)
func builtinPrint(args ...Object) (Object, error) {
for _, arg := range args {
if str, ok := arg.(*String); ok {
fmt.Println(str.Value)
} else {
fmt.Println(arg.String())
}
}
return nil, nil
}
// printf("format", args...)
func builtinPrintf(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 {
fmt.Print(format)
return nil, nil
}
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
formatArgs[idx] = objectToInterface(arg)
}
fmt.Printf(format.Value, formatArgs...)
return nil, nil
}
// sprintf("format", args...)
func builtinSprintf(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
}
formatArgs := make([]interface{}, numArgs-1, numArgs-1)
for idx, arg := range args[1:] {
formatArgs[idx] = objectToInterface(arg)
}
s := fmt.Sprintf(format.Value, formatArgs...)
if len(s) > tengo.MaxStringLen {
return nil, ErrStringLimit
}
return &String{Value: s}, nil
}

View File

@ -181,3 +181,15 @@ func builtinIsCallable(args ...Object) (Object, error) {
return FalseValue, nil
}
func builtinIsIterable(args ...Object) (Object, error) {
if len(args) != 1 {
return nil, ErrWrongNumArguments
}
if _, ok := args[0].(Iterable); ok {
return TrueValue, nil
}
return FalseValue, nil
}

View File

@ -2,19 +2,7 @@ package objects
// 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,
},
var Builtins = []*BuiltinFunction{
{
Name: "len",
Value: builtinLen,
@ -95,6 +83,10 @@ var Builtins = []BuiltinFunction{
Name: "is_immutable_map",
Value: builtinIsImmutableMap,
},
{
Name: "is_iterable",
Value: builtinIsIterable,
},
{
Name: "is_time",
Value: builtinIsTime,
@ -115,50 +107,8 @@ var Builtins = []BuiltinFunction{
Name: "is_callable",
Value: builtinIsCallable,
},
{
Name: "to_json",
Value: builtinToJSON,
},
{
Name: "from_json",
Value: builtinFromJSON,
},
{
Name: "type_name",
Value: 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()...)
}

View File

@ -79,3 +79,11 @@ func (o *Bytes) IndexGet(index Object) (res Object, err error) {
return
}
// Iterate creates a bytes iterator.
func (o *Bytes) Iterate() Iterator {
return &BytesIterator{
v: o.Value,
l: len(o.Value),
}
}

57
vendor/github.com/d5/tengo/objects/bytes_iterator.go generated vendored Normal file
View File

@ -0,0 +1,57 @@
package objects
import "github.com/d5/tengo/compiler/token"
// BytesIterator represents an iterator for a string.
type BytesIterator struct {
v []byte
i int
l int
}
// TypeName returns the name of the type.
func (i *BytesIterator) TypeName() string {
return "bytes-iterator"
}
func (i *BytesIterator) String() string {
return "<bytes-iterator>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
func (i *BytesIterator) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// IsFalsy returns true if the value of the type is falsy.
func (i *BytesIterator) IsFalsy() bool {
return true
}
// Equals returns true if the value of the type
// is equal to the value of another object.
func (i *BytesIterator) Equals(Object) bool {
return false
}
// Copy returns a copy of the type.
func (i *BytesIterator) Copy() Object {
return &BytesIterator{v: i.v, i: i.i, l: i.l}
}
// Next returns true if there are more elements to iterate.
func (i *BytesIterator) Next() bool {
i.i++
return i.i <= i.l
}
// Key returns the key or index value of the current element.
func (i *BytesIterator) Key() Object {
return &Int{Value: int64(i.i - 1)}
}
// Value returns the value of the current element.
func (i *BytesIterator) Value() Object {
return &Int{Value: int64(i.v[i.i-1])}
}

View File

@ -7,7 +7,7 @@ import (
// Closure represents a function closure.
type Closure struct {
Fn *CompiledFunction
Free []*Object
Free []*ObjectPtr
}
// TypeName returns the name of the type.
@ -29,7 +29,7 @@ func (o *Closure) BinaryOp(op token.Token, rhs Object) (Object, error) {
func (o *Closure) Copy() Object {
return &Closure{
Fn: o.Fn.Copy().(*CompiledFunction),
Free: append([]*Object{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers
Free: append([]*ObjectPtr{}, o.Free...), // DO NOT Copy() of elements; these are variable pointers
}
}

View File

@ -47,3 +47,14 @@ func (o *CompiledFunction) IsFalsy() bool {
func (o *CompiledFunction) Equals(x Object) bool {
return false
}
// SourcePos returns the source position of the instruction at ip.
func (o *CompiledFunction) SourcePos(ip int) source.Pos {
for ip >= 0 {
if p, ok := o.SourceMap[ip]; ok {
return p
}
ip--
}
return source.NoPos
}

View File

@ -1,38 +0,0 @@
package objects
import "github.com/d5/tengo/compiler/token"
// Continue represents a continue statement.
type Continue struct {
}
// TypeName returns the name of the type.
func (o *Continue) TypeName() string {
return "continue"
}
func (o *Continue) String() string {
return "<continue>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
func (o *Continue) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *Continue) Copy() Object {
return &Continue{}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *Continue) IsFalsy() bool {
return false
}
// Equals returns true if the value of the type
// is equal to the value of another object.
func (o *Continue) Equals(x Object) bool {
return false
}

View File

@ -1,6 +1,7 @@
package objects
import (
"errors"
"fmt"
"strconv"
"time"
@ -158,8 +159,8 @@ func ToTime(o Object) (v time.Time, ok bool) {
return
}
// objectToInterface attempts to convert an object o to an interface{} value
func objectToInterface(o Object) (res interface{}) {
// ToInterface attempts to convert an object o to an interface{} value
func ToInterface(o Object) (res interface{}) {
switch o := o.(type) {
case *Int:
res = o.Value
@ -176,13 +177,29 @@ func objectToInterface(o Object) (res interface{}) {
case *Array:
res = make([]interface{}, len(o.Value))
for i, val := range o.Value {
res.([]interface{})[i] = objectToInterface(val)
res.([]interface{})[i] = ToInterface(val)
}
case *ImmutableArray:
res = make([]interface{}, len(o.Value))
for i, val := range o.Value {
res.([]interface{})[i] = ToInterface(val)
}
case *Map:
res = make(map[string]interface{})
for key, v := range o.Value {
res.(map[string]interface{})[key] = objectToInterface(v)
res.(map[string]interface{})[key] = ToInterface(v)
}
case *ImmutableMap:
res = make(map[string]interface{})
for key, v := range o.Value {
res.(map[string]interface{})[key] = ToInterface(v)
}
case *Time:
res = o.Value
case *Error:
res = errors.New(o.String())
case *Undefined:
res = nil
case Object:
return o
}

31
vendor/github.com/d5/tengo/objects/count_objects.go generated vendored Normal file
View File

@ -0,0 +1,31 @@
package objects
// CountObjects returns the number of objects that a given object o contains.
// For scalar value types, it will always be 1. For compound value types,
// this will include its elements and all of their elements recursively.
func CountObjects(o Object) (c int) {
c = 1
switch o := o.(type) {
case *Array:
for _, v := range o.Value {
c += CountObjects(v)
}
case *ImmutableArray:
for _, v := range o.Value {
c += CountObjects(v)
}
case *Map:
for _, v := range o.Value {
c += CountObjects(v)
}
case *ImmutableMap:
for _, v := range o.Value {
c += CountObjects(v)
}
case *Error:
c += CountObjects(o.Value)
}
return
}

7
vendor/github.com/d5/tengo/objects/importable.go generated vendored Normal file
View File

@ -0,0 +1,7 @@
package objects
// Importable interface represents importable module instance.
type Importable interface {
// Import should return either an Object or module source code ([]byte).
Import(moduleName string) (interface{}, error)
}

77
vendor/github.com/d5/tengo/objects/module_map.go generated vendored Normal file
View File

@ -0,0 +1,77 @@
package objects
// ModuleMap represents a set of named modules.
// Use NewModuleMap to create a new module map.
type ModuleMap struct {
m map[string]Importable
}
// NewModuleMap creates a new module map.
func NewModuleMap() *ModuleMap {
return &ModuleMap{
m: make(map[string]Importable),
}
}
// Add adds an import module.
func (m *ModuleMap) Add(name string, module Importable) {
m.m[name] = module
}
// AddBuiltinModule adds a builtin module.
func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]Object) {
m.m[name] = &BuiltinModule{Attrs: attrs}
}
// AddSourceModule adds a source module.
func (m *ModuleMap) AddSourceModule(name string, src []byte) {
m.m[name] = &SourceModule{Src: src}
}
// Remove removes a named module.
func (m *ModuleMap) Remove(name string) {
delete(m.m, name)
}
// Get returns an import module identified by name.
// It returns if the name is not found.
func (m *ModuleMap) Get(name string) Importable {
return m.m[name]
}
// GetBuiltinModule returns a builtin module identified by name.
// It returns if the name is not found or the module is not a builtin module.
func (m *ModuleMap) GetBuiltinModule(name string) *BuiltinModule {
mod, _ := m.m[name].(*BuiltinModule)
return mod
}
// GetSourceModule returns a source module identified by name.
// It returns if the name is not found or the module is not a source module.
func (m *ModuleMap) GetSourceModule(name string) *SourceModule {
mod, _ := m.m[name].(*SourceModule)
return mod
}
// Copy creates a copy of the module map.
func (m *ModuleMap) Copy() *ModuleMap {
c := &ModuleMap{
m: make(map[string]Importable),
}
for name, mod := range m.m {
c.m[name] = mod
}
return c
}
// Len returns the number of named modules.
func (m *ModuleMap) Len() int {
return len(m.m)
}
// AddMap adds named modules from another module map.
func (m *ModuleMap) AddMap(o *ModuleMap) {
for name, mod := range o.m {
m.m[name] = mod
}
}

41
vendor/github.com/d5/tengo/objects/object_ptr.go generated vendored Normal file
View File

@ -0,0 +1,41 @@
package objects
import (
"github.com/d5/tengo/compiler/token"
)
// ObjectPtr represents a free variable.
type ObjectPtr struct {
Value *Object
}
func (o *ObjectPtr) String() string {
return "free-var"
}
// TypeName returns the name of the type.
func (o *ObjectPtr) TypeName() string {
return "<free-var>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
func (o *ObjectPtr) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *ObjectPtr) Copy() Object {
return o
}
// IsFalsy returns true if the value of the type is falsy.
func (o *ObjectPtr) IsFalsy() bool {
return o.Value == nil
}
// Equals returns true if the value of the type
// is equal to the value of another object.
func (o *ObjectPtr) Equals(x Object) bool {
return o == x
}

View File

@ -1,39 +0,0 @@
package objects
import "github.com/d5/tengo/compiler/token"
// ReturnValue represents a value that is being returned.
type ReturnValue struct {
Value Object
}
// TypeName returns the name of the type.
func (o *ReturnValue) TypeName() string {
return "return-value"
}
func (o *ReturnValue) String() string {
return "<return-value>"
}
// BinaryOp returns another object that is the result of
// a given binary operator and a right-hand side object.
func (o *ReturnValue) BinaryOp(op token.Token, rhs Object) (Object, error) {
return nil, ErrInvalidOperator
}
// Copy returns a copy of the type.
func (o *ReturnValue) Copy() Object {
return &ReturnValue{Value: o.Copy()}
}
// IsFalsy returns true if the value of the type is falsy.
func (o *ReturnValue) IsFalsy() bool {
return false
}
// Equals returns true if the value of the type
// is equal to the value of another object.
func (o *ReturnValue) Equals(x Object) bool {
return false
}

11
vendor/github.com/d5/tengo/objects/source_module.go generated vendored Normal file
View File

@ -0,0 +1,11 @@
package objects
// SourceModule is an importable module that's written in Tengo.
type SourceModule struct {
Src []byte
}
// Import returns a module source code.
func (m *SourceModule) Import(_ string) (interface{}, error) {
return m.Src, nil
}

View File

@ -6,8 +6,9 @@ import (
// UserFunction represents a user function.
type UserFunction struct {
Name string
Value CallableFunc
Name string
Value CallableFunc
EncodingID string
}
// TypeName returns the name of the type.