@@ -7,7 +7,7 @@ use std::{
77} ;
88use wasm_encoder:: {
99 BlockType , CodeSection , EntityType , ExportSection , Function , FunctionSection , ImportSection ,
10- Instruction , MemArg , MemorySection , MemoryType , Module , TypeSection , ValType ,
10+ Instruction , MemArg , MemoryType , Module , TypeSection , ValType ,
1111} ;
1212
1313/// Resolve `ty` via `generics` and `types`, then return its ID in `typemap`, inserting if need be.
@@ -976,18 +976,23 @@ impl<'a, 'b, O: Eq + Hash, T: Refs<'a, Opaque = O>> Codegen<'a, 'b, O, T> {
976976
977977/// A WebAssembly module for a graph of functions.
978978///
979- /// The module exports its memory with name `"m"` and its entrypoint function with name `"f"`. The
980- /// function takes one parameter in addition to its original parameters, which must be an
981- /// 8-byte-aligned pointer to the start of the memory region it can use for allocation. The memory
982- /// is the exact number of pages necessary to accommodate the function's own memory allocation as
983- /// well as memory allocation for all of its parameters, with each node in each parameter's memory
984- /// allocation tree being 8-byte aligned. That is, the function's last argument should be just large
985- /// enough to accommodate those allocations for all the parameters; in that case, no memory will be
979+ /// The module exports its entrypoint function with name `"f"`. The function takes one parameter in
980+ /// addition to its original parameters, which must be an 8-byte-aligned pointer to the start of the
981+ /// memory region it can use for allocation.
982+ ///
983+ /// Under module name `"m"`, the module imports a memory whose minimum number of pages is the exact
984+ /// number of pages necessary to accommodate the function's own memory allocation as well as memory
985+ /// allocation for all of its parameters, with each node in each parameter's memory allocation tree
986+ /// being 8-byte aligned. That is, the function's last argument should be just large enough to
987+ /// accommodate those allocations for all the parameters; in that case, no memory will be
986988/// incorrectly overwritten and no out-of-bounds memory accesses will occur.
987989pub struct Wasm < O > {
988990 /// The bytes of the WebAssembly module binary.
989991 pub bytes : Vec < u8 > ,
990992
993+ /// The minimum number of pages required by the imported memory.
994+ pub pages : u64 ,
995+
991996 /// All the opaque functions that the WebAssembly module must import, in order.
992997 ///
993998 /// The module name for each import is the empty string, and the field name is the base-ten
@@ -1390,7 +1395,6 @@ pub fn compile<'a, O: Eq + Hash, T: Refs<'a, Opaque = O>>(f: Node<'a, O, T>) ->
13901395 type_section. function ( params. into_vec ( ) , results. into_vec ( ) ) ;
13911396 }
13921397
1393- let mut memory_section = MemorySection :: new ( ) ;
13941398 let page_size = 65536 ; // https://webassembly.github.io/spec/core/exec/runtime.html#page-size
13951399 let cost = funcs. last ( ) . map_or ( 0 , |( ( def, _) , ( _, def_types, _) ) | {
13961400 def. params
@@ -1400,12 +1404,16 @@ pub fn compile<'a, O: Eq + Hash, T: Refs<'a, Opaque = O>>(f: Node<'a, O, T>) ->
14001404 . sum ( )
14011405 } ) + costs. last ( ) . unwrap_or ( & 0 ) ;
14021406 let pages = ( ( cost + page_size - 1 ) / page_size) . into ( ) ; // round up to a whole number of pages
1403- memory_section. memory ( MemoryType {
1404- minimum : pages,
1405- maximum : Some ( pages) ,
1406- memory64 : false ,
1407- shared : false ,
1408- } ) ;
1407+ import_section. import (
1408+ "m" ,
1409+ "" ,
1410+ MemoryType {
1411+ minimum : pages,
1412+ maximum : None ,
1413+ memory64 : false ,
1414+ shared : false ,
1415+ } ,
1416+ ) ;
14091417
14101418 let mut export_section = ExportSection :: new ( ) ;
14111419 export_section. export (
@@ -1419,11 +1427,11 @@ pub fn compile<'a, O: Eq + Hash, T: Refs<'a, Opaque = O>>(f: Node<'a, O, T>) ->
14191427 module. section ( & type_section) ;
14201428 module. section ( & import_section) ;
14211429 module. section ( & function_section) ;
1422- module. section ( & memory_section) ;
14231430 module. section ( & export_section) ;
14241431 module. section ( & code_section) ;
14251432 Wasm {
14261433 bytes : module. finish ( ) ,
1434+ pages,
14271435 imports,
14281436 }
14291437}
0 commit comments