Commit 234ffb76 authored by adamk@chromium.org's avatar adamk@chromium.org

Various cleanup/simplification in object-observe.js

The biggest change is the removal of the map wrapper objects:
we now operate directly on the observation weak map, since there
are already Get/GetOrCreate/Set functions for each info map.
Various other small cleanups as well, including the deletion of
unnecessary forwarding functions and making use of standard macros.

This is a reland of r24972, retaining GetObservationStateJS() to
keep snapshotting working properly.

R=rossberg@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#24990}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24990 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent df312d90
...@@ -35,10 +35,15 @@ ...@@ -35,10 +35,15 @@
var observationState; var observationState;
// We have to wait until after bootstrapping to grab a reference to the
// observationState object, since it's not possible to serialize that
// reference into the snapshot.
function GetObservationStateJS() { function GetObservationStateJS() {
if (IS_UNDEFINED(observationState)) if (IS_UNDEFINED(observationState)) {
observationState = %GetObservationState(); observationState = %GetObservationState();
}
// TODO(adamk): Consider moving this code into heap.cc
if (IS_UNDEFINED(observationState.callbackInfoMap)) { if (IS_UNDEFINED(observationState.callbackInfoMap)) {
observationState.callbackInfoMap = %ObservationWeakMapCreate(); observationState.callbackInfoMap = %ObservationWeakMapCreate();
observationState.objectInfoMap = %ObservationWeakMapCreate(); observationState.objectInfoMap = %ObservationWeakMapCreate();
...@@ -51,55 +56,6 @@ function GetObservationStateJS() { ...@@ -51,55 +56,6 @@ function GetObservationStateJS() {
return observationState; return observationState;
} }
function GetWeakMapWrapper() {
function MapWrapper(map) {
this.map_ = map;
};
MapWrapper.prototype = {
__proto__: null,
get: function(key) {
return %WeakCollectionGet(this.map_, key);
},
set: function(key, value) {
%WeakCollectionSet(this.map_, key, value);
},
has: function(key) {
return !IS_UNDEFINED(this.get(key));
}
};
return MapWrapper;
}
var contextMaps;
function GetContextMaps() {
if (IS_UNDEFINED(contextMaps)) {
var map = GetWeakMapWrapper();
var observationState = GetObservationStateJS();
contextMaps = {
callbackInfoMap: new map(observationState.callbackInfoMap),
objectInfoMap: new map(observationState.objectInfoMap),
notifierObjectInfoMap: new map(observationState.notifierObjectInfoMap)
};
}
return contextMaps;
}
function GetCallbackInfoMap() {
return GetContextMaps().callbackInfoMap;
}
function GetObjectInfoMap() {
return GetContextMaps().objectInfoMap;
}
function GetNotifierObjectInfoMap() {
return GetContextMaps().notifierObjectInfoMap;
}
function GetPendingObservers() { function GetPendingObservers() {
return GetObservationStateJS().pendingObservers; return GetObservationStateJS().pendingObservers;
} }
...@@ -204,35 +160,34 @@ function ObjectInfoGetOrCreate(object) { ...@@ -204,35 +160,34 @@ function ObjectInfoGetOrCreate(object) {
performing: null, performing: null,
performingCount: 0, performingCount: 0,
}; };
GetObjectInfoMap().set(object, objectInfo); %WeakCollectionSet(GetObservationStateJS().objectInfoMap,
object, objectInfo);
} }
return objectInfo; return objectInfo;
} }
function ObjectInfoGet(object) { function ObjectInfoGet(object) {
return GetObjectInfoMap().get(object); return %WeakCollectionGet(GetObservationStateJS().objectInfoMap, object);
} }
function ObjectInfoGetFromNotifier(notifier) { function ObjectInfoGetFromNotifier(notifier) {
return GetNotifierObjectInfoMap().get(notifier); return %WeakCollectionGet(GetObservationStateJS().notifierObjectInfoMap,
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 };
GetNotifierObjectInfoMap().set(objectInfo.notifier, objectInfo); %WeakCollectionSet(GetObservationStateJS().notifierObjectInfoMap,
objectInfo.notifier, objectInfo);
} }
return objectInfo.notifier; return objectInfo.notifier;
} }
function ObjectInfoGetObject(objectInfo) {
return objectInfo.object;
}
function ChangeObserversIsOptimized(changeObservers) { function ChangeObserversIsOptimized(changeObservers) {
return typeof changeObservers === 'function' || return IS_SPEC_FUNCTION(changeObservers) ||
typeof changeObservers.callback === 'function'; 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
...@@ -328,16 +283,21 @@ function ConvertAcceptListToTypeMap(arg) { ...@@ -328,16 +283,21 @@ function ConvertAcceptListToTypeMap(arg) {
// 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.
function CallbackInfoGet(callback) { function CallbackInfoGet(callback) {
return GetCallbackInfoMap().get(callback); return %WeakCollectionGet(GetObservationStateJS().callbackInfoMap, callback);
}
function CallbackInfoSet(callback, callbackInfo) {
%WeakCollectionSet(GetObservationStateJS().callbackInfoMap,
callback, callbackInfo);
} }
function CallbackInfoGetOrCreate(callback) { function CallbackInfoGetOrCreate(callback) {
var callbackInfo = GetCallbackInfoMap().get(callback); var callbackInfo = CallbackInfoGet(callback);
if (!IS_UNDEFINED(callbackInfo)) if (!IS_UNDEFINED(callbackInfo))
return callbackInfo; return callbackInfo;
var priority = GetNextCallbackPriority(); var priority = GetNextCallbackPriority();
GetCallbackInfoMap().set(callback, priority); CallbackInfoSet(callback, priority);
return priority; return priority;
} }
...@@ -349,12 +309,12 @@ function CallbackInfoGetPriority(callbackInfo) { ...@@ -349,12 +309,12 @@ function CallbackInfoGetPriority(callbackInfo) {
} }
function CallbackInfoNormalize(callback) { function CallbackInfoNormalize(callback) {
var callbackInfo = GetCallbackInfoMap().get(callback); var callbackInfo = CallbackInfoGet(callback);
if (IS_NUMBER(callbackInfo)) { if (IS_NUMBER(callbackInfo)) {
var priority = callbackInfo; var priority = callbackInfo;
callbackInfo = new InternalArray; callbackInfo = new InternalArray;
callbackInfo.priority = priority; callbackInfo.priority = priority;
GetCallbackInfoMap().set(callback, callbackInfo); CallbackInfoSet(callback, callbackInfo);
} }
return callbackInfo; return callbackInfo;
} }
...@@ -445,8 +405,8 @@ function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) { ...@@ -445,8 +405,8 @@ function ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord, type) {
var hasType = !IS_UNDEFINED(type); var hasType = !IS_UNDEFINED(type);
var newRecord = hasType ? var newRecord = hasType ?
{ object: ObjectInfoGetObject(objectInfo), type: type } : { object: objectInfo.object, type: type } :
{ object: ObjectInfoGetObject(objectInfo) }; { object: objectInfo.object };
for (var prop in changeRecord) { for (var prop in changeRecord) {
if (prop === 'object' || (hasType && prop === 'type')) continue; if (prop === 'object' || (hasType && prop === 'type')) continue;
...@@ -594,17 +554,18 @@ function NativeObjectGetNotifier(object) { ...@@ -594,17 +554,18 @@ function NativeObjectGetNotifier(object) {
} }
function CallbackDeliverPending(callback) { function CallbackDeliverPending(callback) {
var callbackInfo = GetCallbackInfoMap().get(callback); var callbackInfo = CallbackInfoGet(callback);
if (IS_UNDEFINED(callbackInfo) || IS_NUMBER(callbackInfo)) if (IS_UNDEFINED(callbackInfo) || IS_NUMBER(callbackInfo))
return false; return false;
// Clear the pending change records from callback and return it to its // Clear the pending change records from callback and return it to its
// "optimized" state. // "optimized" state.
var priority = callbackInfo.priority; var priority = callbackInfo.priority;
GetCallbackInfoMap().set(callback, priority); CallbackInfoSet(callback, priority);
if (GetPendingObservers()) var pendingObservers = GetPendingObservers();
delete GetPendingObservers()[priority]; if (!IS_NULL(pendingObservers))
delete pendingObservers[priority];
var delivered = []; var delivered = [];
%MoveArrayContents(callbackInfo, delivered); %MoveArrayContents(callbackInfo, delivered);
...@@ -624,7 +585,7 @@ function ObjectDeliverChangeRecords(callback) { ...@@ -624,7 +585,7 @@ function ObjectDeliverChangeRecords(callback) {
function ObserveMicrotaskRunner() { function ObserveMicrotaskRunner() {
var pendingObservers = GetPendingObservers(); var pendingObservers = GetPendingObservers();
if (pendingObservers) { if (!IS_NULL(pendingObservers)) {
SetPendingObservers(null); SetPendingObservers(null);
for (var i in pendingObservers) { for (var i in pendingObservers) {
CallbackDeliverPending(pendingObservers[i]); CallbackDeliverPending(pendingObservers[i]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment