@@ -197,18 +197,36 @@ export class JObject {
197197// slot has its own functions, so the engine caches the property per slot, not
198198// per call site, and a read-modify-write touches the object once.
199199const MAX_FAST_SLOT = 4096 ;
200+
201+ // Pages that forbid eval throw on the first new Function. Detect it once, then
202+ // build accessors and constructors from closures.
203+ let noEval = false ;
204+ export function evalBlocked ( e : unknown ) : boolean {
205+ return e instanceof EvalError || ( e instanceof Error && / u n s a f e - e v a l | C o n t e n t S e c u r i t y / i. test ( e . message ) ) ;
206+ }
207+ function generate < T > ( make : ( ) => T , fallback : ( ) => T ) : T {
208+ if ( ! noEval ) {
209+ try {
210+ return make ( ) ;
211+ } catch ( e ) {
212+ if ( ! evalBlocked ( e ) ) throw e ;
213+ noEval = true ;
214+ }
215+ }
216+ return fallback ( ) ;
217+ }
200218type SlotFns = { read : ( o : JObject ) => any ; write : ( o : JObject , v : any ) => void ; cas : ( o : JObject , e : any , v : any ) => any ; swap : ( o : JObject , v : any ) => any ; add : ( o : JObject , d : any ) => any ; addLong : ( o : JObject , d : bigint ) => bigint } ;
201219const slotFns : SlotFns [ ] = [ ] ;
202220function fnsFor ( slot : number ) : SlotFns | null {
203221 if ( ! ( slot >= 0 && slot < MAX_FAST_SLOT && Number . isInteger ( slot ) ) ) return null ;
204- return ( slotFns [ slot ] ??= {
222+ return slotFns [ slot ] ?? generate ( ( ) => ( slotFns [ slot ] = {
205223 read : new Function ( "o" , `return o.f${ slot } ` ) as SlotFns [ "read" ] ,
206224 write : new Function ( "o" , "v" , `o.f${ slot } =v` ) as SlotFns [ "write" ] ,
207225 cas : new Function ( "o" , "e" , "v" , `const c=o.f${ slot } ;if(c===e)o.f${ slot } =v;return c` ) as SlotFns [ "cas" ] ,
208226 swap : new Function ( "o" , "v" , `const c=o.f${ slot } ;o.f${ slot } =v;return c` ) as SlotFns [ "swap" ] ,
209227 add : new Function ( "o" , "d" , `const c=o.f${ slot } ;o.f${ slot } =(c+d)|0;return c` ) as SlotFns [ "add" ] ,
210228 addLong : new Function ( "o" , "d" , `const c=o.f${ slot } ;o.f${ slot } =BigInt.asIntN(64,c+d);return c` ) as SlotFns [ "addLong" ] ,
211- } ) ;
229+ } ) , ( ) => null ) ;
212230}
213231export function readField ( o : JObject , slot : number ) : any {
214232 const f = fnsFor ( slot ) ;
@@ -271,14 +289,22 @@ function withAccess(C: unknown, kinds: string): ObjCtor {
271289 const T = C as ObjCtor ;
272290 // generated on first use: boot does not need them
273291 T . dump = ( o ) => {
274- let body = "return[" ;
275- for ( let i = 0 ; i < kinds . length ; i ++ ) body += `o.f${ i } ,` ;
276- return ( T . dump = new Function ( "o" , `${ body } ]` ) as ObjCtor [ "dump" ] ) ( o ) ;
292+ T . dump = generate ( ( ) => {
293+ let body = "return[" ;
294+ for ( let i = 0 ; i < kinds . length ; i ++ ) body += `o.f${ i } ,` ;
295+ return new Function ( "o" , `${ body } ]` ) as ObjCtor [ "dump" ] ;
296+ } , ( ) => ( o ) => Array . from ( kinds , ( _ , i ) => o [ fieldKey ( i ) ] ) ) ;
297+ return T . dump ( o ) ;
277298 } ;
278299 T . load = ( o , f ) => {
279- let body = "" ;
280- for ( let i = 0 ; i < kinds . length ; i ++ ) body += `o.f${ i } =f[${ i } ];` ;
281- ( T . load = new Function ( "o" , "f" , body ) as ObjCtor [ "load" ] ) ( o , f ) ;
300+ T . load = generate ( ( ) => {
301+ let body = "" ;
302+ for ( let i = 0 ; i < kinds . length ; i ++ ) body += `o.f${ i } =f[${ i } ];` ;
303+ return new Function ( "o" , "f" , body ) as ObjCtor [ "load" ] ;
304+ } , ( ) => ( o , f ) => {
305+ for ( let i = 0 ; i < kinds . length ; i ++ ) o [ fieldKey ( i ) ] = f [ i ] ;
306+ } ) ;
307+ T . load ( o , f ) ;
282308 } ;
283309 return T ;
284310}
@@ -288,9 +314,19 @@ function ctorFor(kinds: string): ObjCtor {
288314 if ( C ) return C ;
289315 if ( kinds === "" ) C = withAccess ( JObject , kinds ) ;
290316 else {
291- let body = "this.cls=cls;this.s=null;" ;
292- for ( let i = 0 ; i < kinds . length ; i ++ ) body += `this.f${ i } =${ kinds [ i ] === "J" ? "0n" : kinds [ i ] === "L" ? "null" : "0" } ;` ;
293- const Obj = new Function ( "A" , "T" , `return function Obj(cls){${ body } if(A.check!==null&&A.check.enabled)T(32);}` ) ( alloc , allocated ) ;
317+ const Obj = generate ( ( ) => {
318+ let body = "this.cls=cls;this.s=null;" ;
319+ for ( let i = 0 ; i < kinds . length ; i ++ ) body += `this.f${ i } =${ kinds [ i ] === "J" ? "0n" : kinds [ i ] === "L" ? "null" : "0" } ;` ;
320+ return new Function ( "A" , "T" , `return function Obj(cls){${ body } if(A.check!==null&&A.check.enabled)T(32);}` ) ( alloc , allocated ) ;
321+ } , ( ) => {
322+ const zeros = Array . from ( kinds , ( k ) => ( k === "J" ? 0n : k === "L" ? null : 0 ) ) ;
323+ return function Obj ( this : any , cls : JClass ) {
324+ this . cls = cls ;
325+ this . s = null ;
326+ for ( let i = 0 ; i < zeros . length ; i ++ ) this [ fieldKey ( i ) ] = zeros [ i ] ;
327+ if ( alloc . check !== null && alloc . check . enabled ) allocated ( 32 ) ;
328+ } ;
329+ } ) ;
294330 Obj . prototype = JObject . prototype ;
295331 C = withAccess ( Obj , kinds ) ;
296332 }
@@ -306,13 +342,34 @@ function finalizableCtorFor(kinds: string): ObjCtor {
306342 let C = finalizableCtors . get ( kinds ) ;
307343 if ( C ) return C ;
308344 const Storage = ctorFor ( kinds ) ;
309- let accessors = "" ;
310- for ( let i = 0 ; i < kinds . length ; i ++ ) accessors += `get f${ i } (){return this.st.f${ i } },set f${ i } (v){this.st.f${ i } =v},` ;
311- const Obj = new Function ( "P" , "S" , "A" , "T" , `
345+ const Obj = generate ( ( ) => {
346+ let accessors = "" ;
347+ for ( let i = 0 ; i < kinds . length ; i ++ ) accessors += `get f${ i } (){return this.st.f${ i } },set f${ i } (v){this.st.f${ i } =v},` ;
348+ return new Function ( "P" , "S" , "A" , "T" , `
312349 const proto = Object.create(P, Object.getOwnPropertyDescriptors({${ accessors } }));
313350 function Obj(cls){this.cls=cls;this.s=null;this.st=new S(cls);if(A.check!==null&&A.check.enabled)T(32);}
314351 Obj.prototype = proto;
315352 return Obj;` ) ( JObject . prototype , Storage , alloc , allocated ) ;
353+ } , ( ) => {
354+ const proto = Object . create ( JObject . prototype ) ;
355+ for ( let i = 0 ; i < kinds . length ; i ++ ) {
356+ const k = fieldKey ( i ) ;
357+ Object . defineProperty ( proto , k , {
358+ get ( this : any ) { return this . st [ k ] ; } ,
359+ set ( this : any , v : any ) { this . st [ k ] = v ; } ,
360+ enumerable : true ,
361+ configurable : true ,
362+ } ) ;
363+ }
364+ function Obj ( this : any , cls : JClass ) {
365+ this . cls = cls ;
366+ this . s = null ;
367+ this . st = new Storage ( cls ) ;
368+ if ( alloc . check !== null && alloc . check . enabled ) allocated ( 32 ) ;
369+ }
370+ Obj . prototype = proto ;
371+ return Obj ;
372+ } ) ;
316373 C = withAccess ( Obj , kinds ) ;
317374 finalizableCtors . set ( kinds , C ) ;
318375 return C ;
@@ -684,14 +741,25 @@ export function setAllocationCheck(cap: number, usage: () => number, o: { gc?: (
684741 alloc . check = { cap, step : Math . min ( cap , Math . max ( 4 << 20 , cap / 16 ) ) , allocated : 0 , baseline : 0 , enabled : false , over : false , usage, gc : o . gc , lazy : ! ! o . lazy } ;
685742}
686743export function clearAllocationCheck ( ) { alloc . check = null ; }
687- // Lets go of what the VM holds for the program's sake (soft referents, caches
688- // of unloadable classes) before an OutOfMemoryError; returns whether there was any.
689- let reclaim : ( ( ) => boolean ) | null = null ;
690- export function setReclaim ( f : ( ) => boolean ) { reclaim = f ; }
744+ // Lets go of what each VM holds for the program's sake (soft referents, caches
745+ // of unloadable classes) before an OutOfMemoryError; true when there was any.
746+ const reclaimers = new Set < ( ) => boolean > ( ) ;
747+ export function addReclaim ( f : ( ) => boolean ) { reclaimers . add ( f ) ; }
748+ export function removeReclaim ( f : ( ) => boolean ) { reclaimers . delete ( f ) ; }
749+ function reclaim ( ) : boolean {
750+ let any = false ;
751+ for ( const f of reclaimers ) if ( f ( ) ) any = true ;
752+ return any ;
753+ }
754+ // The collector java-web calls: its own wrapper, else the host's gc. A host's
755+ // globalThis.gc is never replaced.
756+ let vmGc : ( ( ) => void ) | undefined ;
757+ export function setGc ( f : ( ) => void ) { vmGc = f ; }
758+ export function collector ( ) : ( ( ) => void ) | undefined { return vmGc ?? ( globalThis as { gc ?: ( ) => void } ) . gc ; }
691759// An allocation that cannot succeed still collects first, as HotSpot does, so
692760// the program sees soft references cleared and unloaded classes gone.
693761function collectBeforeGivingUp ( ) {
694- if ( reclaim ?. ( ) ) ( alloc . check ?. gc ?? ( globalThis as { gc ?: ( ) => void } ) . gc ) ?.( ) ;
762+ if ( reclaim ( ) ) ( alloc . check ?. gc ?? collector ( ) ) ?.( ) ;
695763}
696764// A JObject made while a collection hands a finalizable object to its Finalizer:
697765// it is not an allocation that can fail.
@@ -776,7 +844,7 @@ function allocated(bytes: number) {
776844 if ( check . usage ( ) - check . baseline > check . cap ) check . gc ?.( ) ;
777845 let over = check . usage ( ) - check . baseline > check . cap ;
778846 // soft references and unloadable classes go before an OutOfMemoryError, as in HotSpot
779- if ( over && reclaim ?. ( ) && check . gc ) {
847+ if ( over && reclaim ( ) && check . gc ) {
780848 check . gc ( ) ;
781849 over = check . usage ( ) - check . baseline > check . cap ;
782850 }
0 commit comments