forked from robertkrimen/otto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
188 lines (154 loc) · 4.64 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
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
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) 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) 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(value Value) (descriptor _property) {
objectDescriptor := value._object()
if objectDescriptor == nil {
panic(newTypeError())
}
{
descriptor.mode = modeSetMask // Initially nothing is set
if objectDescriptor.hasProperty("enumerable") {
if objectDescriptor.get("enumerable").toBoolean() {
descriptor.enumerateOn()
} else {
descriptor.enumerateOff()
}
}
if objectDescriptor.hasProperty("configurable") {
if objectDescriptor.get("configurable").toBoolean() {
descriptor.configureOn()
} else {
descriptor.configureOff()
}
}
if objectDescriptor.hasProperty("writable") {
if objectDescriptor.get("writable").toBoolean() {
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(newTypeError())
}
getter = value._object()
getterSetter = getterSetter || getter != nil
}
}
if objectDescriptor.hasProperty("set") {
value := objectDescriptor.get("set")
if value.IsDefined() {
if !value.isCallable() {
panic(newTypeError())
}
setter = value._object()
getterSetter = getterSetter || setter != nil
}
}
if getterSetter {
if descriptor.writeSet() {
panic(newTypeError())
}
descriptor.value = _propertyGetSet{getter, setter}
}
if objectDescriptor.hasProperty("value") {
if getterSetter {
panic(newTypeError())
}
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)
object.defineProperty("get", toValue_object(getSet[0]), 0111, false)
object.defineProperty("set", toValue_object(getSet[1]), 0111, false)
}
object.defineProperty("enumerable", toValue_bool(descriptor.enumerable()), 0111, false)
object.defineProperty("configurable", toValue_bool(descriptor.configurable()), 0111, false)
return object
}