code wiki / _hdl_build / nx_gcscope_test.nx

nx_gcscope_test.nx source

↩ module page · 25 lines · 1748 B

1// nx_gcscope_test.nx -- SYSTEMATIC hypothesis for the context-dependent jQuery failures: does GC collect a 2// CLOSURE-CAPTURED var under heavy allocation? Sizzle captures `document` in its IIFE scope then allocates 3// massively (matchers, caches) -> if GC doesn't mark closure-captured envs, the var reads undefined at scale. 4import "nx_syscalls.nx" 5import "nx_js_eval.nx" 6func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n } 7func cw(s: *u8) -> i64 { sys_write(1,s,slen(s)); return 0 } 8func run1(label: *u8, js: *u8) -> i64 { 9 js_set_rt_dbg(1) 10 let obx: *i64 = sys_mmap(16) as *i64 11 js_run_source_keep_doc(js, slen(js), 0 as *u8, 0, obx) 12 let genv: *i64 = (obx[1]) as *i64 13 cw(label); cw(": " as *u8) 14 if (genv as i64)==0 { cw("NULL\n" as *u8); return 1 } 15 let ub: *u8 = sys_mmap(2048); let t: i64 = js_pending_total(genv) 16 if t==0 { cw("THREW(no fetch)" as *u8) } else { var i: i64=0; while i<t { let ul: i64=js_pending_url(genv,i,ub,2048); if ul>0 { cw(ub); cw(" " as *u8) } i=i+1 } } 17 cw("\n" as *u8); return 0 18} 19func main() -> i64 { 20 // closure captures `doc`, then allocates 200k objects, then reads doc.createElement through a nested fn. 21 run1("A closure-var vs GC 200k" as *u8, "function mk(){var doc={createElement:function(){return 'EL'}};function a(){return doc.createElement()}var junk=[];var i=0;while(i<200000){junk.push({a:i,b:'s'+i,c:[i,i+1]});i=i+1}return a()}fetch('x/r-'+mk());" as *u8) 22 // top-level var survives heavy alloc? 23 run1("B toplevel-var vs GC " as *u8, "var keep={createElement:function(){return 'K'}};var j=[];var i=0;while(i<200000){j.push({x:i,y:'z'+i});i=i+1}fetch('x/k-'+(typeof keep)+'-'+(typeof keep.createElement));" as *u8) 24 return 0 25}