code wiki / _hdl_build / nx_wdoc_test.nx
nx_wdoc_test.nx source
↩ module page · 29 lines · 1582 B
1// nx_wdoc_test.nx -- does `window.document` resolve in headless mode with the surrogate preamble? jQuery does
2// `var document=window.document` -> if window.document is undefined, EVERY later document.* throws (Sizzle #14).
3import "nx_syscalls.nx"
4import "nx_video_sniff.nx"
5import "nx_video_sniff_fetch.nx"
6import "nx_surrogate.nx"
7func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
8func cw(s: *u8) -> i64 { sys_write(1,s,slen(s)); return 0 }
9func main() -> i64 {
10 let sgt: *i64 = sg_new(); sg_seed(sgt)
11 let pre: *u8 = sys_mmap(16384)
12 let pl: i64 = sg_build_preamble(sgt, pre, 16384)
13 // append a probe of document vs window.document vs createElement
14 let probe: *u8 = "var doc=window.document;var earlyReturn=(doc==null||doc.nodeType!==9||!doc.documentElement);fetch('x/nodeType-'+doc.nodeType+'-docEl-'+(typeof doc.documentElement)+'-setDocWouldEarlyReturn-'+earlyReturn);" as *u8
15 var o: i64 = pl
16 var i: i64 = 0
17 while probe[i] != (0 as u8) { pre[o] = probe[i]; o = o + 1; i = i + 1 }
18 pre[o] = 0 as u8
19 js_set_rt_dbg(1)
20 let obx: *i64 = sys_mmap(16) as *i64
21 js_run_source_keep_doc(pre, o, 0 as *u8, 0, obx)
22 let genv: *i64 = (obx[1]) as *i64
23 if (genv as i64)==0 { cw("NULL genv\n" as *u8); return 1 }
24 let ub: *u8 = sys_mmap(2048); let t: i64 = js_pending_total(genv)
25 var pi: i64 = 0
26 while pi < t { let ul: i64 = js_pending_url(genv, pi, ub, 2048); if ul>0 { cw(" " as *u8); cw(ub); cw("\n" as *u8) } pi = pi + 1 }
27 if t==0 { cw("(no fetch -- probe threw)\n" as *u8) }
28 return 0
29}