-
Notifications
You must be signed in to change notification settings - Fork 1
/
namespace.go
319 lines (270 loc) · 7.23 KB
/
namespace.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package lang
import (
"errors"
"fmt"
"reflect"
"sync"
"sync/atomic"
"github.com/glojurelang/glojure/pkg/pkgmap"
)
type Namespace struct {
name *Symbol
// atomic references to maps
mappings atomic.Value
aliases atomic.Value
meta IPersistentMap
}
var (
SymbolCoreNamespace = NewSymbol("glojure.core")
namespaces = map[string]*Namespace{}
nsMtx sync.RWMutex
)
func Namespaces() []*Namespace {
nsMtx.RLock()
defer nsMtx.RUnlock()
ns := make([]*Namespace, 0, len(namespaces))
for _, n := range namespaces {
ns = append(ns, n)
}
return ns
}
func FindNamespace(sym *Symbol) *Namespace {
nsMtx.RLock()
defer nsMtx.RUnlock()
return namespaces[sym.String()]
}
func FindOrCreateNamespace(sym *Symbol) *Namespace {
ns := FindNamespace(sym)
if ns != nil {
return ns
}
nsMtx.Lock()
defer nsMtx.Unlock()
ns = namespaces[sym.String()]
if ns != nil {
return ns
}
ns = NewNamespace(sym)
namespaces[sym.String()] = ns
return ns
}
func RemoveNamespace(sym *Symbol) {
if sym.String() == "glojure.core" {
panic(errors.New("cannot remove glojure.core namespace"))
}
nsMtx.Lock()
defer nsMtx.Unlock()
delete(namespaces, sym.String())
}
func NamespaceFor(inns *Namespace, sym *Symbol) *Namespace {
//note, presumes non-nil sym.ns
// first check against currentNS' aliases...
nsSym := NewSymbol(sym.Namespace())
ns := inns.LookupAlias(nsSym)
if ns != nil {
return ns
}
return FindNamespace(nsSym)
}
func NewNamespace(name *Symbol) *Namespace {
ns := &Namespace{
name: name,
}
ns.mappings.Store(NewBox(emptyMap))
ns.aliases.Store(NewBox(emptyMap))
// TODO: add default mappings (see RT.java in clojure)
return ns
}
func (ns *Namespace) String() string {
return ns.Name().String()
}
func (ns *Namespace) Name() *Symbol {
return ns.name
}
func (ns *Namespace) mappingsBox() *Box {
return ns.mappings.Load().(*Box)
}
func (ns *Namespace) Mappings() IPersistentMap {
return ns.mappingsBox().val.(IPersistentMap)
}
func (ns *Namespace) aliasesBox() *Box {
return ns.aliases.Load().(*Box)
}
func (ns *Namespace) Aliases() IPersistentMap {
return ns.aliasesBox().val.(IPersistentMap)
}
func (ns *Namespace) isInternedMapping(sym *Symbol, v interface{}) bool {
vr, ok := v.(*Var)
return ok && vr.Namespace() == ns && Equals(vr.Symbol(), sym)
}
// Intern creates a new Var in this namespace with the given name.
func (ns *Namespace) Intern(sym *Symbol) *Var {
if sym.Namespace() != "" {
panic(fmt.Errorf("can't intern qualified name: %s", sym))
}
mb := ns.mappingsBox()
var v *Var
var o interface{}
for {
o = mb.val.(IPersistentMap).ValAt(sym)
if o != nil {
break
}
if v == nil {
v = NewVar(ns, sym)
}
newMap := mb.val.(IPersistentMap).Assoc(sym, v)
ns.mappings.CompareAndSwap(mb, NewBox(newMap))
mb = ns.mappingsBox()
}
if ns.isInternedMapping(sym, o) {
return o.(*Var)
}
if v == nil {
v = NewVar(ns, sym)
}
if ns.checkReplacement(sym, o, v) {
for !ns.mappings.CompareAndSwap(mb, NewBox(mb.val.(IPersistentMap).Assoc(sym, v))) {
mb = ns.mappingsBox()
}
return v
}
return o.(*Var)
}
func (ns *Namespace) checkReplacement(sym *Symbol, old, neu interface{}) bool {
/*
This method checks if a namespace's mapping is applicable and warns on problematic cases.
It will return a boolean indicating if a mapping is replaceable.
The semantics of what constitutes a legal replacement mapping is summarized as follows:
| classification | in namespace ns | newval = anything other than ns/name | newval = ns/name |
|----------------+------------------------+--------------------------------------+-------------------------------------|
| native mapping | name -> ns/name | no replace, warn-if newval not-core | no replace, warn-if newval not-core |
| alias mapping | name -> other/whatever | warn + replace | warn + replace |
*/
errOut := GlobalEnv.Stderr()
if _, ok := old.(*Var); ok {
var nns *Namespace
if neuVar, ok := neu.(*Var); ok {
nns = neuVar.Namespace()
}
if ns.isInternedMapping(sym, old) {
if nns != FindNamespace(SymbolCoreNamespace) {
fmt.Fprintf(errOut, "REJECTED: attempt to replace interned var %s with %s in %s, you must ns-unmap first\n", old, neu, ns.name)
}
return false
}
}
fmt.Fprintf(errOut, "WARNING: %s already refers to %s in namespace: %s, being replaced by: %s\n", sym, old, ns.name, neu)
return true
}
func (ns *Namespace) InternWithValue(sym *Symbol, value interface{}, replaceRoot bool) *Var {
v := ns.Intern(sym)
if !v.HasRoot() || replaceRoot {
v.BindRoot(value)
}
return v
}
func (ns *Namespace) GetMapping(sym *Symbol) interface{} {
m := ns.Mappings()
return m.ValAt(sym)
}
func (ns *Namespace) FindInternedVar(sym *Symbol) *Var {
m := ns.Mappings()
v := m.ValAt(sym)
if v == nil {
return nil
}
vr, ok := v.(*Var)
if !ok {
return nil
}
if vr.Namespace() != ns {
return nil
}
return vr
}
func (ns *Namespace) LookupAlias(sym *Symbol) *Namespace {
m := ns.Aliases()
v := m.ValAt(sym)
if v == nil {
return nil
}
return v.(*Namespace)
}
func (ns *Namespace) AddAlias(alias *Symbol, ns2 *Namespace) {
if alias == nil || ns2 == nil {
panic(fmt.Errorf("add-alias: expecting symbol (%v) + namespace (%v)", alias, ns2))
}
ab := ns.aliasesBox()
for !ab.val.(IPersistentMap).ContainsKey(alias) {
newAliases := ab.val.(IPersistentMap).Assoc(alias, ns2)
ns.aliases.CompareAndSwap(ab, NewBox(newAliases))
ab = ns.aliasesBox()
}
if v := ab.val.(IPersistentMap).ValAt(alias); v != ns2 {
panic(fmt.Errorf("add-alias: alias %s already refers to %s", alias, v))
}
}
// Import references an export from a Go package.
func (ns *Namespace) Import(export string, v interface{}) interface{} {
_, name := pkgmap.SplitExport(export)
ns.reference(NewSymbol(name), v)
return v
}
// Refer adds a reference to an existing Var, possibly in another
// namespace, to this namespace.
func (ns *Namespace) Refer(sym *Symbol, v *Var) *Var {
return ns.reference(sym, v).(*Var)
}
func (ns *Namespace) reference(sym *Symbol, v interface{}) interface{} {
if sym.Namespace() != "" {
panic(fmt.Errorf("can't intern qualified name: %s", sym))
}
if v == nil {
panic(fmt.Errorf("can't refer to nil (%s)", sym))
}
mb := ns.mappingsBox()
var o interface{}
for {
o = mb.val.(IPersistentMap).ValAt(sym)
if o != nil {
break
}
newMap := mb.val.(IPersistentMap).Assoc(sym, v)
ns.mappings.CompareAndSwap(mb, NewBox(newMap))
mb = ns.mappingsBox()
}
if ns.isInternedMapping(sym, o) {
return o.(*Var)
}
// NB: in Go, some types are not comparable.
oCmp := reflect.TypeOf(o).Comparable()
vCmp := reflect.TypeOf(v).Comparable()
if oCmp && vCmp {
if o == v {
return o
}
} else if oCmp == vCmp {
// TODO: what to do here? for now, assume equal
return o
}
if ns.checkReplacement(sym, o, v) {
for !ns.mappings.CompareAndSwap(mb, NewBox(mb.val.(IPersistentMap).Assoc(sym, v))) {
mb = ns.mappingsBox()
}
return v
}
return o
}
func (ns *Namespace) Meta() IPersistentMap {
return ns.meta
}
func (ns *Namespace) AlterMeta(alter IFn, args ISeq) IPersistentMap {
meta := alter.ApplyTo(NewCons(ns.Meta(), args)).(IPersistentMap)
ns.ResetMeta(meta)
return meta
}
func (ns *Namespace) ResetMeta(meta IPersistentMap) IPersistentMap {
ns.meta = meta
return meta
}