mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-04 19:11:34 +00:00
Refactored instantiate function to return Module and Instance
This commit is contained in:
@ -121,7 +121,7 @@ impl<'module> ScriptHandler for StoreCtrl<'module> {
|
|||||||
}
|
}
|
||||||
fn module(&mut self, bytes: Vec<u8>, name: Option<String>) {
|
fn module(&mut self, bytes: Vec<u8>, name: Option<String>) {
|
||||||
let module_wrapped = instantiate(bytes, None);
|
let module_wrapped = instantiate(bytes, None);
|
||||||
let mut result = module_wrapped.expect("Module is invalid");
|
let mut result = module_wrapped.expect("Module is invalid").module;
|
||||||
// let module: &'module Module = result.module;
|
// let module: &'module Module = result.module;
|
||||||
self.last_module = Some(result);
|
self.last_module = Some(result);
|
||||||
// self.add_module(name, &mut result);
|
// self.add_module(name, &mut result);
|
||||||
|
@ -76,7 +76,7 @@ pub struct Instance {
|
|||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
/// Create a new `Instance`.
|
/// Create a new `Instance`.
|
||||||
pub fn new(module: &Module, data_initializers: &[DataInitializer], code_base: *const (), functions: &[usize]) -> Instance {
|
pub fn new(module: &Module, code_base: *const (), functions: &[usize]) -> Instance {
|
||||||
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();
|
||||||
let mut globals: Vec<u8> = Vec::new();
|
let mut globals: Vec<u8> = Vec::new();
|
||||||
@ -119,7 +119,7 @@ impl Instance {
|
|||||||
let v = LinearMemory::new(memory.pages_count as u32, memory.maximum.map(|m| m as u32));
|
let v = LinearMemory::new(memory.pages_count as u32, memory.maximum.map(|m| m as u32));
|
||||||
memories.push(v);
|
memories.push(v);
|
||||||
}
|
}
|
||||||
for init in data_initializers {
|
for init in &module.info.data_initializers {
|
||||||
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
|
debug_assert!(init.base.is_none(), "globalvar base not supported yet");
|
||||||
let mem_mut = memories[init.memory_index].as_mut();
|
let mem_mut = memories[init.memory_index].as_mut();
|
||||||
let to_init = &mut mem_mut[init.offset..init.offset + init.data.len()];
|
let to_init = &mut mem_mut[init.offset..init.offset + init.data.len()];
|
||||||
|
@ -1,10 +1,4 @@
|
|||||||
// pub mod module;
|
|
||||||
// pub mod compilation;
|
|
||||||
// pub mod memory;
|
|
||||||
// pub mod environ;
|
|
||||||
// pub mod instance;
|
|
||||||
pub mod errors;
|
pub mod errors;
|
||||||
// pub mod execute;
|
|
||||||
pub mod utils;
|
pub mod utils;
|
||||||
pub mod module;
|
pub mod module;
|
||||||
pub mod memory;
|
pub mod memory;
|
||||||
@ -13,40 +7,27 @@ pub mod instance;
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use std::panic;
|
use std::panic;
|
||||||
|
use std::ptr;
|
||||||
use cranelift_native;
|
use cranelift_native;
|
||||||
use cranelift_codegen::isa::TargetIsa;
|
|
||||||
// use cranelift_codegen::settings;
|
|
||||||
use cranelift_codegen::settings::Configurable;
|
|
||||||
|
|
||||||
use target_lexicon::{self, Triple};
|
use target_lexicon::{self, Triple};
|
||||||
|
use wasmparser;
|
||||||
use cranelift_codegen::isa;
|
use cranelift_codegen::isa;
|
||||||
use cranelift_codegen::print_errors::pretty_verifier_error;
|
// use cranelift_codegen::print_errors::pretty_verifier_error;
|
||||||
use cranelift_codegen::settings::{self, Flags};
|
// use cranelift_codegen::verifier;
|
||||||
use cranelift_codegen::verifier;
|
|
||||||
use cranelift_wasm::{translate_module, ReturnMode};
|
|
||||||
|
|
||||||
pub use self::module::Module;
|
pub use self::module::Module;
|
||||||
pub use self::instance::Instance;
|
pub use self::instance::Instance;
|
||||||
|
|
||||||
// pub use self::compilation::{compile_module, Compilation};
|
|
||||||
// pub use self::environ::{ModuleEnvironment};
|
|
||||||
// pub use self::module::Module;
|
|
||||||
// pub use self::instance::Instance;
|
|
||||||
pub use self::errors::{Error, ErrorKind};
|
pub use self::errors::{Error, ErrorKind};
|
||||||
// pub use self::execute::{compile_and_link_module,execute};
|
pub use self::memory::LinearMemory;
|
||||||
use wasmparser;
|
|
||||||
|
|
||||||
// pub struct ResultObject {
|
pub struct ResultObject {
|
||||||
// /// A webassembly::Module object representing the compiled WebAssembly module.
|
/// A webassembly::Module object representing the compiled WebAssembly module.
|
||||||
// /// This Module can be instantiated again
|
/// This Module can be instantiated again
|
||||||
// pub module: Module,
|
pub module: Module,
|
||||||
// /// A webassembly::Instance object that contains all the Exported WebAssembly
|
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
||||||
// /// functions.
|
/// functions.
|
||||||
// pub instance: Instance,
|
pub instance: Instance,
|
||||||
|
}
|
||||||
// pub compilation: Compilation,
|
|
||||||
// }
|
|
||||||
|
|
||||||
pub struct ImportObject {
|
pub struct ImportObject {
|
||||||
}
|
}
|
||||||
@ -68,62 +49,13 @@ pub struct ImportObject {
|
|||||||
/// If the operation fails, the Result rejects with a
|
/// If the operation fails, the Result rejects with a
|
||||||
/// webassembly::CompileError, webassembly::LinkError, or
|
/// webassembly::CompileError, webassembly::LinkError, or
|
||||||
/// webassembly::RuntimeError, depending on the cause of the failure.
|
/// webassembly::RuntimeError, depending on the cause of the failure.
|
||||||
pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>) -> Result<Module, ErrorKind> {
|
pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>) -> Result<ResultObject, ErrorKind> {
|
||||||
// TODO: This should be automatically validated when creating the Module
|
let module = compile(buffer_source)?;
|
||||||
if !validate(&buffer_source) {
|
let instance = Instance::new(&module, ptr::null(), &vec![]);
|
||||||
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
Ok(ResultObject{
|
||||||
}
|
module,
|
||||||
|
instance
|
||||||
let module =
|
})
|
||||||
Module::from_bytes(buffer_source, triple!("riscv64"), None)?;
|
|
||||||
|
|
||||||
// let isa = isa::lookup(module.info.triple)
|
|
||||||
// .unwrap()
|
|
||||||
// .finish(module.info.flags);
|
|
||||||
|
|
||||||
// for func in module.info.function_bodies.values() {
|
|
||||||
// verifier::verify_function(func, &*isa)
|
|
||||||
// .map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
|
||||||
// .unwrap();
|
|
||||||
// };
|
|
||||||
|
|
||||||
Ok(module)
|
|
||||||
// Ok(environ)
|
|
||||||
// let now = Instant::now();
|
|
||||||
// let isa = construct_isa();
|
|
||||||
// println!("instantiate::init {:?}", now.elapsed());
|
|
||||||
// // if !validate(&buffer_source) {
|
|
||||||
// // return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
|
||||||
// // }
|
|
||||||
// println!("instantiate::validation {:?}", now.elapsed());
|
|
||||||
// let mut module = Module::new();
|
|
||||||
// let environ = ModuleEnvironment::new(&*isa, &mut module);
|
|
||||||
// let translation = environ.translate(&buffer_source).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
|
|
||||||
// println!("instantiate::compile and link {:?}", now.elapsed());
|
|
||||||
// let compilation = compile_and_link_module(&*isa, &translation)?;
|
|
||||||
// // let (compilation, relocations) = compile_module(&translation, &*isa)?;
|
|
||||||
// println!("instantiate::instantiate {:?}", now.elapsed());
|
|
||||||
|
|
||||||
// let mut instance = Instance::new(
|
|
||||||
// translation.module,
|
|
||||||
// &compilation,
|
|
||||||
// &translation.lazy.data_initializers,
|
|
||||||
// );
|
|
||||||
// println!("instantiate::execute {:?}", now.elapsed());
|
|
||||||
|
|
||||||
// let x = execute(&module, &compilation, &mut instance)?;
|
|
||||||
|
|
||||||
// // let instance = Instance {
|
|
||||||
// // tables: Vec::new(),
|
|
||||||
// // memories: Vec::new(),
|
|
||||||
// // globals: Vec::new(),
|
|
||||||
// // };
|
|
||||||
|
|
||||||
// Ok(ResultObject {
|
|
||||||
// module,
|
|
||||||
// compilation,
|
|
||||||
// instance: instance,
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The webassembly::compile() function compiles a webassembly::Module
|
/// The webassembly::compile() function compiles a webassembly::Module
|
||||||
@ -138,16 +70,25 @@ pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>)
|
|||||||
/// Errors:
|
/// Errors:
|
||||||
/// If the operation fails, the Result rejects with a
|
/// If the operation fails, the Result rejects with a
|
||||||
/// webassembly::CompileError.
|
/// webassembly::CompileError.
|
||||||
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, Error> {
|
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
||||||
unimplemented!();
|
// TODO: This should be automatically validated when creating the Module
|
||||||
// let isa = construct_isa();
|
if !validate(&buffer_source) {
|
||||||
|
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
||||||
|
}
|
||||||
|
|
||||||
// let mut module = Module::new();
|
let module = Module::from_bytes(buffer_source, triple!("riscv64"), None)?;
|
||||||
// let environ = ModuleEnvironment::new(&*isa, &mut module);
|
|
||||||
// let translation = environ.translate(&buffer_source).map_err(|e| ErrorKind::CompileError(e.to_string()))?;
|
// let isa = isa::lookup(module.info.triple)
|
||||||
// // compile_module(&translation, &*isa)?;
|
// .unwrap()
|
||||||
// compile_and_link_module(&*isa, &translation)?;
|
// .finish(module.info.flags);
|
||||||
// Ok(module)
|
|
||||||
|
// for func in module.info.function_bodies.values() {
|
||||||
|
// verifier::verify_function(func, &*isa)
|
||||||
|
// .map_err(|errors| panic!(pretty_verifier_error(func, Some(&*isa), None, errors)))
|
||||||
|
// .unwrap();
|
||||||
|
// };
|
||||||
|
|
||||||
|
Ok(module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The webassembly::validate() function validates a given typed
|
/// The webassembly::validate() function validates a given typed
|
||||||
|
Reference in New Issue
Block a user