code wiki / (root) / nx_wasm_layout_lib.nx

nx_wasm_layout_lib.nx source

↩ module page · 41 lines · 1621 B

1// Source-derived reservation record for WAT packaging. This is evidence, not pair acceptance. 2import "nx_wasm_data.nx" 3 4const WL_HEADER: i64=64 5const WL_ROW: i64=32 6const WL_VERSION: i64=1 7 8func wl_word(out: *u8,offset: i64,value: i64) -> i64 { 9 var i: i64=0 10 while i<8 { out[offset+i]=((value>>(i*8))&255) as u8;i=i+1 } 11 return 0 12} 13func wl_record(m: *Module,arena_pages: i64,shared: i64,out: *u8,capacity: i64) -> i64 { 14 if m==(0 as *Module) || out==(0 as *u8) { return 0 } 15 if arena_pages<0 || arena_pages>WD_MAX_BYTES/WD_PAGE { return 0 } 16 if shared!=0 && shared!=1 { return 0 } 17 if capacity<WL_HEADER || m.n_globals<0 { return 0 } 18 if m.n_globals>(capacity-WL_HEADER)/WL_ROW { return 0 } 19 let n: i64=WL_HEADER+m.n_globals*WL_ROW 20 // The compiler's allocator remains the sole address authority. 21 let pages: i64=wd_prepare(m,arena_pages,shared) 22 let magic: *u8="NXWLAY01" as *u8 23 var k: i64=0 24 while k<8 { out[k]=magic[k];k=k+1 } 25 wl_word(out,8,WL_VERSION);wl_word(out,16,m.n_globals) 26 wl_word(out,24,wd_static_base_bytes);wl_word(out,32,pages) 27 wl_word(out,40,wd_init_address) 28 var state_bytes: i64=0 29 if shared==1 && m.n_globals>0 { state_bytes=WD_CELL } 30 wl_word(out,48,state_bytes);wl_word(out,56,n) 31 k=0 32 while k<m.n_globals { 33 let g: *Global=(m.globals as i64+k*WD_GLOBAL_STRIDE) as *Global 34 let row: i64=WL_HEADER+k*WL_ROW 35 wl_word(out,row,k);wl_word(out,row+8,wd_address(k)) 36 wl_word(out,row+16,g.len+g.is_string) 37 wl_word(out,row+24,g.zero_init+g.is_string*2+g.writable*4) 38 k=k+1 39 } 40 return n 41}