tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_js_weak_references.js (1461B)


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