diff --git a/src/runtime.ts b/src/runtime.ts index d734568..f75c267 100644 --- a/src/runtime.ts +++ b/src/runtime.ts @@ -635,6 +635,8 @@ export class JClass { } return t.name === "java/lang/Object" || t.name === "java/lang/Cloneable" || t.name === "java/io/Serializable"; } + // Object, the one class without a superclass, as for stores into Object[] + if (t.superclass === null && !t.isInterface) return true; return this.allSupers().has(t); } diff --git a/src/thread.ts b/src/thread.ts index e227d29..8944c7a 100644 --- a/src/thread.ts +++ b/src/thread.ts @@ -92,11 +92,18 @@ export class NativeFrame { export type AnyFrame = Frame | NativeFrame; +// JavaThrow and PendingThrow are control flow: V8 would capture a JS stack +// trace for each, which costs more than the throw and is never read. +const limitTraces = Object.getOwnPropertyDescriptor(Error, "stackTraceLimit")?.writable === true; + // A Java exception in flight through JS code. export class JavaThrow extends Error { readonly obj: JObject; constructor(obj: JObject) { + const limit = Error.stackTraceLimit; + if (limitTraces) Error.stackTraceLimit = 0; super("java exception"); + if (limitTraces) Error.stackTraceLimit = limit; this.obj = obj; } } @@ -113,7 +120,10 @@ export class PendingThrow extends Error { // the native method that threw it, which its stack trace starts with native?: JMethod; constructor(cls: string, msg: string | null = null, ctor?: { desc: string; args: any[] }) { + const limit = Error.stackTraceLimit; + if (limitTraces) Error.stackTraceLimit = 0; super(`${cls}: ${msg}`); + if (limitTraces) Error.stackTraceLimit = limit; this.cls = cls; this.msg = msg; this.ctor = ctor; @@ -203,15 +213,11 @@ export class JThread { // filled without holes, which would make V8 store it as a dictionary while (this.pool.length <= d) this.pool.push(this.pooled(m, this.pool.length)); const f = this.pool[d] ?? (this.pool[d] = this.pooled(m, d)); + // a pooled frame has nothing spilled, no monitor still to take and stays direct; a frame that changes those leaves the pool f.m = m; f.code = m.code!.code; f.pc = 0; - f.sp = 0; - f.locals = null as any; - f.stack = null as any; f.lock = lock; - f.needLock = false; - f.direct = true; this.frames.push(f); return f; } @@ -219,6 +225,7 @@ export class JThread { private pooled(m: JMethod, slot: number): Frame { const f = new Frame(m, true); f.poolSlot = slot; + f.direct = true; return f; }