code wiki / _hdl_build / nx_js_strcoerce_gate.nx

nx_js_strcoerce_gate.nx source

↩ module page · 37 lines · 2606 B

1import "nx_gate_gn.nx" 2// nx_js_strcoerce_gate.nx -- JS ToNumber coercion in arithmetic + String.split(STRING) + Math.pow over reals, 3// V8-pinned, both tiers. These three drove RayTrace: `100/"5"`=20 (was "5"->0 -> /0 -> Infinity -> a 4M-alloc 4// infinite pixel loop); `"5,5".split(",")`=["5","5"] (was ["5,5"]); Math.pow(float,float) exact (was int-trunc 5// -> 0 -> wrong specular highlights). expect_exit: 0 license_tier: ORIGINAL 6import "nx_js_vm.nx" 7func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 8func chk2(label: *u8, src: *u8, want: i64) -> i64 { 9 let ov: *i64 = sys_mmap(16) as *i64 10 let ot: *i64 = sys_mmap(16) as *i64 11 let rv: i64 = compile_run(src, ov) 12 let rt: i64 = js_run_source(src, bsl(src), ot) 13 gw(" " as *u8); gw(label); gw(": vm=" as *u8); gn(ov[1]); gw(" tree=" as *u8); gn(ot[1]); gw(" want=" as *u8); gn(want) 14 if rv == 0 { if rt == 0 { if ov[1] == want { if ot[1] == want { gw(" ok\n" as *u8); return 1 } } } } 15 gw(" FAIL\n" as *u8) 16 return 0 17} 18func main(argc: i64, argv: *i64) -> i64 { 19 gw("=== nx_js_strcoerce_gate: ToNumber arithmetic + split(string) + Math.pow(reals) (both tiers) ===\n" as *u8) 20 var pass: i64 = 0 21 var tot: i64 = 0 22 tot=tot+1; pass = pass + chk2("div-string" as *u8, "100/\x225\x22" as *u8, 20) 23 tot=tot+1; pass = pass + chk2("mul-string" as *u8, "2*\x225\x22" as *u8, 10) 24 tot=tot+1; pass = pass + chk2("sub-string" as *u8, "10-\x223\x22" as *u8, 7) 25 tot=tot+1; pass = pass + chk2("compound-div-string" as *u8, "var c=100; c/=\x225\x22; c" as *u8, 20) 26 tot=tot+1; pass = pass + chk2("div-by-zero-not-string0" as *u8, "var x=100/\x22z\x22; (x!=x)?1:0" as *u8, 1) 27 tot=tot+1; pass = pass + chk2("split-string-sep" as *u8, "\x225,5\x22.split(\x22,\x22).length" as *u8, 2) 28 tot=tot+1; pass = pass + chk2("split-piece" as *u8, "var a=\x22a,bb,c\x22.split(\x22,\x22); a[1].length" as *u8, 2) 29 tot=tot+1; pass = pass + chk2("split-empty-chars" as *u8, "\x22abc\x22.split(\x22\x22).length" as *u8, 3) 30 tot=tot+1; pass = pass + chk2("pow-int-exact" as *u8, "Math.pow(2,10)" as *u8, 1024) 31 tot=tot+1; pass = pass + chk2("pow-float-exp" as *u8, "(Math.pow(0.5,0.5)*1000)|0" as *u8, 707) 32 tot=tot+1; pass = pass + chk2("pow-float-base" as *u8, "(Math.pow(0.9,20)*1000)|0" as *u8, 121) 33 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8) 34 if pass == tot { gw("=== GREEN: ToNumber + split(string) + Math.pow(reals) match V8, both tiers ===\n" as *u8); return 0 } 35 gw("=== RED ===\n" as *u8) 36 return 1 37}