danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 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. |
| 27 | |
| 28 | "use strict"; |
| 29 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 30 | var observationState = %GetObservationState(); |
| 31 | if (IS_UNDEFINED(observationState.observerInfoMap)) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 32 | observationState.observerInfoMap = %ObservationWeakMapCreate(); |
| 33 | observationState.objectInfoMap = %ObservationWeakMapCreate(); |
| 34 | observationState.notifierTargetMap = %ObservationWeakMapCreate(); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 35 | observationState.pendingObservers = new InternalArray; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 36 | observationState.observerPriority = 0; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 39 | function ObservationWeakMap(map) { |
| 40 | this.map_ = map; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 41 | } |
| 42 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 43 | ObservationWeakMap.prototype = { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 44 | get: function(key) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 45 | key = %UnwrapGlobalProxy(key); |
| 46 | if (!IS_SPEC_OBJECT(key)) return void 0; |
| 47 | return %WeakMapGet(this.map_, key); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 48 | }, |
| 49 | set: function(key, value) { |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 50 | key = %UnwrapGlobalProxy(key); |
| 51 | if (!IS_SPEC_OBJECT(key)) return void 0; |
| 52 | %WeakMapSet(this.map_, key, value); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 53 | }, |
| 54 | has: function(key) { |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 55 | return !IS_UNDEFINED(this.get(key)); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 56 | } |
| 57 | }; |
| 58 | |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 59 | var observerInfoMap = |
| 60 | new ObservationWeakMap(observationState.observerInfoMap); |
| 61 | var objectInfoMap = new ObservationWeakMap(observationState.objectInfoMap); |
| 62 | var notifierTargetMap = |
| 63 | new ObservationWeakMap(observationState.notifierTargetMap); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 64 | |
| 65 | function CreateObjectInfo(object) { |
| 66 | var info = { |
| 67 | changeObservers: new InternalArray, |
| 68 | notifier: null, |
| 69 | }; |
| 70 | objectInfoMap.set(object, info); |
| 71 | return info; |
| 72 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 73 | |
| 74 | function ObjectObserve(object, callback) { |
| 75 | if (!IS_SPEC_OBJECT(object)) |
| 76 | throw MakeTypeError("observe_non_object", ["observe"]); |
| 77 | if (!IS_SPEC_FUNCTION(callback)) |
| 78 | throw MakeTypeError("observe_non_function", ["observe"]); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 79 | if (ObjectIsFrozen(callback)) |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 80 | throw MakeTypeError("observe_callback_frozen"); |
| 81 | |
| 82 | if (!observerInfoMap.has(callback)) { |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 83 | observerInfoMap.set(callback, { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 84 | pendingChangeRecords: null, |
| 85 | priority: observationState.observerPriority++, |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
| 89 | var objectInfo = objectInfoMap.get(object); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 90 | if (IS_UNDEFINED(objectInfo)) objectInfo = CreateObjectInfo(object); |
| 91 | %SetIsObserved(object, true); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 92 | |
| 93 | var changeObservers = objectInfo.changeObservers; |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 94 | if (changeObservers.indexOf(callback) < 0) changeObservers.push(callback); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 95 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 96 | return object; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | function ObjectUnobserve(object, callback) { |
| 100 | if (!IS_SPEC_OBJECT(object)) |
| 101 | throw MakeTypeError("observe_non_object", ["unobserve"]); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 102 | if (!IS_SPEC_FUNCTION(callback)) |
| 103 | throw MakeTypeError("observe_non_function", ["unobserve"]); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 104 | |
| 105 | var objectInfo = objectInfoMap.get(object); |
| 106 | if (IS_UNDEFINED(objectInfo)) |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 107 | return object; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 108 | |
| 109 | var changeObservers = objectInfo.changeObservers; |
| 110 | var index = changeObservers.indexOf(callback); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 111 | if (index >= 0) { |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 112 | changeObservers.splice(index, 1); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 113 | if (changeObservers.length === 0) %SetIsObserved(object, false); |
| 114 | } |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 115 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 116 | return object; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | function EnqueueChangeRecord(changeRecord, observers) { |
| 120 | for (var i = 0; i < observers.length; i++) { |
| 121 | var observer = observers[i]; |
| 122 | var observerInfo = observerInfoMap.get(observer); |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 123 | observationState.pendingObservers[observerInfo.priority] = observer; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 124 | %SetObserverDeliveryPending(); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 125 | if (IS_NULL(observerInfo.pendingChangeRecords)) { |
| 126 | observerInfo.pendingChangeRecords = new InternalArray(changeRecord); |
| 127 | } else { |
| 128 | observerInfo.pendingChangeRecords.push(changeRecord); |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 133 | function NotifyChange(type, object, name, oldValue) { |
| 134 | var objectInfo = objectInfoMap.get(object); |
| 135 | var changeRecord = (arguments.length < 4) ? |
| 136 | { type: type, object: object, name: name } : |
| 137 | { type: type, object: object, name: name, oldValue: oldValue }; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 138 | ObjectFreeze(changeRecord); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 139 | EnqueueChangeRecord(changeRecord, objectInfo.changeObservers); |
| 140 | } |
| 141 | |
| 142 | var notifierPrototype = {}; |
| 143 | |
| 144 | function ObjectNotifierNotify(changeRecord) { |
| 145 | if (!IS_SPEC_OBJECT(this)) |
| 146 | throw MakeTypeError("called_on_non_object", ["notify"]); |
| 147 | |
| 148 | var target = notifierTargetMap.get(this); |
| 149 | if (IS_UNDEFINED(target)) |
| 150 | throw MakeTypeError("observe_notify_non_notifier"); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 151 | if (!IS_STRING(changeRecord.type)) |
| 152 | throw MakeTypeError("observe_type_non_string"); |
| 153 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 154 | var objectInfo = objectInfoMap.get(target); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 155 | if (IS_UNDEFINED(objectInfo) || objectInfo.changeObservers.length === 0) |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 156 | return; |
| 157 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 158 | var newRecord = { object: target }; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 159 | for (var prop in changeRecord) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 160 | if (prop === 'object') continue; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 161 | %DefineOrRedefineDataProperty(newRecord, prop, changeRecord[prop], |
| 162 | READ_ONLY + DONT_DELETE); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 163 | } |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 164 | ObjectFreeze(newRecord); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 165 | |
| 166 | EnqueueChangeRecord(newRecord, objectInfo.changeObservers); |
| 167 | } |
| 168 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 169 | function ObjectGetNotifier(object) { |
| 170 | if (!IS_SPEC_OBJECT(object)) |
| 171 | throw MakeTypeError("observe_non_object", ["getNotifier"]); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 172 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 173 | if (ObjectIsFrozen(object)) return null; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 174 | |
| 175 | var objectInfo = objectInfoMap.get(object); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 176 | if (IS_UNDEFINED(objectInfo)) objectInfo = CreateObjectInfo(object); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 177 | |
| 178 | if (IS_NULL(objectInfo.notifier)) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 179 | objectInfo.notifier = { __proto__: notifierPrototype }; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 180 | notifierTargetMap.set(objectInfo.notifier, object); |
| 181 | } |
| 182 | |
| 183 | return objectInfo.notifier; |
| 184 | } |
| 185 | |
| 186 | function DeliverChangeRecordsForObserver(observer) { |
| 187 | var observerInfo = observerInfoMap.get(observer); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 188 | if (IS_UNDEFINED(observerInfo)) |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 189 | return false; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 190 | |
| 191 | var pendingChangeRecords = observerInfo.pendingChangeRecords; |
| 192 | if (IS_NULL(pendingChangeRecords)) |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 193 | return false; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 194 | |
| 195 | observerInfo.pendingChangeRecords = null; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 196 | delete observationState.pendingObservers[observerInfo.priority]; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 197 | var delivered = []; |
| 198 | %MoveArrayContents(pendingChangeRecords, delivered); |
| 199 | try { |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 200 | %Call(void 0, delivered, observer); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 201 | } catch (ex) {} |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 202 | return true; |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 203 | } |
| 204 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 205 | function ObjectDeliverChangeRecords(callback) { |
| 206 | if (!IS_SPEC_FUNCTION(callback)) |
| 207 | throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]); |
| 208 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 209 | while (DeliverChangeRecordsForObserver(callback)) {} |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | function DeliverChangeRecords() { |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 213 | while (observationState.pendingObservers.length) { |
| 214 | var pendingObservers = observationState.pendingObservers; |
| 215 | observationState.pendingObservers = new InternalArray; |
| 216 | for (var i in pendingObservers) { |
| 217 | DeliverChangeRecordsForObserver(pendingObservers[i]); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 222 | function SetupObjectObserve() { |
| 223 | %CheckIsBootstrapping(); |
| 224 | InstallFunctions($Object, DONT_ENUM, $Array( |
| 225 | "deliverChangeRecords", ObjectDeliverChangeRecords, |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 226 | "getNotifier", ObjectGetNotifier, |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 227 | "observe", ObjectObserve, |
| 228 | "unobserve", ObjectUnobserve |
| 229 | )); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 230 | InstallFunctions(notifierPrototype, DONT_ENUM, $Array( |
| 231 | "notify", ObjectNotifierNotify |
| 232 | )); |
danno@chromium.org | 72204d5 | 2012-10-31 10:02:10 +0000 | [diff] [blame] | 233 | } |
| 234 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 235 | SetupObjectObserve(); |