diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 00:47:55 +0000 |
commit | 26a029d407be480d791972afb5975cf62c9360a6 (patch) | |
tree | f435a8308119effd964b339f76abb83a57c29483 /dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts | |
parent | Initial commit. (diff) | |
download | firefox-26a029d407be480d791972afb5975cf62c9360a6.tar.xz firefox-26a029d407be480d791972afb5975cf62c9360a6.zip |
Adding upstream version 124.0.1.upstream/124.0.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts')
-rw-r--r-- | dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts b/dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts new file mode 100644 index 0000000000..670028d41c --- /dev/null +++ b/dom/webgpu/tests/cts/checkout/src/common/util/collect_garbage.ts @@ -0,0 +1,58 @@ +import { resolveOnTimeout } from './util.js'; + +/* eslint-disable-next-line @typescript-eslint/no-explicit-any */ +declare const Components: any; + +/** + * Attempts to trigger JavaScript garbage collection, either using explicit methods if exposed + * (may be available in testing environments with special browser runtime flags set), or using + * some weird tricks to incur GC pressure. Adopted from the WebGL CTS. + */ +export async function attemptGarbageCollection(): Promise<void> { + /* eslint-disable-next-line @typescript-eslint/no-explicit-any */ + const w: any = globalThis; + if (w.GCController) { + w.GCController.collect(); + return; + } + + if (w.opera && w.opera.collect) { + w.opera.collect(); + return; + } + + try { + w.QueryInterface(Components.interfaces.nsIInterfaceRequestor) + .getInterface(Components.interfaces.nsIDOMWindowUtils) + .garbageCollect(); + return; + } catch (e) { + // ignore any failure + } + + if (w.gc) { + w.gc(); + return; + } + + if (w.CollectGarbage) { + w.CollectGarbage(); + return; + } + + let i: number; + function gcRec(n: number): void { + if (n < 1) return; + /* eslint-disable @typescript-eslint/restrict-plus-operands */ + let temp: object | string = { i: 'ab' + i + i / 100000 }; + /* eslint-disable @typescript-eslint/restrict-plus-operands */ + temp = temp + 'foo'; + temp; // dummy use of unused variable + gcRec(n - 1); + } + for (i = 0; i < 1000; i++) { + gcRec(10); + } + + return resolveOnTimeout(35); // Let the event loop run a few frames in case it helps. +} |