-
Notifications
You must be signed in to change notification settings - Fork 1
/
modules_namespace.go
181 lines (164 loc) · 4.42 KB
/
modules_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
package sobek
import (
"sort"
"github.com/grafana/sobek/unistring"
)
type namespaceObject struct {
baseObject
m ModuleRecord
exports map[unistring.String]struct{}
exportsNames []unistring.String
}
func (r *Runtime) NamespaceObjectFor(m ModuleRecord) *Object {
if r.moduleNamespaces == nil {
r.moduleNamespaces = make(map[ModuleRecord]*namespaceObject)
}
if o, ok := r.moduleNamespaces[m]; ok {
return o.val
}
o := r.createNamespaceObject(m)
r.moduleNamespaces[m] = o
return o.val
}
func (r *Runtime) createNamespaceObject(m ModuleRecord) *namespaceObject {
o := &Object{runtime: r}
no := &namespaceObject{m: m}
no.val = o
no.extensible = true
no.defineOwnPropertySym(SymToStringTag, PropertyDescriptor{
Value: newStringValue("Module"),
}, true)
no.extensible = false
o.self = no
no.init()
no.exports = make(map[unistring.String]struct{})
m.GetExportedNames(func(names []string) {
for _, exportName := range names {
v, ambiguous := no.m.ResolveExport(exportName)
if ambiguous || v == nil {
continue
}
no.exports[unistring.NewFromString(exportName)] = struct{}{}
no.exportsNames = append(no.exportsNames, unistring.NewFromString(exportName))
}
})
return no
}
func (no *namespaceObject) stringKeys(all bool, accum []Value) []Value {
for name := range no.exports {
if !all { // TODO this seems off
_ = no.getOwnPropStr(name)
}
accum = append(accum, stringValueFromRaw(name))
}
// TODO optimize this
sort.Slice(accum, func(i, j int) bool {
return accum[i].String() < accum[j].String()
})
return accum
}
type namespacePropIter struct {
no *namespaceObject
idx int
}
func (no *namespaceObject) iterateStringKeys() iterNextFunc {
return (&namespacePropIter{
no: no,
}).next
}
func (no *namespaceObject) iterateKeys() iterNextFunc {
return no.iterateStringKeys()
}
func (i *namespacePropIter) next() (propIterItem, iterNextFunc) {
for i.idx < len(i.no.exportsNames) {
name := i.no.exportsNames[i.idx]
i.idx++
prop := i.no.getOwnPropStr(name)
if prop != nil {
return propIterItem{name: stringValueFromRaw(name), value: prop}, i.next
}
}
return propIterItem{}, nil
}
func (no *namespaceObject) getOwnPropStr(name unistring.String) Value {
if _, ok := no.exports[name]; !ok {
return nil
}
v, ambiguous := no.m.ResolveExport(name.String())
if ambiguous || v == nil {
no.val.runtime.throwReferenceError((name))
}
if v.BindingName == "*namespace*" {
return &valueProperty{
value: no.val.runtime.NamespaceObjectFor(v.Module),
writable: true,
configurable: false,
enumerable: true,
}
}
mi := no.val.runtime.modules[v.Module]
b := mi.GetBindingValue(v.BindingName)
if b == nil {
// TODO figure this out - this is needed as otherwise valueproperty is thought to not have a value
// which isn't really correct in a particular test around isFrozen
b = _null
}
return &valueProperty{
value: b,
writable: true,
configurable: false,
enumerable: true,
}
}
func (no *namespaceObject) hasOwnPropertyStr(name unistring.String) bool {
_, ok := no.exports[name]
return ok
}
func (no *namespaceObject) getStr(name unistring.String, receiver Value) Value {
prop := no.getOwnPropStr(name)
if prop, ok := prop.(*valueProperty); ok {
if receiver == nil {
return prop.get(no.val)
}
return prop.get(receiver)
}
return prop
}
func (no *namespaceObject) setOwnStr(name unistring.String, val Value, throw bool) bool {
no.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
return false
}
func (no *namespaceObject) deleteStr(name unistring.String, throw bool) bool {
if _, exists := no.exports[name]; exists {
no.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
return false
}
return true
}
func (no *namespaceObject) defineOwnPropertyStr(name unistring.String, desc PropertyDescriptor, throw bool) bool {
returnFalse := func() bool {
if throw {
no.val.runtime.typeErrorResult(throw, "Cannot add property %s, object is not extensible", name)
}
return false
}
if !no.hasOwnPropertyStr(name) {
return returnFalse()
}
if desc.Empty() {
return true
}
if desc.Writable == FLAG_FALSE {
return returnFalse()
}
if desc.Configurable == FLAG_TRUE {
return returnFalse()
}
if desc.Enumerable == FLAG_FALSE {
return returnFalse()
}
if desc.Value != nil && desc.Value != no.getOwnPropStr(name) {
return returnFalse()
}
return true
}