Bug 926725 - Create DevToolsUtils.defineLazyPrototypeGetter and use it in VariablesView Scopes. r=fitzgen, r=vp

This commit is contained in:
Brandon Benvie 2013-10-15 09:49:15 -07:00
Родитель be92e3df23
Коммит 3770e14b51
5 изменённых файлов: 112 добавлений и 9 удалений

Просмотреть файл

@ -21,6 +21,7 @@ Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource:///modules/devtools/ViewHelpers.jsm");
Cu.import("resource:///modules/devtools/shared/event-emitter.js");
Cu.import("resource://gre/modules/devtools/DevToolsUtils.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "devtools",
"resource://gre/modules/devtools/Loader.jsm");
@ -1122,14 +1123,6 @@ function Scope(aView, aName, aFlags = {}) {
this.contextMenuId = aView.contextMenuId;
this.separatorStr = aView.separatorStr;
// Creating maps and arrays thousands of times for variables or properties
// with a large number of children fills up a lot of memory. Make sure
// these are instantiated only if needed.
XPCOMUtils.defineLazyGetter(this, "_store", () => new Map());
XPCOMUtils.defineLazyGetter(this, "_enumItems", () => []);
XPCOMUtils.defineLazyGetter(this, "_nonEnumItems", () => []);
XPCOMUtils.defineLazyGetter(this, "_batchItems", () => []);
this._init(aName.trim(), aFlags);
}
@ -2039,6 +2032,14 @@ Scope.prototype = {
_throbber: null
};
// Creating maps and arrays thousands of times for variables or properties
// with a large number of children fills up a lot of memory. Make sure
// these are instantiated only if needed.
DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_store", Map);
DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_enumItems", Array);
DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_nonEnumItems", Array);
DevToolsUtils.defineLazyPrototypeGetter(Scope.prototype, "_batchItems", Array);
/**
* A Variable is a Scope holding Property instances.
* Iterable via "for (let [name, property] in instance) { }".

Просмотреть файл

@ -134,3 +134,35 @@ this.yieldingEach = function yieldingEach(aArray, aFn) {
return deferred.promise;
}
/**
* Like XPCOMUtils.defineLazyGetter, but with a |this| sensitive getter that
* allows the lazy getter to be defined on a prototype and work correctly with
* instances.
*
* @param Object aObject
* The prototype object to define the lazy getter on.
* @param String aKey
* The key to define the lazy getter on.
* @param Function aCallback
* The callback that will be called to determine the value. Will be
* called with the |this| value of the current instance.
*/
this.defineLazyPrototypeGetter =
function defineLazyPrototypeGetter(aObject, aKey, aCallback) {
Object.defineProperty(aObject, aKey, {
configurable: true,
get: function() {
const value = aCallback.call(this);
Object.defineProperty(this, aKey, {
configurable: true,
writable: true,
value: value
});
return value;
}
});
}

Просмотреть файл

@ -22,5 +22,6 @@ this.DevToolsUtils = {
safeErrorString: safeErrorString,
reportException: reportException,
makeInfallible: makeInfallible,
yieldingEach: yieldingEach
yieldingEach: yieldingEach,
defineLazyPrototypeGetter: defineLazyPrototypeGetter
};

Просмотреть файл

@ -0,0 +1,68 @@
/* -*- Mode: js; js-indent-level: 2; -*- */
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
// Test DevToolsUtils.defineLazyPrototypeGetter
function Class() {}
DevToolsUtils.defineLazyPrototypeGetter(Class.prototype, "foo", () => []);
function run_test() {
test_prototype_attributes();
test_instance_attributes();
test_multiple_instances();
test_callback_receiver();
}
function test_prototype_attributes() {
// Check that the prototype has a getter property with expected attributes.
let descriptor = Object.getOwnPropertyDescriptor(Class.prototype, "foo");
do_check_eq(typeof descriptor.get, "function");
do_check_eq(descriptor.set, undefined);
do_check_eq(descriptor.enumerable, false);
do_check_eq(descriptor.configurable, true);
}
function test_instance_attributes() {
// Instances should not have an own property until the lazy getter has been
// activated.
let instance = new Class();
do_check_false(instance.hasOwnProperty("foo"));
instance.foo;
do_check_true(instance.hasOwnProperty("foo"));
// Check that the instance has an own property with the expecred value and
// attributes after the lazy getter is activated.
let descriptor = Object.getOwnPropertyDescriptor(instance, "foo");
do_check_true(descriptor.value instanceof Array);
do_check_eq(descriptor.writable, true);
do_check_eq(descriptor.enumerable, false);
do_check_eq(descriptor.configurable, true);
}
function test_multiple_instances() {
let instance1 = new Class();
let instance2 = new Class();
let foo1 = instance1.foo;
let foo2 = instance2.foo;
// Check that the lazy getter returns the expected type of value.
do_check_true(foo1 instanceof Array);
do_check_true(foo2 instanceof Array);
// Make sure the lazy getter runs once and only once per instance.
do_check_eq(instance1.foo, foo1);
do_check_eq(instance2.foo, foo2);
// Make sure each instance gets its own unique value.
do_check_neq(foo1, foo2);
}
function test_callback_receiver() {
function Foo() {};
DevToolsUtils.defineLazyPrototypeGetter(Foo.prototype, "foo", function() {
return this;
});
// Check that the |this| value in the callback is the instance itself.
let instance = new Foo();
do_check_eq(instance.foo, instance);
}

Просмотреть файл

@ -3,3 +3,4 @@ head = head_devtools.js
tail =
[test_safeErrorString.js]
[test_defineLazyPrototypeGetter.js]