code wiki / _hdl_build / nx_simlab_pack.nx

nx_simlab_pack.nx source

↩ module page · 93 lines · 4999 B

1// nx_simlab_pack.nx -- Compiles nx_simlab.nx to WASM and packages it into HTML using sovereign tools and syscalls. 2import "nx_gate_gn.nx" 3import "nx_gate_base.nx" 4// nx_simlab_pack.nx -- SOVEREIGN build orchestrator for the browser sim-lab: nx -> WAT -> WASM -> self-contained 5// HTML, entirely via the Nishi ecosystem from the hardware rung up (sys_fork / sys_execve / sys_wait4 / sys_dup3 / 6// sys_openat / sys_read_file). NO shell, NO sh/cp/wc/xxd/tail -- the hand-shell build scripts were the wrong, 7// non-sovereign path (and the unstable one). Same fork/exec idiom as nx_gate_sweep / nx_synced_push. It (1) builds 8// the three sovereign tools through the runner, (2) compiles runtime/nx_simlab.nx to WAT (capturing the tool's 9// stdout to a file via dup3 -- the sovereign equivalent of `> out`), (3) assembles WAT->WASM, (4) packages 10// WASM->HTML, (5) reports byte sizes via sys_read_file, and (6) unlinkat-removes the retired hand-shell scripts. 11// CWD must be nxc2. license_tier: ORIGINAL expect_exit: 0 12import "nx_syscalls.nx" 13const K_MAGIC_2000: i64 = 2000 14 15func grow(name: *u8, ok: i64) -> i64 { if ok==1 { gw(" PASS " as *u8) } else { gw(" FAIL " as *u8) } gw(name); gw(" 16" as *u8); return ok } 17func sp_unlink(p: *u8) -> i64 { return __syscall(263, AT_FDCWD, p, 0, 0, 0, 0) } // unlinkat (raw x86_64 263, per nx_gate_sweep) 18 19// spawn `path` with up to 3 args; if redir != 0, child stdout -> that file (truncating). returns child exit code. 20func spawn(path: *u8, a0: *u8, a1: *u8, a2: *u8, redir: *u8) -> i64 { 21 let pid: i64 = sys_fork() 22 if pid == 0 { 23 if (redir as i64) != 0 { 24 let fd: i64 = sys_openat_wr(redir, 420) 25 if fd >= 0 { sys_dup3(fd, 1, 0) } 26 } else { 27 let dn: i64 = sys_openat_wr("/dev/null\x00" as *u8, 420) 28 if dn >= 0 { sys_dup3(dn, 2, 0) } // silence stderr; keep stdout for our own report 29 } 30 let argv: *i64 = sys_mmap(64) as *i64 31 var n: i64 = 0 32 argv[0] = path as i64; n = 1 33 if (a0 as i64) != 0 { argv[n] = a0 as i64; n = n + 1 } 34 if (a1 as i64) != 0 { argv[n] = a1 as i64; n = n + 1 } 35 if (a2 as i64) != 0 { argv[n] = a2 as i64; n = n + 1 } 36 argv[n] = 0 37 let envp: *i64 = sys_mmap(16) as *i64 38 envp[0] = "PATH=/usr/bin:/bin\x00" as *u8 as i64; envp[1] = 0 39 sys_execve(path, argv, envp) 40 sys_exit(127) 41 } 42 let st: *i64 = sys_mmap(16) as *i64 43 sys_wait4(pid, st, 0) 44 return (st[0] >> 8) & 0xff 45} 46 47func fsize(path: *u8) -> i64 { 48 let lp: *i64 = sys_mmap(8) as *i64; lp[0] = 0 49 let p: *u8 = sys_read_file(path, lp) 50 if (p as i64) == 0 { return -1 } 51 return lp[0] 52} 53 54func main() -> i64 { 55 gw("=== nx_simlab_pack: SOVEREIGN nx->wat->wasm->html (fork/exec, NO shell) ===\n") 56 let RUNNER: *u8 = "_offc/nx_sov_build_run.elf\x00" as *u8 57 let WAT_OUT: *u8 = "knowledge/nx_simlab.wat\x00" as *u8 58 let WASM_OUT: *u8 = "web_assets/nx_simlab.wasm\x00" as *u8 59 let HTML_OUT: *u8 = "knowledge/nx_simlab.html\x00" as *u8 60 61 gw(" [1] build sovereign tools via the runner...\n") 62 spawn(RUNNER, "nx_compile_wat\x00" as *u8, 0 as *u8, 0 as *u8, "/dev/null\x00" as *u8) 63 spawn(RUNNER, "nx_wat_compiler\x00" as *u8, 0 as *u8, 0 as *u8, "/dev/null\x00" as *u8) 64 spawn(RUNNER, "nx_simlab_html\x00" as *u8, 0 as *u8, 0 as *u8, "/dev/null\x00" as *u8) 65 66 gw(" [2] nx -> wat (capture stdout sovereignly via dup3)\n") 67 let c1: i64 = spawn("/tmp/nx_compile_wat.sov.elf\x00" as *u8, "runtime/nx_simlab.nx\x00" as *u8, 0 as *u8, 0 as *u8, WAT_OUT) 68 gw(" rc="); gn(c1); gw(" wat_bytes="); gn(fsize(WAT_OUT)); gw("\n") 69 70 gw(" [3] wat -> wasm\n") 71 let c2: i64 = spawn("/tmp/nx_wat_compiler.sov.elf\x00" as *u8, WAT_OUT, WASM_OUT, 0 as *u8, 0 as *u8) 72 gw(" rc="); gn(c2); gw(" wasm_bytes="); gn(fsize(WASM_OUT)); gw("\n") 73 74 gw(" [4] wasm -> html\n") 75 let c3: i64 = spawn("/tmp/nx_simlab_html.sov.elf\x00" as *u8, WASM_OUT, HTML_OUT, 0 as *u8, 0 as *u8) 76 gw(" rc="); gn(c3); gw(" html_bytes="); gn(fsize(HTML_OUT)); gw("\n") 77 78 gw(" [5] retire the non-sovereign hand-shell scripts (unlinkat, no rm)\n") 79 sp_unlink("_simlab_build.sh\x00" as *u8) 80 sp_unlink("_simfin.sh\x00" as *u8) 81 82 let wsz: i64 = fsize(WASM_OUT); let hsz: i64 = fsize(HTML_OUT) 83 // verify the wasm magic byte-0 = 0x00 'a' 's' 'm' by reading the file head 84 let lp: *i64 = sys_mmap(8) as *i64; let wb: *u8 = sys_read_file(WASM_OUT, lp) 85 var magic_ok: i64 = 0 86 if (wb as i64) != 0 { if lp[0] >= 8 { if wb[0]==(0 as u8) { if wb[1]==(0x61 as u8) { if wb[2]==(0x73 as u8) { if wb[3]==(0x6d as u8) { magic_ok=1 } } } } } } 87 gw(" wasm magic OK="); gn(magic_ok); gw(" wasm="); gn(wsz); gw("B html="); gn(hsz); gw("B\n") 88 89 var ok: i64 = 0 90 if magic_ok==1 { if hsz>K_MAGIC_2000 { ok=1 } } 91 if ok==1 { gw("SIMLAB-PACK GREEN: sovereign wasm+html built (Nishi all the way up, no shell)\n"); sys_exit(0); return 0 } 92 gw("SIMLAB-PACK RED\n"); sys_exit(1); return 1 93}