forked from risor-io/risor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.go
157 lines (147 loc) · 4.33 KB
/
cfg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package risor
import (
"sort"
"github.com/risor-io/risor/builtins"
"github.com/risor-io/risor/compiler"
"github.com/risor-io/risor/importer"
modBase64 "github.com/risor-io/risor/modules/base64"
modBytes "github.com/risor-io/risor/modules/bytes"
modDns "github.com/risor-io/risor/modules/dns"
modExec "github.com/risor-io/risor/modules/exec"
modFilepath "github.com/risor-io/risor/modules/filepath"
modFmt "github.com/risor-io/risor/modules/fmt"
modHTTP "github.com/risor-io/risor/modules/http"
modJSON "github.com/risor-io/risor/modules/json"
modMath "github.com/risor-io/risor/modules/math"
modOs "github.com/risor-io/risor/modules/os"
modRand "github.com/risor-io/risor/modules/rand"
modRegexp "github.com/risor-io/risor/modules/regexp"
modStrconv "github.com/risor-io/risor/modules/strconv"
modStrings "github.com/risor-io/risor/modules/strings"
modTime "github.com/risor-io/risor/modules/time"
modYAML "github.com/risor-io/risor/modules/yaml"
"github.com/risor-io/risor/object"
"github.com/risor-io/risor/vm"
)
// Config assists in configuring a Risor evaluation.
type Config struct {
Globals map[string]any
DefaultGlobals map[string]object.Object
Importer importer.Importer
LocalImportPath string
WithoutDefaultGlobals bool
WithConcurrency bool
}
func NewConfig() *Config {
cfg := &Config{
Globals: map[string]any{},
DefaultGlobals: map[string]object.Object{},
}
cfg.addDefaultGlobals()
return cfg
}
// CombinedGlobals returns a map of all global variables that should be
// available in a Risor evaluation.
func (cfg *Config) CombinedGlobals() map[string]any {
combined := map[string]any{}
for k, v := range cfg.DefaultGlobals {
combined[k] = v
}
for k, v := range cfg.Globals {
combined[k] = v
}
return combined
}
// GlobalNames returns a list of all global variables names that should be
// available in a Risor evaluation.
func (cfg *Config) GlobalNames() []string {
nameMap := map[string]bool{}
for k := range cfg.DefaultGlobals {
nameMap[k] = true
}
for k := range cfg.Globals {
nameMap[k] = true
}
var names []string
for name := range nameMap {
names = append(names, name)
}
sort.Strings(names)
return names
}
func (cfg *Config) addDefaultGlobals() {
addGlobals := func(globals map[string]object.Object) {
for k, v := range globals {
cfg.DefaultGlobals[k] = v
}
}
// Add default builtin functions
builtins := []map[string]object.Object{
builtins.Builtins(),
modHTTP.Builtins(),
modFmt.Builtins(),
modOs.Builtins(),
modDns.Builtins(),
}
for _, b := range builtins {
addGlobals(b)
}
// Add default modules
modules := map[string]object.Object{
"base64": modBase64.Module(),
"bytes": modBytes.Module(),
"exec": modExec.Module(),
"filepath": modFilepath.Module(),
"fmt": modFmt.Module(),
"http": modHTTP.Module(),
"json": modJSON.Module(),
"math": modMath.Module(),
"os": modOs.Module(),
"rand": modRand.Module(),
"regexp": modRegexp.Module(),
"strconv": modStrconv.Module(),
"strings": modStrings.Module(),
"time": modTime.Module(),
"yaml": modYAML.Module(),
}
addGlobals(modules)
}
// CompilerOpts returns compiler options derived from this configuration.
func (cfg *Config) CompilerOpts() []compiler.Option {
globalNames := cfg.GlobalNames()
var opts []compiler.Option
if len(globalNames) > 0 {
opts = append(opts, compiler.WithGlobalNames(globalNames))
}
return opts
}
// VMOpts returns virtual machine options derived from this configuration.
func (cfg *Config) VMOpts() []vm.Option {
var opts []vm.Option
combinedGlobals := cfg.CombinedGlobals()
if len(combinedGlobals) > 0 {
opts = append(opts, vm.WithGlobals(combinedGlobals))
}
importer := cfg.Importer
if importer == nil && cfg.LocalImportPath != "" {
var names []string
for name := range combinedGlobals {
names = append(names, name)
}
importer = newLocalImporter(names, cfg.LocalImportPath)
}
if importer != nil {
opts = append(opts, vm.WithImporter(importer))
}
if cfg.WithConcurrency {
opts = append(opts, vm.WithConcurrency())
}
return opts
}
func newLocalImporter(globalNames []string, sourceDir string) importer.Importer {
return importer.NewLocalImporter(importer.LocalImporterOptions{
GlobalNames: globalNames,
SourceDir: sourceDir,
Extensions: []string{".risor", ".rsr"},
})
}