code wiki / _hdl_build / nx_js_es_census.nx

nx_js_es_census.nx source

↩ module page · 62 lines · 4449 B

1// nx_js_es_census.nx -- MEASURE the modern-ES surface of the sovereign JS engine (present vs absent), so 2// the SPA-giant arc builds the RIGHT features in priority order (data-driven, no-wave). Each probe runs a 3// real snippet via js_run_source; PRESENT iff it yields the expected value (absence usually = parse error). 4// GREEN iff the census is VALID (pos-control PRESENT + neg-control ABSENT) -- NOT iff all present. The gaps 5// ARE the deliverable: they tell us exactly what real bundles (React/etc.) need that we lack. 6import "nx_syscalls.nx" 7import "nx_itoa_lib.nx" // shared MSB-first emitter (zero-alloc) 8import "nx_js_eval.nx" 9 10func es_p(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 11// MIGRATED to the shared emitter (debt 1785563586). The old body mmapped a scratch buffer 12// per call and never freed it. At PAGE granularity that is 4096B leaked PER CALL -- the 13// defect that took 28.5GB of a 36GB host in nx_ts_lumadiff (2MB input, ~3.66M calls). 14// nxi_* is MSB-first, allocates NOTHING, and emits identical bytes including the sign. 15func es_pn(v: i64) -> i64 { nxi_out(v); return 0 } 16func es_clen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n } 17// run src; 1 if it yields VAL_NUM == want, else 0 (error or wrong value = ABSENT). 18func esc_num(src: *u8, want: i64) -> i64 { 19 let out: *i64 = sys_mmap(16) as *i64 20 if js_run_source(src, es_clen(src), out) != 0 { return 0 } 21 if out[0] != VAL_NUM { return 0 } 22 if out[1] != want { return 0 } 23 return 1 24} 25func feat(name: *u8, ok: i64, pc: *i64, tc: *i64) -> i64 { 26 tc[0] = tc[0] + 1 27 es_p(name) 28 if ok == 1 { pc[0] = pc[0] + 1; es_p(": PRESENT\n\x00" as *u8) } else { es_p(": ABSENT\n\x00" as *u8) } 29 return ok 30} 31 32func main() -> i64 { 33 let pc: *i64 = sys_mmap(8) as *i64; pc[0] = 0 34 let tc: *i64 = sys_mmap(8) as *i64; tc[0] = 0 35 es_p("=== nx_js_es_census: modern-ES surface of the sovereign engine ===\n\x00" as *u8) 36 37 let arrow: i64 = feat("arrow functions (POS CTRL)\x00" as *u8, esc_num("var f=x=>x+1;f(6)\x00" as *u8, 7), pc, tc) 38 feat("template literals `a${x}b`\x00" as *u8, esc_num("var x=`a${1+1}b`;x.length\x00" as *u8, 3), pc, tc) 39 feat("array destructuring [a,b]=\x00" as *u8, esc_num("var [a,b]=[3,4];a+b\x00" as *u8, 7), pc, tc) 40 feat("object destructuring {a,b}=\x00" as *u8, esc_num("var o={p:3,q:4};var {p,q}=o;p+q\x00" as *u8, 7), pc, tc) 41 feat("spread in array [...a]\x00" as *u8, esc_num("var s=[1,2];var t=[0,...s];t.length\x00" as *u8, 3), pc, tc) 42 feat("rest params function(...r)\x00" as *u8, esc_num("function g(...r){return r.length}g(1,2,3)\x00" as *u8, 3), pc, tc) 43 feat("default params (a,b=5)\x00" as *u8, esc_num("function h(a,b=5){return a+b}h(1)\x00" as *u8, 6), pc, tc) 44 feat("for-of loop\x00" as *u8, esc_num("var s=0;for(var x of [1,2,3]){s=s+x}s\x00" as *u8, 6), pc, tc) 45 feat("for-in loop\x00" as *u8, esc_num("var o={a:1,b:2};var k=0;for(var p in o){k=k+1}k\x00" as *u8, 2), pc, tc) 46 feat("class + new + this\x00" as *u8, esc_num("class C{constructor(n){this.n=n}}var c=new C(8);c.n\x00" as *u8, 8), pc, tc) 47 feat("method shorthand + this\x00" as *u8, esc_num("var o={n:5,get(){return this.n}};o.get()\x00" as *u8, 5), pc, tc) 48 feat("nullish coalescing ??\x00" as *u8, esc_num("var r=null??9;r\x00" as *u8, 9), pc, tc) 49 feat("optional chaining ?.\x00" as *u8, esc_num("var o={};var r=(o.a?.b===undefined)?1:0;r\x00" as *u8, 1), pc, tc) 50 feat("Array.push()\x00" as *u8, esc_num("var a=[1];a.push(2);a.length\x00" as *u8, 2), pc, tc) 51 feat("JSON.parse()\x00" as *u8, esc_num("var o=JSON.parse('{\"n\":7}');o.n\x00" as *u8, 7), pc, tc) 52 53 let permil: i64 = (pc[0] * 1000) / tc[0] 54 es_p("ES-CENSUS present=\x00" as *u8); es_pn(pc[0]); es_p("/\x00" as *u8); es_pn(tc[0]); es_p(" permil=\x00" as *u8); es_pn(permil); es_p("\n\x00" as *u8) 55 56 // validity controls: pos = arrow (must be PRESENT), neg = a bogus method (must be ABSENT). 57 let neg: i64 = esc_num("var q=[1].zzznope123();q\x00" as *u8, 1) // no such method -> error -> 0 58 es_p("validity: pos(arrow)=\x00" as *u8); es_pn(arrow); es_p(" neg(bogus)=\x00" as *u8); es_pn(neg); es_p("\n\x00" as *u8) 59 if arrow == 1 { if neg == 0 { es_p("ESCENSUS verdict=GREEN (census valid)\n\x00" as *u8); return 0 } } 60 es_p("ESCENSUS verdict=RED (census INVALID - controls failed)\n\x00" as *u8) 61 return 1 62}