mirror of
https://github.com/fluencelabs/wasmer
synced 2025-07-01 17:41:32 +00:00
Refactored instantiate function to return Module and Instance
This commit is contained in:
@ -1,10 +1,4 @@
|
||||
// pub mod module;
|
||||
// pub mod compilation;
|
||||
// pub mod memory;
|
||||
// pub mod environ;
|
||||
// pub mod instance;
|
||||
pub mod errors;
|
||||
// pub mod execute;
|
||||
pub mod utils;
|
||||
pub mod module;
|
||||
pub mod memory;
|
||||
@ -13,40 +7,27 @@ pub mod instance;
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::panic;
|
||||
use std::ptr;
|
||||
use cranelift_native;
|
||||
use cranelift_codegen::isa::TargetIsa;
|
||||
// use cranelift_codegen::settings;
|
||||
use cranelift_codegen::settings::Configurable;
|
||||
|
||||
use target_lexicon::{self, Triple};
|
||||
|
||||
use wasmparser;
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_codegen::print_errors::pretty_verifier_error;
|
||||
use cranelift_codegen::settings::{self, Flags};
|
||||
use cranelift_codegen::verifier;
|
||||
use cranelift_wasm::{translate_module, ReturnMode};
|
||||
// use cranelift_codegen::print_errors::pretty_verifier_error;
|
||||
// use cranelift_codegen::verifier;
|
||||
|
||||
pub use self::module::Module;
|
||||
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::execute::{compile_and_link_module,execute};
|
||||
use wasmparser;
|
||||
pub use self::memory::LinearMemory;
|
||||
|
||||
// pub struct ResultObject {
|
||||
// /// A webassembly::Module object representing the compiled WebAssembly module.
|
||||
// /// This Module can be instantiated again
|
||||
// pub module: Module,
|
||||
// /// A webassembly::Instance object that contains all the Exported WebAssembly
|
||||
// /// functions.
|
||||
// pub instance: Instance,
|
||||
|
||||
// pub compilation: Compilation,
|
||||
// }
|
||||
pub struct ResultObject {
|
||||
/// A webassembly::Module object representing the compiled WebAssembly module.
|
||||
/// This Module can be instantiated again
|
||||
pub module: Module,
|
||||
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
||||
/// functions.
|
||||
pub instance: Instance,
|
||||
}
|
||||
|
||||
pub struct ImportObject {
|
||||
}
|
||||
@ -68,62 +49,13 @@ pub struct ImportObject {
|
||||
/// If the operation fails, the Result rejects with a
|
||||
/// webassembly::CompileError, webassembly::LinkError, or
|
||||
/// webassembly::RuntimeError, depending on the cause of the failure.
|
||||
pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>) -> Result<Module, ErrorKind> {
|
||||
// TODO: This should be automatically validated when creating the Module
|
||||
if !validate(&buffer_source) {
|
||||
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
||||
}
|
||||
|
||||
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,
|
||||
// })
|
||||
pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>) -> Result<ResultObject, ErrorKind> {
|
||||
let module = compile(buffer_source)?;
|
||||
let instance = Instance::new(&module, ptr::null(), &vec![]);
|
||||
Ok(ResultObject{
|
||||
module,
|
||||
instance
|
||||
})
|
||||
}
|
||||
|
||||
/// The webassembly::compile() function compiles a webassembly::Module
|
||||
@ -138,16 +70,25 @@ pub fn instantiate(buffer_source: Vec<u8>, import_object: Option<ImportObject>)
|
||||
/// Errors:
|
||||
/// If the operation fails, the Result rejects with a
|
||||
/// webassembly::CompileError.
|
||||
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, Error> {
|
||||
unimplemented!();
|
||||
// let isa = construct_isa();
|
||||
pub fn compile(buffer_source: Vec<u8>) -> Result<Module, ErrorKind> {
|
||||
// TODO: This should be automatically validated when creating the Module
|
||||
if !validate(&buffer_source) {
|
||||
return Err(ErrorKind::CompileError("Module not valid".to_string()));
|
||||
}
|
||||
|
||||
let module = Module::from_bytes(buffer_source, triple!("riscv64"), None)?;
|
||||
|
||||
// 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()))?;
|
||||
// // compile_module(&translation, &*isa)?;
|
||||
// compile_and_link_module(&*isa, &translation)?;
|
||||
// Ok(module)
|
||||
// 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)
|
||||
}
|
||||
|
||||
/// The webassembly::validate() function validates a given typed
|
||||
|
Reference in New Issue
Block a user