code wiki / _hdl_build / nx_js_semantics_gate.nx

nx_js_semantics_gate.nx source

↩ module page · 81 lines · 6422 B

1// nx_js_semantics_gate.nx -- JS semantics pinned during the EarleyBoyer root-cause hunt (2026-07-11), 2// V8-pinned on BOTH tiers. Consolidates the session probes (vardecl/dangling/eq/shape) into one gate: 3// 1. bare `var x;` RE-DECLARATION is a runtime NO-OP (params + prior vars survive; scheme2js re-declares 4// params pervasively -- clobbering them silently corrupted EarleyBoyer's whole computation) 5// 2. \uXXXX string escapes decode ("A"=="A"); UTF-8 byte-length NAMED DIVERGENCE ("ẜ".length==3 6// here vs V8's 1 UTF-16 unit -- equality/concat/ordering stay self-consistent) 7// 3. dangling-else binds to the INNER if; braced-then + unbraced-else keeps its else 8// 4. string === is CONTENT equality for built (non-literal) strings 9// 5. comma-seq return value + 9-arg closure call from an array slot (scheme2js parse->trees shape) 10// 6. instanceof USER constructor walks a cons chain 11// 7. (n).toString(16) radix + "s".toString() identity 12// 8. property READ on a number primitive -> undefined (not an error); undefined base still errors 13// expect_exit: 0 license_tier: ORIGINAL 14import "nx_js_vm.nx" 15func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 16func nn(v: i64) -> i64 { if v==0 { sys_write(1,"0" as *u8,1); return 0 } var m: i64=v; if m<0 { sys_write(1,"-" as *u8,1); m=0-m } let t: *u8=sys_mmap(24); var k: i64=0; while m>0 { t[k]=(48+(m%10)) as u8; m=m/10; k=k+1 } let o: *u8=sys_mmap(24); var q: i64=k-1; var x: i64=0; while q>=0 { o[x]=t[q]; x=x+1; q=q-1 } sys_write(1,o,x); return 0 } 17static g_want: i64 18func ct(label: *u8, src: *u8) -> i64 { // TREE tier 19 let rt: *i64 = sys_mmap(16) as *i64 20 let r: i64 = js_run_source(src, bsl(src), rt) 21 w(" " as *u8); w(label); w(": rc=" as *u8); nn(r); w(" got=" as *u8); nn(rt[1]); w(" want=" as *u8); nn(g_want) 22 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } 23 w(" FAIL\n" as *u8) 24 return 0 25} 26func cv(label: *u8, src: *u8) -> i64 { // VM/JIT tier 27 let rt: *i64 = sys_mmap(16) as *i64 28 let r: i64 = compile_run(src, rt) 29 w(" " as *u8); w(label); w(": rc=" as *u8); nn(r); w(" got=" as *u8); nn(rt[1]); w(" want=" as *u8); nn(g_want) 30 if r == 0 { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } 31 w(" FAIL\n" as *u8) 32 return 0 33} 34func tree_suite() -> i64 { 35 var p: i64 = 0 36 g_want=5; p=p+ct("redecl-param " as *u8, "function f(a){ var a; return a; } f(5)" as *u8) 37 g_want=7; p=p+ct("redecl-var " as *u8, "function g(){ var t; t=7; var t; return t; } g()" as *u8) 38 g_want=9; p=p+ct("redecl-init " as *u8, "function h(a){ var a = 9; return a; } h(5)" as *u8) 39 g_want=1; p=p+ct("uesc-eq-A " as *u8, "(\x22\\u0041\x22==\x22A\x22)?1:0" as *u8) 40 g_want=1; p=p+ct("uesc-len-1 " as *u8, "\x22\\u1E9C\x22.length" as *u8) 41 g_want=2; p=p+ct("dangle-inner " as *u8, "function f(a,b){ if(a) if(b) return 1; else return 2; else return 3; } f(1,0)" as *u8) 42 g_want=3; p=p+ct("brace-else " as *u8, "function f(a){ if(a) { return 1; } else return 3; } f(0)" as *u8) 43 g_want=1; p=p+ct("streq-built " as *u8, "((\x22a\x22+\x22b\x22) === \x22ab\x22)?1:0" as *u8) 44 g_want=14; p=p+ct("seq9-arrslot " as *u8, "function mk(){ var v = new Array(11); v[9] = function(a,b,c,d,e,f,g,h,i){ return a+b+c+d+e+f+g+h+i; }; return v; } function pt(p2,a,b){ var f; return ((f = (p2[(9)])), (f(a,b,1,1,1,1,1,1,1))); } pt(mk(), 3, 4)" as *u8) 45 g_want=2; p=p+ct("instof-chain " as *u8, "function P(a,b){this.car=a;this.cdr=b;} var l=new P(1,new P(2,null)); var n=0; while ((l instanceof P)) { n=n+1; l=(l.cdr); } n" as *u8) 46 g_want=1; p=p+ct("num-tostr-16 " as *u8, "((255).toString(16)==\x22ff\x22)?1:0" as *u8) 47 g_want=1; p=p+ct("str-tostr-id " as *u8, "(\x22xy\x22.toString()==\x22xy\x22)?1:0" as *u8) 48 g_want=1; p=p+ct("numprop-undef " as *u8, "(typeof (5).anything==\x22undefined\x22)?1:0" as *u8) 49 g_want=1; p=p+ct("undefprop-errs " as *u8, "var r=0; try{ var u; u.x; }catch(e){ r=1; } r" as *u8) 50 return p 51} 52func vm_suite() -> i64 { 53 var p: i64 = 0 54 g_want=5; p=p+cv("redecl-param " as *u8, "function f(a){ var a; return a; } f(5)" as *u8) 55 g_want=7; p=p+cv("redecl-var " as *u8, "function g(){ var t; t=7; var t; return t; } g()" as *u8) 56 g_want=9; p=p+cv("redecl-init " as *u8, "function h(a){ var a = 9; return a; } h(5)" as *u8) 57 g_want=1; p=p+cv("uesc-eq-A " as *u8, "(\x22\\u0041\x22==\x22A\x22)?1:0" as *u8) 58 g_want=1; p=p+cv("uesc-len-1 " as *u8, "\x22\\u1E9C\x22.length" as *u8) 59 g_want=2; p=p+cv("dangle-inner " as *u8, "function f(a,b){ if(a) if(b) return 1; else return 2; else return 3; } f(1,0)" as *u8) 60 g_want=3; p=p+cv("brace-else " as *u8, "function f(a){ if(a) { return 1; } else return 3; } f(0)" as *u8) 61 g_want=1; p=p+cv("streq-built " as *u8, "((\x22a\x22+\x22b\x22) === \x22ab\x22)?1:0" as *u8) 62 g_want=14; p=p+cv("seq9-arrslot " as *u8, "function mk(){ var v = new Array(11); v[9] = function(a,b,c,d,e,f,g,h,i){ return a+b+c+d+e+f+g+h+i; }; return v; } function pt(p2,a,b){ var f; return ((f = (p2[(9)])), (f(a,b,1,1,1,1,1,1,1))); } pt(mk(), 3, 4)" as *u8) 63 g_want=2; p=p+cv("instof-chain " as *u8, "function P(a,b){this.car=a;this.cdr=b;} var l=new P(1,new P(2,null)); var n=0; while ((l instanceof P)) { n=n+1; l=(l.cdr); } n" as *u8) 64 g_want=1; p=p+cv("num-tostr-16 " as *u8, "((255).toString(16)==\x22ff\x22)?1:0" as *u8) 65 g_want=1; p=p+cv("str-tostr-id " as *u8, "(\x22xy\x22.toString()==\x22xy\x22)?1:0" as *u8) 66 g_want=1; p=p+cv("numprop-undef " as *u8, "(typeof (5).anything==\x22undefined\x22)?1:0" as *u8) 67 g_want=1; p=p+cv("undefprop-errs " as *u8, "var r=0; try{ var u; u.x; }catch(e){ r=1; } r" as *u8) 68 return p 69} 70func main(argc: i64, argv: *i64) -> i64 { 71 w("=== nx_js_semantics_gate: EarleyBoyer-hunt semantics, V8-pinned, both tiers ===\n" as *u8) 72 var pass: i64 = 0 73 w("--- tree tier (js_run_source) ---\n" as *u8) 74 pass = pass + tree_suite() 75 w("--- VM/JIT tier (compile_run) ---\n" as *u8) 76 pass = pass + vm_suite() 77 w(" --- " as *u8); nn(pass); w("/28 ---\n" as *u8) 78 if pass == 28 { w("=== GREEN: var-redecl no-op, \\u escapes, dangling-else, string ===, seq9, instanceof-chain, toString, primitive props ===\n" as *u8); return 0 } 79 w("=== RED ===\n" as *u8) 80 return 1 81}