code wiki / _hdl_build / nx_ui_exec_intent_gate.nx
nx_ui_exec_intent_gate.nx source
↩ module page · 58 lines · 5023 B
1// nx_ui_exec_intent_gate.nx -- EXECUTION-verify the DEMOTED-button pattern (task #35/#36): a click fires
2// an INTENT into a sovereign core (stubbed here as a plain fn owning the state), the core flips state, and
3// the adapter RENDERS it via classList -- the exact shape of the demoted mic/cam buttons in app.v2.js
4// (NX.uic_intent -> read state -> e.currentTarget.classList.toggle("on", state)). Proves the engine now
5// executes event + classList so this pattern is machine-verifiable, not browser-only. Drives js_render_page
6// (real engine path). NEG-controls give teeth. 0=GREEN / 1=RED.
7import "nx_syscalls.nx"
8import "nx_js_eval.nx"
9
10func r_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
11func r_pn(v: i64) -> i64 {
12 var m: i64 = v; if m < 0 { r_p("-\x00" as *u8); m = 0 - m }
13 let t: *u8 = sys_mmap(24); var k: i64 = 0
14 if m == 0 { t[0] = 48 as u8; k = 1 } else { while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } }
15 let o: *u8 = sys_mmap(24); var j: i64 = 0; while j < k { o[j] = t[k - 1 - j]; j = j + 1 }
16 sys_write(1, o, k); return 0 }
17func r_clen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
18func r_contains(buf: *u8, blen: i64, needle: *u8) -> i64 {
19 let nl: i64 = r_clen(needle); if nl == 0 { return 1 }
20 var i: i64 = 0
21 while (i + nl) <= blen { var j: i64 = 0; var ok: i64 = 1; while j < nl { if (buf[i + j] & 0xff) != (needle[j] & 0xff) { ok = 0; j = nl } else { j = j + 1 } } if ok == 1 { return 1 } i = i + 1 }
22 return 0 }
23func chk(cond: i64, name: *u8, pp: *i64, tp: *i64) -> i64 {
24 tp[0] = tp[0] + 1; r_p(name)
25 if cond == 1 { pp[0] = pp[0] + 1; r_p(" ok\n\x00" as *u8) } else { r_p(" FAIL\n\x00" as *u8) }
26 return 0 }
27
28func main() -> i64 {
29 r_p("=== nx_ui_exec_intent_gate: click -> core intent -> classList render (the demoted-button pattern) ===\n\x00" as *u8)
30 let pp: *i64 = sys_mmap(8) as *i64; pp[0] = 0
31 let tp: *i64 = sys_mmap(8) as *i64; tp[0] = 0
32 let out: *u8 = sys_mmap(131072)
33
34 // R1: mic starts ON; ONE click -> core flips to OFF -> classList("on") removed -> contains=false -> "MICOFF"
35 let p1: *u8 = "<html><body><button id=\"mute\" class=\"ctl\">mic</button><span id=\"out\">?</span><script>var mic=1;function core(){mic=mic?0:1;}var b=document.getElementById('mute');b.classList.toggle('on',mic===1);b.addEventListener('click',function(e){core();e.currentTarget.classList.toggle('on',mic===1);});b.click();document.getElementById('out').textContent=b.classList.contains('on')?'MICON':'MICOFF';</script></body></html>\x00" as *u8
36 let n1: i64 = js_render_page(p1, r_clen(p1), out, 131072)
37 chk(r_contains(out, n1, ">MICOFF<\x00" as *u8), "R1 click -> core OFF -> classList render OFF\x00" as *u8, pp, tp)
38
39 // R2: TWO clicks -> back ON -> "MICON" (proves the intent round-trips, not a one-way latch)
40 let p2: *u8 = "<html><body><button id=\"mute\" class=\"ctl\">mic</button><span id=\"out\">?</span><script>var mic=1;function core(){mic=mic?0:1;}var b=document.getElementById('mute');b.classList.toggle('on',mic===1);b.addEventListener('click',function(e){core();e.currentTarget.classList.toggle('on',mic===1);});b.click();b.click();document.getElementById('out').textContent=b.classList.contains('on')?'MICON':'MICOFF';</script></body></html>\x00" as *u8
41 let n2: i64 = js_render_page(p2, r_clen(p2), out, 131072)
42 chk(r_contains(out, n2, ">MICON<\x00" as *u8), "R2 two clicks -> back ON\x00" as *u8, pp, tp)
43
44 // R3: classList add/remove/contains directly (the primitives)
45 let p3: *u8 = "<html><body><div id=\"x\" class=\"a\"></div><span id=\"out\">?</span><script>var d=document.getElementById('x');d.classList.add('foo');d.classList.add('bar');d.classList.remove('foo');document.getElementById('out').textContent=(d.classList.contains('bar')&&!d.classList.contains('foo')&&d.classList.contains('a'))?'RM_OK':'RM_BAD';</script></body></html>\x00" as *u8
46 let n3: i64 = js_render_page(p3, r_clen(p3), out, 131072)
47 chk(r_contains(out, n3, ">RM_OK<\x00" as *u8), "R3 add/remove/contains primitives\x00" as *u8, pp, tp)
48
49 // R4 (NEG): a class never added -> contains=false -> 'ABSENT' (teeth: contains isn't always-true)
50 let p4: *u8 = "<html><body><div id=\"x\" class=\"a b\"></div><span id=\"out\">?</span><script>document.getElementById('out').textContent=document.getElementById('x').classList.contains('zzz')?'PRESENT':'ABSENT';</script></body></html>\x00" as *u8
51 let n4: i64 = js_render_page(p4, r_clen(p4), out, 131072)
52 chk(r_contains(out, n4, ">ABSENT<\x00" as *u8), "R4 NEG absent class -> contains false\x00" as *u8, pp, tp)
53
54 r_p(" SCORE: \x00" as *u8); r_pn(pp[0]); r_p("/\x00" as *u8); r_pn(tp[0]); r_p("\n\x00" as *u8)
55 if pp[0] == tp[0] { r_p("UI-EXEC-INTENT-GATE verdict=GREEN -- click->core-intent->classList render EXECUTES in the sovereign engine\n\x00" as *u8); return 0 }
56 r_p("UI-EXEC-INTENT-GATE verdict=RED\n\x00" as *u8)
57 return 1
58}