-
Notifications
You must be signed in to change notification settings - Fork 25
/
motto.go
174 lines (139 loc) · 3.55 KB
/
motto.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Copyright 2014 dong<[email protected]>.
// Licensed under the MIT license.
//
// Motto - Modular Javascript environment.
package motto
import (
"path/filepath"
"sync"
"github.com/robertkrimen/otto"
)
// Globally registered modules
var globalModules map[string]ModuleLoader = make(map[string]ModuleLoader)
// Globally registered paths (paths to search for modules)
var globalPaths []string
// Globally protects global state
var globalMu sync.RWMutex
// Motto is modular vm environment
type Motto struct {
// Motto is based on otto
*otto.Otto
// try to read source map
SourceMapEnabled bool
// Modules that registered for current vm
modules map[string]ModuleLoader
modulesMu sync.RWMutex
// Location to search for modules
paths []string
pathsMu sync.RWMutex
// Onece a module is required by vm, the exported value is cached for further
// use.
moduleCache map[string]otto.Value
moduleCacheMu sync.RWMutex
}
// Run a module or file
func (m *Motto) Run(name string) (otto.Value, error) {
if ok, _ := isFile(name); ok {
name, _ = filepath.Abs(name)
}
return m.Require(name, ".")
}
// Require a module with cache
func (m *Motto) Require(id, pwd string) (otto.Value, error) {
if cache, ok := m.cachedModule(id); ok {
return cache, nil
}
loader := m.module(id)
if loader == nil {
loader = Module(id)
}
if loader != nil {
v, err := loader(m)
if err != nil {
return otto.UndefinedValue(), err
}
m.addCachedModule(id, v)
return v, nil
}
filename, err := FindFileModule(id, pwd, append(m.paths, globalPaths...))
if err != nil {
return otto.UndefinedValue(), err
}
// resove id
id = filename
if cache, ok := m.cachedModule(id); ok {
return cache, nil
}
v, err := CreateLoaderFromFile(id)(m)
if err != nil {
return otto.UndefinedValue(), err
}
m.addCachedModule(id, v)
return v, nil
}
func (m *Motto) addCachedModule(id string, v otto.Value) {
m.moduleCacheMu.Lock()
m.moduleCache[id] = v
m.moduleCacheMu.Unlock()
}
func (m *Motto) cachedModule(id string) (otto.Value, bool) {
m.moduleCacheMu.RLock()
defer m.moduleCacheMu.RUnlock()
v, ok := m.moduleCache[id]
return v, ok
}
// ClearModule clear all registered module from current vm
func (m *Motto) ClearModule() {
m.moduleCacheMu.Lock()
m.moduleCache = make(map[string]otto.Value)
m.moduleCacheMu.Unlock()
}
// AddModule registers a new module to current vm.
func (m *Motto) AddModule(id string, l ModuleLoader) {
m.modulesMu.Lock()
m.modules[id] = l
m.modulesMu.Unlock()
}
func (m *Motto) module(id string) ModuleLoader {
m.modulesMu.RLock()
defer m.modulesMu.RUnlock()
return m.modules[id]
}
// AddPath adds paths to search for modules.
func (m *Motto) AddPath(paths ...string) {
m.pathsMu.Lock()
m.paths = append(m.paths, paths...)
m.pathsMu.Unlock()
}
// AddModule registers global module
func AddModule(id string, m ModuleLoader) {
globalMu.Lock()
globalModules[id] = m
globalMu.Unlock()
}
// Module returns ModuleLoader for a given ID.
func Module(id string) ModuleLoader {
globalMu.RLock()
defer globalMu.RUnlock()
return globalModules[id]
}
// AddPath registers global path.
func AddPath(paths ...string) {
globalMu.Lock()
globalPaths = append(globalPaths, paths...)
globalMu.Unlock()
}
// Run module by name in the motto module environment.
func Run(name string) (*Motto, otto.Value, error) {
vm := New()
v, err := vm.Run(name)
return vm, v, err
}
// New creates a new motto vm instance.
func New() *Motto {
return &Motto{
Otto: otto.New(),
modules: make(map[string]ModuleLoader),
moduleCache: make(map[string]otto.Value),
}
}