2012-10-25 14:56:44 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
var $observeNotifyChange;
|
|
|
|
var $observeEnqueueSpliceRecord;
|
|
|
|
var $observeBeginPerformSplice;
|
|
|
|
var $observeEndPerformSplice;
|
|
|
|
var $observeNativeObjectObserve;
|
|
|
|
var $observeNativeObjectGetNotifier;
|
|
|
|
var $observeNativeObjectNotifierPerformChange;
|
|
|
|
|
2015-05-21 06:15:33 +00:00
|
|
|
(function(global, utils) {
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2012-10-25 14:56:44 +00:00
|
|
|
"use strict";
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
%CheckIsBootstrapping();
|
|
|
|
|
2015-05-12 14:00:47 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
// Imports
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
var GlobalArray = global.Array;
|
|
|
|
var GlobalObject = global.Object;
|
2015-05-21 06:15:33 +00:00
|
|
|
var InternalArray = utils.InternalArray;
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2015-05-26 07:24:13 +00:00
|
|
|
var ObjectFreeze;
|
|
|
|
var ObjectIsFrozen;
|
|
|
|
|
|
|
|
utils.Import(function(from) {
|
|
|
|
ObjectFreeze = from.ObjectFreeze;
|
|
|
|
ObjectIsFrozen = from.ObjectIsFrozen;
|
|
|
|
});
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
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.
|
|
|
|
|
2014-10-29 12:14:44 +00:00
|
|
|
var observationState;
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
var notifierPrototype = {};
|
|
|
|
|
2014-10-29 17:27:09 +00:00
|
|
|
// 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.
|
2014-10-29 12:14:44 +00:00
|
|
|
function GetObservationStateJS() {
|
2014-10-29 17:27:09 +00:00
|
|
|
if (IS_UNDEFINED(observationState)) {
|
2014-10-29 12:14:44 +00:00
|
|
|
observationState = %GetObservationState();
|
2014-10-29 17:27:09 +00:00
|
|
|
}
|
2014-10-29 12:14:44 +00:00
|
|
|
|
2014-10-29 17:27:09 +00:00
|
|
|
// TODO(adamk): Consider moving this code into heap.cc
|
2014-10-29 12:14:44 +00:00
|
|
|
if (IS_UNDEFINED(observationState.callbackInfoMap)) {
|
|
|
|
observationState.callbackInfoMap = %ObservationWeakMapCreate();
|
|
|
|
observationState.objectInfoMap = %ObservationWeakMapCreate();
|
|
|
|
observationState.notifierObjectInfoMap = %ObservationWeakMapCreate();
|
|
|
|
observationState.pendingObservers = null;
|
|
|
|
observationState.nextCallbackPriority = 0;
|
|
|
|
observationState.lastMicrotaskId = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return observationState;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-03-13 00:20:06 +00:00
|
|
|
function GetPendingObservers() {
|
2014-10-29 12:14:44 +00:00
|
|
|
return GetObservationStateJS().pendingObservers;
|
2014-03-13 00:20:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-03-13 00:20:06 +00:00
|
|
|
function SetPendingObservers(pendingObservers) {
|
2014-10-29 12:14:44 +00:00
|
|
|
GetObservationStateJS().pendingObservers = pendingObservers;
|
2014-03-13 00:20:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-03-13 00:20:06 +00:00
|
|
|
function GetNextCallbackPriority() {
|
2014-10-29 12:14:44 +00:00
|
|
|
return GetObservationStateJS().nextCallbackPriority++;
|
2014-03-13 00:20:06 +00:00
|
|
|
}
|
2013-09-10 18:13:54 +00:00
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function TypeMapCreate() {
|
|
|
|
return nullProtoObject();
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function TypeMapAddType(typeMap, type, ignoreDuplicate) {
|
|
|
|
typeMap[type] = ignoreDuplicate ? 1 : (typeMap[type] || 0) + 1;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function TypeMapRemoveType(typeMap, type) {
|
|
|
|
typeMap[type]--;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-09 18:22:28 +00:00
|
|
|
function TypeMapCreateFromList(typeList, length) {
|
2013-09-10 18:13:54 +00:00
|
|
|
var typeMap = TypeMapCreate();
|
2014-05-09 18:22:28 +00:00
|
|
|
for (var i = 0; i < length; i++) {
|
2013-09-10 18:13:54 +00:00
|
|
|
TypeMapAddType(typeMap, typeList[i], true);
|
|
|
|
}
|
|
|
|
return typeMap;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function TypeMapHasType(typeMap, type) {
|
|
|
|
return !!typeMap[type];
|
|
|
|
}
|
2013-09-04 19:21:30 +00:00
|
|
|
|
2015-04-17 14:10:05 +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;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-09 18:22:28 +00:00
|
|
|
var defaultAcceptTypes = (function() {
|
|
|
|
var defaultTypes = [
|
|
|
|
'add',
|
|
|
|
'update',
|
|
|
|
'delete',
|
|
|
|
'setPrototype',
|
|
|
|
'reconfigure',
|
|
|
|
'preventExtensions'
|
|
|
|
];
|
|
|
|
return TypeMapCreateFromList(defaultTypes, defaultTypes.length);
|
|
|
|
})();
|
2013-09-10 18:13:54 +00:00
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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;
|
2014-05-09 18:22:28 +00:00
|
|
|
observer.accept = acceptList;
|
2014-01-08 20:25:08 +00:00
|
|
|
return observer;
|
2013-09-10 18:13:54 +00:00
|
|
|
}
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
|
2015-04-17 14:10:05 +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)) {
|
2014-10-15 13:26:43 +00:00
|
|
|
if (!%_IsJSProxy(object)) {
|
2013-09-10 18:13:54 +00:00
|
|
|
%SetIsObserved(object);
|
2014-10-15 13:26:43 +00:00
|
|
|
}
|
2013-09-10 18:13:54 +00:00
|
|
|
objectInfo = {
|
|
|
|
object: object,
|
|
|
|
changeObservers: null,
|
|
|
|
notifier: null,
|
|
|
|
performing: null,
|
|
|
|
performingCount: 0,
|
|
|
|
};
|
2014-10-29 17:27:09 +00:00
|
|
|
%WeakCollectionSet(GetObservationStateJS().objectInfoMap,
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
object, objectInfo, $getHash(object));
|
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
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-01-08 20:25:08 +00:00
|
|
|
function ObjectInfoGet(object) {
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
return %WeakCollectionGet(GetObservationStateJS().objectInfoMap, object,
|
|
|
|
$getHash(object));
|
2014-01-08 20:25:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoGetFromNotifier(notifier) {
|
2014-10-29 17:27:09 +00:00
|
|
|
return %WeakCollectionGet(GetObservationStateJS().notifierObjectInfoMap,
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
notifier, $getHash(notifier));
|
2013-09-04 19:21:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoGetNotifier(objectInfo) {
|
|
|
|
if (IS_NULL(objectInfo.notifier)) {
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
var notifier = { __proto__: notifierPrototype };
|
|
|
|
objectInfo.notifier = notifier;
|
2014-10-29 17:27:09 +00:00
|
|
|
%WeakCollectionSet(GetObservationStateJS().notifierObjectInfoMap,
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
notifier, objectInfo, $getHash(notifier));
|
2013-09-10 18:13:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return objectInfo.notifier;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ChangeObserversIsOptimized(changeObservers) {
|
2014-10-29 17:27:09 +00:00
|
|
|
return IS_SPEC_FUNCTION(changeObservers) ||
|
|
|
|
IS_SPEC_FUNCTION(changeObservers.callback);
|
2013-09-10 18:13:54 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function ObjectInfoAddPerformingType(objectInfo, type) {
|
|
|
|
objectInfo.performing = objectInfo.performing || TypeMapCreate();
|
|
|
|
TypeMapAddType(objectInfo.performing, type);
|
2013-05-15 22:09:40 +00:00
|
|
|
objectInfo.performingCount++;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-09 18:22:28 +00:00
|
|
|
function ConvertAcceptListToTypeMap(arg) {
|
|
|
|
// We use undefined as a sentinel for the default accept list.
|
2013-05-15 22:09:40 +00:00
|
|
|
if (IS_UNDEFINED(arg))
|
2014-05-09 18:22:28 +00:00
|
|
|
return arg;
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2015-05-05 07:57:37 +00:00
|
|
|
if (!IS_SPEC_OBJECT(arg)) throw MakeTypeError(kObserveInvalidAccept);
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2015-05-07 08:39:53 +00:00
|
|
|
var len = $toInteger(arg.length);
|
2014-05-09 18:22:28 +00:00
|
|
|
if (len < 0) len = 0;
|
|
|
|
|
|
|
|
return TypeMapCreateFromList(arg, len);
|
2013-05-15 22:09:40 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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) {
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
return %WeakCollectionGet(GetObservationStateJS().callbackInfoMap, callback,
|
|
|
|
$getHash(callback));
|
2014-10-29 17:27:09 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-10-29 17:27:09 +00:00
|
|
|
function CallbackInfoSet(callback, callbackInfo) {
|
|
|
|
%WeakCollectionSet(GetObservationStateJS().callbackInfoMap,
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
callback, callbackInfo, $getHash(callback));
|
2013-07-15 22:16:30 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function CallbackInfoGetOrCreate(callback) {
|
2014-10-29 17:27:09 +00:00
|
|
|
var callbackInfo = CallbackInfoGet(callback);
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!IS_UNDEFINED(callbackInfo))
|
|
|
|
return callbackInfo;
|
|
|
|
|
2014-10-29 17:27:09 +00:00
|
|
|
var priority = GetNextCallbackPriority();
|
|
|
|
CallbackInfoSet(callback, priority);
|
2013-09-10 18:13:54 +00:00
|
|
|
return priority;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function CallbackInfoGetPriority(callbackInfo) {
|
|
|
|
if (IS_NUMBER(callbackInfo))
|
|
|
|
return callbackInfo;
|
|
|
|
else
|
|
|
|
return callbackInfo.priority;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
function CallbackInfoNormalize(callback) {
|
2014-10-29 17:27:09 +00:00
|
|
|
var callbackInfo = CallbackInfoGet(callback);
|
2013-07-15 22:16:30 +00:00
|
|
|
if (IS_NUMBER(callbackInfo)) {
|
|
|
|
var priority = callbackInfo;
|
|
|
|
callbackInfo = new InternalArray;
|
|
|
|
callbackInfo.priority = priority;
|
2014-10-29 17:27:09 +00:00
|
|
|
CallbackInfoSet(callback, callbackInfo);
|
2013-07-15 22:16:30 +00:00
|
|
|
}
|
|
|
|
return callbackInfo;
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonObject, "observe", "observe");
|
2014-05-02 13:55:11 +00:00
|
|
|
if (%IsJSGlobalProxy(object))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveGlobalProxy, "observe");
|
2012-10-25 14:56:44 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonFunction, "observe");
|
2015-05-26 07:24:13 +00:00
|
|
|
if (ObjectIsFrozen(callback))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveCallbackFrozen);
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2014-05-28 19:13:41 +00:00
|
|
|
var objectObserveFn = %GetObjectContextObjectObserve(object);
|
|
|
|
return objectObserveFn(object, callback, acceptList);
|
2014-05-02 16:13:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-02 16:13:10 +00:00
|
|
|
function NativeObjectObserve(object, callback, acceptList) {
|
2014-01-08 20:25:08 +00:00
|
|
|
var objectInfo = ObjectInfoGetOrCreate(object);
|
2014-05-09 18:22:28 +00:00
|
|
|
var typeList = ConvertAcceptListToTypeMap(acceptList);
|
|
|
|
ObjectInfoAddObserver(objectInfo, callback, typeList);
|
2012-11-20 10:38:14 +00:00
|
|
|
return object;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2012-10-25 14:56:44 +00:00
|
|
|
function ObjectUnobserve(object, callback) {
|
|
|
|
if (!IS_SPEC_OBJECT(object))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonObject, "unobserve", "unobserve");
|
2014-05-02 13:55:11 +00:00
|
|
|
if (%IsJSGlobalProxy(object))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveGlobalProxy, "unobserve");
|
2012-11-13 15:50:59 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonFunction, "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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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']);
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-05-16 11:19:37 +00:00
|
|
|
function ArrayUnobserve(object, callback) {
|
|
|
|
return ObjectUnobserve(object, callback);
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-02 13:55:11 +00:00
|
|
|
function ObserverEnqueueIfActive(observer, objectInfo, changeRecord) {
|
2013-09-10 18:13:54 +00:00
|
|
|
if (!ObserverIsActive(observer, objectInfo) ||
|
|
|
|
!TypeMapHasType(ObserverGetAcceptTypes(observer), changeRecord.type)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var callback = ObserverGetCallback(observer);
|
2014-05-02 13:55:11 +00:00
|
|
|
if (!%ObserverObjectAndRecordHaveSameOrigin(callback, changeRecord.object,
|
|
|
|
changeRecord)) {
|
2013-09-11 20:03:54 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var callbackInfo = CallbackInfoNormalize(callback);
|
2014-03-13 00:20:06 +00:00
|
|
|
if (IS_NULL(GetPendingObservers())) {
|
2014-05-19 07:57:04 +00:00
|
|
|
SetPendingObservers(nullProtoObject());
|
2014-07-03 15:56:01 +00:00
|
|
|
if (DEBUG_IS_ACTIVE) {
|
2014-10-29 12:14:44 +00:00
|
|
|
var id = ++GetObservationStateJS().lastMicrotaskId;
|
2014-07-03 15:56:01 +00:00
|
|
|
var name = "Object.observe";
|
|
|
|
%EnqueueMicrotask(function() {
|
|
|
|
%DebugAsyncTaskEvent({ type: "willHandle", id: id, name: name });
|
|
|
|
ObserveMicrotaskRunner();
|
|
|
|
%DebugAsyncTaskEvent({ type: "didHandle", id: id, name: name });
|
|
|
|
});
|
|
|
|
%DebugAsyncTaskEvent({ type: "enqueue", id: id, name: name });
|
|
|
|
} else {
|
|
|
|
%EnqueueMicrotask(ObserveMicrotaskRunner);
|
|
|
|
}
|
2014-02-12 22:04:19 +00:00
|
|
|
}
|
2014-03-13 00:20:06 +00:00
|
|
|
GetPendingObservers()[callbackInfo.priority] = callback;
|
2013-07-15 22:16:30 +00:00
|
|
|
callbackInfo.push(changeRecord);
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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 ?
|
2014-10-29 17:27:09 +00:00
|
|
|
{ object: objectInfo.object, type: type } :
|
|
|
|
{ object: objectInfo.object };
|
2013-11-05 11:23:08 +00:00
|
|
|
|
|
|
|
for (var prop in changeRecord) {
|
|
|
|
if (prop === 'object' || (hasType && prop === 'type')) continue;
|
2014-07-14 14:05:30 +00:00
|
|
|
%DefineDataPropertyUnchecked(
|
|
|
|
newRecord, prop, changeRecord[prop], READ_ONLY + DONT_DELETE);
|
2013-11-05 11:23:08 +00:00
|
|
|
}
|
2015-05-26 07:24:13 +00:00
|
|
|
ObjectFreeze(newRecord);
|
2013-11-05 11:23:08 +00:00
|
|
|
|
2014-05-02 13:55:11 +00:00
|
|
|
ObjectInfoEnqueueInternalChangeRecord(objectInfo, newRecord);
|
2013-11-05 11:23:08 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-02 13:55:11 +00:00
|
|
|
function ObjectInfoEnqueueInternalChangeRecord(objectInfo, changeRecord) {
|
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-10 18:13:54 +00:00
|
|
|
if (ChangeObserversIsOptimized(objectInfo.changeObservers)) {
|
|
|
|
var observer = objectInfo.changeObservers;
|
2014-05-02 13:55:11 +00:00
|
|
|
ObserverEnqueueIfActive(observer, objectInfo, changeRecord);
|
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;
|
2014-05-02 13:55:11 +00:00
|
|
|
ObserverEnqueueIfActive(observer, objectInfo, changeRecord);
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
|
};
|
|
|
|
|
2015-05-26 07:24:13 +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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2015-05-26 07:24:13 +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
|
|
|
|
|
|
|
function ObjectNotifierNotify(changeRecord) {
|
|
|
|
if (!IS_SPEC_OBJECT(this))
|
2015-04-21 09:03:24 +00:00
|
|
|
throw MakeTypeError(kCalledOnNonObject, "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))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNotifyNonNotifier);
|
2012-10-25 14:56:44 +00:00
|
|
|
if (!IS_STRING(changeRecord.type))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveTypeNonString);
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2013-11-05 11:23:08 +00:00
|
|
|
ObjectInfoEnqueueExternalChangeRecord(objectInfo, changeRecord);
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +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))
|
2015-04-21 09:03:24 +00:00
|
|
|
throw MakeTypeError(kCalledOnNonObject, "performChange");
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2013-09-10 18:13:54 +00:00
|
|
|
var objectInfo = ObjectInfoGetFromNotifier(this);
|
|
|
|
if (IS_UNDEFINED(objectInfo))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNotifyNonNotifier);
|
2013-05-15 22:09:40 +00:00
|
|
|
if (!IS_STRING(changeType))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObservePerformNonString);
|
2013-05-15 22:09:40 +00:00
|
|
|
if (!IS_SPEC_FUNCTION(changeFn))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObservePerformNonFunction);
|
2013-05-15 22:09:40 +00:00
|
|
|
|
2014-05-28 19:13:41 +00:00
|
|
|
var performChangeFn = %GetObjectContextNotifierPerformChange(objectInfo);
|
|
|
|
performChangeFn(objectInfo, changeType, changeFn);
|
2014-05-02 16:13:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-02 16:13:10 +00:00
|
|
|
function NativeObjectNotifierPerformChange(objectInfo, changeType, changeFn) {
|
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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2012-11-09 16:14:42 +00:00
|
|
|
function ObjectGetNotifier(object) {
|
|
|
|
if (!IS_SPEC_OBJECT(object))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonObject, "getNotifier", "getNotifier");
|
2014-05-02 13:55:11 +00:00
|
|
|
if (%IsJSGlobalProxy(object))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveGlobalProxy, "getNotifier");
|
2012-11-09 16:14:42 +00:00
|
|
|
|
2015-05-26 07:24:13 +00:00
|
|
|
if (ObjectIsFrozen(object)) return null;
|
2012-11-09 16:14:42 +00:00
|
|
|
|
2014-05-02 13:55:11 +00:00
|
|
|
if (!%ObjectWasCreatedInCurrentOrigin(object)) return null;
|
|
|
|
|
2014-05-28 19:13:41 +00:00
|
|
|
var getNotifierFn = %GetObjectContextObjectGetNotifier(object);
|
|
|
|
return getNotifierFn(object);
|
2014-05-02 16:13:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2014-05-02 16:13:10 +00:00
|
|
|
function NativeObjectGetNotifier(object) {
|
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
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
function CallbackDeliverPending(callback) {
|
2014-10-29 17:27:09 +00:00
|
|
|
var callbackInfo = CallbackInfoGet(callback);
|
2013-07-15 22:16:30 +00:00
|
|
|
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;
|
2014-10-29 17:27:09 +00:00
|
|
|
CallbackInfoSet(callback, priority);
|
2012-10-25 14:56:44 +00:00
|
|
|
|
2014-10-29 17:27:09 +00:00
|
|
|
var pendingObservers = GetPendingObservers();
|
|
|
|
if (!IS_NULL(pendingObservers))
|
|
|
|
delete pendingObservers[priority];
|
2013-09-10 18:13:54 +00:00
|
|
|
|
2014-11-05 07:23:28 +00:00
|
|
|
// TODO: combine the following runtime calls for perf optimization.
|
2012-10-25 14:56:44 +00:00
|
|
|
var delivered = [];
|
2013-07-15 22:16:30 +00:00
|
|
|
%MoveArrayContents(callbackInfo, delivered);
|
2014-11-05 07:23:28 +00:00
|
|
|
%DeliverObservationChangeRecords(callback, delivered);
|
2013-07-15 22:16:30 +00:00
|
|
|
|
2012-12-19 09:51:46 +00:00
|
|
|
return true;
|
2012-10-25 14:56:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2012-11-08 13:44:59 +00:00
|
|
|
function ObjectDeliverChangeRecords(callback) {
|
|
|
|
if (!IS_SPEC_FUNCTION(callback))
|
2015-05-05 07:57:37 +00:00
|
|
|
throw MakeTypeError(kObserveNonFunction, "deliverChangeRecords");
|
2012-11-08 13:44:59 +00:00
|
|
|
|
2013-07-15 22:16:30 +00:00
|
|
|
while (CallbackDeliverPending(callback)) {}
|
2012-11-08 13:44:59 +00:00
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
|
2013-11-27 17:21:40 +00:00
|
|
|
function ObserveMicrotaskRunner() {
|
2014-03-13 00:20:06 +00:00
|
|
|
var pendingObservers = GetPendingObservers();
|
2014-10-29 17:27:09 +00:00
|
|
|
if (!IS_NULL(pendingObservers)) {
|
2014-03-13 00:20:06 +00:00
|
|
|
SetPendingObservers(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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-17 14:10:05 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2015-05-26 07:24:13 +00:00
|
|
|
utils.InstallFunctions(GlobalObject, DONT_ENUM, [
|
2015-04-17 14:10:05 +00:00
|
|
|
"deliverChangeRecords", ObjectDeliverChangeRecords,
|
|
|
|
"getNotifier", ObjectGetNotifier,
|
|
|
|
"observe", ObjectObserve,
|
|
|
|
"unobserve", ObjectUnobserve
|
|
|
|
]);
|
2015-05-26 07:24:13 +00:00
|
|
|
utils.InstallFunctions(GlobalArray, DONT_ENUM, [
|
2015-04-17 14:10:05 +00:00
|
|
|
"observe", ArrayObserve,
|
|
|
|
"unobserve", ArrayUnobserve
|
|
|
|
]);
|
2015-05-26 07:24:13 +00:00
|
|
|
utils.InstallFunctions(notifierPrototype, DONT_ENUM, [
|
2015-04-17 14:10:05 +00:00
|
|
|
"notify", ObjectNotifierNotify,
|
|
|
|
"performChange", ObjectNotifierPerformChange
|
|
|
|
]);
|
|
|
|
|
|
|
|
$observeNotifyChange = NotifyChange;
|
|
|
|
$observeEnqueueSpliceRecord = EnqueueSpliceRecord;
|
|
|
|
$observeBeginPerformSplice = BeginPerformSplice;
|
|
|
|
$observeEndPerformSplice = EndPerformSplice;
|
|
|
|
$observeNativeObjectObserve = NativeObjectObserve;
|
|
|
|
$observeNativeObjectGetNotifier = NativeObjectGetNotifier;
|
|
|
|
$observeNativeObjectNotifierPerformChange = NativeObjectNotifierPerformChange;
|
|
|
|
|
2015-05-11 08:14:54 +00:00
|
|
|
})
|