js/xpconnect/tests/unit/test_js_weak_references.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /* See https://bugzilla.mozilla.org/show_bug.cgi?id=317304 */
michael@0 6
michael@0 7 function run_test()
michael@0 8 {
michael@0 9 // Bug 712649: Calling getWeakReference(null) should work.
michael@0 10 try {
michael@0 11 var nullWeak = Components.utils.getWeakReference(null);
michael@0 12 do_check_true(nullWeak.get() === null);
michael@0 13 } catch (e) {
michael@0 14 do_check_true(false);
michael@0 15 }
michael@0 16
michael@0 17 var obj = { num: 5, str: 'foo' };
michael@0 18 var weak = Components.utils.getWeakReference(obj);
michael@0 19
michael@0 20 do_check_true(weak.get() === obj);
michael@0 21 do_check_true(weak.get().num == 5);
michael@0 22 do_check_true(weak.get().str == 'foo');
michael@0 23
michael@0 24 // Force garbage collection
michael@0 25 Components.utils.forceGC();
michael@0 26
michael@0 27 // obj still references the object, so it should still be accessible via weak
michael@0 28 do_check_true(weak.get() === obj);
michael@0 29 do_check_true(weak.get().num == 5);
michael@0 30 do_check_true(weak.get().str == 'foo');
michael@0 31
michael@0 32 // Clear obj's reference to the object and force garbage collection. To make
michael@0 33 // sure that there are no instances of obj stored in the registers or on the
michael@0 34 // native stack and the conservative GC would not find it we force the same
michael@0 35 // code paths that we used for the initial allocation.
michael@0 36 obj = { num: 6, str: 'foo2' };
michael@0 37 var weak2 = Components.utils.getWeakReference(obj);
michael@0 38 do_check_true(weak2.get() === obj);
michael@0 39
michael@0 40 Components.utils.forceGC();
michael@0 41
michael@0 42 // The object should have been garbage collected and so should no longer be
michael@0 43 // accessible via weak
michael@0 44 do_check_true(weak.get() === null);
michael@0 45 }

mercurial