diff options
Diffstat (limited to 'vendor/github.com/d5/tengo/objects/builtins.go')
-rw-r--r-- | vendor/github.com/d5/tengo/objects/builtins.go | 167 |
1 files changed, 98 insertions, 69 deletions
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. -var Builtins = []NamedBuiltinFunc{ +// Use GetBuiltinFunctions instead of accessing Builtins directly. +var Builtins = []BuiltinFunction{ { - Name: "print", - Func: builtinPrint, + Name: "print", + Value: builtinPrint, }, { - Name: "printf", - Func: builtinPrintf, + Name: "printf", + Value: builtinPrintf, }, { - Name: "sprintf", - Func: builtinSprintf, + Name: "sprintf", + Value: builtinSprintf, }, { - Name: "len", - Func: builtinLen, + Name: "len", + Value: builtinLen, }, { - Name: "copy", - Func: builtinCopy, + Name: "copy", + Value: builtinCopy, }, { - Name: "append", - Func: builtinAppend, + Name: "append", + Value: builtinAppend, }, { - Name: "string", - Func: builtinString, + Name: "string", + Value: builtinString, }, { - Name: "int", - Func: builtinInt, + Name: "int", + Value: builtinInt, }, { - Name: "bool", - Func: builtinBool, + Name: "bool", + Value: builtinBool, }, { - Name: "float", - Func: builtinFloat, + Name: "float", + Value: builtinFloat, }, { - Name: "char", - Func: builtinChar, + Name: "char", + Value: builtinChar, }, { - Name: "bytes", - Func: builtinBytes, + Name: "bytes", + Value: builtinBytes, }, { - Name: "time", - Func: builtinTime, + Name: "time", + Value: builtinTime, }, { - Name: "is_int", - Func: builtinIsInt, + Name: "is_int", + Value: builtinIsInt, }, { - Name: "is_float", - Func: builtinIsFloat, + Name: "is_float", + Value: builtinIsFloat, }, { - Name: "is_string", - Func: builtinIsString, + Name: "is_string", + Value: builtinIsString, }, { - Name: "is_bool", - Func: builtinIsBool, + Name: "is_bool", + Value: builtinIsBool, }, { - Name: "is_char", - Func: builtinIsChar, + Name: "is_char", + Value: builtinIsChar, }, { - Name: "is_bytes", - Func: builtinIsBytes, + Name: "is_bytes", + Value: builtinIsBytes, }, { - Name: "is_array", - Func: builtinIsArray, + Name: "is_array", + Value: builtinIsArray, }, { - Name: "is_immutable_array", - Func: builtinIsImmutableArray, + Name: "is_immutable_array", + Value: builtinIsImmutableArray, }, { - Name: "is_map", - Func: builtinIsMap, + Name: "is_map", + Value: builtinIsMap, }, { - Name: "is_immutable_map", - Func: builtinIsImmutableMap, + Name: "is_immutable_map", + Value: builtinIsImmutableMap, }, { - Name: "is_time", - Func: builtinIsTime, + Name: "is_time", + Value: builtinIsTime, }, { - Name: "is_error", - Func: builtinIsError, + Name: "is_error", + Value: builtinIsError, }, { - Name: "is_undefined", - Func: builtinIsUndefined, + Name: "is_undefined", + Value: builtinIsUndefined, }, { - Name: "is_function", - Func: builtinIsFunction, + Name: "is_function", + Value: builtinIsFunction, }, { - Name: "is_callable", - Func: builtinIsCallable, + Name: "is_callable", + Value: builtinIsCallable, }, { - Name: "to_json", - Func: builtinToJSON, + Name: "to_json", + Value: builtinToJSON, }, { - Name: "from_json", - Func: builtinFromJSON, + Name: "from_json", + Value: builtinFromJSON, }, { - Name: "type_name", - Func: builtinTypeName, + 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()...) +} |