Mercurial > my-ubiquity-commands
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pseudo-weak-refs/pseudo-weak-refs.js Sat Apr 25 13:38:25 2009 -0700 @@ -0,0 +1,55 @@ +// This Ubiquity page-load function injects the following functions for memory +// debugging into webpages: +// +// * window.PseudoWeakRef(object) makes a pseudo-weak reference for the +// object. It has one method, exists(), which returns whether or not +// the referent still exists (i.e., has not been garbage collected). +// +// * window.forceGC() forces garbage collection. + +function pageLoad_injectPseudoWeakRefs(document) { + var sandbox = Components.utils.Sandbox(document.defaultView); + var weakrefs = []; + + function makePseudoWeakRef(object) { + var weakref = new Components.utils.getWeakReference(object); + object = null; + weakrefs.push(weakref); + return weakrefs.length - 1; + } + + function isReferentAlive(id) { + if (typeof(id) == "number" && weakrefs[id]) { + if (weakrefs[id].get() != null) + return true; + else + delete weakrefs[id]; + } + return false; + } + + function forceGC() { + Components.utils.forceGC(); + } + + sandbox.importFunction(makePseudoWeakRef); + sandbox.importFunction(isReferentAlive); + sandbox.importFunction(forceGC); + + function PseudoWeakRef(object) { + var id = makePseudoWeakRef(object); + object = null; + this.exists = function() { + return isReferentAlive(id); + }; + } + + Components.utils.evalInSandbox(PseudoWeakRef.toString(), sandbox); + + sandbox.window = document.defaultView.wrappedJSObject; + Components.utils.evalInSandbox( + ("window.PseudoWeakRef = PseudoWeakRef;" + + "window.forceGC = forceGC;"), + sandbox + ); +}