Add functions to ModuleEnvironment to support reserving buffers up front.

These default to doing nothing, but implementations can override them to
preallocate buffers.

Also, reorder the functions in `ModuleEnvironment` to more closely
reflect the sequence from the WebAssembly module layout.
This commit is contained in:
Dan Gohman
2018-12-18 12:55:11 -08:00
parent 7f250e340c
commit 054e6fcf07

View File

@@ -245,9 +245,17 @@ pub trait ModuleEnvironment<'data> {
/// Get the information needed to produce Cranelift IR for the current target. /// Get the information needed to produce Cranelift IR for the current target.
fn target_config(&self) -> TargetFrontendConfig; fn target_config(&self) -> TargetFrontendConfig;
/// Provides the number of signatures up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_signatures(&mut self, _num: u32) {}
/// Declares a function signature to the environment. /// Declares a function signature to the environment.
fn declare_signature(&mut self, sig: &ir::Signature); fn declare_signature(&mut self, sig: &ir::Signature);
/// Provides the number of imports up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_imports(&mut self, _num: u32) {}
/// Declares a function import to the environment. /// Declares a function import to the environment.
fn declare_func_import( fn declare_func_import(
&mut self, &mut self,
@@ -256,20 +264,71 @@ pub trait ModuleEnvironment<'data> {
field: &'data str, field: &'data str,
); );
/// Declares the type (signature) of a local function in the module. /// Declares a table import to the environment.
fn declare_func_type(&mut self, sig_index: SignatureIndex); fn declare_table_import(&mut self, table: Table, module: &'data str, field: &'data str);
/// Declares a global to the environment. /// Declares a memory import to the environment.
fn declare_global(&mut self, global: Global); fn declare_memory_import(&mut self, memory: Memory, module: &'data str, field: &'data str);
/// Declares a global import to the environment. /// Declares a global import to the environment.
fn declare_global_import(&mut self, global: Global, module: &'data str, field: &'data str); fn declare_global_import(&mut self, global: Global, module: &'data str, field: &'data str);
/// Notifies the implementation that all imports have been declared.
fn finish_imports(&mut self) {}
/// Provides the number of defined functions up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_func_types(&mut self, _num: u32) {}
/// Declares the type (signature) of a local function in the module.
fn declare_func_type(&mut self, sig_index: SignatureIndex);
/// Provides the number of defined tables up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_tables(&mut self, _num: u32) {}
/// Declares a table to the environment. /// Declares a table to the environment.
fn declare_table(&mut self, table: Table); fn declare_table(&mut self, table: Table);
/// Declares a table import to the environment. /// Provides the number of defined memories up front. By default this does nothing, but
fn declare_table_import(&mut self, table: Table, module: &'data str, field: &'data str); /// implementations can use this to preallocate memory if desired.
fn reserve_memories(&mut self, _num: u32) {}
/// Declares a memory to the environment
fn declare_memory(&mut self, memory: Memory);
/// Provides the number of defined globals up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_globals(&mut self, _num: u32) {}
/// Declares a global to the environment.
fn declare_global(&mut self, global: Global);
/// Provides the number of exports up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_exports(&mut self, _num: u32) {}
/// Declares a function export to the environment.
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str);
/// Declares a table export to the environment.
fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str);
/// Declares a memory export to the environment.
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &'data str);
/// Declares a global export to the environment.
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str);
/// Notifies the implementation that all exports have been declared.
fn finish_exports(&mut self) {}
/// Declares the optional start function.
fn declare_start_func(&mut self, index: FuncIndex);
/// Provides the number of element initializers up front. By default this does nothing, but
/// implementations can use this to preallocate memory if desired.
fn reserve_table_elements(&mut self, _num: u32) {}
/// Fills a declared table with references to functions in the module. /// Fills a declared table with references to functions in the module.
fn declare_table_elements( fn declare_table_elements(
@@ -280,11 +339,12 @@ pub trait ModuleEnvironment<'data> {
elements: Vec<FuncIndex>, elements: Vec<FuncIndex>,
); );
/// Declares a memory to the environment /// Provides the contents of a function body.
fn declare_memory(&mut self, memory: Memory); fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()>;
/// Declares a memory import to the environment. /// Provides the number of data initializers up front. By default this does nothing, but
fn declare_memory_import(&mut self, memory: Memory, module: &'data str, field: &'data str); /// implementations can use this to preallocate memory if desired.
fn reserve_data_initializers(&mut self, _num: u32) {}
/// Fills a declared memory with bytes at module instantiation. /// Fills a declared memory with bytes at module instantiation.
fn declare_data_initialization( fn declare_data_initialization(
@@ -294,19 +354,4 @@ pub trait ModuleEnvironment<'data> {
offset: usize, offset: usize,
data: &'data [u8], data: &'data [u8],
); );
/// Declares a function export to the environment.
fn declare_func_export(&mut self, func_index: FuncIndex, name: &'data str);
/// Declares a table export to the environment.
fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str);
/// Declares a memory export to the environment.
fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &'data str);
/// Declares a global export to the environment.
fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str);
/// Declares a start function.
fn declare_start_func(&mut self, index: FuncIndex);
/// Provides the contents of a function body.
fn define_function_body(&mut self, body_bytes: &'data [u8]) -> WasmResult<()>;
} }