Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / devtools / shared / test-helpers / tracked-objects.sys.mjs
blob54f53fb5fa74a3bed80ee09e9ad5a8ce6bb5a5a1
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/. */
5 // Test-only module in order to register objects later inspected by
6 // the allocation tracker (in the same folder).
7 //
8 // We are going to store a weak reference to the passed objects,
9 // in order to prevent holding them in memory.
10 // Allocation tracker will then print detailed information
11 // about why these objects are still allocated.
13 const objects = [];
15 /**
16  * Request to track why the given object is kept in memory,
17  * later on, when retrieving all the watched object via getAllNodeIds.
18  */
19 export function track(obj) {
20   // We store a weak reference, so that we do force keeping the object in memory!!
21   objects.push(Cu.getWeakReference(obj));
24 /**
25  * Return the NodeId's of all the objects passed via `track()` method.
26  *
27  * NodeId's are used by spidermonkey memory API to designates JS objects in head snapshots.
28  */
29 export function getAllNodeIds() {
30   // Filter out objects which have been freed already
31   return (
32     objects
33       .map(weak => weak.get())
34       .filter(obj => !!obj)
35       // Convert objects from here instead of from allocation tracker in order
36       // to be from the shared system compartment and avoid trying to compute the NodeId
37       // of a wrapper!
38       .map(ChromeUtils.getObjectNodeId)
39   );
42 /**
43  * Used by tests to clear all tracked objects
44  */
45 export function clear() {
46   objects.length = 0;