code wiki / _hdl_build / nx_ui_checks.nx
nx_ui_checks.nx source
↩ module page · 75 lines · 4666 B
1// nx_ui_checks.nx -- SHARED UI-wiring checks, called by BOTH validation eyes so they apply an IDENTICAL
2// definition of "wired + init-safe" to two DIFFERENT data sources (operator 2026-07-04: "redundancy in
3// seeing things -- top-down user/front-end validation AND bottom-up hardware-rung-up validation"):
4// - BOTTOM-UP (nx_ui_wiring_census): runs these on the LOCAL SOURCE we are about to ship.
5// - TOP-DOWN (nx_ui_live_verify): runs these on the LIVE SERVED BYTES the user actually receives.
6// Same checker, two sources -> disagreement = deploy drift (stale/truncated/edge-mangled) caught by
7// construction. No main(): pure library. license_tier: ORIGINAL
8import "nx_syscalls.nx"
9import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc)
10
11func uc_w(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
12// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch
13// buffer per call and never freed it -- 4096B leaked PER CALL at page granularity,
14// the defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M
15// calls). nxi_* is MSB-first, allocates NOTHING, emits identical bytes incl. sign.
16func uc_n(v: i64) -> i64 { nxi_out(v); return 0 }
17func uc_has(hay: *u8, n: i64, needle: *u8) -> i64 {
18 var nl: i64=0; while needle[nl]!=(0 as u8){nl=nl+1}
19 var i: i64=0
20 while i+nl<=n { var j: i64=0; var ok: i64=1; while j<nl { if hay[i+j]!=needle[j]{ok=0;j=nl} else {j=j+1} } if ok==1{return 1} i=i+1 }
21 return 0 }
22
23// scan html for `<button id="X">`; for each id, require js references `"X"` (its addEventListener wiring).
24// tally[0]=total buttons, tally[1]=dead. Prints a line per button. Returns dead count.
25func uc_check_buttons(html: *u8, hn: i64, js: *u8, jn: i64, tally: *i64) -> i64 {
26 var dead: i64 = 0
27 var i: i64 = 0
28 while i + 7 <= hn {
29 if html[i]==(60 as u8) { if html[i+1]==(98 as u8) { if html[i+2]==(117 as u8) { if html[i+3]==(116 as u8) { if html[i+4]==(116 as u8) { if html[i+5]==(111 as u8) { if html[i+6]==(110 as u8) {
30 var j: i64 = i + 7
31 var tagend: i64 = j
32 var g: i64 = 1
33 while g==1 { if tagend<hn { if html[tagend]==(62 as u8) { g=0 } else { tagend=tagend+1 } } else { g=0 } }
34 var k: i64 = j
35 while k+4 < tagend {
36 if html[k]==(105 as u8) { if html[k+1]==(100 as u8) { if html[k+2]==(61 as u8) { if html[k+3]==(34 as u8) {
37 let ido: i64 = k+4
38 var ide: i64 = ido
39 var g2: i64 = 1
40 while g2==1 { if ide<tagend { if html[ide]==(34 as u8) { g2=0 } else { ide=ide+1 } } else { g2=0 } }
41 let idlen: i64 = ide - ido
42 if idlen > 0 { if idlen < 40 {
43 let ndl: *u8 = sys_mmap(64)
44 ndl[0]=34 as u8
45 var z: i64=0; while z<idlen { ndl[1+z]=html[ido+z]; z=z+1 }
46 ndl[1+idlen]=34 as u8; ndl[2+idlen]=0 as u8
47 let referenced: i64 = uc_has(js, jn, ndl)
48 uc_w(" button #" as *u8); sys_write(1, ((html as i64)+ido) as *u8, idlen)
49 if referenced==1 { uc_w(" WIRED\n" as *u8) } else { uc_w(" !! DEAD (no handler)\n" as *u8); dead=dead+1 }
50 tally[0]=tally[0]+1
51 } }
52 k = tagend
53 } } } }
54 k = k + 1
55 }
56 i = tagend
57 } } } } } } }
58 i = i + 1
59 }
60 tally[1] = dead
61 return dead }
62
63// resource-intelligence init must be crash-safe: a top-level host-API call (getBattery) throwing
64// SYNCHRONOUSLY before the button wiring kills every button. Require the try{} guard. Returns bad count.
65func uc_check_init(js: *u8, jn: i64) -> i64 {
66 var init_bad: i64 = 0
67 if uc_has(js, jn, "getBattery" as *u8)==1 {
68 let g1: i64 = uc_has(js, jn, "try {\n if (navigator.getBattery)" as *u8)
69 let g2: i64 = uc_has(js, jn, "try { if (navigator.getBattery)" as *u8)
70 if g1==1 { uc_w(" getBattery init: TRY-GUARDED\n" as *u8) } else { if g2==1 { uc_w(" getBattery init: TRY-GUARDED\n" as *u8) } else { uc_w(" !! getBattery NOT try-guarded (sync throw kills buttons)\n" as *u8); init_bad=1 } }
71 } else { uc_w(" (no getBattery -- n/a)\n" as *u8) }
72 if uc_has(js, jn, "function replan()" as *u8)==1 {
73 if uc_has(js, jn, "function replan() {\n try {" as *u8)==1 { uc_w(" replan() NX.mb_plan: TRY-GUARDED\n" as *u8) } else { uc_w(" !! replan() not try-guarded\n" as *u8); init_bad=1 }
74 }
75 return init_bad }