code wiki / _hdl_build / nx_js_missingarg_gate.nx

nx_js_missingarg_gate.nx source

↩ module page · 36 lines · 2656 B

1import "nx_gate_gn.nx" 2// nx_js_missingarg_gate.nx -- OMITTED parameters MUST bind `undefined`, even when the call arena has been 3// REUSED by prior calls (the bytes are stale env garbage otherwise). This bit jsbn: `new BigInteger(a)` 4// (2 params omitted) read a previous call's values for b/c -> `b==null` false -> wrong ctor branch, only 5// AFTER a call storm dirtied the arena. So the gate WARMS the arena first, THEN checks defaulting. 6// V8-pinned, both tiers. expect_exit: 0 license_tier: ORIGINAL 7import "nx_js_vm.nx" 8func gw(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } sys_write(1, s, n); return 0 } 9func chk2(label: *u8, src: *u8, want: i64) -> i64 { 10 let ov: *i64 = sys_mmap(16) as *i64 11 let ot: *i64 = sys_mmap(16) as *i64 12 let rv: i64 = compile_run(src, ov) 13 let rt: i64 = js_run_source(src, bsl(src), ot) 14 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) 15 if rv == 0 { if rt == 0 { if ov[1] == want { if ot[1] == want { gw(" ok\n" as *u8); return 1 } } } } 16 gw(" FAIL\n" as *u8) 17 return 0 18} 19func main(argc: i64, argv: *i64) -> i64 { 20 gw("=== nx_js_missingarg_gate: omitted params bind undefined even w/ a DIRTY (reused) arena ===\n" as *u8) 21 var pass: i64 = 0 22 var tot: i64 = 0 23 // 1) dirty the arena with a 3-arg call storm, THEN a 1-arg call: b,c MUST read undefined not stale 7/9. 24 tot=tot+1; pass = pass + chk2("dirty-then-default" as *u8, 25 "function fill(a,b,c){return a+b+c;} var s=0; var k=0; while(k<300){ s=s+fill(7,8,9); k=k+1; } function pick(a,b,c){ if(b==null){ if(c==null){ return 1; } } return 0; } pick(5)" as *u8, 1) 26 // 2) the exact jsbn dispatch shape after a storm 27 tot=tot+1; pass = pass + chk2("jsbn-ctor-dispatch" as *u8, 28 "function noise(a,b,c){return a*b+c;} var s=0; var i=0; while(i<300){ s=s+noise(2,3,4); i=i+1; } function BI(a,b,c){ this.k=0; if(a!=null){ if('number'==typeof a){ this.k=1; } else if(b==null){ this.k=2; } else { this.k=3; } } } var o=new BI([9,8,7]); o.k" as *u8, 2) 29 // 3) omitted param used arithmetically defaults undefined -> NaN -> (x||0) fallback pattern 30 tot=tot+1; pass = pass + chk2("omitted-or-fallback" as *u8, 31 "function warm(a,b){return a+b;} var s=0; var j=0; while(j<200){ s=s+warm(1,2); j=j+1; } function f(a,b){ return a + (b?b:100); } f(5)" as *u8, 105) 32 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8) 33 if pass == tot { gw("=== GREEN: omitted params default undefined across a dirty arena, both tiers ===\n" as *u8); return 0 } 34 gw("=== RED ===\n" as *u8) 35 return 1 36}