Wrap object observe implementation in a function.

R=jkummerow@chromium.org

Review URL: https://codereview.chromium.org/1086813005

Cr-Commit-Position: refs/heads/master@{#27920}
This commit is contained in:
yangguo 2015-04-17 07:10:05 -07:00 committed by Commit bot
parent 281d30d758
commit c6a36305c7
4 changed files with 127 additions and 51 deletions

View File

@ -407,12 +407,12 @@ function ObservedArrayPop(n) {
var value = this[n]; var value = this[n];
try { try {
BeginPerformSplice(this); $observeBeginPerformSplice(this);
delete this[n]; delete this[n];
this.length = n; this.length = n;
} finally { } finally {
EndPerformSplice(this); $observeEndPerformSplice(this);
EnqueueSpliceRecord(this, n, [value], 0); $observeEnqueueSpliceRecord(this, n, [value], 0);
} }
return value; return value;
@ -446,15 +446,15 @@ function ObservedArrayPush() {
var m = %_ArgumentsLength(); var m = %_ArgumentsLength();
try { try {
BeginPerformSplice(this); $observeBeginPerformSplice(this);
for (var i = 0; i < m; i++) { for (var i = 0; i < m; i++) {
this[i+n] = %_Arguments(i); this[i+n] = %_Arguments(i);
} }
var new_length = n + m; var new_length = n + m;
this.length = new_length; this.length = new_length;
} finally { } finally {
EndPerformSplice(this); $observeEndPerformSplice(this);
EnqueueSpliceRecord(this, n, [], m); $observeEnqueueSpliceRecord(this, n, [], m);
} }
return new_length; return new_length;
@ -584,12 +584,12 @@ function ObservedArrayShift(len) {
var first = this[0]; var first = this[0];
try { try {
BeginPerformSplice(this); $observeBeginPerformSplice(this);
SimpleMove(this, 0, 1, len, 0); SimpleMove(this, 0, 1, len, 0);
this.length = len - 1; this.length = len - 1;
} finally { } finally {
EndPerformSplice(this); $observeEndPerformSplice(this);
EnqueueSpliceRecord(this, 0, [first], 0); $observeEnqueueSpliceRecord(this, 0, [first], 0);
} }
return first; return first;
@ -632,7 +632,7 @@ function ObservedArrayUnshift() {
var num_arguments = %_ArgumentsLength(); var num_arguments = %_ArgumentsLength();
try { try {
BeginPerformSplice(this); $observeBeginPerformSplice(this);
SimpleMove(this, 0, 0, len, num_arguments); SimpleMove(this, 0, 0, len, num_arguments);
for (var i = 0; i < num_arguments; i++) { for (var i = 0; i < num_arguments; i++) {
this[i] = %_Arguments(i); this[i] = %_Arguments(i);
@ -640,8 +640,8 @@ function ObservedArrayUnshift() {
var new_length = len + num_arguments; var new_length = len + num_arguments;
this.length = new_length; this.length = new_length;
} finally { } finally {
EndPerformSplice(this); $observeEndPerformSplice(this);
EnqueueSpliceRecord(this, 0, [], num_arguments); $observeEnqueueSpliceRecord(this, 0, [], num_arguments);
} }
return new_length; return new_length;
@ -758,7 +758,7 @@ function ObservedArraySplice(start, delete_count) {
var num_elements_to_add = num_arguments > 2 ? num_arguments - 2 : 0; var num_elements_to_add = num_arguments > 2 ? num_arguments - 2 : 0;
try { try {
BeginPerformSplice(this); $observeBeginPerformSplice(this);
SimpleSlice(this, start_i, del_count, len, deleted_elements); SimpleSlice(this, start_i, del_count, len, deleted_elements);
SimpleMove(this, start_i, del_count, len, num_elements_to_add); SimpleMove(this, start_i, del_count, len, num_elements_to_add);
@ -774,12 +774,12 @@ function ObservedArraySplice(start, delete_count) {
this.length = len - del_count + num_elements_to_add; this.length = len - del_count + num_elements_to_add;
} finally { } finally {
EndPerformSplice(this); $observeEndPerformSplice(this);
if (deleted_elements.length || num_elements_to_add) { if (deleted_elements.length || num_elements_to_add) {
EnqueueSpliceRecord(this, $observeEnqueueSpliceRecord(this,
start_i, start_i,
deleted_elements.slice(), deleted_elements.slice(),
num_elements_to_add); num_elements_to_add);
} }
} }

View File

@ -1565,17 +1565,18 @@ void Genesis::InstallNativeFunctions() {
INSTALL_NATIVE(JSFunction, "PromiseCatch", promise_catch); INSTALL_NATIVE(JSFunction, "PromiseCatch", promise_catch);
INSTALL_NATIVE(JSFunction, "PromiseThen", promise_then); INSTALL_NATIVE(JSFunction, "PromiseThen", promise_then);
INSTALL_NATIVE(JSFunction, "NotifyChange", observers_notify_change); INSTALL_NATIVE(JSFunction, "$observeNotifyChange", observers_notify_change);
INSTALL_NATIVE(JSFunction, "EnqueueSpliceRecord", observers_enqueue_splice); INSTALL_NATIVE(JSFunction, "$observeEnqueueSpliceRecord",
INSTALL_NATIVE(JSFunction, "BeginPerformSplice", observers_enqueue_splice);
INSTALL_NATIVE(JSFunction, "$observeBeginPerformSplice",
observers_begin_perform_splice); observers_begin_perform_splice);
INSTALL_NATIVE(JSFunction, "EndPerformSplice", INSTALL_NATIVE(JSFunction, "$observeEndPerformSplice",
observers_end_perform_splice); observers_end_perform_splice);
INSTALL_NATIVE(JSFunction, "NativeObjectObserve", INSTALL_NATIVE(JSFunction, "$observeNativeObjectObserve",
native_object_observe); native_object_observe);
INSTALL_NATIVE(JSFunction, "NativeObjectGetNotifier", INSTALL_NATIVE(JSFunction, "$observeNativeObjectGetNotifier",
native_object_get_notifier); native_object_get_notifier);
INSTALL_NATIVE(JSFunction, "NativeObjectNotifierPerformChange", INSTALL_NATIVE(JSFunction, "$observeNativeObjectNotifierPerformChange",
native_object_notifier_perform_change); native_object_notifier_perform_change);
INSTALL_NATIVE(JSFunction, "$arrayValues", array_values_iterator); INSTALL_NATIVE(JSFunction, "$arrayValues", array_values_iterator);
} }

View File

@ -2,8 +2,25 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
var $observeNotifyChange;
var $observeEnqueueSpliceRecord;
var $observeBeginPerformSplice;
var $observeEndPerformSplice;
var $observeNativeObjectObserve;
var $observeNativeObjectGetNotifier;
var $observeNativeObjectNotifierPerformChange;
(function() {
"use strict"; "use strict";
%CheckIsBootstrapping();
var GlobalArray = global.Array;
var GlobalObject = global.Object;
// -------------------------------------------------------------------
// Overview: // Overview:
// //
// This file contains all of the routing and accounting for Object.observe. // This file contains all of the routing and accounting for Object.observe.
@ -35,6 +52,8 @@
var observationState; var observationState;
var notifierPrototype = {};
// We have to wait until after bootstrapping to grab a reference to the // We have to wait until after bootstrapping to grab a reference to the
// observationState object, since it's not possible to serialize that // observationState object, since it's not possible to serialize that
// reference into the snapshot. // reference into the snapshot.
@ -56,34 +75,42 @@ function GetObservationStateJS() {
return observationState; return observationState;
} }
function GetPendingObservers() { function GetPendingObservers() {
return GetObservationStateJS().pendingObservers; return GetObservationStateJS().pendingObservers;
} }
function SetPendingObservers(pendingObservers) { function SetPendingObservers(pendingObservers) {
GetObservationStateJS().pendingObservers = pendingObservers; GetObservationStateJS().pendingObservers = pendingObservers;
} }
function GetNextCallbackPriority() { function GetNextCallbackPriority() {
return GetObservationStateJS().nextCallbackPriority++; return GetObservationStateJS().nextCallbackPriority++;
} }
function nullProtoObject() { function nullProtoObject() {
return { __proto__: null }; return { __proto__: null };
} }
function TypeMapCreate() { function TypeMapCreate() {
return nullProtoObject(); return nullProtoObject();
} }
function TypeMapAddType(typeMap, type, ignoreDuplicate) { function TypeMapAddType(typeMap, type, ignoreDuplicate) {
typeMap[type] = ignoreDuplicate ? 1 : (typeMap[type] || 0) + 1; typeMap[type] = ignoreDuplicate ? 1 : (typeMap[type] || 0) + 1;
} }
function TypeMapRemoveType(typeMap, type) { function TypeMapRemoveType(typeMap, type) {
typeMap[type]--; typeMap[type]--;
} }
function TypeMapCreateFromList(typeList, length) { function TypeMapCreateFromList(typeList, length) {
var typeMap = TypeMapCreate(); var typeMap = TypeMapCreate();
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
@ -92,10 +119,12 @@ function TypeMapCreateFromList(typeList, length) {
return typeMap; return typeMap;
} }
function TypeMapHasType(typeMap, type) { function TypeMapHasType(typeMap, type) {
return !!typeMap[type]; return !!typeMap[type];
} }
function TypeMapIsDisjointFrom(typeMap1, typeMap2) { function TypeMapIsDisjointFrom(typeMap1, typeMap2) {
if (!typeMap1 || !typeMap2) if (!typeMap1 || !typeMap2)
return true; return true;
@ -108,6 +137,7 @@ function TypeMapIsDisjointFrom(typeMap1, typeMap2) {
return true; return true;
} }
var defaultAcceptTypes = (function() { var defaultAcceptTypes = (function() {
var defaultTypes = [ var defaultTypes = [
'add', 'add',
@ -120,6 +150,7 @@ var defaultAcceptTypes = (function() {
return TypeMapCreateFromList(defaultTypes, defaultTypes.length); return TypeMapCreateFromList(defaultTypes, defaultTypes.length);
})(); })();
// An Observer is a registration to observe an object by a callback with // 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 // 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 // set for Object.observe, the observer is represented as a direct reference
@ -134,19 +165,23 @@ function ObserverCreate(callback, acceptList) {
return observer; return observer;
} }
function ObserverGetCallback(observer) { function ObserverGetCallback(observer) {
return IS_SPEC_FUNCTION(observer) ? observer : observer.callback; return IS_SPEC_FUNCTION(observer) ? observer : observer.callback;
} }
function ObserverGetAcceptTypes(observer) { function ObserverGetAcceptTypes(observer) {
return IS_SPEC_FUNCTION(observer) ? defaultAcceptTypes : observer.accept; return IS_SPEC_FUNCTION(observer) ? defaultAcceptTypes : observer.accept;
} }
function ObserverIsActive(observer, objectInfo) { function ObserverIsActive(observer, objectInfo) {
return TypeMapIsDisjointFrom(ObjectInfoGetPerformingTypes(objectInfo), return TypeMapIsDisjointFrom(ObjectInfoGetPerformingTypes(objectInfo),
ObserverGetAcceptTypes(observer)); ObserverGetAcceptTypes(observer));
} }
function ObjectInfoGetOrCreate(object) { function ObjectInfoGetOrCreate(object) {
var objectInfo = ObjectInfoGet(object); var objectInfo = ObjectInfoGet(object);
if (IS_UNDEFINED(objectInfo)) { if (IS_UNDEFINED(objectInfo)) {
@ -166,15 +201,18 @@ function ObjectInfoGetOrCreate(object) {
return objectInfo; return objectInfo;
} }
function ObjectInfoGet(object) { function ObjectInfoGet(object) {
return %WeakCollectionGet(GetObservationStateJS().objectInfoMap, object); return %WeakCollectionGet(GetObservationStateJS().objectInfoMap, object);
} }
function ObjectInfoGetFromNotifier(notifier) { function ObjectInfoGetFromNotifier(notifier) {
return %WeakCollectionGet(GetObservationStateJS().notifierObjectInfoMap, return %WeakCollectionGet(GetObservationStateJS().notifierObjectInfoMap,
notifier); notifier);
} }
function ObjectInfoGetNotifier(objectInfo) { function ObjectInfoGetNotifier(objectInfo) {
if (IS_NULL(objectInfo.notifier)) { if (IS_NULL(objectInfo.notifier)) {
objectInfo.notifier = { __proto__: notifierPrototype }; objectInfo.notifier = { __proto__: notifierPrototype };
@ -185,11 +223,13 @@ function ObjectInfoGetNotifier(objectInfo) {
return objectInfo.notifier; return objectInfo.notifier;
} }
function ChangeObserversIsOptimized(changeObservers) { function ChangeObserversIsOptimized(changeObservers) {
return IS_SPEC_FUNCTION(changeObservers) || return IS_SPEC_FUNCTION(changeObservers) ||
IS_SPEC_FUNCTION(changeObservers.callback); IS_SPEC_FUNCTION(changeObservers.callback);
} }
// The set of observers on an object is called 'changeObservers'. The first // The set of observers on an object is called 'changeObservers'. The first
// observer is referenced directly via objectInfo.changeObservers. When a second // observer is referenced directly via objectInfo.changeObservers. When a second
// is added, changeObservers "normalizes" to become a mapping of callback // is added, changeObservers "normalizes" to become a mapping of callback
@ -205,6 +245,7 @@ function ObjectInfoNormalizeChangeObservers(objectInfo) {
} }
} }
function ObjectInfoAddObserver(objectInfo, callback, acceptList) { function ObjectInfoAddObserver(objectInfo, callback, acceptList) {
var callbackInfo = CallbackInfoGetOrCreate(callback); var callbackInfo = CallbackInfoGetOrCreate(callback);
var observer = ObserverCreate(callback, acceptList); var observer = ObserverCreate(callback, acceptList);
@ -250,21 +291,25 @@ function ObjectInfoHasActiveObservers(objectInfo) {
return false; return false;
} }
function ObjectInfoAddPerformingType(objectInfo, type) { function ObjectInfoAddPerformingType(objectInfo, type) {
objectInfo.performing = objectInfo.performing || TypeMapCreate(); objectInfo.performing = objectInfo.performing || TypeMapCreate();
TypeMapAddType(objectInfo.performing, type); TypeMapAddType(objectInfo.performing, type);
objectInfo.performingCount++; objectInfo.performingCount++;
} }
function ObjectInfoRemovePerformingType(objectInfo, type) { function ObjectInfoRemovePerformingType(objectInfo, type) {
objectInfo.performingCount--; objectInfo.performingCount--;
TypeMapRemoveType(objectInfo.performing, type); TypeMapRemoveType(objectInfo.performing, type);
} }
function ObjectInfoGetPerformingTypes(objectInfo) { function ObjectInfoGetPerformingTypes(objectInfo) {
return objectInfo.performingCount > 0 ? objectInfo.performing : null; return objectInfo.performingCount > 0 ? objectInfo.performing : null;
} }
function ConvertAcceptListToTypeMap(arg) { function ConvertAcceptListToTypeMap(arg) {
// We use undefined as a sentinel for the default accept list. // We use undefined as a sentinel for the default accept list.
if (IS_UNDEFINED(arg)) if (IS_UNDEFINED(arg))
@ -279,6 +324,7 @@ function ConvertAcceptListToTypeMap(arg) {
return TypeMapCreateFromList(arg, len); return TypeMapCreateFromList(arg, len);
} }
// CallbackInfo's optimized state is just a number which represents its global // CallbackInfo's optimized state is just a number which represents its global
// priority. When a change record must be enqueued for the callback, it // priority. When a change record must be enqueued for the callback, it
// normalizes. When delivery clears any pending change records, it re-optimizes. // normalizes. When delivery clears any pending change records, it re-optimizes.
@ -286,11 +332,13 @@ function CallbackInfoGet(callback) {
return %WeakCollectionGet(GetObservationStateJS().callbackInfoMap, callback); return %WeakCollectionGet(GetObservationStateJS().callbackInfoMap, callback);
} }
function CallbackInfoSet(callback, callbackInfo) { function CallbackInfoSet(callback, callbackInfo) {
%WeakCollectionSet(GetObservationStateJS().callbackInfoMap, %WeakCollectionSet(GetObservationStateJS().callbackInfoMap,
callback, callbackInfo); callback, callbackInfo);
} }
function CallbackInfoGetOrCreate(callback) { function CallbackInfoGetOrCreate(callback) {
var callbackInfo = CallbackInfoGet(callback); var callbackInfo = CallbackInfoGet(callback);
if (!IS_UNDEFINED(callbackInfo)) if (!IS_UNDEFINED(callbackInfo))
@ -301,6 +349,7 @@ function CallbackInfoGetOrCreate(callback) {
return priority; return priority;
} }
function CallbackInfoGetPriority(callbackInfo) { function CallbackInfoGetPriority(callbackInfo) {
if (IS_NUMBER(callbackInfo)) if (IS_NUMBER(callbackInfo))
return callbackInfo; return callbackInfo;
@ -308,6 +357,7 @@ function CallbackInfoGetPriority(callbackInfo) {
return callbackInfo.priority; return callbackInfo.priority;
} }
function CallbackInfoNormalize(callback) { function CallbackInfoNormalize(callback) {
var callbackInfo = CallbackInfoGet(callback); var callbackInfo = CallbackInfoGet(callback);
if (IS_NUMBER(callbackInfo)) { if (IS_NUMBER(callbackInfo)) {
@ -319,6 +369,7 @@ function CallbackInfoNormalize(callback) {
return callbackInfo; return callbackInfo;
} }
function ObjectObserve(object, callback, acceptList) { function ObjectObserve(object, callback, acceptList) {
if (!IS_SPEC_OBJECT(object)) if (!IS_SPEC_OBJECT(object))
throw MakeTypeError("observe_non_object", ["observe"]); throw MakeTypeError("observe_non_object", ["observe"]);
@ -333,6 +384,7 @@ function ObjectObserve(object, callback, acceptList) {
return objectObserveFn(object, callback, acceptList); return objectObserveFn(object, callback, acceptList);
} }
function NativeObjectObserve(object, callback, acceptList) { function NativeObjectObserve(object, callback, acceptList) {
var objectInfo = ObjectInfoGetOrCreate(object); var objectInfo = ObjectInfoGetOrCreate(object);
var typeList = ConvertAcceptListToTypeMap(acceptList); var typeList = ConvertAcceptListToTypeMap(acceptList);
@ -340,6 +392,7 @@ function NativeObjectObserve(object, callback, acceptList) {
return object; return object;
} }
function ObjectUnobserve(object, callback) { function ObjectUnobserve(object, callback) {
if (!IS_SPEC_OBJECT(object)) if (!IS_SPEC_OBJECT(object))
throw MakeTypeError("observe_non_object", ["unobserve"]); throw MakeTypeError("observe_non_object", ["unobserve"]);
@ -356,6 +409,7 @@ function ObjectUnobserve(object, callback) {
return object; return object;
} }
function ArrayObserve(object, callback) { function ArrayObserve(object, callback) {
return ObjectObserve(object, callback, ['add', return ObjectObserve(object, callback, ['add',
'update', 'update',
@ -363,10 +417,12 @@ function ArrayObserve(object, callback) {
'splice']); 'splice']);
} }
function ArrayUnobserve(object, callback) { function ArrayUnobserve(object, callback) {
return ObjectUnobserve(object, callback); return ObjectUnobserve(object, callback);
} }
function ObserverEnqueueIfActive(observer, objectInfo, changeRecord) { function ObserverEnqueueIfActive(observer, objectInfo, changeRecord) {
if (!ObserverIsActive(observer, objectInfo) || if (!ObserverIsActive(observer, objectInfo) ||
!TypeMapHasType(ObserverGetAcceptTypes(observer), changeRecord.type)) { !TypeMapHasType(ObserverGetAcceptTypes(observer), changeRecord.type)) {
@ -399,6 +455,7 @@ function ObserverEnqueueIfActive(observer, objectInfo, changeRecord) {
callbackInfo.push(changeRecord); callbackInfo.push(changeRecord);
} }
function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) { function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) {
if (!ObjectInfoHasActiveObservers(objectInfo)) if (!ObjectInfoHasActiveObservers(objectInfo))
return; return;
@ -418,6 +475,7 @@ function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) {
ObjectInfoEnqueueInternalChangeRecord(objectInfo, newRecord); ObjectInfoEnqueueInternalChangeRecord(objectInfo, newRecord);
} }
function ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord) { function ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord) {
// TODO(rossberg): adjust once there is a story for symbols vs proxies. // TODO(rossberg): adjust once there is a story for symbols vs proxies.
if (IS_SYMBOL(changeRecord.name)) return; if (IS_SYMBOL(changeRecord.name)) return;
@ -436,18 +494,21 @@ function ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord) {
} }
} }
function BeginPerformSplice(array) { function BeginPerformSplice(array) {
var objectInfo = ObjectInfoGet(array); var objectInfo = ObjectInfoGet(array);
if (!IS_UNDEFINED(objectInfo)) if (!IS_UNDEFINED(objectInfo))
ObjectInfoAddPerformingType(objectInfo, 'splice'); ObjectInfoAddPerformingType(objectInfo, 'splice');
} }
function EndPerformSplice(array) { function EndPerformSplice(array) {
var objectInfo = ObjectInfoGet(array); var objectInfo = ObjectInfoGet(array);
if (!IS_UNDEFINED(objectInfo)) if (!IS_UNDEFINED(objectInfo))
ObjectInfoRemovePerformingType(objectInfo, 'splice'); ObjectInfoRemovePerformingType(objectInfo, 'splice');
} }
function EnqueueSpliceRecord(array, index, removed, addedCount) { function EnqueueSpliceRecord(array, index, removed, addedCount) {
var objectInfo = ObjectInfoGet(array); var objectInfo = ObjectInfoGet(array);
if (!ObjectInfoHasActiveObservers(objectInfo)) if (!ObjectInfoHasActiveObservers(objectInfo))
@ -466,6 +527,7 @@ function EnqueueSpliceRecord(array, index, removed, addedCount) {
ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord); ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord);
} }
function NotifyChange(type, object, name, oldValue) { function NotifyChange(type, object, name, oldValue) {
var objectInfo = ObjectInfoGet(object); var objectInfo = ObjectInfoGet(object);
if (!ObjectInfoHasActiveObservers(objectInfo)) if (!ObjectInfoHasActiveObservers(objectInfo))
@ -489,7 +551,6 @@ function NotifyChange(type, object, name, oldValue) {
ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord); ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord);
} }
var notifierPrototype = {};
function ObjectNotifierNotify(changeRecord) { function ObjectNotifierNotify(changeRecord) {
if (!IS_SPEC_OBJECT(this)) if (!IS_SPEC_OBJECT(this))
@ -504,6 +565,7 @@ function ObjectNotifierNotify(changeRecord) {
ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord); ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord);
} }
function ObjectNotifierPerformChange(changeType, changeFn) { function ObjectNotifierPerformChange(changeType, changeFn) {
if (!IS_SPEC_OBJECT(this)) if (!IS_SPEC_OBJECT(this))
throw MakeTypeError("called_on_non_object", ["performChange"]); throw MakeTypeError("called_on_non_object", ["performChange"]);
@ -520,6 +582,7 @@ function ObjectNotifierPerformChange(changeType, changeFn) {
performChangeFn(objectInfo, changeType, changeFn); performChangeFn(objectInfo, changeType, changeFn);
} }
function NativeObjectNotifierPerformChange(objectInfo, changeType, changeFn) { function NativeObjectNotifierPerformChange(objectInfo, changeType, changeFn) {
ObjectInfoAddPerformingType(objectInfo, changeType); ObjectInfoAddPerformingType(objectInfo, changeType);
@ -534,6 +597,7 @@ function NativeObjectNotifierPerformChange(objectInfo, changeType, changeFn) {
ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, changeType); ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, changeType);
} }
function ObjectGetNotifier(object) { function ObjectGetNotifier(object) {
if (!IS_SPEC_OBJECT(object)) if (!IS_SPEC_OBJECT(object))
throw MakeTypeError("observe_non_object", ["getNotifier"]); throw MakeTypeError("observe_non_object", ["getNotifier"]);
@ -548,11 +612,13 @@ function ObjectGetNotifier(object) {
return getNotifierFn(object); return getNotifierFn(object);
} }
function NativeObjectGetNotifier(object) { function NativeObjectGetNotifier(object) {
var objectInfo = ObjectInfoGetOrCreate(object); var objectInfo = ObjectInfoGetOrCreate(object);
return ObjectInfoGetNotifier(objectInfo); return ObjectInfoGetNotifier(objectInfo);
} }
function CallbackDeliverPending(callback) { function CallbackDeliverPending(callback) {
var callbackInfo = CallbackInfoGet(callback); var callbackInfo = CallbackInfoGet(callback);
if (IS_UNDEFINED(callbackInfo) || IS_NUMBER(callbackInfo)) if (IS_UNDEFINED(callbackInfo) || IS_NUMBER(callbackInfo))
@ -575,6 +641,7 @@ function CallbackDeliverPending(callback) {
return true; return true;
} }
function ObjectDeliverChangeRecords(callback) { function ObjectDeliverChangeRecords(callback) {
if (!IS_SPEC_FUNCTION(callback)) if (!IS_SPEC_FUNCTION(callback))
throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]); throw MakeTypeError("observe_non_function", ["deliverChangeRecords"]);
@ -582,6 +649,7 @@ function ObjectDeliverChangeRecords(callback) {
while (CallbackDeliverPending(callback)) {} while (CallbackDeliverPending(callback)) {}
} }
function ObserveMicrotaskRunner() { function ObserveMicrotaskRunner() {
var pendingObservers = GetPendingObservers(); var pendingObservers = GetPendingObservers();
if (!IS_NULL(pendingObservers)) { if (!IS_NULL(pendingObservers)) {
@ -592,22 +660,29 @@ function ObserveMicrotaskRunner() {
} }
} }
function SetupObjectObserve() { // -------------------------------------------------------------------
%CheckIsBootstrapping();
InstallFunctions($Object, DONT_ENUM, [
"deliverChangeRecords", ObjectDeliverChangeRecords,
"getNotifier", ObjectGetNotifier,
"observe", ObjectObserve,
"unobserve", ObjectUnobserve
]);
InstallFunctions($Array, DONT_ENUM, [
"observe", ArrayObserve,
"unobserve", ArrayUnobserve
]);
InstallFunctions(notifierPrototype, DONT_ENUM, [
"notify", ObjectNotifierNotify,
"performChange", ObjectNotifierPerformChange
]);
}
SetupObjectObserve(); InstallFunctions(GlobalObject, DONT_ENUM, [
"deliverChangeRecords", ObjectDeliverChangeRecords,
"getNotifier", ObjectGetNotifier,
"observe", ObjectObserve,
"unobserve", ObjectUnobserve
]);
InstallFunctions(GlobalArray, DONT_ENUM, [
"observe", ArrayObserve,
"unobserve", ArrayUnobserve
]);
InstallFunctions(notifierPrototype, DONT_ENUM, [
"notify", ObjectNotifierNotify,
"performChange", ObjectNotifierPerformChange
]);
$observeNotifyChange = NotifyChange;
$observeEnqueueSpliceRecord = EnqueueSpliceRecord;
$observeBeginPerformSplice = BeginPerformSplice;
$observeEndPerformSplice = EndPerformSplice;
$observeNativeObjectObserve = NativeObjectObserve;
$observeNativeObjectGetNotifier = NativeObjectGetNotifier;
$observeNativeObjectNotifierPerformChange = NativeObjectNotifierPerformChange;
})();

View File

@ -909,7 +909,7 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
var emit_splice = %IsObserved(obj) && new_length !== old_length; var emit_splice = %IsObserved(obj) && new_length !== old_length;
var removed; var removed;
if (emit_splice) { if (emit_splice) {
BeginPerformSplice(obj); $observeBeginPerformSplice(obj);
removed = []; removed = [];
if (new_length < old_length) if (new_length < old_length)
removed.length = old_length - new_length; removed.length = old_length - new_length;
@ -930,8 +930,8 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
} }
threw = !DefineObjectProperty(obj, "length", desc, should_throw) || threw; threw = !DefineObjectProperty(obj, "length", desc, should_throw) || threw;
if (emit_splice) { if (emit_splice) {
EndPerformSplice(obj); $observeEndPerformSplice(obj);
EnqueueSpliceRecord(obj, $observeEnqueueSpliceRecord(obj,
new_length < old_length ? new_length : old_length, new_length < old_length ? new_length : old_length,
removed, removed,
new_length > old_length ? new_length - old_length : 0); new_length > old_length ? new_length - old_length : 0);
@ -954,14 +954,14 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
var length = obj.length; var length = obj.length;
if (index >= length && %IsObserved(obj)) { if (index >= length && %IsObserved(obj)) {
emit_splice = true; emit_splice = true;
BeginPerformSplice(obj); $observeBeginPerformSplice(obj);
} }
var length_desc = GetOwnPropertyJS(obj, "length"); var length_desc = GetOwnPropertyJS(obj, "length");
if ((index >= length && !length_desc.isWritable()) || if ((index >= length && !length_desc.isWritable()) ||
!DefineObjectProperty(obj, p, desc, true)) { !DefineObjectProperty(obj, p, desc, true)) {
if (emit_splice) if (emit_splice)
EndPerformSplice(obj); $observeEndPerformSplice(obj);
if (should_throw) { if (should_throw) {
throw MakeTypeError("define_disallowed", [p]); throw MakeTypeError("define_disallowed", [p]);
} else { } else {
@ -972,8 +972,8 @@ function DefineArrayProperty(obj, p, desc, should_throw) {
obj.length = index + 1; obj.length = index + 1;
} }
if (emit_splice) { if (emit_splice) {
EndPerformSplice(obj); $observeEndPerformSplice(obj);
EnqueueSpliceRecord(obj, length, [], index + 1 - length); $observeEnqueueSpliceRecord(obj, length, [], index + 1 - length);
} }
return true; return true;
} }