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
2 changes: 1 addition & 1 deletion docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ resolves to the exit code. Its options:
- `stdout`, `stderr` (byte callbacks; default the process's), `input` (fixed stdin bytes)
- `env` (what `System.getenv()` returns; default the host's environment)
- `natives`: Java `native` methods in JS, as in the browser ([browser.md](browser.md))
- `assertions` (the flags, for example `["-ea"]`), `enablePreview`, `localeData`
- `assertions` (the flags, for example `["-ea"]`), `enablePreview`, `localeData`, `finalization` (`false` is `--finalization=disabled`)
- `hostName` (what `InetAddress.getLocalHost()` reports), `maxRssMb`, `jdk`, `debug`
- `maxDirectMemory` (`-XX:MaxDirectMemorySize`, in bytes), `onOutOfMemoryError` (`-XX:OnOutOfMemoryError` commands),
`vmOutputToStderr` (`-XX:+DisplayVMOutputToStderr`)
Expand Down
11 changes: 11 additions & 0 deletions docs/support.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ java-web targets headless Java: console apps, libraries and javac.
processes do to a watched host directory, with Node's `fs.watch` (so a short-lived
entry can show as a delete without its create). Elsewhere it does not see
changes made outside the VM.
- Garbage collection is the JS engine's. Under Node, `System.gc()` collects: weak and
phantom references are cleared and enqueued, finalizers (off with
`--finalization=disabled`) and Cleaners run, and a class loader that nothing
refers to is unloaded with its classes and hidden classes. Soft references
are cleared before an `OutOfMemoryError`, and by `System.gc()` when more than
half the heap limit is in use.
An object with a finalizer is finalized only if no cycle leads from its own
fields back to it (JS cannot finalize such an object): the VM drops the
finalizer of an object it finds on such a cycle, so the object is freed
without `finalize()` running, and a cycle it does not find (a class loader
with a `finalize()`) keeps the object for good. In the browser `System.gc()` is a hint and the engine collects when it chooses.
- Not supported: GUI apps (AWT and Swing windows), `ConvolveOp`, child
processes in the browser, and native libraries other than JS natives.

Expand Down
2 changes: 1 addition & 1 deletion src/indy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ export function invokePolymorphic(vm: JVM, t: JThread, f: Frame, m: JMethod): bo
let target = memberTarget(t, stack[f.sp - 1]);
if (m.name === "linkToStatic" && target.cls.state !== INITIALIZED && !vm.initialize(t, target.cls)) return false;
f.sp--;
if (m.name === "linkToVirtual" || m.name === "linkToInterface") {
if ((m.name === "linkToVirtual" || m.name === "linkToInterface") && !target.isPrivate) {
const recv = stack[f.sp - target.argSlots] as JObject | null;
if (recv === null) throw new PendingThrow("java/lang/NullPointerException");
const sel = recv.cls.selectMethod(target.key);
Expand Down
19 changes: 15 additions & 4 deletions src/interp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Bytecode interpreter and the per-thread run loop.

import {
INITIALIZED, JArray, JClass, JMethod, JObject, isWide, longToFloat, newArrayData, sched,
INITIALIZED, JArray, JClass, JMethod, JObject, isWide, longToFloat, newArrayData, pin, sched,
} from "./runtime.ts";
import {
Frame, JThread, JavaThrow, MAX_DIRECT, NativeFrame, PendingThrow, RUNNABLE, TERMINATED, WAITING, fromNative,
Expand All @@ -10,6 +10,7 @@ import type { NativeGen } from "./thread.ts";
import type { JVM } from "./jvm.ts";
import { INTERPRET, PUSHED, compileIfHot, hooks, noteBackedge } from "./jit.ts";
import { unmountNative } from "./natives/continuation.ts";
import { registerFinalizer } from "./natives/ref.ts";

const MAX_FRAMES = 6000;

Expand Down Expand Up @@ -374,12 +375,20 @@ export function interpret(t: JThread, f: Frame, budget: number): number {
target = vm.specialTarget(cls, target);
} else if (!target.isPrivate) {
const cache = m.cache ??= [];
const ic = cache[ipc];
let ic = cache[ipc];
if (ic !== undefined && ic.c === o.cls) target = ic.m;
else {
const sel = o.cls.selectMethod(target.key);
if (!sel) throw new PendingThrow("java/lang/AbstractMethodError", `${o.cls.javaName}.${target.name}${target.desc}`);
cache[ipc] = { c: o.cls, m: sel };
if (ic === undefined) cache[ipc] = ic = { c: o.cls, m: sel, p: false };
else {
ic.c = o.cls;
ic.m = sel;
}
if (o.cls.unloadable && !ic.p) {
ic.p = true;
pin(ic);
}
target = sel;
}
}
Expand All @@ -403,7 +412,9 @@ export function interpret(t: JThread, f: Frame, budget: number): number {
if (c === undefined) { f.pc = ipc; f.sp = sp; return budget; }
if (c.state !== INITIALIZED && !vm.initialize(t, c)) { f.pc = ipc; f.sp = sp; return budget; }
if (c.access & 0x0600) throw new PendingThrow("java/lang/InstantiationError", c.javaName);
stack[sp++] = new JObject(c, c.template.slice());
const o = new JObject(c, c.template.slice());
if (c.finalizable) registerFinalizer(vm, o);
stack[sp++] = o;
pc += 3;
break;
}
Expand Down
27 changes: 19 additions & 8 deletions src/jit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { cpB, cpTag, cpValue, Tag, utf8 } from "./classfile.ts";
import {
ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc,
ACC_FINAL, INITIALIZED, JArray, JClass, JField, JMethod, JObject, isWide, longToFloat, newArrayData, parseMethodDesc, pin,
} from "./runtime.ts";
import { memberTarget } from "./natives/invoke.ts";
import { concatStrings } from "./natives/strings.ts";
Expand Down Expand Up @@ -74,6 +74,12 @@ export function restoreJit(vm: JVM, m: JMethod, code: SavedJit) {
m.hot = THRESHOLD - 1;
}

// A constant of compiled code that holds a class another loader can drop.
function namesUnloadable(v: unknown, caller: JClass): boolean {
Comment thread
project516-review-bot[bot] marked this conversation as resolved.
Resolved
const c = v instanceof JClass ? v : v instanceof JMethod || v instanceof JField ? v.cls : v instanceof JObject ? (v.x instanceof JClass ? v.x : v.cls) : null;
return c !== null && c !== caller && c.unloadable && (c.hidden || c.loader !== caller.loader);
}

function compile(t: JThread, m: JMethod) {
if (disabled || m.noJit || !m.code || m.name === "<clinit>" || m.code.code.length > MAX_CODE) return;
const vm = t.vm;
Expand All @@ -85,6 +91,7 @@ function compile(t: JThread, m: JMethod) {
if (vm.keepJit && !stats && !noInline) vm.jitCode.set(m, code);
}
const { src, consts } = code;
if (consts.some((v) => namesUnloadable(v, m.cls))) pin(m);
if (dump && m.toString().includes(dump)) console.error(`// ${m}\n${src}`);
m.jit = new Function("rt", "K", src)(runtime, consts) as Compiled;
if (stats) stats.set(`compiled ${m}`, src.length);
Expand Down Expand Up @@ -230,7 +237,7 @@ const runtime = {
virtualTarget(t: JThread, mn: JObject, recv: JObject | null, pc: number): JMethod {
const m = memberTarget(t, mn);
if (recv === null) npeAt(pc);
const sel = recv.cls.selectMethod(m.key);
const sel = m.isPrivate ? m : recv.cls.selectMethod(m.key);
if (!sel) throw new PendingThrow("java/lang/AbstractMethodError", `${recv.cls.javaName}.${m.name}${m.desc}`);
return sel;
},
Expand Down Expand Up @@ -348,7 +355,7 @@ const runtime = {
},
fcmp(a: number, b: number, nan: number) { return a > b ? 1 : a < b ? -1 : a === b ? 0 : nan; },
lcmp(a: bigint, b: bigint) { return a > b ? 1 : a < b ? -1 : 0; },
// cell: the inline cache, [receiver class, method, method key]
// cell: the inline cache, [receiver class, method, method key, registered by pin]
select(o: JObject | null, cell: any[], pc: number): JMethod {
if (o === null) return runtime.npe(pc);
if (cell[0] === o.cls) return cell[1];
Expand All @@ -357,6 +364,10 @@ const runtime = {
if (!m) throw new PendingThrow("java/lang/AbstractMethodError", `${o.cls.javaName}.${key}`);
cell[0] = o.cls;
cell[1] = m;
if (o.cls.unloadable && !cell[3]) {
cell[3] = true;
pin(cell);
}
return m;
},
isGen(r: any) { return r !== null && typeof r === "object" && typeof r.next === "function" && typeof r.throw === "function"; },
Expand Down Expand Up @@ -1043,7 +1054,7 @@ function insnCode(g: Gen, b: Body, pc: number) {
break;
}
const k = cls.rcp[idx2] as JClass | undefined;
if (!k || k.state !== INITIALIZED || k.access & 0x0600) { emit(slow(pc, d)); break; }
if (!k || k.state !== INITIALIZED || k.access & 0x0600 || k.finalizable) { emit(slow(pc, d)); break; }
emit(`${S(d)}=new rt.JObject(${K(k)},${K(k)}.template.slice());`);
break;
}
Expand Down Expand Up @@ -1146,7 +1157,7 @@ function regionInvoke(g: Gen, b: Body, op: number, pc: number, d: number, target
let guard: JClass | null = null;
if (op === 0xb9 || !isFixed(op, callee)) {
const site = b.m.cache?.[pc];
if (!site || !site.m.code || site.m.isAbstract || site.m.polymorphic) throw new NoInline();
if (!site?.m?.code || site.m.isAbstract || site.m.polymorphic) throw new NoInline();
callee = site.m;
guard = site.c;
}
Expand Down Expand Up @@ -1314,7 +1325,7 @@ function genInvoke(
let seen = false;
if (guard !== null && !target.isPrivate) {
const site = g.top.m.cache?.[pc];
if (site && site.m.code && !site.m.isAbstract && !site.m.polymorphic) {
if (site?.m?.code && !site.m.isAbstract && !site.m.polymorphic) {
callee = site.m;
guard = site.c;
seen = true;
Expand Down Expand Up @@ -1345,7 +1356,7 @@ function genInvoke(
if (op === 0xb6 || op === 0xb9) {
if (target.isPrivate) parts.push(`tm=${K(target)};${known ? "" : `if(${args[0]}===null)np(${pc});`}`);
else {
const cell = K([null, null, target.key]);
const cell = K([null, null, target.key, false]);
parts.push(`ic=${cell};tm=${args[0]}!==null&&${args[0]}.cls===ic[0]?ic[1]:rt.select(${args[0]},ic,${pc});`);
}
} else if (op === 0xb7) {
Expand Down Expand Up @@ -1374,7 +1385,7 @@ function genGuardMiss(g: Gen, pc: number, next: number, d: number, target: JMeth
const args = Array.from({ length: n }, (_, i) => `s${base + i}`);
const ret = target.ret;
const xp = g.xcode(pc, base, EXIT_SUSPEND), xi = g.xcode(pc, d, EXIT_INTERPRET);
const cell = g.K([null, null, target.key]);
const cell = g.K([null, null, target.key, false]);
const call = n <= 4 ? `rt.callv(t,f,${cell},${pc},${xp},${xi},${args})` : `rt.callvMany(t,f,${cell},[${args}],${pc},${xp},${xi})`;
return `v=${call};if(v===rt.E){x=rt.xc;break X;}${ret === "V" ? "" : `s${base}=v;`}` +
`if(t.state!==0)${g.exit(next, ret === "V" ? base : base + (isWide(ret) ? 2 : 1), EXIT_SUSPEND)}`;
Expand Down
31 changes: 27 additions & 4 deletions src/jvm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { JImage } from "./jimage.ts";
import {
ACC_ABSTRACT, ACC_FINAL, ACC_INTERFACE, ACC_NATIVE, ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC,
ACC_STATIC, ACC_SUPER, ACC_VARARGS, ERRONEOUS, INITIALIZED, INITIALIZING, JArray, JClass, JField,
JMethod, JObject, LINKED, Monitor, defaultValue, newArrayData, noteYield, sched,
JMethod, JObject, LINKED, Monitor, defaultValue, dropsWithLoader, newArrayData, noteYield, sched,
} from "./runtime.ts";
import type { CallSite, NativeFn } from "./runtime.ts";
import {
Expand All @@ -21,6 +21,7 @@ import {
import type { NativeGen, Request } from "./thread.ts";
import { intrinsics, natives as builtinNatives } from "./natives/index.ts";
import { canUnmount, dropOwned, unmountBlocked } from "./natives/continuation.ts";
import { registerFinalizer } from "./natives/ref.ts";
import { invokePolymorphic, linkCallSite, resolveConstant } from "./indy.ts";
import { hostNative, jniNames } from "./hostnatives.ts";
import { Library } from "./library.ts";
Expand Down Expand Up @@ -67,6 +68,8 @@ export interface JvmOptions {
assertions?: Assertions;
// run class files that use this release's preview features
enablePreview?: boolean;
// false: finalize() is never called and there is no Finalizer thread
finalization?: boolean;
// class bytes from --patch-module for a boot loader module, which win over the image's
bootPatch?: (module: string, name: string) => Uint8Array | null;
// class bytes on the -Xbootclasspath/a path, and the path entry that holds them
Expand Down Expand Up @@ -258,6 +261,20 @@ export class JVM {
return (loader.x ??= { classes: new Map(), modules: new Map() });
}

// A hidden class defined STRONG lives as long as its loader; the others, as long as they are reachable.
private strongHidden = new WeakMap<JObject, JClass[]>();
private bootStrongHidden: JClass[] = [];
keepWithLoader(loader: JObject | null, c: JClass) {
c.unloadable = dropsWithLoader(loader);
if (loader === null) {
this.bootStrongHidden.push(c);
return;
}
const list = this.strongHidden.get(loader);
if (list) list.push(c);
else this.strongHidden.set(loader, [c]);
}

findLoaded(loader: JObject | null, name: string): JClass | undefined {
return this.loaderClasses(loader).get(name);
}
Expand Down Expand Up @@ -334,6 +351,7 @@ export class JVM {
a.superclass = this.loadBoot("java/lang/Object");
a.interfaces = [this.loadBoot("java/lang/Cloneable")!, this.loadBoot("java/io/Serializable")!];
a.state = INITIALIZED;
a.unloadable = c.unloadable;
c.arrayClass = a;
this.loaderClasses(c.loader).set(name, a);
this.created?.push(a);
Expand All @@ -350,6 +368,7 @@ export class JVM {
const c = new JClass(name, loader, cf, cf.access);
this.classCount++;
c.hidden = hidden;
if (hidden) c.unloadable = true;
c.superclass = superclass;
c.interfaces = interfaces;
let slot = superclass ? superclass.instanceSlots : 0;
Expand Down Expand Up @@ -382,6 +401,8 @@ export class JVM {
c.methods.push(m);
c.methodMap.set(m.key, m);
}
const fin = c.methodMap.get("finalize()V");
c.finalizable = superclass?.finalizable === true || (fin !== undefined && !fin.isStatic && (fin.code?.code.length ?? 0) > 1);
// Runtime members own the metadata now; discard the parsed member records.
cf.fields = [];
cf.methods = [];
Expand Down Expand Up @@ -675,7 +696,9 @@ export class JVM {
// ---- objects -------------------------------------------------------------

newObject(c: JClass): JObject {
return new JObject(c, c.template.slice());
const o = new JObject(c, c.template.slice());
if (c.finalizable) registerFinalizer(this, o);
return o;
}

newArray(component: JClass, n: number): JArray {
Expand Down Expand Up @@ -1159,7 +1182,7 @@ export class JVM {
*exitThread(t: JThread): NativeGen {
const obj = t.obj!;
try {
yield t.callVirtual(obj, "exit", "()V");
yield t.call(this.loadBoot("java/lang/Thread")!.methodMap.get("exit()V")!, obj);
} catch {
// Thread.exit failures are swallowed, see JavaThread::exit
}
Expand Down Expand Up @@ -1487,7 +1510,7 @@ export class JVM {
} else {
const self = this.newObject(main);
yield t.call(main.methodMap.get("<init>()V")!, self);
const m = main.selectMethod(key)!;
const m = main.selectMethod(key) ?? main.methodMap.get(key)!;
yield noArgs ? t.call(m, self) : t.call(m, self, jargs);
}
} catch (e) {
Expand Down
9 changes: 7 additions & 2 deletions src/natives/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { isHidden } from "./stackwalk.ts";
import { gcStats, javaHeap } from "./management.ts";
import { assertionsEnabled } from "../assertions.ts";
import { normalize } from "../fs.ts";
import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData } from "../runtime.ts";
import { ACC_INTERFACE, JArray, JClass, JObject, MAX_HEAP, newArrayData, releasePins } from "../runtime.ts";
import type { Monitor } from "../runtime.ts";
import { canUnmount, dropOwned, unblockOnRelease } from "./continuation.ts";
import { collect, registerFinalizer, releaseFinalizerCycles, releaseSoftReferences } from "./ref.ts";
import {
Frame, JThread, JavaThrow, PendingThrow, STATUS_BLOCKED, STATUS_RUNNABLE, STATUS_SLEEPING, STATUS_TIMED_WAITING,
STATUS_WAITING, Signal, TERMINATED,
Expand Down Expand Up @@ -203,6 +204,7 @@ export function cloneObject(t: JThread, o: JObject): JObject {
}
const copy = new JObject(o.cls, o.f.slice());
copy.x = o.x;
if (copy.cls.finalizable) registerFinalizer(t.vm, copy);
return copy;
}

Expand Down Expand Up @@ -383,8 +385,11 @@ export const natives: Natives = {
yield t.block(new Promise((r) => setTimeout(r, 0)));
// values popped off an operand stack are not roots in HotSpot
for (const th of t.vm.threads) for (const f of th.frames) if (f instanceof Frame && f.stack) f.stack.fill(undefined, f.sp);
releaseSoftReferences(t.vm);
releaseFinalizerCycles(t.vm);
releasePins(5000);
const t0 = performance.now();
gc();
yield t.block(collect(t.vm, gc));
stats.ms += performance.now() - t0;
},
"java/lang/Shutdown.beforeHalt()V": () => undefined,
Expand Down
3 changes: 2 additions & 1 deletion src/natives/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { jstr, u8 } from "./util.ts";
import type { Natives } from "./util.ts";

const CL = "java/lang/ClassLoader";
const HIDDEN_CLASS = 0x2, NESTMATE_CLASS = 0x1;
const HIDDEN_CLASS = 0x2, NESTMATE_CLASS = 0x1, STRONG_LOADER_LINK = 0x4;

function internalName(t: JThread, name: JObject | null): string | null {
return name === null ? null : jstr(t, name)!.replace(/\./g, "/");
Expand Down Expand Up @@ -70,6 +70,7 @@ export const natives: Natives = {
const vm = t.vm;
const hidden = (flags & HIDDEN_CLASS) !== 0;
const c = yield* vm.defineClassGen(t, loader, u8(b, off, len).slice(), internalName(t, name), pd, hidden);
if (hidden && flags & STRONG_LOADER_LINK) vm.keepWithLoader(loader, c);
if (flags & NESTMATE_CLASS) {
c.nestHost = yield* nestHostOf(t, vm.classOf(lookup));
}
Expand Down
12 changes: 12 additions & 0 deletions src/natives/management.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,18 @@ export function javaHeap(): { used: number; committed: number } | null {
return { used: Math.min(used, committed), committed };
}

// The engine's own heap use and limit, unclamped; null where it cannot tell.
export function engineHeap(): { used: number; limit: number } | null {
const g = globalThis as any;
const node = g.process?.memoryUsage?.();
if (node) {
const v8 = g.process.getBuiltinModule?.("node:v8");
return v8 ? { used: node.heapUsed + node.arrayBuffers, limit: v8.getHeapStatistics().heap_size_limit } : null;
}
const chrome = g.performance?.memory;
return chrome ? { used: chrome.usedJSHeapSize, limit: chrome.jsHeapSizeLimit } : null;
}

function* beans(t: JThread): NativeGen {
const vm = t.vm;
let b = vm.state.get("mgmtBeans") as Beans | undefined;
Expand Down
Loading
Loading