code wiki / _hdl_build / nx_js_indel_gate.nx
nx_js_indel_gate.nx source
↩ module page · 41 lines · 3494 B
1// nx_js_indel_gate.nx -- the `in` operator + `delete` operator, V8-pinned, both tiers. `in` = property existence
2// (own+inherited, arrays by index); `delete` = tombstone removal (prop gone, siblings survive, for-in/keys skip it,
3// re-add works). delete DECLINES to the tree in the VM (ref-based, no BC yet) so compile_run falls back -- still
4// correct. Helpers <=2 data args + g_want global (nx_cc 3-arg-helper miscompile). expect_exit: 0 license_tier: ORIGINAL
5import "nx_js_vm.nx"
6func w(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
7func 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 }
8static g_want: i64
9func tck(label: *u8, src: *u8) -> i64 {
10 let ov: *i64 = sys_mmap(16) as *i64
11 let rt: *i64 = sys_mmap(16) as *i64
12 let rv: i64 = compile_run(src, ov)
13 let rr: i64 = js_run_source(src, bsl(src), rt) // tree tier too (delete declines VM->tree; assert both)
14 w(" " as *u8); w(label); w(": vm="); nn(ov[1]); w(" tree="); nn(rt[1]); w(" want="); nn(g_want)
15 if rv == 0 { if rr == 0 { if ov[1] == g_want { if rt[1] == g_want { w(" ok\n" as *u8); return 1 } } } }
16 w(" FAIL\n" as *u8)
17 return 0
18}
19func main(argc: i64, argv: *i64) -> i64 {
20 w("=== nx_js_indel_gate: `in` + `delete`, V8-pinned, both tiers ===\n" as *u8)
21 var pass: i64 = 0
22 var tot: i64 = 0
23 g_want=1; tot=tot+1; pass=pass+tck("in-present " as *u8, "var o={a:1,b:2}; (\x22a\x22 in o)?1:0" as *u8)
24 g_want=0; tot=tot+1; pass=pass+tck("in-absent " as *u8, "var o={a:1,b:2}; (\x22z\x22 in o)?1:0" as *u8)
25 g_want=1; tot=tot+1; pass=pass+tck("in-array-idx " as *u8, "var a=[10,20,30]; (1 in a)?1:0" as *u8)
26 g_want=0; tot=tot+1; pass=pass+tck("in-array-oob " as *u8, "var a=[10,20,30]; (5 in a)?1:0" as *u8)
27 g_want=0; tot=tot+1; pass=pass+tck("del-then-in " as *u8, "var o={a:1,b:2}; delete o.a; (\x22a\x22 in o)?1:0" as *u8)
28 g_want=2; tot=tot+1; pass=pass+tck("del-sibling " as *u8, "var o={a:1,b:2}; delete o.a; o.b" as *u8)
29 g_want=1; tot=tot+1; pass=pass+tck("del-undefined " as *u8, "var o={a:1,b:2}; delete o.a; (o.a===undefined)?1:0" as *u8)
30 g_want=2; tot=tot+1; pass=pass+tck("del-forin-cnt " as *u8, "var o={a:1,b:2,c:3}; delete o.b; var s=0; for(var k in o){s=s+1;} s" as *u8)
31 g_want=5; tot=tot+1; pass=pass+tck("del-readd " as *u8, "var o={a:1}; delete o.a; o.a=5; o.a" as *u8)
32 g_want=1; tot=tot+1; pass=pass+tck("del-index " as *u8, "var o={}; o[\x22k\x22]=9; delete o[\x22k\x22]; (\x22k\x22 in o)?0:1" as *u8)
33 g_want=1; tot=tot+1; pass=pass+tck("del-return-t " as *u8, "var o={a:1}; (delete o.a)?1:0" as *u8)
34 g_want=2; tot=tot+1; pass=pass+tck("in-forloop-ok " as *u8, "var n=0; for(var i=0;i<2;i=i+1){n=n+1;} n" as *u8)
35 g_want=7; tot=tot+1; pass=pass+tck("seq-value-last" as *u8, "var x=0; (x=1, x=2, x+5)" as *u8)
36 g_want=6; tot=tot+1; pass=pass+tck("seq-for-update" as *u8, "var i=0,j=0; for(var k=0;k<3;i++,j++,k++){} i+j" as *u8)
37 w(" --- " as *u8); nn(pass); w("/" as *u8); nn(tot); w(" ---\n" as *u8)
38 if pass == tot { w("=== GREEN: `in` + `delete` match V8, both tiers ===\n" as *u8); return 0 }
39 w("=== RED ===\n" as *u8)
40 return 1
41}