michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* See https://bugzilla.mozilla.org/show_bug.cgi?id=317304 */ michael@0: michael@0: function run_test() michael@0: { michael@0: // Bug 712649: Calling getWeakReference(null) should work. michael@0: try { michael@0: var nullWeak = Components.utils.getWeakReference(null); michael@0: do_check_true(nullWeak.get() === null); michael@0: } catch (e) { michael@0: do_check_true(false); michael@0: } michael@0: michael@0: var obj = { num: 5, str: 'foo' }; michael@0: var weak = Components.utils.getWeakReference(obj); michael@0: michael@0: do_check_true(weak.get() === obj); michael@0: do_check_true(weak.get().num == 5); michael@0: do_check_true(weak.get().str == 'foo'); michael@0: michael@0: // Force garbage collection michael@0: Components.utils.forceGC(); michael@0: michael@0: // obj still references the object, so it should still be accessible via weak michael@0: do_check_true(weak.get() === obj); michael@0: do_check_true(weak.get().num == 5); michael@0: do_check_true(weak.get().str == 'foo'); michael@0: michael@0: // Clear obj's reference to the object and force garbage collection. To make michael@0: // sure that there are no instances of obj stored in the registers or on the michael@0: // native stack and the conservative GC would not find it we force the same michael@0: // code paths that we used for the initial allocation. michael@0: obj = { num: 6, str: 'foo2' }; michael@0: var weak2 = Components.utils.getWeakReference(obj); michael@0: do_check_true(weak2.get() === obj); michael@0: michael@0: Components.utils.forceGC(); michael@0: michael@0: // The object should have been garbage collected and so should no longer be michael@0: // accessible via weak michael@0: do_check_true(weak.get() === null); michael@0: }