Move the 'data lifetime parameter to the ModuleEnvironment trait.

This is needed to allow implementations to have 'data-lifetime
references if they choose to. The DummyEnvironment is an example of an
implementation that doesn't choose to.
This commit is contained in:
Dan Gohman
2017-10-11 21:28:36 -07:00
parent d6ab7e3abf
commit bd94a3b202
4 changed files with 26 additions and 26 deletions

View File

@@ -13,7 +13,7 @@ use runtime::ModuleEnvironment;
/// indexes in the wasm module and the indexes inside each functions. /// indexes in the wasm module and the indexes inside each functions.
pub fn translate_module<'data>( pub fn translate_module<'data>(
data: &'data [u8], data: &'data [u8],
environ: &mut ModuleEnvironment, environ: &mut ModuleEnvironment<'data>,
) -> Result<(), String> { ) -> Result<(), String> {
let mut parser = Parser::new(data); let mut parser = Parser::new(data);
match *parser.read() { match *parser.read() {

View File

@@ -195,7 +195,7 @@ impl<'dummy_environment> FuncEnvironment for DummyFuncEnvironment<'dummy_environ
} }
} }
impl ModuleEnvironment for DummyEnvironment { impl<'data> ModuleEnvironment<'data> for DummyEnvironment {
fn get_func_name(&self, func_index: FunctionIndex) -> ir::FunctionName { fn get_func_name(&self, func_index: FunctionIndex) -> ir::FunctionName {
get_func_name(func_index) get_func_name(func_index)
} }
@@ -208,7 +208,7 @@ impl ModuleEnvironment for DummyEnvironment {
&self.info.signatures[sig_index] &self.info.signatures[sig_index]
} }
fn declare_func_import<'data>( fn declare_func_import(
&mut self, &mut self,
sig_index: SignatureIndex, sig_index: SignatureIndex,
module: &'data str, module: &'data str,
@@ -261,7 +261,7 @@ impl ModuleEnvironment for DummyEnvironment {
fn declare_memory(&mut self, memory: Memory) { fn declare_memory(&mut self, memory: Memory) {
self.info.memories.push(Exportable::new(memory)); self.info.memories.push(Exportable::new(memory));
} }
fn declare_data_initialization<'data>( fn declare_data_initialization(
&mut self, &mut self,
_memory_index: MemoryIndex, _memory_index: MemoryIndex,
_base: Option<GlobalIndex>, _base: Option<GlobalIndex>,
@@ -271,7 +271,7 @@ impl ModuleEnvironment for DummyEnvironment {
// We do nothing // We do nothing
} }
fn declare_func_export<'data>(&mut self, func_index: FunctionIndex, name: &'data str) { fn declare_func_export(&mut self, func_index: FunctionIndex, name: &'data str) {
self.info.functions[func_index].export_names.push( self.info.functions[func_index].export_names.push(
String::from( String::from(
name, name,
@@ -279,13 +279,13 @@ impl ModuleEnvironment for DummyEnvironment {
); );
} }
fn declare_table_export<'data>(&mut self, table_index: TableIndex, name: &'data str) { fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str) {
self.info.tables[table_index].export_names.push( self.info.tables[table_index].export_names.push(
String::from(name), String::from(name),
); );
} }
fn declare_memory_export<'data>(&mut self, memory_index: MemoryIndex, name: &'data str) { fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &'data str) {
self.info.memories[memory_index].export_names.push( self.info.memories[memory_index].export_names.push(
String::from( String::from(
name, name,
@@ -293,7 +293,7 @@ impl ModuleEnvironment for DummyEnvironment {
); );
} }
fn declare_global_export<'data>(&mut self, global_index: GlobalIndex, name: &'data str) { fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str) {
self.info.globals[global_index].export_names.push( self.info.globals[global_index].export_names.push(
String::from( String::from(
name, name,
@@ -307,7 +307,7 @@ impl ModuleEnvironment for DummyEnvironment {
} }
/// Provides the contents of a function body. /// Provides the contents of a function body.
fn define_function_body<'data>(&mut self, body_bytes: &'data [u8]) -> Result<(), String> { fn define_function_body(&mut self, body_bytes: &'data [u8]) -> Result<(), String> {
let function_index = self.get_num_func_imports() + self.info.function_bodies.len(); let function_index = self.get_num_func_imports() + self.info.function_bodies.len();
let name = get_func_name(function_index); let name = get_func_name(function_index);
let sig = self.get_signature(self.get_func_type(function_index)) let sig = self.get_signature(self.get_func_type(function_index))

View File

@@ -149,7 +149,7 @@ pub trait FuncEnvironment {
/// An object satisfyng the `ModuleEnvironment` trait can be passed as argument to the /// An object satisfyng the `ModuleEnvironment` trait can be passed as argument to the
/// [`translate_module`](fn.translate_module.html) function. These methods should not be called /// [`translate_module`](fn.translate_module.html) function. These methods should not be called
/// by the user, they are only for `cretonne-wasm` internal use. /// by the user, they are only for `cretonne-wasm` internal use.
pub trait ModuleEnvironment { pub trait ModuleEnvironment<'data> {
/// Return the name for the given function index. /// Return the name for the given function index.
fn get_func_name(&self, func_index: FunctionIndex) -> ir::FunctionName; fn get_func_name(&self, func_index: FunctionIndex) -> ir::FunctionName;
@@ -160,7 +160,7 @@ pub trait ModuleEnvironment {
fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature; fn get_signature(&self, sig_index: SignatureIndex) -> &ir::Signature;
/// Declares a function import to the environment. /// Declares a function import to the environment.
fn declare_func_import<'data>( fn declare_func_import(
&mut self, &mut self,
sig_index: SignatureIndex, sig_index: SignatureIndex,
module: &'data str, module: &'data str,
@@ -195,7 +195,7 @@ pub trait ModuleEnvironment {
/// Declares a memory to the environment /// Declares a memory to the environment
fn declare_memory(&mut self, memory: Memory); fn declare_memory(&mut self, memory: Memory);
/// Fills a declared memory with bytes at module instantiation. /// Fills a declared memory with bytes at module instantiation.
fn declare_data_initialization<'data>( fn declare_data_initialization(
&mut self, &mut self,
memory_index: MemoryIndex, memory_index: MemoryIndex,
base: Option<GlobalIndex>, base: Option<GlobalIndex>,
@@ -204,17 +204,17 @@ pub trait ModuleEnvironment {
); );
/// Declares a function export to the environment. /// Declares a function export to the environment.
fn declare_func_export<'data>(&mut self, func_index: FunctionIndex, name: &'data str); fn declare_func_export(&mut self, func_index: FunctionIndex, name: &'data str);
/// Declares a table export to the environment. /// Declares a table export to the environment.
fn declare_table_export<'data>(&mut self, table_index: TableIndex, name: &'data str); fn declare_table_export(&mut self, table_index: TableIndex, name: &'data str);
/// Declares a memory export to the environment. /// Declares a memory export to the environment.
fn declare_memory_export<'data>(&mut self, memory_index: MemoryIndex, name: &'data str); fn declare_memory_export(&mut self, memory_index: MemoryIndex, name: &'data str);
/// Declares a global export to the environment. /// Declares a global export to the environment.
fn declare_global_export<'data>(&mut self, global_index: GlobalIndex, name: &'data str); fn declare_global_export(&mut self, global_index: GlobalIndex, name: &'data str);
/// Declares a start function. /// Declares a start function.
fn declare_start_func(&mut self, index: FunctionIndex); fn declare_start_func(&mut self, index: FunctionIndex);
/// Provides the contents of a function body. /// Provides the contents of a function body.
fn define_function_body<'data>(&mut self, body_bytes: &'data [u8]) -> Result<(), String>; fn define_function_body(&mut self, body_bytes: &'data [u8]) -> Result<(), String>;
} }

View File

@@ -56,9 +56,9 @@ pub fn parse_function_signatures(
} }
/// Retrieves the imports from the imports section of the binary. /// Retrieves the imports from the imports section of the binary.
pub fn parse_import_section( pub fn parse_import_section<'data>(
parser: &mut Parser, parser: &mut Parser<'data>,
environ: &mut ModuleEnvironment, environ: &mut ModuleEnvironment<'data>,
) -> Result<(), SectionParsingError> { ) -> Result<(), SectionParsingError> {
loop { loop {
match *parser.read() { match *parser.read() {
@@ -128,9 +128,9 @@ pub fn parse_function_section(
} }
/// Retrieves the names of the functions from the export section /// Retrieves the names of the functions from the export section
pub fn parse_export_section( pub fn parse_export_section<'data>(
parser: &mut Parser, parser: &mut Parser<'data>,
environ: &mut ModuleEnvironment, environ: &mut ModuleEnvironment<'data>,
) -> Result<(), SectionParsingError> { ) -> Result<(), SectionParsingError> {
loop { loop {
match *parser.read() { match *parser.read() {
@@ -246,9 +246,9 @@ pub fn parse_global_section(
Ok(()) Ok(())
} }
pub fn parse_data_section( pub fn parse_data_section<'data>(
parser: &mut Parser, parser: &mut Parser<'data>,
environ: &mut ModuleEnvironment, environ: &mut ModuleEnvironment<'data>,
) -> Result<(), SectionParsingError> { ) -> Result<(), SectionParsingError> {
loop { loop {
let memory_index = match *parser.read() { let memory_index = match *parser.read() {