Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { isAssertionFlag, parseAssertions } from "./assertions.ts";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { setFlagsFromString } from "node:v8";
import { runInNewContext } from "node:vm";
import { fileURLToPath, pathToFileURL } from "node:url";
import { VFS } from "./fs.ts";
import type { Backend, Stat } from "./fs.ts";
Expand Down Expand Up @@ -180,7 +182,20 @@ function fixedInput(data: Uint8Array) {
const bridges = new WeakMap<object, Bridge>();

// The host file system is the VFS root, with the JDK image mounted at /jdk.
// System.gc() calls globalThis.gc, which node defines only under --expose-gc
function exposeGc() {
const g = globalThis as { gc?: unknown };
if (typeof g.gc === "function") return;
// V8 aborts on a flag change once flags are frozen
if (/(^|\s)--freeze-flags-after-init\b/.test([...process.execArgv, process.env.NODE_OPTIONS ?? ""].join(" "))) return;
setFlagsFromString("--expose-gc");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
// absent when --expose-gc-as gave it another name
const gc = runInNewContext("typeof gc === 'function' ? gc : undefined");
if (gc) g.gc = gc;
}

function nodeVm(o: NodeRunOptions): JVM {
exposeGc();
const vfs = new VFS();
vfs.mount("/", new NodeBackend("/", true));
// built as dist/node.js, it reads the packaged image next to it
Expand Down
8 changes: 8 additions & 0 deletions test/programs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ for (const file of files) {
});
}

// the java-web command, which node starts without --expose-gc
test("System.gc collects under the command", { skip: !files.includes("Refs.java") }, () => {
const ref = spawnSync(hotspot("java"), ["-cp", out, "Refs"], { encoding: "utf8" });
assert.equal(ref.status, 0);
const got = spawnSync(process.execPath, [join(root, "src", "node.ts"), "-cp", out, "Refs"], { encoding: "utf8" });
assert.deepEqual({ stdout: got.stdout, code: got.status }, { stdout: ref.stdout, code: ref.status });
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
});

function flags(file: string, tool: string): string[] {
const m = new RegExp(`^// ${tool}: (.*)$`, "m").exec(readFileSync(join(src, file), "utf8"));
return m ? m[1].split(" ") : [];
Expand Down
Loading