code wiki / _hdl_build / nx_jq_run.nx
nx_jq_run.nx source
↩ module page · 82 lines · 4646 B
1// nx_jq_run.nx -- run REAL minified jQuery with the surrogate preamble + js_rt_dbg ON, so every runtime error
2// (REF-ERR undeclared / CALL-ERR missing-method / PROP-ERR base-tag) prints to STDERR. jQuery wraps feature
3// probes in try/catch, so MANY are caught+harmless -- the FATAL one (uncaught, halts execution) is the LAST few
4// stderr lines. That names jQuery's first real runtime gap (a missing global/method/DOM-property) to fix -> iterate.
5import "nx_syscalls.nx"
6import "nx_x509_trust_store.nx"
7import "nx_trust_store_load_from_certdata.nx"
8import "nx_video_sniff.nx"
9import "nx_video_sniff_fetch.nx"
10import "nx_surrogate.nx"
11const K_MAGIC_4194304: i64 = 4194304
12const K_MAGIC_16384: i64 = 16384
13const K_MAGIC_32768: i64 = 32768
14const K_MAGIC_2048: i64 = 2048
15
16func cw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
17func slen(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} return n }
18func pnum(n0: i64) -> i64 {
19 var n: i64 = n0
20 if n < 0 { cw("-" as *u8); n = 0 - n }
21 if n == 0 { cw("0" as *u8); return 0 }
22 let buf: *u8 = sys_mmap(32)
23 var i: i64 = 0
24 while n > 0 { buf[i] = ((n % 10) + 48) as u8; n = n / 10; i = i + 1 }
25 while i > 0 { i = i - 1; sys_write(1, ((buf as i64) + i) as *u8, 1) }
26 return 0
27}
28
29func main() -> i64 {
30 let r: i64 = nx_trust_store_load_from_certdata("data/mozilla_certdata.txt" as *u8, 512, K_MAGIC_4194304)
31 if r <= 0 { cw("certdata load failed\n" as *u8); sys_exit(1); return 1 }
32 let store: *TrustStore = r as *TrustStore
33 let status: *i64 = sys_mmap(8) as *i64
34 let jq: *u8 = sys_mmap(K_MAGIC_4194304)
35 let jn: i64 = nx_https_fetch_follow("https://code.jquery.com/jquery-3.7.1.min.js" as *u8, store, jq, K_MAGIC_4194304, 6, status)
36 if jn <= 0 { cw("(jquery fetch failed)\n" as *u8); return 1 }
37 let sgt: *i64 = sg_new(); sg_seed(sgt)
38 let pre: *u8 = sys_mmap(K_MAGIC_16384)
39 let pl: i64 = sg_build_preamble(sgt, pre, K_MAGIC_16384)
40 let cap: i64 = jn + K_MAGIC_32768
41 let prog: *u8 = sys_mmap(cap)
42 var o: i64 = 0
43 var a: i64 = 0; while a < pl { prog[o] = pre[a]; o = o + 1; a = a + 1 }
44 prog[o] = 59 as u8; o = o + 1; prog[o] = 10 as u8; o = o + 1
45 var j: i64 = 0; while j < jn { prog[o] = jq[j]; o = o + 1; j = j + 1 }
46 prog[o] = 0 as u8
47 // js_rt_dbg levels (dormant in prod): 1=leaf REF/CALL/PROP/STMT-ERR; 2=MARK/SEQ-ERR/OBJLIT-ERR/SETPROP-FAIL
48 // reliable-offset context; 3=per-statement STMT trace. Loop: run@2/3, read innermost gap, fix, re-run.
49 // To read a specific region, temporarily dump prog[a..b] (see git history). Resume gap: Sizzle document undefined.
50 js_set_rt_dbg(2)
51 let obx: *i64 = sys_mmap(16) as *i64
52 js_run_source_keep_doc(prog, o, 0 as *u8, 0, obx)
53 cw("=== jQuery run complete (rt errors above on stderr; last uncaught = the gap) ===\n" as *u8)
54 // INNERMOST unswallowed runtime-error offset (into prog = preamble+jQuery). This names the exact
55 // failing byte -> map it back to the jQuery construct that our engine can't yet run.
56 let ep: i64 = js_rt_errpos_get()
57 cw("RT-ERRPOS = " as *u8); pnum(ep); cw("\n" as *u8)
58 if ep >= 0 {
59 var a: i64 = ep - 40; if a < 0 { a = 0 }
60 let b: i64 = ep + 60
61 cw(" ctx: |" as *u8)
62 var c: i64 = a
63 while c < b { if c < o { sys_write(1, ((prog as i64) + c) as *u8, 1) } c = c + 1 }
64 cw("|\n at: " as *u8)
65 var d: i64 = a
66 while d < ep { cw(" " as *u8); d = d + 1 }
67 cw("^\n" as *u8)
68 }
69 // HOW FAR did jQuery get? Probe the globals it installs at the END of the factory (window.jQuery=window.$=jQuery).
70 // If these are defined, the factory RAN TO COMPLETION and any STMT-ERR is a late cosmetic path we can ignore.
71 let genv: *i64 = (obx[1]) as *i64
72 if (genv as i64) == 0 { cw("(NULL genv -- jQuery failed to parse)\n" as *u8); return 1 }
73 js_set_rt_dbg(0) // quiet the probe itself
74 let probe: *u8 = "fetch('https://p/window-'+(typeof window)+'.x');fetch('https://p/jQuery-'+(typeof jQuery)+'.x');fetch('https://p/dollar-'+(typeof window.jQuery)+'.x');fetch('https://p/fn-'+(typeof jQuery.fn)+'.x');fetch('https://p/each-'+(typeof jQuery.each)+'.x');" as *u8
75 js_run_more_in_genv(genv, probe, slen(probe))
76 let ub: *u8 = sys_mmap(K_MAGIC_2048)
77 let t: i64 = js_pending_total(genv)
78 var pi: i64 = 0
79 while pi < t { let ul: i64 = js_pending_url(genv, pi, ub, K_MAGIC_2048); if ul > 0 { cw(" PROBE " as *u8); cw(ub); cw("\n" as *u8) } pi = pi + 1 }
80 if t == 0 { cw(" (no probe fetches serviced -- jQuery global read itself threw)\n" as *u8) }
81 return 0
82}