sku-pg
Version:
Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!
345 lines (262 loc) • 9.64 kB
JavaScript
// Copyright 2005 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Helper object to manage the event system pools. This should not
* be used by itself and there should be no reason for you to depend on this
* library.
*
* JScript 5.6 has some serious issues with GC so we use object pools to reduce
* the number of object allocations.
*
*
*
*/
goog.provide('goog.events.pools');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.Listener');
goog.require('goog.structs.SimplePool');
goog.require('goog.userAgent.jscript');
/**
* Helper function for returning an object that is used for the lookup trees.
* This might use an object pool depending on the script engine.
* @return { {count_: number, remaining_: number} } A new or reused object.
*/
goog.events.pools.getObject;
/**
* Helper function for releasing an object that was returned by
* {@code goog.events.pools.getObject}. In case an object pool was used the
* object is returned to the pool.
* @param { {count_: number, remaining_: number} } obj The object to release.
*/
goog.events.pools.releaseObject;
/**
* Helper function for returning an array.
* This might use an object pool depending on the script engine.
* @return {Array} A new or reused array.
*/
goog.events.pools.getArray;
/**
* Helper function for releasing an array that was returned by
* {@code goog.events.pools.getArray}. In case an object pool was used the
* array is returned to the pool.
* @param {Array} arr The array to release.
*/
goog.events.pools.releaseArray;
/**
* Helper function for returning a proxy function as needed by
* {@code goog.events}. This might use an object pool depending on the script
* engine.
* @return {Function} A new or reused function object.
*/
goog.events.pools.getProxy;
/**
* Sets the callback function to use in the proxy.
* @param {Function} cb The callback function to use.
*/
goog.events.pools.setProxyCallbackFunction;
/**
* Helper function for releasing a function that was returned by
* {@code goog.events.pools.getProxy}. In case an object pool was used the
* function is returned to the pool.
* @param {Function} f The function to release.
*/
goog.events.pools.releaseProxy;
/**
* Helper function for returning a listener object as needed by
* {@code goog.events}. This might use an object pool depending on the script
* engine.
* @return {goog.events.Listener} A new or reused listener object.
*/
goog.events.pools.getListener;
/**
* Helper function for releasing a listener object that was returned by
* {@code goog.events.pools.getListener}. In case an object pool was used the
* listener object is returned to the pool.
* @param {goog.events.Listener} listener The listener object to release.
*/
goog.events.pools.releaseListener;
/**
* Helper function for returning a {@code goog.events.BrowserEvent} object as
* needed by {@code goog.events}. This might use an object pool depending on the
* script engine.
* @return {!goog.events.BrowserEvent} A new or reused event object.
*/
goog.events.pools.getEvent;
/**
* Helper function for releasing a browser event object that was returned by
* {@code goog.events.pools.getEvent}. In case an object pool was used the
* browser event object is returned to the pool.
* @param {goog.events.BrowserEvent} event The event object to release.
*/
goog.events.pools.releaseEvent;
(function() {
var BAD_GC = goog.userAgent.jscript.HAS_JSCRIPT &&
!goog.userAgent.jscript.isVersion('5.7');
// These functions are shared between the pools' createObject functions and
// the non pooled versions.
function getObject() {
return {count_: 0, remaining_: 0};
}
function getArray() {
return [];
}
/**
* This gets set to {@code goog.events.handleBrowserEvent_} by events.js.
* @type {function(string, (Event|undefined))}
*/
var proxyCallbackFunction;
goog.events.pools.setProxyCallbackFunction = function(cb) {
proxyCallbackFunction = cb;
};
function getProxy() {
// Use a local var f to prevent one allocation.
var f = function(eventObject) {
return proxyCallbackFunction.call(f.src, f.key, eventObject);
};
return f;
}
function getListener() {
return new goog.events.Listener();
}
function getEvent() {
return new goog.events.BrowserEvent();
}
if (!BAD_GC) {
goog.events.pools.getObject = getObject;
goog.events.pools.releaseObject = goog.nullFunction;
goog.events.pools.getArray = getArray;
goog.events.pools.releaseArray = goog.nullFunction;
goog.events.pools.getProxy = getProxy;
goog.events.pools.releaseProxy = goog.nullFunction;
goog.events.pools.getListener = getListener;
goog.events.pools.releaseListener = goog.nullFunction;
goog.events.pools.getEvent = getEvent;
goog.events.pools.releaseEvent = goog.nullFunction;
} else {
goog.events.pools.getObject = function() {
return objectPool.getObject();
};
goog.events.pools.releaseObject = function(obj) {
objectPool.releaseObject(obj);
};
goog.events.pools.getArray = function() {
return /** @type {Array} */ (arrayPool.getObject());
};
goog.events.pools.releaseArray = function(obj) {
arrayPool.releaseObject(obj);
};
goog.events.pools.getProxy = function() {
return /** @type {Function} */ (proxyPool.getObject());
};
goog.events.pools.releaseProxy = function(obj) {
proxyPool.releaseObject(getProxy());
};
goog.events.pools.getListener = function() {
return /** @type {goog.events.Listener} */ (
listenerPool.getObject());
};
goog.events.pools.releaseListener = function(obj) {
listenerPool.releaseObject(obj);
};
goog.events.pools.getEvent = function() {
return /** @type {goog.events.BrowserEvent} */ (eventPool.getObject());
};
goog.events.pools.releaseEvent = function(obj) {
eventPool.releaseObject(obj);
};
/**
* Initial count for the objectPool
*/
var OBJECT_POOL_INITIAL_COUNT = 0;
/**
* Max count for the objectPool_
*/
var OBJECT_POOL_MAX_COUNT = 600;
/**
* SimplePool to cache the lookup objects. This was implemented to make IE6
* performance better and removed an object allocation in goog.events.listen
* when in steady state.
*/
var objectPool = new goog.structs.SimplePool(OBJECT_POOL_INITIAL_COUNT,
OBJECT_POOL_MAX_COUNT);
objectPool.setCreateObjectFn(getObject);
/**
* Initial count for the arrayPool
*/
var ARRAY_POOL_INITIAL_COUNT = 0;
/**
* Max count for the arrayPool
*/
var ARRAY_POOL_MAX_COUNT = 600;
/**
* SimplePool to cache the type arrays. This was implemented to make IE6
* performance better and removed an object allocation in goog.events.listen
* when in steady state.
* @type {goog.structs.SimplePool}
*/
var arrayPool = new goog.structs.SimplePool(ARRAY_POOL_INITIAL_COUNT,
ARRAY_POOL_MAX_COUNT);
arrayPool.setCreateObjectFn(getArray);
/**
* Initial count for the proxyPool
*/
var HANDLE_EVENT_PROXY_POOL_INITIAL_COUNT = 0;
/**
* Max count for the proxyPool
*/
var HANDLE_EVENT_PROXY_POOL_MAX_COUNT = 600;
/**
* SimplePool to cache the handle event proxy. This was implemented to make
* IE6 performance better and removed an object allocation in
* goog.events.listen when in steady state.
*/
var proxyPool = new goog.structs.SimplePool(
HANDLE_EVENT_PROXY_POOL_INITIAL_COUNT,
HANDLE_EVENT_PROXY_POOL_MAX_COUNT);
proxyPool.setCreateObjectFn(getProxy);
/**
* Initial count for the listenerPool
*/
var LISTENER_POOL_INITIAL_COUNT = 0;
/**
* Max count for the listenerPool
*/
var LISTENER_POOL_MAX_COUNT = 600;
/**
* SimplePool to cache the listener objects. This was implemented to make
* IE6 performance better and removed an object allocation in
* goog.events.listen when in steady state.
*/
var listenerPool = new goog.structs.SimplePool(LISTENER_POOL_INITIAL_COUNT,
LISTENER_POOL_MAX_COUNT);
listenerPool.setCreateObjectFn(getListener);
/**
* Initial count for the eventPool
*/
var EVENT_POOL_INITIAL_COUNT = 0;
/**
* Max count for the eventPool
*/
var EVENT_POOL_MAX_COUNT = 600;
/**
* SimplePool to cache the event objects. This was implemented to make IE6
* performance better and removed an object allocation in
* goog.events.handleBrowserEvent_ when in steady state.
* This pool is only used for IE events.
*/
var eventPool = new goog.structs.SimplePool(EVENT_POOL_INITIAL_COUNT,
EVENT_POOL_MAX_COUNT);
eventPool.setCreateObjectFn(getEvent);
}
})();