code wiki / _hdl_build / nx_js_splay_diag.nx
nx_js_splay_diag.nx source
↩ module page · 25 lines · 1976 B
1// diagnose which Splay feature is missing. expect_exit: 0 license_tier: ORIGINAL
2import "nx_js_vm.nx"
3func dw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 }
4func dn(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 }
5func chk(label: *u8, src: *u8, want: i64) -> i64 {
6 let o: *i64 = sys_mmap(16) as *i64
7 let rc: i64 = compile_run(src, o)
8 dw(label); dw(": rc="); dn(rc); dw(" tag="); dn(o[0]); dw(" val="); dn(o[1])
9 if rc == 0 { if o[0] == VAL_NUM { if o[1] == want { dw(" OK\n" as *u8); return 1 } } }
10 dw(" <-- GAP (want "); dn(want); dw(")\n" as *u8); return 0
11}
12func main(argc: i64, argv: *i64) -> i64 {
13 dw("=== Splay feature diagnostic ===\n" as *u8)
14 chk("T1 value-|| (null||7)" as *u8, "var a=null;var b=7;a||b" as *u8, 7)
15 chk("T2 Array.push+length" as *u8, "var a=[];a.push(3);a.push(5);a.length" as *u8, 2)
16 chk("T3 push then index" as *u8, "var a=[];a.push(3);a.push(5);a[0]*10+a[1]" as *u8, 35)
17 chk("T6 do-while" as *u8, "var i=0;var s=0;do{s=s+i;i=i+1;}while(i<5);s" as *u8, 10)
18 chk("T7 !obj truthiness" as *u8, "var o={};var e=null;(!e?1:0)+(!o?0:1)" as *u8, 2)
19 chk("T9 proto.p=null read" as *u8, "function F(){}F.prototype.p=null;var o=new F();(o.p==null?5:0)" as *u8, 5)
20 chk("T4 fn-static-prop + new" as *u8, "function F(){}F.G=function(x){this.x=x;};var o=new F.G(7);o.x" as *u8, 7)
21 chk("T5 fn-static prototype method" as *u8, "function F(){}F.G=function(x){this.x=x;};F.G.prototype.get=function(){return this.x;};var o=new F.G(9);o.get()" as *u8, 9)
22 chk("T8 String(int).length" as *u8, "String(42).length" as *u8, 2)
23 dw("ALL DONE\n" as *u8)
24 return 0
25}