code wiki / _hdl_build / nx_js_autoglobal_hoist_gate.nx

nx_js_autoglobal_hoist_gate.nx source

↩ module page · 36 lines · 2272 B

1import "nx_gate_gn.nx" 2// nx_js_autoglobal_hoist_gate.nx -- a NON-STRICT auto-global (`x = v`, no var) declared at top-level must be 3// visible to FUNCTION bodies even when the function is defined BEFORE the assignment in source (function 4// decls hoist + compile early; the global must be pre-declared into the root scope during the hoist pass). 5// This bit Octane Crypto: `nValue = "..."` (implicit global) read by jsbn functions. V8-pinned, both tiers. 6// 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_autoglobal_hoist_gate: fn defined BEFORE a top-level auto-global still sees it ===\n" as *u8) 21 var pass: i64 = 0 22 var tot: i64 = 0 23 // 1) function DEFINED BEFORE the auto-global assignment reads it (the jsbn shape). 24 tot=tot+1; pass = pass + chk2("fn-before-autoglobal-read" as *u8, 25 "function rd(){ return G + 1; } G = 41; rd()" as *u8, 42) 26 // 2) function WRITES an auto-global that a later fn reads 27 tot=tot+1; pass = pass + chk2("fn-writes-autoglobal" as *u8, 28 "function setit(){ H = 7; } function getit(){ return H * 2; } setit(); getit()" as *u8, 14) 29 // 3) auto-global assigned deep in source, read by an early function, called last 30 tot=tot+1; pass = pass + chk2("late-autoglobal-early-fn" as *u8, 31 "function use(){ return K.length; } var pad='xxxx'; K = 'abcdef'; use()" as *u8, 6) 32 gw(" --- " as *u8); gn(pass); gw("/" as *u8); gn(tot); gw(" ---\n" as *u8) 33 if pass == tot { gw("=== GREEN: top-level auto-globals visible to fn bodies (hoisted into root scope) ===\n" as *u8); return 0 } 34 gw("=== RED ===\n" as *u8) 35 return 1 36}