nx_wat_type_layout_gate.nx source
↩ module page · 71 lines · 5365 B
1// Source-linked WAT compiler regression. Emission equivalence, not VM execution or a C ABI claim.
2// Reuse the already exercised subject runner and shared fixture/verdict primitives.
3import "nx_wat_refuse_gate.nx"
4import "nx_gate_verdict.nx"
5
6func wlg_equal(a: *u8, an: i64, b: *u8, bn: i64) -> i64 {
7 if an != bn { return 0 }
8 var i: i64=0
9 while i < an { if a[i] != b[i] { return 0 }; i=i+1 }
10 return 1
11}
12func wlg_run(subject: *u8, path: *u8, source: *u8, out: *u8, count: *i64, ctr: *i64) -> i64 {
13 let written: i64=gk_write(path,source)
14 gv_check("entire isolated fixture written" as *u8,written == gk_len(source),ctr)
15 if written != gk_len(source) { count[0]=0; return 0-1 }
16 let rc: i64=wr_run(subject,path,out,count)
17 gv_check("compiler capture nonempty and below inherited capture bound" as *u8,count[0] > 0 && count[0] < WR_CAP-1,ctr)
18 return rc
19}
20func wlg_pair(subject: *u8, path: *u8, query: *u8, literal: *u8, constant: *u8, label: *u8, ctr: *i64) -> i64 {
21 let a: *u8=sys_mmap(WR_CAP); let b: *u8=sys_mmap(WR_CAP)
22 let count: *i64=sys_mmap(WR_SPAN) as *i64
23 if (a as i64) <= 0 || (b as i64) <= 0 || (count as i64) <= 0 { gv_check("pair buffers allocated" as *u8,0,ctr); return 0 }
24 let ra: i64=wlg_run(subject,path,query,a,count,ctr); let an: i64=count[0]
25 let rb: i64=wlg_run(subject,path,literal,b,count,ctr); let bn: i64=count[0]
26 gv_check("layout query compiles successfully" as *u8,ra == 0,ctr)
27 gv_check("literal positive control compiles successfully" as *u8,rb == 0,ctr)
28 gv_check("query and control both emit a module" as *u8,gk_has(a,"(module\n" as *u8) == 1 && gk_has(b,"(module\n" as *u8) == 1,ctr)
29 gv_check("literal control emits its independently expected constant" as *u8,gk_has(b,constant),ctr)
30 // Same source path and function shape, exact full captured bytes: no prefix or unscoped number search.
31 gv_check(label,ra == 0 && rb == 0 && an > 0 && an < WR_CAP-1 && bn > 0 && bn < WR_CAP-1 && wlg_equal(a,an,b,bn) == 1,ctr)
32 sys_munmap(a,WR_CAP); sys_munmap(b,WR_CAP); sys_munmap(count as *u8,WR_SPAN)
33 return 0
34}
35func wlg_refuse(subject: *u8, path: *u8, source: *u8, label: *u8, ctr: *i64) -> i64 {
36 let out: *u8=sys_mmap(WR_CAP); let count: *i64=sys_mmap(WR_SPAN) as *i64
37 if (out as i64) <= 0 || (count as i64) <= 0 { gv_check("refusal buffers allocated" as *u8,0,ctr); return 0 }
38 let rc: i64=wlg_run(subject,path,source,out,count,ctr)
39 gv_check(label,rc > 0 && gk_has(out,"type-layout query requires a completed nonempty struct definition before use" as *u8) == 1,ctr)
40 gv_check("invalid layout emits no module" as *u8,gk_has(out,"(module" as *u8) == 0,ctr)
41 gv_values_head(); gv_kv("invalid_layout_compiler_exit" as *u8,rc); gv_kv("invalid_layout_capture_bytes" as *u8,count[0])
42 sys_munmap(out,WR_CAP); sys_munmap(count as *u8,WR_SPAN)
43 return 0
44}
45func main(argc: i64, argv: **u8) -> i64 {
46 if argc != 2 { gv_puts("usage: wat-type-layout-gate <subject-compiler>\n" as *u8); return 3 }
47 let ctr: *i64=gv_ctr(); gv_head("WAT-TYPE-LAYOUT / exact emitted equivalence" as *u8)
48 let subject: *u8=argv[1] as *u8
49 gv_need("subject compiler present" as *u8,gk_exists(subject),ctr)
50 let base: *u8=sys_mmap(WR_PATH); let dir: *u8=sys_mmap(WR_PATH); let path: *u8=sys_mmap(WR_PATH)
51 if (base as i64) <= 0 || (dir as i64) <= 0 || (path as i64) <= 0 { gv_check("fixture paths allocated" as *u8,0,ctr); return gv_verdict("WAT-TYPE-LAYOUT" as *u8,ctr,"fixture allocation failed" as *u8) }
52 gk_fixture_dir("nx_wat_type_layout_gate" as *u8,base)
53 gk_join(dir,base,"layout-inputs" as *u8)
54 // Shared fixture helper names the per-run parent; exclusive child creation refuses any collision.
55 let made: i64=sys_mkdir(dir,448)
56 gv_check("exclusive private fixture directory created" as *u8,made == 0,ctr)
57 if made != 0 { return gv_verdict("WAT-TYPE-LAYOUT" as *u8,ctr,"fixture isolation not established" as *u8) }
58 gk_join(path,dir,"subject.nx" as *u8)
59 wlg_pair(subject,path,
60 "struct Mixed { first: i32, second: i64 }\nfunc main() -> i64 { return __size_of(Mixed) }\n" as *u8,
61 "struct Mixed { first: i32, second: i64 }\nfunc main() -> i64 { return 12 }\n" as *u8,
62 "i64.const 12\n" as *u8,"mixed i32/i64 size query emits exactly the literal-12 control" as *u8,ctr)
63 wlg_pair(subject,path,
64 "struct Mixed { first: i32, second: i64 }\nfunc main() -> i64 { return __align_of(Mixed) }\n" as *u8,
65 "struct Mixed { first: i32, second: i64 }\nfunc main() -> i64 { return 8 }\n" as *u8,
66 "i64.const 8\n" as *u8,"mixed i32/i64 alignment query emits exactly the literal-8 control" as *u8,ctr)
67 wlg_refuse(subject,path,"struct Empty {}\nfunc main() -> i64 { return __size_of(Empty) }\n" as *u8,"empty struct size query refused by exact named cause" as *u8,ctr)
68 wlg_refuse(subject,path,"struct Empty {}\nfunc main() -> i64 { return __align_of(Empty) }\n" as *u8,"empty struct alignment query refused by exact named cause" as *u8,ctr)
69 gv_puts("retained_fixture=" as *u8); gv_puts(dir); gv_puts("\n" as *u8)
70 return gv_verdict("WAT-TYPE-LAYOUT" as *u8,ctr,"current Nishi mixed-width struct metadata emits the same complete WAT as size12/align8 literal controls; empty structs refused by name; emission qualification only, no VM/browser or foreign ABI claim" as *u8)
71}