2012-10-25 14:56:44 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
// Overview:
|
|
|
|
//
|
|
|
|
// This file contains all of the routing and accounting for Object.observe.
|
|
|
|
// User code will interact with these mechanisms via the Object.observe APIs
|
|
|
|
// and, as a side effect of mutation objects which are observed. The V8 runtime
|
|
|
|
// (both C++ and JS) will interact with these mechanisms primarily by enqueuing
|
|
|
|
// proper change records for objects which were mutated. The Object.observe
|
|
|
|
// routing and accounting consists primarily of three participants
|
|
|
|
//
|
|
|
|
// 1) ObjectInfo. This represents the observed state of a given object. It
|
|
|
|
// records what callbacks are observing the object, with what options, and
|
|
|
|
// what "change types" are in progress on the object (i.e. via
|
|
|
|
// notifier.performChange).
|
|
|
|
//
|
|
|
|
// 2) CallbackInfo. This represents a callback used for observation. It holds
|
|
|
|
// the records which must be delivered to the callback, as well as the global
|
|
|
|
// priority of the callback (which determines delivery order between
|
|
|
|
// callbacks).
|
|
|
|
//
|
|
|
|
// 3) observationState.pendingObservers. This is the set of observers which
|
|
|
|
// have change records which must be delivered. During "normal" delivery
|
|
|
|
// (i.e. not Object.deliverChangeRecords), this is the mechanism by which
|
|
|
|
// callbacks are invoked in the proper order until there are no more
|
|
|
|
// change records pending to a callback.
|
|
|
|
//
|
|
|
|
// Note that in order to reduce allocation and processing costs, the
|
|
|
|
// implementation of (1) and (2) have "optimized" states which represent
|
|
|
|
// common cases which can be handled more efficiently.
|
|
|
|
|
2012-11-06 16:47:15 +00:00
|
|
|
var observationState = %GetObservationState();
|
2013-07-15 22:16:30 +00:00
|
|
|
if (IS_UNDEFINED(observationState.callbackInfoMap)) {
|
|
|
|
observationState.callbackInfoMap = %ObservationWeakMapCreate();
|
2013-02-04 20:24:11 +00:00
|
|
|
observationState.objectInfoMap = %ObservationWeakMapCreate();
|
2013-09-10 18:13:54 +00:00
|
|
|
observationState.notifierObjectInfoMap = %ObservationWeakMapCreate();
|
|
|
|
observationState.pendingObservers = null;
|
2013-07-15 22:16:30 +00:00
|
|
|
observationState.nextCallbackPriority = 0;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 20:24:11 +00:00
|
|
|
function ObservationWeakMap(map) {
|
|
|
|
this.map_ = map;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2013-02-04 20:24:11 +00:00
|
|
|
ObservationWeakMap.prototype = {
|
2012-11-06 16:47:15 +00:00
|
|
|
get: function(key) {
|
2013-02-04 20:24:11 +00:00
|
|
|
key = %UnwrapGlobalProxy(key);
|
2013-10-17 10:02:45 +00:00
|
|
|
if (!IS_SPEC_OBJECT(key)) return UNDEFINED;
|
2013-07-22 08:32:24 +00:00
|
|
|
return %WeakCollectionGet(this.map_, key);
|
2012-11-06 16:47:15 +00:00
|
|
|
},
|
|
|
|
set: function(key, value) {
|
2013-02-04 20:24:11 +00:00
|
|
|
key = %UnwrapGlobalProxy(key);
|
2013-10-17 10:02:45 +00:00
|
|
|
if (!IS_SPEC_OBJECT(key)) return UNDEFINED;
|
2013-07-22 08:32:24 +00:00
|
|
|
%WeakCollectionSet(this.map_, key, value);
|
2012-11-06 16:47:15 +00:00
|
|
|
},
|
|
|
|
has: function(key) {
|
2012-12-05 12:03:57 +00:00
|
|
|
return !IS_UNDEFINED(this.get(key));
|
2012-11-06 16:47:15 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
var callbackInfoMap =
|
|
|
|
new ObservationWeakMap(observationState.callbackInfoMap);
|
2013-02-04 20:24:11 +00:00
|
|
|
var objectInfoMap = new ObservationWeakMap(observationState.objectInfoMap);
|
2013-09-10 18:13:54 +00:00
|
|
|
var notifierObjectInfoMap =
|
|
|
|
new ObservationWeakMap(observationState.notifierObjectInfoMap);
|
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function nullProtoObject() {
|
2013-09-10 18:13:54 +00:00
|
|
|
return { __proto__: null };
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
2013-08-26 21:37:21 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function TypeMapCreate() {
|
|
|
|
return nullProtoObject();
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function TypeMapAddType(typeMap, type, ignoreDuplicate) {
|
|
|
|
typeMap[type] = ignoreDuplicate ? 1 : (typeMap[type] || 0) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TypeMapRemoveType(typeMap, type) {
|
|
|
|
typeMap[type]--;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TypeMapCreateFromList(typeList) {
|
|
|
|
var typeMap = TypeMapCreate();
|
|
|
|
for (var i = 0; i < typeList.length; i++) {
|
|
|
|
TypeMapAddType(typeMap, typeList[i], true);
|
|
|
|
}
|
|
|
|
return typeMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
function TypeMapHasType(typeMap, type) {
|
|
|
|
return !!typeMap[type];
|
|
|
|
}
|
2013-09-04 19:21:30 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function TypeMapIsDisjointFrom(typeMap1, typeMap2) {
|
|
|
|
if (!typeMap1 || !typeMap2)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
for (var type in typeMap1) {
|
|
|
|
if (TypeMapHasType(typeMap1, type) && TypeMapHasType(typeMap2, type))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultAcceptTypes = TypeMapCreateFromList([
|
2013-11-06 12:14:24 +00:00
|
|
|
'add',
|
|
|
|
'update',
|
|
|
|
'delete',
|
|
|
|
'setPrototype',
|
|
|
|
'reconfigure',
|
2013-11-05 12:25:32 +00:00
|
|
|
'preventExtensions'
|
2013-09-10 18:13:54 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
// An Observer is a registration to observe an object by a callback with
|
|
|
|
// a given set of accept types. If the set of accept types is the default
|
|
|
|
// set for Object.observe, the observer is represented as a direct reference
|
|
|
|
// to the callback. An observer never changes its accept types and thus never
|
|
|
|
// needs to "normalize".
|
|
|
|
function ObserverCreate(callback, acceptList) {
|
2014-01-08 20:25:08 +00:00
|
|
|
if (IS_UNDEFINED(acceptList))
|
|
|
|
return callback;
|
|
|
|
var observer = nullProtoObject();
|
|
|
|
observer.callback = callback;
|
|
|
|
observer.accept = TypeMapCreateFromList(acceptList);
|
|
|
|
return observer;
|
2013-09-10 18:13:54 +00:00
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObserverGetCallback(observer) {
|
|
|
|
return IS_SPEC_FUNCTION(observer) ? observer : observer.callback;
|
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObserverGetAcceptTypes(observer) {
|
|
|
|
return IS_SPEC_FUNCTION(observer) ? defaultAcceptTypes : observer.accept;
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-04 20:43:21 +00:00
|
|
|
function ObserverIsActive(observer, objectInfo) {
|
2013-09-10 18:13:54 +00:00
|
|
|
return TypeMapIsDisjointFrom(ObjectInfoGetPerformingTypes(objectInfo),
|
|
|
|
ObserverGetAcceptTypes(observer));
|
|
|
|
}
|
2013-08-26 21:37:21 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function ObjectInfoGetOrCreate(object) {
|
|
|
|
var objectInfo = ObjectInfoGet(object);
|
2013-09-10 18:13:54 +00:00
|
|
|
if (IS_UNDEFINED(objectInfo)) {
|
|
|
|
if (!%IsJSProxy(object))
|
|
|
|
%SetIsObserved(object);
|
|
|
|
|
|
|
|
objectInfo = {
|
|
|
|
object: object,
|
|
|
|
changeObservers: null,
|
|
|
|
notifier: null,
|
|
|
|
performing: null,
|
|
|
|
performingCount: 0,
|
|
|
|
};
|
|
|
|
objectInfoMap.set(object, objectInfo);
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
2013-09-10 18:13:54 +00:00
|
|
|
return objectInfo;
|
|
|
|
}
|
2013-09-04 19:21:30 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function ObjectInfoGet(object) {
|
|
|
|
return objectInfoMap.get(object);
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoGetFromNotifier(notifier) {
|
|
|
|
return notifierObjectInfoMap.get(notifier);
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoGetNotifier(objectInfo) {
|
|
|
|
if (IS_NULL(objectInfo.notifier)) {
|
|
|
|
objectInfo.notifier = { __proto__: notifierPrototype };
|
|
|
|
notifierObjectInfoMap.set(objectInfo.notifier, objectInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return objectInfo.notifier;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ObjectInfoGetObject(objectInfo) {
|
|
|
|
return objectInfo.object;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ChangeObserversIsOptimized(changeObservers) {
|
|
|
|
return typeof changeObservers === 'function' ||
|
|
|
|
typeof changeObservers.callback === 'function';
|
|
|
|
}
|
|
|
|
|
|
|
|
// The set of observers on an object is called 'changeObservers'. The first
|
|
|
|
// observer is referenced directly via objectInfo.changeObservers. When a second
|
|
|
|
// is added, changeObservers "normalizes" to become a mapping of callback
|
|
|
|
// priority -> observer and is then stored on objectInfo.changeObservers.
|
|
|
|
function ObjectInfoNormalizeChangeObservers(objectInfo) {
|
|
|
|
if (ChangeObserversIsOptimized(objectInfo.changeObservers)) {
|
|
|
|
var observer = objectInfo.changeObservers;
|
|
|
|
var callback = ObserverGetCallback(observer);
|
|
|
|
var callbackInfo = CallbackInfoGet(callback);
|
|
|
|
var priority = CallbackInfoGetPriority(callbackInfo);
|
2014-01-08 20:25:08 +00:00
|
|
|
objectInfo.changeObservers = nullProtoObject();
|
2013-09-10 18:13:54 +00:00
|
|
|
objectInfo.changeObservers[priority] = observer;
|
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoAddObserver(objectInfo, callback, acceptList) {
|
|
|
|
var callbackInfo = CallbackInfoGetOrCreate(callback);
|
|
|
|
var observer = ObserverCreate(callback, acceptList);
|
|
|
|
|
|
|
|
if (!objectInfo.changeObservers) {
|
|
|
|
objectInfo.changeObservers = observer;
|
|
|
|
return;
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoNormalizeChangeObservers(objectInfo);
|
|
|
|
var priority = CallbackInfoGetPriority(callbackInfo);
|
|
|
|
objectInfo.changeObservers[priority] = observer;
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoRemoveObserver(objectInfo, callback) {
|
|
|
|
if (!objectInfo.changeObservers)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ChangeObserversIsOptimized(objectInfo.changeObservers)) {
|
|
|
|
if (callback === ObserverGetCallback(objectInfo.changeObservers))
|
|
|
|
objectInfo.changeObservers = null;
|
|
|
|
return;
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var callbackInfo = CallbackInfoGet(callback);
|
|
|
|
var priority = CallbackInfoGetPriority(callbackInfo);
|
2014-01-08 20:25:08 +00:00
|
|
|
objectInfo.changeObservers[priority] = null;
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoHasActiveObservers(objectInfo) {
|
|
|
|
if (IS_UNDEFINED(objectInfo) || !objectInfo.changeObservers)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (ChangeObserversIsOptimized(objectInfo.changeObservers))
|
|
|
|
return ObserverIsActive(objectInfo.changeObservers, objectInfo);
|
|
|
|
|
|
|
|
for (var priority in objectInfo.changeObservers) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var observer = objectInfo.changeObservers[priority];
|
|
|
|
if (!IS_NULL(observer) && ObserverIsActive(observer, objectInfo))
|
2013-09-10 18:13:54 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ObjectInfoAddPerformingType(objectInfo, type) {
|
|
|
|
objectInfo.performing = objectInfo.performing || TypeMapCreate();
|
|
|
|
TypeMapAddType(objectInfo.performing, type);
|
2013-05-15 22:09:40 +00:00
|
|
|
objectInfo.performingCount++;
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoRemovePerformingType(objectInfo, type) {
|
2013-05-15 22:09:40 +00:00
|
|
|
objectInfo.performingCount--;
|
2013-09-10 18:13:54 +00:00
|
|
|
TypeMapRemoveType(objectInfo.performing, type);
|
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoGetPerformingTypes(objectInfo) {
|
|
|
|
return objectInfo.performingCount > 0 ? objectInfo.performing : null;
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function AcceptArgIsValid(arg) {
|
|
|
|
if (IS_UNDEFINED(arg))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!IS_SPEC_OBJECT(arg) ||
|
|
|
|
!IS_NUMBER(arg.length) ||
|
|
|
|
arg.length < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
// CallbackInfo's optimized state is just a number which represents its global
|
|
|
|
// priority. When a change record must be enqueued for the callback, it
|
|
|
|
// normalizes. When delivery clears any pending change records, it re-optimizes.
|
|
|
|
function CallbackInfoGet(callback) {
|
|
|
|
return callbackInfoMap.get(callback);
|
2013-07-15 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function CallbackInfoGetOrCreate(callback) {
|
|
|
|
var callbackInfo = callbackInfoMap.get(callback);
|
|
|
|
if (!IS_UNDEFINED(callbackInfo))
|
|
|
|
return callbackInfo;
|
|
|
|
|
|
|
|
var priority = observationState.nextCallbackPriority++
|
|
|
|
callbackInfoMap.set(callback, priority);
|
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
function CallbackInfoGetPriority(callbackInfo) {
|
|
|
|
if (IS_NUMBER(callbackInfo))
|
|
|
|
return callbackInfo;
|
|
|
|
else
|
|
|
|
return callbackInfo.priority;
|
|
|
|
}
|
|
|
|
|
|
|
|
function CallbackInfoNormalize(callback) {
|
2013-07-15 22:16:30 +00:00
|
|
|
var callbackInfo = callbackInfoMap.get(callback);
|
|
|
|
if (IS_NUMBER(callbackInfo)) {
|
|
|
|
var priority = callbackInfo;
|
|
|
|
callbackInfo = new InternalArray;
|
|
|
|
callbackInfo.priority = priority;
|
|
|
|
callbackInfoMap.set(callback, callbackInfo);
|
|
|
|
}
|
|
|
|
return callbackInfo;
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectObserve(object, callback, acceptList) {
|
2012-10-25 14:56:44 +00:00
|
|
|
if (!IS_SPEC_OBJECT(object))
|
|
|
|
throw MakeTypeError("observe_non_object", ["observe"]);
|
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
|
|
|
throw MakeTypeError("observe_non_function", ["observe"]);
|
2012-11-16 09:35:27 +00:00
|
|
|
if (ObjectIsFrozen(callback))
|
2012-10-25 14:56:44 +00:00
|
|
|
throw MakeTypeError("observe_callback_frozen");
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!AcceptArgIsValid(acceptList))
|
2013-05-15 22:09:40 +00:00
|
|
|
throw MakeTypeError("observe_accept_invalid");
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGetOrCreate(object);
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoAddObserver(objectInfo, callback, acceptList);
|
2012-11-20 10:38:14 +00:00
|
|
|
return object;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ObjectUnobserve(object, callback) {
|
|
|
|
if (!IS_SPEC_OBJECT(object))
|
|
|
|
throw MakeTypeError("observe_non_object", ["unobserve"]);
|
2012-11-13 15:50:59 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
|
|
|
throw MakeTypeError("observe_non_function", ["unobserve"]);
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGet(object);
|
2012-10-25 14:56:44 +00:00
|
|
|
if (IS_UNDEFINED(objectInfo))
|
2012-11-20 10:38:14 +00:00
|
|
|
return object;
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoRemoveObserver(objectInfo, callback);
|
2012-11-20 10:38:14 +00:00
|
|
|
return object;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2013-05-16 11:19:37 +00:00
|
|
|
function ArrayObserve(object, callback) {
|
2013-11-06 12:14:24 +00:00
|
|
|
return ObjectObserve(object, callback, ['add',
|
|
|
|
'update',
|
|
|
|
'delete',
|
2013-05-16 11:19:37 +00:00
|
|
|
'splice']);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ArrayUnobserve(object, callback) {
|
|
|
|
return ObjectUnobserve(object, callback);
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:54 +00:00
|
|
|
function ObserverEnqueueIfActive(observer, objectInfo, changeRecord,
|
|
|
|
needsAccessCheck) {
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!ObserverIsActive(observer, objectInfo) ||
|
|
|
|
!TypeMapHasType(ObserverGetAcceptTypes(observer), changeRecord.type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var callback = ObserverGetCallback(observer);
|
2013-09-11 20:03:54 +00:00
|
|
|
if (needsAccessCheck &&
|
|
|
|
// Drop all splice records on the floor for access-checked objects
|
|
|
|
(changeRecord.type == 'splice' ||
|
|
|
|
!%IsAccessAllowedForObserver(
|
|
|
|
callback, changeRecord.object, changeRecord.name))) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var callbackInfo = CallbackInfoNormalize(callback);
|
|
|
|
if (!observationState.pendingObservers)
|
2014-01-08 20:25:08 +00:00
|
|
|
observationState.pendingObservers = nullProtoObject();
|
2013-07-15 22:16:30 +00:00
|
|
|
observationState.pendingObservers[callbackInfo.priority] = callback;
|
|
|
|
callbackInfo.push(changeRecord);
|
2013-11-27 17:21:40 +00:00
|
|
|
%SetMicrotaskPending(true);
|
2013-07-15 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 11:23:08 +00:00
|
|
|
function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) {
|
|
|
|
if (!ObjectInfoHasActiveObservers(objectInfo))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var hasType = !IS_UNDEFINED(type);
|
|
|
|
var newRecord = hasType ?
|
|
|
|
{ object: ObjectInfoGetObject(objectInfo), type: type } :
|
|
|
|
{ object: ObjectInfoGetObject(objectInfo) };
|
|
|
|
|
|
|
|
for (var prop in changeRecord) {
|
|
|
|
if (prop === 'object' || (hasType && prop === 'type')) continue;
|
|
|
|
%DefineOrRedefineDataProperty(newRecord, prop, changeRecord[prop],
|
|
|
|
READ_ONLY + DONT_DELETE);
|
|
|
|
}
|
|
|
|
ObjectFreeze(newRecord);
|
|
|
|
|
|
|
|
ObjectInfoEnqueueInternalChangeRecord(objectInfo, newRecord,
|
|
|
|
true /* skip access check */);
|
|
|
|
}
|
|
|
|
|
|
|
|
function ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord,
|
|
|
|
skipAccessCheck) {
|
2013-03-22 17:27:44 +00:00
|
|
|
// TODO(rossberg): adjust once there is a story for symbols vs proxies.
|
|
|
|
if (IS_SYMBOL(changeRecord.name)) return;
|
|
|
|
|
2013-09-11 20:03:54 +00:00
|
|
|
var needsAccessCheck = !skipAccessCheck &&
|
|
|
|
%IsAccessCheckNeeded(changeRecord.object);
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
if (ChangeObserversIsOptimized(objectInfo.changeObservers)) {
|
|
|
|
var observer = objectInfo.changeObservers;
|
2013-09-11 20:03:54 +00:00
|
|
|
ObserverEnqueueIfActive(observer, objectInfo, changeRecord,
|
|
|
|
needsAccessCheck);
|
2013-09-10 18:13:54 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
for (var priority in objectInfo.changeObservers) {
|
|
|
|
var observer = objectInfo.changeObservers[priority];
|
2014-01-08 20:25:08 +00:00
|
|
|
if (IS_NULL(observer))
|
|
|
|
continue;
|
2013-09-11 20:03:54 +00:00
|
|
|
ObserverEnqueueIfActive(observer, objectInfo, changeRecord,
|
|
|
|
needsAccessCheck);
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-16 11:19:37 +00:00
|
|
|
function BeginPerformSplice(array) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGet(array);
|
2013-05-16 11:19:37 +00:00
|
|
|
if (!IS_UNDEFINED(objectInfo))
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoAddPerformingType(objectInfo, 'splice');
|
2013-05-16 11:19:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function EndPerformSplice(array) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGet(array);
|
2013-05-16 11:19:37 +00:00
|
|
|
if (!IS_UNDEFINED(objectInfo))
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoRemovePerformingType(objectInfo, 'splice');
|
2013-05-16 11:19:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 19:15:40 +00:00
|
|
|
function EnqueueSpliceRecord(array, index, removed, addedCount) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGet(array);
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!ObjectInfoHasActiveObservers(objectInfo))
|
2013-05-16 11:19:37 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
var changeRecord = {
|
|
|
|
type: 'splice',
|
|
|
|
object: array,
|
|
|
|
index: index,
|
|
|
|
removed: removed,
|
|
|
|
addedCount: addedCount
|
|
|
|
};
|
|
|
|
|
2013-05-28 11:34:31 +00:00
|
|
|
ObjectFreeze(changeRecord);
|
|
|
|
ObjectFreeze(changeRecord.removed);
|
2013-11-05 11:23:08 +00:00
|
|
|
ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord);
|
2013-05-16 11:19:37 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 12:32:36 +00:00
|
|
|
function NotifyChange(type, object, name, oldValue) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGet(object);
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!ObjectInfoHasActiveObservers(objectInfo))
|
2013-05-15 22:09:40 +00:00
|
|
|
return;
|
|
|
|
|
2013-11-05 12:25:32 +00:00
|
|
|
var changeRecord;
|
|
|
|
if (arguments.length == 2) {
|
|
|
|
changeRecord = { type: type, object: object };
|
|
|
|
} else if (arguments.length == 3) {
|
|
|
|
changeRecord = { type: type, object: object, name: name };
|
|
|
|
} else {
|
|
|
|
changeRecord = {
|
|
|
|
type: type,
|
|
|
|
object: object,
|
|
|
|
name: name,
|
|
|
|
oldValue: oldValue
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2013-05-28 11:34:31 +00:00
|
|
|
ObjectFreeze(changeRecord);
|
2013-11-05 11:23:08 +00:00
|
|
|
ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord);
|
2012-11-06 12:32:36 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:14:42 +00:00
|
|
|
var notifierPrototype = {};
|
|
|
|
|
|
|
|
function ObjectNotifierNotify(changeRecord) {
|
|
|
|
if (!IS_SPEC_OBJECT(this))
|
|
|
|
throw MakeTypeError("called_on_non_object", ["notify"]);
|
2012-12-19 10:28:36 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var objectInfo = ObjectInfoGetFromNotifier(this);
|
|
|
|
if (IS_UNDEFINED(objectInfo))
|
2012-11-09 16:14:42 +00:00
|
|
|
throw MakeTypeError("observe_notify_non_notifier");
|
2012-10-25 14:56:44 +00:00
|
|
|
if (!IS_STRING(changeRecord.type))
|
|
|
|
throw MakeTypeError("observe_type_non_string");
|
|
|
|
|
2013-11-05 11:23:08 +00:00
|
|
|
ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord);
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2013-09-11 10:52:20 +00:00
|
|
|
function ObjectNotifierPerformChange(changeType, changeFn) {
|
2013-05-15 22:09:40 +00:00
|
|
|
if (!IS_SPEC_OBJECT(this))
|
|
|
|
throw MakeTypeError("called_on_non_object", ["performChange"]);
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var objectInfo = ObjectInfoGetFromNotifier(this);
|
|
|
|
|
|
|
|
if (IS_UNDEFINED(objectInfo))
|
2013-05-15 22:09:40 +00:00
|
|
|
throw MakeTypeError("observe_notify_non_notifier");
|
|
|
|
if (!IS_STRING(changeType))
|
|
|
|
throw MakeTypeError("observe_perform_non_string");
|
|
|
|
if (!IS_SPEC_FUNCTION(changeFn))
|
|
|
|
throw MakeTypeError("observe_perform_non_function");
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoAddPerformingType(objectInfo, changeType);
|
2013-11-05 11:23:08 +00:00
|
|
|
|
|
|
|
var changeRecord;
|
2013-05-15 22:09:40 +00:00
|
|
|
try {
|
2013-11-05 11:23:08 +00:00
|
|
|
changeRecord = %_CallFunction(UNDEFINED, changeFn);
|
2013-05-15 22:09:40 +00:00
|
|
|
} finally {
|
2013-09-10 18:13:54 +00:00
|
|
|
ObjectInfoRemovePerformingType(objectInfo, changeType);
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
2013-11-05 11:23:08 +00:00
|
|
|
|
|
|
|
if (IS_SPEC_OBJECT(changeRecord))
|
|
|
|
ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, changeType);
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 16:14:42 +00:00
|
|
|
function ObjectGetNotifier(object) {
|
|
|
|
if (!IS_SPEC_OBJECT(object))
|
|
|
|
throw MakeTypeError("observe_non_object", ["getNotifier"]);
|
|
|
|
|
2012-12-19 09:51:46 +00:00
|
|
|
if (ObjectIsFrozen(object)) return null;
|
2012-11-09 16:14:42 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGetOrCreate(object);
|
2013-09-10 18:13:54 +00:00
|
|
|
return ObjectInfoGetNotifier(objectInfo);
|
2012-11-09 16:14:42 +00:00
|
|
|
}
|
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
function CallbackDeliverPending(callback) {
|
|
|
|
var callbackInfo = callbackInfoMap.get(callback);
|
|
|
|
if (IS_UNDEFINED(callbackInfo) || IS_NUMBER(callbackInfo))
|
2012-12-19 09:51:46 +00:00
|
|
|
return false;
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
// Clear the pending change records from callback and return it to its
|
|
|
|
// "optimized" state.
|
|
|
|
var priority = callbackInfo.priority;
|
|
|
|
callbackInfoMap.set(callback, priority);
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
if (observationState.pendingObservers)
|
|
|
|
delete observationState.pendingObservers[priority];
|
|
|
|
|
2012-10-25 14:56:44 +00:00
|
|
|
var delivered = [];
|
2013-07-15 22:16:30 +00:00
|
|
|
%MoveArrayContents(callbackInfo, delivered);
|
|
|
|
|
2012-10-25 14:56:44 +00:00
|
|
|
try {
|
2013-10-17 10:02:45 +00:00
|
|
|
%_CallFunction(UNDEFINED, delivered, callback);
|
2013-11-27 17:21:40 +00:00
|
|
|
} catch (ex) {} // TODO(rossberg): perhaps log uncaught exceptions.
|
2012-12-19 09:51:46 +00:00
|
|
|
return true;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2012-11-08 13:44:59 +00:00
|
|
|
function ObjectDeliverChangeRecords(callback) {
|
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
|
|
|
throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]);
|
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
while (CallbackDeliverPending(callback)) {}
|
2012-11-08 13:44:59 +00:00
|
|
|
}
|
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
function ObserveMicrotaskRunner() {
|
|
|
|
var pendingObservers = observationState.pendingObservers;
|
|
|
|
if (pendingObservers) {
|
2013-09-10 18:13:54 +00:00
|
|
|
observationState.pendingObservers = null;
|
2012-11-13 15:53:28 +00:00
|
|
|
for (var i in pendingObservers) {
|
2013-07-15 22:16:30 +00:00
|
|
|
CallbackDeliverPending(pendingObservers[i]);
|
2012-11-08 13:44:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-11-27 17:21:40 +00:00
|
|
|
RunMicrotasks.runners.push(ObserveMicrotaskRunner);
|
2012-11-08 13:44:59 +00:00
|
|
|
|
2012-10-25 14:56:44 +00:00
|
|
|
function SetupObjectObserve() {
|
|
|
|
%CheckIsBootstrapping();
|
|
|
|
InstallFunctions($Object, DONT_ENUM, $Array(
|
|
|
|
"deliverChangeRecords", ObjectDeliverChangeRecords,
|
2012-11-09 16:14:42 +00:00
|
|
|
"getNotifier", ObjectGetNotifier,
|
2012-10-25 14:56:44 +00:00
|
|
|
"observe", ObjectObserve,
|
|
|
|
"unobserve", ObjectUnobserve
|
|
|
|
));
|
2013-05-16 11:19:37 +00:00
|
|
|
InstallFunctions($Array, DONT_ENUM, $Array(
|
|
|
|
"observe", ArrayObserve,
|
|
|
|
"unobserve", ArrayUnobserve
|
|
|
|
));
|
2012-11-09 16:14:42 +00:00
|
|
|
InstallFunctions(notifierPrototype, DONT_ENUM, $Array(
|
2013-05-15 22:09:40 +00:00
|
|
|
"notify", ObjectNotifierNotify,
|
|
|
|
"performChange", ObjectNotifierPerformChange
|
2012-11-09 16:14:42 +00:00
|
|
|
));
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2012-11-06 12:32:36 +00:00
|
|
|
SetupObjectObserve();
|