xgo/stdlib/stdlib.go
Daniel 306055ad65
add helper functions for builtin functions and builtin modules (#122)
* add helper functions for builtin functions and builtin modules

* fix a builtin function bug for modules
2019-02-28 18:41:29 -08:00

37 lines
1.1 KiB
Go

package stdlib
import "github.com/d5/tengo/objects"
// Modules contain the standard modules.
var Modules = map[string]*objects.Object{
"math": objectPtr(&objects.ImmutableMap{Value: mathModule}),
"os": objectPtr(&objects.ImmutableMap{Value: osModule}),
"text": objectPtr(&objects.ImmutableMap{Value: textModule}),
"times": objectPtr(&objects.ImmutableMap{Value: timesModule}),
"rand": objectPtr(&objects.ImmutableMap{Value: randModule}),
}
// AllModuleNames returns a list of all default module names.
func AllModuleNames() []string {
var names []string
for name := range Modules {
names = append(names, name)
}
return names
}
// GetModules returns the modules for the given names.
// Duplicate names and invalid names are ignore.
func GetModules(names ...string) map[string]*objects.ImmutableMap {
modules := make(map[string]*objects.ImmutableMap)
for _, name := range names {
if mod := Modules[name]; mod != nil {
modules[name] = (*mod).(*objects.ImmutableMap)
}
}
return modules
}
func objectPtr(o objects.Object) *objects.Object {
return &o
}