js/xpconnect/tests/unit/test_js_weak_references.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/xpconnect/tests/unit/test_js_weak_references.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,45 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/* See https://bugzilla.mozilla.org/show_bug.cgi?id=317304 */
     1.9 +
    1.10 +function run_test()
    1.11 +{
    1.12 +  // Bug 712649: Calling getWeakReference(null) should work.
    1.13 +  try {
    1.14 +    var nullWeak = Components.utils.getWeakReference(null);
    1.15 +    do_check_true(nullWeak.get() === null);
    1.16 +  } catch (e) {
    1.17 +    do_check_true(false);
    1.18 +  }
    1.19 +
    1.20 +  var obj = { num: 5, str: 'foo' };
    1.21 +  var weak = Components.utils.getWeakReference(obj);
    1.22 +
    1.23 +  do_check_true(weak.get() === obj);
    1.24 +  do_check_true(weak.get().num == 5);
    1.25 +  do_check_true(weak.get().str == 'foo');
    1.26 +
    1.27 +  // Force garbage collection
    1.28 +  Components.utils.forceGC();
    1.29 +
    1.30 +  // obj still references the object, so it should still be accessible via weak
    1.31 +  do_check_true(weak.get() === obj);
    1.32 +  do_check_true(weak.get().num == 5);
    1.33 +  do_check_true(weak.get().str == 'foo');
    1.34 +
    1.35 +  // Clear obj's reference to the object and force garbage collection. To make
    1.36 +  // sure that there are no instances of obj stored in the registers or on the
    1.37 +  // native stack and the conservative GC would not find it we force the same
    1.38 +  // code paths that we used for the initial allocation.
    1.39 +  obj = { num: 6, str: 'foo2' };
    1.40 +  var weak2 = Components.utils.getWeakReference(obj);
    1.41 +  do_check_true(weak2.get() === obj);
    1.42 +
    1.43 +  Components.utils.forceGC();
    1.44 +
    1.45 +  // The object should have been garbage collected and so should no longer be
    1.46 +  // accessible via weak
    1.47 +  do_check_true(weak.get() === null);
    1.48 +}

mercurial