Skip to content

Commit 0d6c4b7

Browse files
committed
expose a harness gc() wrapper
Signed-off-by: Balakrishna Avulapati <ba@bavulapati.com>
1 parent c033445 commit 0d6c4b7

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export default defineConfig([
1515
loadAddon: "readonly",
1616
mustCall: "readonly",
1717
mustNotCall: "readonly",
18+
gc: "readonly",
1819
gcUntil: "readonly",
1920
experimentalFeatures: "readonly",
2021
onUncaughtException: "readonly",

implementors/node/gc.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1+
// Capture the engine-provided gc (Node exposes it under --expose-gc) before
2+
// we overwrite globalThis.gc with the harness wrapper below.
3+
const engineGc = globalThis.gc;
4+
if (typeof engineGc !== "function") {
5+
throw new Error(
6+
"Node harness expects globalThis.gc to be available (run with --expose-gc)",
7+
);
8+
}
9+
10+
const gc = () => {
11+
engineGc();
12+
};
13+
114
const gcUntil = async (name, condition) => {
215
let count = 0;
316
while (!condition()) {
417
await new Promise((resolve) => setImmediate(resolve));
518
if (++count < 10) {
6-
globalThis.gc();
19+
engineGc();
720
} else {
821
throw new Error(`GC test "${name}" failed after ${count} attempts`);
922
}
1023
}
1124
};
1225

13-
Object.assign(globalThis, { gcUntil });
26+
Object.assign(globalThis, { gc, gcUntil });

tests/harness/gc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
if (typeof gc !== 'function') {
2+
throw new Error('Expected a global gc function');
3+
}
4+
15
if (typeof gcUntil !== 'function') {
26
throw new Error('Expected a global gcUntil function');
37
}
48

9+
// gc should run synchronously without throwing
10+
gc();
11+
512
// gcUntil should resolve once the condition becomes true
613
let count = 0;
714
await gcUntil('test-passes', () => {

tests/js-native-api/test_reference/test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,5 @@ runTests();
164164
for (let i = 0; i < 1000; i++) {
165165
const wrapObject = new Object();
166166
test_reference.validateDeleteBeforeFinalize(wrapObject);
167-
let gcCount = 1;
168-
gcUntil("test", () => gcCount-- <= 0);
167+
gc();
169168
}

tests/js-native-api/test_reference/test_finalizer.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,5 @@ onUncaughtException(
1919
}),
2020
);
2121
}
22-
let gcCount = 1;
23-
await gcUntil("test", () => gcCount-- > 0);
22+
gc();
2423
})().then(mustCall());

0 commit comments

Comments
 (0)