OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 "use strict"; | 28 "use strict"; |
29 | 29 |
30 var InternalObjectIsFrozen = $Object.isFrozen; | |
31 var InternalObjectFreeze = $Object.freeze; | |
32 | |
33 var observationState = %GetObservationState(); | 30 var observationState = %GetObservationState(); |
34 if (IS_UNDEFINED(observationState.observerInfoMap)) { | 31 if (IS_UNDEFINED(observationState.observerInfoMap)) { |
35 observationState.observerInfoMap = %CreateObjectHashTable(); | 32 observationState.observerInfoMap = %CreateObjectHashTable(); |
36 observationState.objectInfoMap = %CreateObjectHashTable(); | 33 observationState.objectInfoMap = %CreateObjectHashTable(); |
37 observationState.notifierTargetMap = %CreateObjectHashTable(); | 34 observationState.notifierTargetMap = %CreateObjectHashTable(); |
38 observationState.pendingObservers = new InternalArray; | 35 observationState.pendingObservers = new InternalArray; |
39 observationState.observerPriority = 0; | 36 observationState.observerPriority = 0; |
40 } | 37 } |
41 | 38 |
42 function InternalObjectHashTable(tableName) { | 39 function InternalObjectHashTable(tableName) { |
(...skipping 24 matching lines...) Expand all Loading... |
67 }; | 64 }; |
68 objectInfoMap.set(object, info); | 65 objectInfoMap.set(object, info); |
69 return info; | 66 return info; |
70 } | 67 } |
71 | 68 |
72 function ObjectObserve(object, callback) { | 69 function ObjectObserve(object, callback) { |
73 if (!IS_SPEC_OBJECT(object)) | 70 if (!IS_SPEC_OBJECT(object)) |
74 throw MakeTypeError("observe_non_object", ["observe"]); | 71 throw MakeTypeError("observe_non_object", ["observe"]); |
75 if (!IS_SPEC_FUNCTION(callback)) | 72 if (!IS_SPEC_FUNCTION(callback)) |
76 throw MakeTypeError("observe_non_function", ["observe"]); | 73 throw MakeTypeError("observe_non_function", ["observe"]); |
77 if (InternalObjectIsFrozen(callback)) | 74 if (ObjectIsFrozen(callback)) |
78 throw MakeTypeError("observe_callback_frozen"); | 75 throw MakeTypeError("observe_callback_frozen"); |
79 | 76 |
80 if (!observerInfoMap.has(callback)) { | 77 if (!observerInfoMap.has(callback)) { |
81 observerInfoMap.set(callback, { | 78 observerInfoMap.set(callback, { |
82 pendingChangeRecords: null, | 79 pendingChangeRecords: null, |
83 priority: observationState.observerPriority++, | 80 priority: observationState.observerPriority++, |
84 }); | 81 }); |
85 } | 82 } |
86 | 83 |
87 var objectInfo = objectInfoMap.get(object); | 84 var objectInfo = objectInfoMap.get(object); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 observerInfo.pendingChangeRecords.push(changeRecord); | 124 observerInfo.pendingChangeRecords.push(changeRecord); |
128 } | 125 } |
129 } | 126 } |
130 } | 127 } |
131 | 128 |
132 function NotifyChange(type, object, name, oldValue) { | 129 function NotifyChange(type, object, name, oldValue) { |
133 var objectInfo = objectInfoMap.get(object); | 130 var objectInfo = objectInfoMap.get(object); |
134 var changeRecord = (arguments.length < 4) ? | 131 var changeRecord = (arguments.length < 4) ? |
135 { type: type, object: object, name: name } : | 132 { type: type, object: object, name: name } : |
136 { type: type, object: object, name: name, oldValue: oldValue }; | 133 { type: type, object: object, name: name, oldValue: oldValue }; |
137 InternalObjectFreeze(changeRecord); | 134 ObjectFreeze(changeRecord); |
138 EnqueueChangeRecord(changeRecord, objectInfo.changeObservers); | 135 EnqueueChangeRecord(changeRecord, objectInfo.changeObservers); |
139 } | 136 } |
140 | 137 |
141 var notifierPrototype = {}; | 138 var notifierPrototype = {}; |
142 | 139 |
143 function ObjectNotifierNotify(changeRecord) { | 140 function ObjectNotifierNotify(changeRecord) { |
144 if (!IS_SPEC_OBJECT(this)) | 141 if (!IS_SPEC_OBJECT(this)) |
145 throw MakeTypeError("called_on_non_object", ["notify"]); | 142 throw MakeTypeError("called_on_non_object", ["notify"]); |
146 | 143 |
147 var target = notifierTargetMap.get(this); | 144 var target = notifierTargetMap.get(this); |
148 if (IS_UNDEFINED(target)) | 145 if (IS_UNDEFINED(target)) |
149 throw MakeTypeError("observe_notify_non_notifier"); | 146 throw MakeTypeError("observe_notify_non_notifier"); |
150 | 147 |
151 if (!IS_STRING(changeRecord.type)) | 148 if (!IS_STRING(changeRecord.type)) |
152 throw MakeTypeError("observe_type_non_string"); | 149 throw MakeTypeError("observe_type_non_string"); |
153 | 150 |
154 var objectInfo = objectInfoMap.get(target); | 151 var objectInfo = objectInfoMap.get(target); |
155 if (IS_UNDEFINED(objectInfo)) | 152 if (IS_UNDEFINED(objectInfo)) |
156 return; | 153 return; |
157 | 154 |
158 if (!objectInfo.changeObservers.length) | 155 if (!objectInfo.changeObservers.length) |
159 return; | 156 return; |
160 | 157 |
161 var newRecord = { | 158 var newRecord = { |
162 object: target | 159 object: target |
163 }; | 160 }; |
164 for (var prop in changeRecord) { | 161 for (var prop in changeRecord) { |
165 if (prop === 'object') | 162 if (prop === 'object') |
166 continue; | 163 continue; |
167 newRecord[prop] = changeRecord[prop]; | 164 |
| 165 %DefineOrRedefineDataProperty(newRecord, prop, changeRecord[prop], |
| 166 READ_ONLY + DONT_DELETE); |
168 } | 167 } |
169 InternalObjectFreeze(newRecord); | 168 ObjectFreeze(newRecord); |
170 | 169 |
171 EnqueueChangeRecord(newRecord, objectInfo.changeObservers); | 170 EnqueueChangeRecord(newRecord, objectInfo.changeObservers); |
172 } | 171 } |
173 | 172 |
174 function ObjectGetNotifier(object) { | 173 function ObjectGetNotifier(object) { |
175 if (!IS_SPEC_OBJECT(object)) | 174 if (!IS_SPEC_OBJECT(object)) |
176 throw MakeTypeError("observe_non_object", ["getNotifier"]); | 175 throw MakeTypeError("observe_non_object", ["getNotifier"]); |
177 | 176 |
178 if (InternalObjectIsFrozen(object)) | 177 if (ObjectIsFrozen(object)) |
179 return null; | 178 return null; |
180 | 179 |
181 var objectInfo = objectInfoMap.get(object); | 180 var objectInfo = objectInfoMap.get(object); |
182 if (IS_UNDEFINED(objectInfo)) | 181 if (IS_UNDEFINED(objectInfo)) |
183 objectInfo = CreateObjectInfo(object); | 182 objectInfo = CreateObjectInfo(object); |
184 | 183 |
185 if (IS_NULL(objectInfo.notifier)) { | 184 if (IS_NULL(objectInfo.notifier)) { |
186 objectInfo.notifier = { | 185 objectInfo.notifier = { |
187 __proto__: notifierPrototype | 186 __proto__: notifierPrototype |
188 }; | 187 }; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 "getNotifier", ObjectGetNotifier, | 233 "getNotifier", ObjectGetNotifier, |
235 "observe", ObjectObserve, | 234 "observe", ObjectObserve, |
236 "unobserve", ObjectUnobserve | 235 "unobserve", ObjectUnobserve |
237 )); | 236 )); |
238 InstallFunctions(notifierPrototype, DONT_ENUM, $Array( | 237 InstallFunctions(notifierPrototype, DONT_ENUM, $Array( |
239 "notify", ObjectNotifierNotify | 238 "notify", ObjectNotifierNotify |
240 )); | 239 )); |
241 } | 240 } |
242 | 241 |
243 SetupObjectObserve(); | 242 SetupObjectObserve(); |
OLD | NEW |