This repository has been archived by the owner on Apr 15, 2024. It is now read-only.
forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
220 lines (183 loc) · 5.25 KB
/
property.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
package otto
// property
type _propertyMode int
const (
modeWriteMask _propertyMode = 0700
modeEnumerateMask = 0070
modeConfigureMask = 0007
modeOnMask = 0111
modeOffMask = 0000
modeSetMask = 0222 // If value is 2, then mode is neither "On" nor "Off"
)
type _propertyGetSet [2]*_object
var _nilGetSetObject _object = _object{}
type _property struct {
value interface{}
mode _propertyMode
}
func (self _property) writable() bool {
return self.mode&modeWriteMask == modeWriteMask&modeOnMask
}
func (self *_property) writeOn() {
self.mode = (self.mode & ^modeWriteMask) | (modeWriteMask & modeOnMask)
}
func (self *_property) writeOff() {
self.mode &= ^modeWriteMask
}
func (self *_property) writeClear() {
self.mode = (self.mode & ^modeWriteMask) | (modeWriteMask & modeSetMask)
}
func (self _property) writeSet() bool {
return 0 == self.mode&modeWriteMask&modeSetMask
}
func (self _property) enumerable() bool {
return self.mode&modeEnumerateMask == modeEnumerateMask&modeOnMask
}
func (self *_property) enumerateOn() {
self.mode = (self.mode & ^modeEnumerateMask) | (modeEnumerateMask & modeOnMask)
}
func (self *_property) enumerateOff() {
self.mode &= ^modeEnumerateMask
}
func (self _property) enumerateSet() bool {
return 0 == self.mode&modeEnumerateMask&modeSetMask
}
func (self _property) configurable() bool {
return self.mode&modeConfigureMask == modeConfigureMask&modeOnMask
}
func (self *_property) configureOn() {
self.mode = (self.mode & ^modeConfigureMask) | (modeConfigureMask & modeOnMask)
}
func (self *_property) configureOff() {
self.mode &= ^modeConfigureMask
}
func (self _property) configureSet() bool {
return 0 == self.mode&modeConfigureMask&modeSetMask
}
func (self _property) copy() *_property {
property := self
return &property
}
func (self _property) get(this *_object) Value {
switch value := self.value.(type) {
case Value:
return value
case _propertyGetSet:
if value[0] != nil {
return value[0].call(toValue(this), nil, false, nativeFrame)
}
}
return Value{}
}
func (self _property) isAccessorDescriptor() bool {
setGet, test := self.value.(_propertyGetSet)
return test && (setGet[0] != nil || setGet[1] != nil)
}
func (self _property) isDataDescriptor() bool {
if self.writeSet() { // Either "On" or "Off"
return true
}
value, valid := self.value.(Value)
return valid && !value.isEmpty()
}
func (self _property) isGenericDescriptor() bool {
return !(self.isDataDescriptor() || self.isAccessorDescriptor())
}
func (self _property) isEmpty() bool {
return self.mode == 0222 && self.isGenericDescriptor()
}
// _enumerableValue, _enumerableTrue, _enumerableFalse?
// .enumerableValue() .enumerableExists()
func toPropertyDescriptor(rt *_runtime, value Value) (descriptor _property) {
objectDescriptor := value._object()
if objectDescriptor == nil {
panic(rt.panicTypeError())
}
{
descriptor.mode = modeSetMask // Initially nothing is set
if objectDescriptor.hasProperty("enumerable") {
if objectDescriptor.get("enumerable").bool() {
descriptor.enumerateOn()
} else {
descriptor.enumerateOff()
}
}
if objectDescriptor.hasProperty("configurable") {
if objectDescriptor.get("configurable").bool() {
descriptor.configureOn()
} else {
descriptor.configureOff()
}
}
if objectDescriptor.hasProperty("writable") {
if objectDescriptor.get("writable").bool() {
descriptor.writeOn()
} else {
descriptor.writeOff()
}
}
}
var getter, setter *_object
getterSetter := false
if objectDescriptor.hasProperty("get") {
value := objectDescriptor.get("get")
if value.IsDefined() {
if !value.isCallable() {
panic(rt.panicTypeError())
}
getter = value._object()
getterSetter = true
} else {
getter = &_nilGetSetObject
getterSetter = true
}
}
if objectDescriptor.hasProperty("set") {
value := objectDescriptor.get("set")
if value.IsDefined() {
if !value.isCallable() {
panic(rt.panicTypeError())
}
setter = value._object()
getterSetter = true
} else {
setter = &_nilGetSetObject
getterSetter = true
}
}
if getterSetter {
if descriptor.writeSet() {
panic(rt.panicTypeError())
}
descriptor.value = _propertyGetSet{getter, setter}
}
if objectDescriptor.hasProperty("value") {
if getterSetter {
panic(rt.panicTypeError())
}
descriptor.value = objectDescriptor.get("value")
}
return
}
func (self *_runtime) fromPropertyDescriptor(descriptor _property) *_object {
object := self.newObject()
if descriptor.isDataDescriptor() {
object.defineProperty("value", descriptor.value.(Value), 0111, false)
object.defineProperty("writable", toValue_bool(descriptor.writable()), 0111, false)
} else if descriptor.isAccessorDescriptor() {
getSet := descriptor.value.(_propertyGetSet)
get := Value{}
if getSet[0] != nil {
get = toValue_object(getSet[0])
}
set := Value{}
if getSet[1] != nil {
set = toValue_object(getSet[1])
}
object.defineProperty("get", get, 0111, false)
object.defineProperty("set", set, 0111, false)
}
object.defineProperty("enumerable", toValue_bool(descriptor.enumerable()), 0111, false)
object.defineProperty("configurable", toValue_bool(descriptor.configurable()), 0111, false)
return object
}