comparison pseudo-weak-refs/pseudo-weak-refs.js @ 18:08bddf757ed8

Added pseudo-weak-refs feed.
author Atul Varma <varmaa@toolness.com>
date Sat, 25 Apr 2009 13:38:25 -0700
parents
children 900a2fabd11c
comparison
equal deleted inserted replaced
17:f3e03596fcf9 18:08bddf757ed8
1 // This Ubiquity page-load function injects the following functions for memory
2 // debugging into webpages:
3 //
4 // * window.PseudoWeakRef(object) makes a pseudo-weak reference for the
5 // object. It has one method, exists(), which returns whether or not
6 // the referent still exists (i.e., has not been garbage collected).
7 //
8 // * window.forceGC() forces garbage collection.
9
10 function pageLoad_injectPseudoWeakRefs(document) {
11 var sandbox = Components.utils.Sandbox(document.defaultView);
12 var weakrefs = [];
13
14 function makePseudoWeakRef(object) {
15 var weakref = new Components.utils.getWeakReference(object);
16 object = null;
17 weakrefs.push(weakref);
18 return weakrefs.length - 1;
19 }
20
21 function isReferentAlive(id) {
22 if (typeof(id) == "number" && weakrefs[id]) {
23 if (weakrefs[id].get() != null)
24 return true;
25 else
26 delete weakrefs[id];
27 }
28 return false;
29 }
30
31 function forceGC() {
32 Components.utils.forceGC();
33 }
34
35 sandbox.importFunction(makePseudoWeakRef);
36 sandbox.importFunction(isReferentAlive);
37 sandbox.importFunction(forceGC);
38
39 function PseudoWeakRef(object) {
40 var id = makePseudoWeakRef(object);
41 object = null;
42 this.exists = function() {
43 return isReferentAlive(id);
44 };
45 }
46
47 Components.utils.evalInSandbox(PseudoWeakRef.toString(), sandbox);
48
49 sandbox.window = document.defaultView.wrappedJSObject;
50 Components.utils.evalInSandbox(
51 ("window.PseudoWeakRef = PseudoWeakRef;" +
52 "window.forceGC = forceGC;"),
53 sandbox
54 );
55 }