mirror of
https://github.com/fluencelabs/wasmer
synced 2025-06-30 00:51:34 +00:00
Added Instance options, automatically mock missing imports
This commit is contained in:
@ -123,11 +123,22 @@ pub struct DataPointers {
|
|||||||
pub globals: UncheckedSlice<u8>,
|
pub globals: UncheckedSlice<u8>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct InstanceOptions {
|
||||||
|
// Shall we mock automatically the imported functions if they don't exist?
|
||||||
|
pub mock_missing_imports: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
extern fn mock_fn() -> i32 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
/// Create a new `Instance`.
|
/// Create a new `Instance`.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
module: &Module,
|
module: &Module,
|
||||||
import_object: &ImportObject<&str, &str>,
|
import_object: &ImportObject<&str, &str>,
|
||||||
|
options: InstanceOptions,
|
||||||
) -> Result<Instance, ErrorKind> {
|
) -> Result<Instance, ErrorKind> {
|
||||||
let mut tables: Vec<Vec<usize>> = Vec::new();
|
let mut tables: Vec<Vec<usize>> = Vec::new();
|
||||||
let mut memories: Vec<LinearMemory> = Vec::new();
|
let mut memories: Vec<LinearMemory> = Vec::new();
|
||||||
@ -145,17 +156,31 @@ impl Instance {
|
|||||||
.finish(module.info.flags.clone());
|
.finish(module.info.flags.clone());
|
||||||
let mut relocations = Vec::new();
|
let mut relocations = Vec::new();
|
||||||
|
|
||||||
|
// let imported_functions: Vec<String> = module.info.imported_funcs.iter().map(|(module, field)| {
|
||||||
|
// format!(" * {}.{}", module, field)
|
||||||
|
// }).collect();
|
||||||
|
|
||||||
|
// println!("Instance imported functions: \n{}", imported_functions.join("\n"));
|
||||||
|
|
||||||
// We walk through the imported functions and set the relocations
|
// We walk through the imported functions and set the relocations
|
||||||
// for each of this functions to be an empty vector (as is defined outside of wasm)
|
// for each of this functions to be an empty vector (as is defined outside of wasm)
|
||||||
for (module, field) in module.info.imported_funcs.iter() {
|
for (module, field) in module.info.imported_funcs.iter() {
|
||||||
let function = import_object
|
let function = import_object
|
||||||
.get(&module.as_str(), &field.as_str())
|
.get(&module.as_str(), &field.as_str());
|
||||||
.ok_or_else(|| {
|
let function = if options.mock_missing_imports {
|
||||||
|
function.unwrap_or_else(|| {
|
||||||
|
debug!("The import {}.{} is not provided, therefore will be mocked.", module, field);
|
||||||
|
mock_fn as *const u8
|
||||||
|
})
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
function.ok_or_else(|| {
|
||||||
ErrorKind::LinkError(format!(
|
ErrorKind::LinkError(format!(
|
||||||
"Imported function {}.{} was not provided in the import_functions",
|
"Imported function {}.{} was not provided in the import_functions",
|
||||||
module, field
|
module, field
|
||||||
))
|
))
|
||||||
})?;
|
})?
|
||||||
|
};
|
||||||
// println!("GET FUNC {:?}", function);
|
// println!("GET FUNC {:?}", function);
|
||||||
import_functions.push(function);
|
import_functions.push(function);
|
||||||
relocations.push(vec![]);
|
relocations.push(vec![]);
|
||||||
|
@ -13,7 +13,7 @@ use wasmparser;
|
|||||||
|
|
||||||
pub use self::errors::{Error, ErrorKind};
|
pub use self::errors::{Error, ErrorKind};
|
||||||
pub use self::import_object::ImportObject;
|
pub use self::import_object::ImportObject;
|
||||||
pub use self::instance::{Instance};
|
pub use self::instance::{Instance, InstanceOptions};
|
||||||
pub use self::memory::LinearMemory;
|
pub use self::memory::LinearMemory;
|
||||||
pub use self::module::{Export, Module, ModuleInfo};
|
pub use self::module::{Export, Module, ModuleInfo};
|
||||||
|
|
||||||
@ -46,7 +46,7 @@ pub fn instantiate(
|
|||||||
) -> Result<ResultObject, ErrorKind> {
|
) -> Result<ResultObject, ErrorKind> {
|
||||||
let module = compile(buffer_source)?;
|
let module = compile(buffer_source)?;
|
||||||
debug!("webassembly - creating instance");
|
debug!("webassembly - creating instance");
|
||||||
let instance = Instance::new(&module, &import_object)?;
|
let instance = Instance::new(&module, &import_object, InstanceOptions { mock_missing_imports: true })?;
|
||||||
debug!("webassembly - instance created");
|
debug!("webassembly - instance created");
|
||||||
Ok(ResultObject { module, instance })
|
Ok(ResultObject { module, instance })
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user