mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-16 17:18:57 +00:00
Renamed CompiledModule to Artifact
This commit is contained in:
@@ -9,7 +9,7 @@ use thiserror::Error;
|
|||||||
use wasmer_compiler::CompileError;
|
use wasmer_compiler::CompileError;
|
||||||
#[cfg(feature = "wat")]
|
#[cfg(feature = "wat")]
|
||||||
use wasmer_compiler::WasmError;
|
use wasmer_compiler::WasmError;
|
||||||
use wasmer_engine::{CompiledModule, DeserializeError, Resolver, SerializeError};
|
use wasmer_engine::{Artifact, DeserializeError, Resolver, SerializeError};
|
||||||
use wasmer_runtime::{ExportsIterator, ImportsIterator, InstanceHandle, ModuleInfo};
|
use wasmer_runtime::{ExportsIterator, ImportsIterator, InstanceHandle, ModuleInfo};
|
||||||
|
|
||||||
#[derive(Error, Debug)]
|
#[derive(Error, Debug)]
|
||||||
@@ -33,7 +33,7 @@ pub enum IoCompileError {
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Module {
|
pub struct Module {
|
||||||
store: Store,
|
store: Store,
|
||||||
compiled: Arc<dyn CompiledModule>,
|
artifact: Arc<dyn Artifact>,
|
||||||
|
|
||||||
#[cfg(feature = "wat")]
|
#[cfg(feature = "wat")]
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
@@ -159,7 +159,7 @@ impl Module {
|
|||||||
/// let serialized = module.serialize()?;
|
/// let serialized = module.serialize()?;
|
||||||
/// ```
|
/// ```
|
||||||
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
||||||
self.store.engine().serialize(self.compiled.borrow())
|
self.store.engine().serialize(self.artifact.borrow())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes a a serialized Module binary into a `Module`.
|
/// Deserializes a a serialized Module binary into a `Module`.
|
||||||
@@ -209,7 +209,7 @@ impl Module {
|
|||||||
Ok(Self::from_compiled_module(store, compiled))
|
Ok(Self::from_compiled_module(store, compiled))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_compiled_module(store: &Store, compiled: Arc<dyn CompiledModule>) -> Self {
|
fn from_compiled_module(store: &Store, artifact: Arc<dyn Artifact>) -> Self {
|
||||||
Module {
|
Module {
|
||||||
store: store.clone(),
|
store: store.clone(),
|
||||||
compiled,
|
compiled,
|
||||||
@@ -226,7 +226,7 @@ impl Module {
|
|||||||
let instance_handle = self
|
let instance_handle = self
|
||||||
.store
|
.store
|
||||||
.engine()
|
.engine()
|
||||||
.instantiate(self.compiled.borrow(), resolver)?;
|
.instantiate(self.artifact.borrow(), resolver)?;
|
||||||
|
|
||||||
// After the instance handle is created, we need to initialize
|
// After the instance handle is created, we need to initialize
|
||||||
// the data, call the start function and so. However, if any
|
// the data, call the start function and so. However, if any
|
||||||
@@ -235,7 +235,7 @@ impl Module {
|
|||||||
// instance tables.
|
// instance tables.
|
||||||
self.store
|
self.store
|
||||||
.engine()
|
.engine()
|
||||||
.finish_instantiation(self.compiled.borrow(), &instance_handle)?;
|
.finish_instantiation(self.artifact.borrow(), &instance_handle)?;
|
||||||
|
|
||||||
Ok(instance_handle)
|
Ok(instance_handle)
|
||||||
}
|
}
|
||||||
@@ -254,7 +254,7 @@ impl Module {
|
|||||||
/// assert_eq!(module.name(), Some("moduleName"));
|
/// assert_eq!(module.name(), Some("moduleName"));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn name(&self) -> Option<&str> {
|
pub fn name(&self) -> Option<&str> {
|
||||||
self.compiled.module().name.as_deref()
|
self.artifact.module().name.as_deref()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the name of the current module.
|
/// Sets the name of the current module.
|
||||||
@@ -271,7 +271,7 @@ impl Module {
|
|||||||
/// assert_eq!(module.name(), Some("foo"));
|
/// assert_eq!(module.name(), Some("foo"));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn set_name(&mut self, name: &str) {
|
pub fn set_name(&mut self, name: &str) {
|
||||||
let compiled = Arc::get_mut(&mut self.compiled).unwrap();
|
let compiled = Arc::get_mut(&mut self.artifact).unwrap();
|
||||||
compiled.module_mut().name = Some(name.to_string());
|
compiled.module_mut().name = Some(name.to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -296,7 +296,7 @@ impl Module {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn imports<'a>(&'a self) -> ImportsIterator<impl Iterator<Item = ImportType> + 'a> {
|
pub fn imports<'a>(&'a self) -> ImportsIterator<impl Iterator<Item = ImportType> + 'a> {
|
||||||
self.compiled.module().imports()
|
self.artifact.module().imports()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator over the exported types in the Module.
|
/// Returns an iterator over the exported types in the Module.
|
||||||
@@ -319,7 +319,7 @@ impl Module {
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub fn exports<'a>(&'a self) -> ExportsIterator<impl Iterator<Item = ExportType> + 'a> {
|
pub fn exports<'a>(&'a self) -> ExportsIterator<impl Iterator<Item = ExportType> + 'a> {
|
||||||
self.compiled.module().exports()
|
self.artifact.module().exports()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn store(&self) -> &Store {
|
pub fn store(&self) -> &Store {
|
||||||
@@ -333,6 +333,6 @@ impl Module {
|
|||||||
/// However, the usage is highly discouraged.
|
/// However, the usage is highly discouraged.
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn info(&self) -> &ModuleInfo {
|
pub fn info(&self) -> &ModuleInfo {
|
||||||
&self.compiled.module()
|
&self.artifact.module()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! Define `CompiledModule` to allow compiling and instantiating to be
|
//! Define `JITArtifact` to allow compiling and instantiating to be
|
||||||
//! done as separate steps.
|
//! done as separate steps.
|
||||||
|
|
||||||
use crate::engine::{JITEngine, JITEngineInner};
|
use crate::engine::{JITEngine, JITEngineInner};
|
||||||
@@ -15,8 +15,8 @@ use wasmer_compiler::CompileError;
|
|||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
use wasmer_compiler::ModuleEnvironment;
|
use wasmer_compiler::ModuleEnvironment;
|
||||||
use wasmer_engine::{
|
use wasmer_engine::{
|
||||||
register_frame_info, resolve_imports, CompiledModule as BaseCompiledModule, DeserializeError,
|
register_frame_info, resolve_imports, Artifact, DeserializeError, Engine,
|
||||||
Engine, GlobalFrameInfoRegistration, InstantiationError, Resolver, RuntimeError,
|
GlobalFrameInfoRegistration, InstantiationError, Resolver, RuntimeError,
|
||||||
SerializableFunctionFrameInfo, SerializeError,
|
SerializableFunctionFrameInfo, SerializeError,
|
||||||
};
|
};
|
||||||
use wasmer_runtime::{
|
use wasmer_runtime::{
|
||||||
@@ -26,7 +26,7 @@ use wasmer_runtime::{
|
|||||||
use wasmer_runtime::{MemoryPlan, TablePlan};
|
use wasmer_runtime::{MemoryPlan, TablePlan};
|
||||||
|
|
||||||
/// A compiled wasm module, ready to be instantiated.
|
/// A compiled wasm module, ready to be instantiated.
|
||||||
pub struct CompiledModule {
|
pub struct JITArtifact {
|
||||||
serializable: SerializableModule,
|
serializable: SerializableModule,
|
||||||
|
|
||||||
finished_functions: BoxedSlice<LocalFunctionIndex, *mut [VMFunctionBody]>,
|
finished_functions: BoxedSlice<LocalFunctionIndex, *mut [VMFunctionBody]>,
|
||||||
@@ -35,8 +35,8 @@ pub struct CompiledModule {
|
|||||||
frame_info_registration: Mutex<Option<Option<GlobalFrameInfoRegistration>>>,
|
frame_info_registration: Mutex<Option<Option<GlobalFrameInfoRegistration>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompiledModule {
|
impl JITArtifact {
|
||||||
/// Compile a data buffer into a `CompiledModule`, which may then be instantiated.
|
/// Compile a data buffer into a `JITArtifact`, which may then be instantiated.
|
||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
pub fn new(jit: &JITEngine, data: &[u8]) -> Result<Self, CompileError> {
|
pub fn new(jit: &JITEngine, data: &[u8]) -> Result<Self, CompileError> {
|
||||||
let environ = ModuleEnvironment::new();
|
let environ = ModuleEnvironment::new();
|
||||||
@@ -118,7 +118,7 @@ impl CompiledModule {
|
|||||||
Self::from_parts(&mut jit_compiler, serializable)
|
Self::from_parts(&mut jit_compiler, serializable)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a data buffer into a `CompiledModule`, which may then be instantiated.
|
/// Compile a data buffer into a `JITArtifact`, which may then be instantiated.
|
||||||
#[cfg(not(feature = "compiler"))]
|
#[cfg(not(feature = "compiler"))]
|
||||||
pub fn new(jit: &JITEngine, data: &[u8]) -> Result<Self, CompileError> {
|
pub fn new(jit: &JITEngine, data: &[u8]) -> Result<Self, CompileError> {
|
||||||
Err(CompileError::Codegen(
|
Err(CompileError::Codegen(
|
||||||
@@ -126,7 +126,7 @@ impl CompiledModule {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize a CompiledModule
|
/// Serialize a JITArtifact
|
||||||
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
||||||
// let mut s = flexbuffers::FlexbufferSerializer::new();
|
// let mut s = flexbuffers::FlexbufferSerializer::new();
|
||||||
// self.serializable.serialize(&mut s).map_err(|e| SerializeError::Generic(format!("{:?}", e)));
|
// self.serializable.serialize(&mut s).map_err(|e| SerializeError::Generic(format!("{:?}", e)));
|
||||||
@@ -135,7 +135,7 @@ impl CompiledModule {
|
|||||||
.map_err(|e| SerializeError::Generic(format!("{:?}", e)))
|
.map_err(|e| SerializeError::Generic(format!("{:?}", e)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a CompiledModule
|
/// Deserialize a JITArtifact
|
||||||
pub fn deserialize(jit: &JITEngine, bytes: &[u8]) -> Result<Self, DeserializeError> {
|
pub fn deserialize(jit: &JITEngine, bytes: &[u8]) -> Result<Self, DeserializeError> {
|
||||||
// let r = flexbuffers::Reader::get_root(bytes).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?;
|
// let r = flexbuffers::Reader::get_root(bytes).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?;
|
||||||
// let serializable = SerializableModule::deserialize(r).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?;
|
// let serializable = SerializableModule::deserialize(r).map_err(|e| DeserializeError::CorruptedBinary(format!("{:?}", e)))?;
|
||||||
@@ -146,7 +146,7 @@ impl CompiledModule {
|
|||||||
Self::from_parts(&mut jit.compiler_mut(), serializable).map_err(DeserializeError::Compiler)
|
Self::from_parts(&mut jit.compiler_mut(), serializable).map_err(DeserializeError::Compiler)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `CompiledModule` from component parts.
|
/// Construct a `JITArtifact` from component parts.
|
||||||
pub fn from_parts(
|
pub fn from_parts(
|
||||||
jit_compiler: &mut JITEngineInner,
|
jit_compiler: &mut JITEngineInner,
|
||||||
serializable: SerializableModule,
|
serializable: SerializableModule,
|
||||||
@@ -199,7 +199,7 @@ impl CompiledModule {
|
|||||||
&self.serializable.table_plans
|
&self.serializable.table_plans
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Crate an `Instance` from this `CompiledModule`.
|
/// Crate an `Instance` from this `JITArtifact`.
|
||||||
///
|
///
|
||||||
/// # Unsafety
|
/// # Unsafety
|
||||||
///
|
///
|
||||||
@@ -296,7 +296,7 @@ impl CompiledModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BaseCompiledModule for CompiledModule {
|
impl Artifact for JITArtifact {
|
||||||
fn module(&self) -> &ModuleInfo {
|
fn module(&self) -> &ModuleInfo {
|
||||||
&self.serializable.module
|
&self.serializable.module
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//! JIT compilation.
|
//! JIT compilation.
|
||||||
|
|
||||||
use crate::{CodeMemory, CompiledModule};
|
use crate::{CodeMemory, JITArtifact};
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
use wasm_common::entity::PrimaryMap;
|
use wasm_common::entity::PrimaryMap;
|
||||||
@@ -9,8 +9,7 @@ use wasmer_compiler::{CompileError, FunctionBody};
|
|||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
use wasmer_compiler::{Compiler, CompilerConfig};
|
use wasmer_compiler::{Compiler, CompilerConfig};
|
||||||
use wasmer_engine::{
|
use wasmer_engine::{
|
||||||
CompiledModule as BaseCompiledModule, DeserializeError, Engine, InstantiationError, Resolver,
|
Artifact, DeserializeError, Engine, InstantiationError, Resolver, SerializeError, Tunables,
|
||||||
SerializeError, Tunables,
|
|
||||||
};
|
};
|
||||||
use wasmer_runtime::{
|
use wasmer_runtime::{
|
||||||
InstanceHandle, ModuleInfo, SignatureRegistry, VMFunctionBody, VMSharedSignatureIndex,
|
InstanceHandle, ModuleInfo, SignatureRegistry, VMFunctionBody, VMSharedSignatureIndex,
|
||||||
@@ -115,18 +114,18 @@ impl Engine for JITEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a WebAssembly binary
|
/// Compile a WebAssembly binary
|
||||||
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn BaseCompiledModule>, CompileError> {
|
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn Artifact>, CompileError> {
|
||||||
Ok(Arc::new(CompiledModule::new(&self, binary)?))
|
Ok(Arc::new(JITArtifact::new(&self, binary)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Instantiates a WebAssembly module
|
/// Instantiates a WebAssembly module
|
||||||
unsafe fn instantiate(
|
unsafe fn instantiate(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
resolver: &dyn Resolver,
|
resolver: &dyn Resolver,
|
||||||
) -> Result<InstanceHandle, InstantiationError> {
|
) -> Result<InstanceHandle, InstantiationError> {
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<CompiledModule>()
|
.downcast_ref::<JITArtifact>()
|
||||||
.expect("The provided module is not a JIT compiled module");
|
.expect("The provided module is not a JIT compiled module");
|
||||||
compiled_module.instantiate(&self, resolver, Box::new(()))
|
compiled_module.instantiate(&self, resolver, Box::new(()))
|
||||||
}
|
}
|
||||||
@@ -134,22 +133,19 @@ impl Engine for JITEngine {
|
|||||||
/// Finish the instantiation of a WebAssembly module
|
/// Finish the instantiation of a WebAssembly module
|
||||||
unsafe fn finish_instantiation(
|
unsafe fn finish_instantiation(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
handle: &InstanceHandle,
|
handle: &InstanceHandle,
|
||||||
) -> Result<(), InstantiationError> {
|
) -> Result<(), InstantiationError> {
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<CompiledModule>()
|
.downcast_ref::<JITArtifact>()
|
||||||
.expect("The provided module is not a JIT compiled module");
|
.expect("The provided module is not a JIT compiled module");
|
||||||
compiled_module.finish_instantiation(&handle)
|
compiled_module.finish_instantiation(&handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes a WebAssembly module
|
/// Serializes a WebAssembly module
|
||||||
fn serialize(
|
fn serialize(&self, compiled_module: &dyn Artifact) -> Result<Vec<u8>, SerializeError> {
|
||||||
&self,
|
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
|
||||||
) -> Result<Vec<u8>, SerializeError> {
|
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<CompiledModule>()
|
.downcast_ref::<JITArtifact>()
|
||||||
.expect("The provided module is not a JIT compiled module");
|
.expect("The provided module is not a JIT compiled module");
|
||||||
// We append the header
|
// We append the header
|
||||||
let mut serialized = Self::MAGIC_HEADER.to_vec();
|
let mut serialized = Self::MAGIC_HEADER.to_vec();
|
||||||
@@ -158,13 +154,13 @@ impl Engine for JITEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes a WebAssembly module
|
/// Deserializes a WebAssembly module
|
||||||
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn BaseCompiledModule>, DeserializeError> {
|
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn Artifact>, DeserializeError> {
|
||||||
if !Self::is_deserializable(bytes) {
|
if !Self::is_deserializable(bytes) {
|
||||||
return Err(DeserializeError::Incompatible(
|
return Err(DeserializeError::Incompatible(
|
||||||
"The provided bytes are not wasmer-jit".to_string(),
|
"The provided bytes are not wasmer-jit".to_string(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
Ok(Arc::new(CompiledModule::deserialize(
|
Ok(Arc::new(JITArtifact::deserialize(
|
||||||
&self,
|
&self,
|
||||||
&bytes[Self::MAGIC_HEADER.len()..],
|
&bytes[Self::MAGIC_HEADER.len()..],
|
||||||
)?))
|
)?))
|
||||||
|
|||||||
@@ -25,18 +25,18 @@
|
|||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
mod artifact;
|
||||||
mod code_memory;
|
mod code_memory;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod function_table;
|
mod function_table;
|
||||||
mod link;
|
mod link;
|
||||||
mod module;
|
|
||||||
mod serialize;
|
mod serialize;
|
||||||
|
|
||||||
|
pub use crate::artifact::JITArtifact;
|
||||||
pub use crate::code_memory::CodeMemory;
|
pub use crate::code_memory::CodeMemory;
|
||||||
pub use crate::engine::JITEngine;
|
pub use crate::engine::JITEngine;
|
||||||
pub use crate::function_table::FunctionTable;
|
pub use crate::function_table::FunctionTable;
|
||||||
pub use crate::link::link_module;
|
pub use crate::link::link_module;
|
||||||
pub use crate::module::CompiledModule;
|
|
||||||
|
|
||||||
/// Version number of this crate.
|
/// Version number of this crate.
|
||||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ pub struct SerializableCompilation {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Serializable struct that is able to serialize from and to
|
/// Serializable struct that is able to serialize from and to
|
||||||
/// a `CompiledModuleInfo`.
|
/// a `JITArtifactInfo`.
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub struct SerializableModule {
|
pub struct SerializableModule {
|
||||||
pub compilation: SerializableCompilation,
|
pub compilation: SerializableCompilation,
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
//! Define `NativeModule` to allow compiling and instantiating to be
|
//! Define `NativeArtifact` to allow compiling and instantiating to be
|
||||||
//! done as separate steps.
|
//! done as separate steps.
|
||||||
|
|
||||||
use crate::engine::{NativeEngine, NativeEngineInner};
|
use crate::engine::{NativeEngine, NativeEngineInner};
|
||||||
@@ -21,7 +21,7 @@ use wasmer_compiler::CompileError;
|
|||||||
use wasmer_compiler::ModuleEnvironment;
|
use wasmer_compiler::ModuleEnvironment;
|
||||||
use wasmer_compiler::RelocationTarget;
|
use wasmer_compiler::RelocationTarget;
|
||||||
use wasmer_engine::{
|
use wasmer_engine::{
|
||||||
resolve_imports, CompiledModule, DeserializeError, Engine, InstantiationError, Resolver,
|
resolve_imports, Artifact, DeserializeError, Engine, InstantiationError, Resolver,
|
||||||
RuntimeError, SerializeError,
|
RuntimeError, SerializeError,
|
||||||
};
|
};
|
||||||
use wasmer_runtime::{
|
use wasmer_runtime::{
|
||||||
@@ -31,7 +31,7 @@ use wasmer_runtime::{
|
|||||||
use wasmer_runtime::{MemoryPlan, TablePlan};
|
use wasmer_runtime::{MemoryPlan, TablePlan};
|
||||||
|
|
||||||
/// A compiled wasm module, ready to be instantiated.
|
/// A compiled wasm module, ready to be instantiated.
|
||||||
pub struct NativeModule {
|
pub struct NativeArtifact {
|
||||||
sharedobject_path: PathBuf,
|
sharedobject_path: PathBuf,
|
||||||
metadata: ModuleMetadata,
|
metadata: ModuleMetadata,
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@@ -45,8 +45,8 @@ fn to_compile_error(err: impl Error) -> CompileError {
|
|||||||
CompileError::Codegen(format!("{}", err))
|
CompileError::Codegen(format!("{}", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NativeModule {
|
impl NativeArtifact {
|
||||||
/// Compile a data buffer into a `NativeModule`, which may then be instantiated.
|
/// Compile a data buffer into a `NativeArtifact`, which may then be instantiated.
|
||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
pub fn new(engine: &NativeEngine, data: &[u8]) -> Result<Self, CompileError> {
|
pub fn new(engine: &NativeEngine, data: &[u8]) -> Result<Self, CompileError> {
|
||||||
let environ = ModuleEnvironment::new();
|
let environ = ModuleEnvironment::new();
|
||||||
@@ -289,7 +289,7 @@ impl NativeModule {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Construct a `NativeModule` from component parts.
|
/// Construct a `NativeArtifact` from component parts.
|
||||||
pub fn from_parts(
|
pub fn from_parts(
|
||||||
engine_inner: &mut NativeEngineInner,
|
engine_inner: &mut NativeEngineInner,
|
||||||
metadata: ModuleMetadata,
|
metadata: ModuleMetadata,
|
||||||
@@ -379,7 +379,7 @@ impl NativeModule {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a data buffer into a `NativeModule`, which may then be instantiated.
|
/// Compile a data buffer into a `NativeArtifact`, which may then be instantiated.
|
||||||
#[cfg(not(feature = "compiler"))]
|
#[cfg(not(feature = "compiler"))]
|
||||||
pub fn new(engine: &NativeEngine, data: &[u8]) -> Result<Self, CompileError> {
|
pub fn new(engine: &NativeEngine, data: &[u8]) -> Result<Self, CompileError> {
|
||||||
Err(CompileError::Codegen(
|
Err(CompileError::Codegen(
|
||||||
@@ -387,16 +387,16 @@ impl NativeModule {
|
|||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serialize a NativeModule
|
/// Serialize a NativeArtifact
|
||||||
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
|
||||||
Ok(std::fs::read(&self.sharedobject_path)?)
|
Ok(std::fs::read(&self.sharedobject_path)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserialize a NativeModule
|
/// Deserialize a NativeArtifact
|
||||||
pub unsafe fn deserialize_from_file(
|
pub unsafe fn deserialize_from_file(
|
||||||
engine: &NativeEngine,
|
engine: &NativeEngine,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
) -> Result<NativeModule, DeserializeError> {
|
) -> Result<NativeArtifact, DeserializeError> {
|
||||||
let lib = Library::new(&path).map_err(|e| {
|
let lib = Library::new(&path).map_err(|e| {
|
||||||
DeserializeError::CorruptedBinary(format!("Library loading failed: {}", e))
|
DeserializeError::CorruptedBinary(format!("Library loading failed: {}", e))
|
||||||
})?;
|
})?;
|
||||||
@@ -434,7 +434,7 @@ impl NativeModule {
|
|||||||
&self.metadata.table_plans
|
&self.metadata.table_plans
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Crate an `Instance` from this `NativeModule`.
|
/// Crate an `Instance` from this `NativeArtifact`.
|
||||||
///
|
///
|
||||||
/// # Unsafety
|
/// # Unsafety
|
||||||
///
|
///
|
||||||
@@ -511,7 +511,7 @@ impl NativeModule {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CompiledModule for NativeModule {
|
impl Artifact for NativeArtifact {
|
||||||
fn module(&self) -> &ModuleInfo {
|
fn module(&self) -> &ModuleInfo {
|
||||||
&self.metadata.module
|
&self.metadata.module
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//! Native Engine.
|
//! Native Engine.
|
||||||
|
|
||||||
use crate::NativeModule;
|
use crate::NativeArtifact;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Read, Write};
|
use std::io::{Read, Write};
|
||||||
@@ -13,8 +13,7 @@ use wasmer_compiler::CompileError;
|
|||||||
#[cfg(feature = "compiler")]
|
#[cfg(feature = "compiler")]
|
||||||
use wasmer_compiler::{Compiler, CompilerConfig};
|
use wasmer_compiler::{Compiler, CompilerConfig};
|
||||||
use wasmer_engine::{
|
use wasmer_engine::{
|
||||||
CompiledModule as BaseCompiledModule, DeserializeError, Engine, InstantiationError, Resolver,
|
Artifact, DeserializeError, Engine, InstantiationError, Resolver, SerializeError, Tunables,
|
||||||
SerializeError, Tunables,
|
|
||||||
};
|
};
|
||||||
use wasmer_runtime::{InstanceHandle, SignatureRegistry, VMSharedSignatureIndex, VMTrampoline};
|
use wasmer_runtime::{InstanceHandle, SignatureRegistry, VMSharedSignatureIndex, VMTrampoline};
|
||||||
|
|
||||||
@@ -158,18 +157,18 @@ impl Engine for NativeEngine {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Compile a WebAssembly binary
|
/// Compile a WebAssembly binary
|
||||||
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn BaseCompiledModule>, CompileError> {
|
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn Artifact>, CompileError> {
|
||||||
Ok(Arc::new(NativeModule::new(&self, binary)?))
|
Ok(Arc::new(NativeArtifact::new(&self, binary)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Instantiates a WebAssembly module
|
/// Instantiates a WebAssembly module
|
||||||
unsafe fn instantiate(
|
unsafe fn instantiate(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
resolver: &dyn Resolver,
|
resolver: &dyn Resolver,
|
||||||
) -> Result<InstanceHandle, InstantiationError> {
|
) -> Result<InstanceHandle, InstantiationError> {
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<NativeModule>()
|
.downcast_ref::<NativeArtifact>()
|
||||||
.expect("The provided module is not a Native compiled module");
|
.expect("The provided module is not a Native compiled module");
|
||||||
compiled_module.instantiate(&self, resolver, Box::new(()))
|
compiled_module.instantiate(&self, resolver, Box::new(()))
|
||||||
}
|
}
|
||||||
@@ -177,29 +176,26 @@ impl Engine for NativeEngine {
|
|||||||
/// Finish the instantiation of a WebAssembly module
|
/// Finish the instantiation of a WebAssembly module
|
||||||
unsafe fn finish_instantiation(
|
unsafe fn finish_instantiation(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
handle: &InstanceHandle,
|
handle: &InstanceHandle,
|
||||||
) -> Result<(), InstantiationError> {
|
) -> Result<(), InstantiationError> {
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<NativeModule>()
|
.downcast_ref::<NativeArtifact>()
|
||||||
.expect("The provided module is not a Native compiled module");
|
.expect("The provided module is not a Native compiled module");
|
||||||
compiled_module.finish_instantiation(&handle)
|
compiled_module.finish_instantiation(&handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Serializes a WebAssembly module
|
/// Serializes a WebAssembly module
|
||||||
fn serialize(
|
fn serialize(&self, compiled_module: &dyn Artifact) -> Result<Vec<u8>, SerializeError> {
|
||||||
&self,
|
|
||||||
compiled_module: &dyn BaseCompiledModule,
|
|
||||||
) -> Result<Vec<u8>, SerializeError> {
|
|
||||||
let compiled_module = compiled_module
|
let compiled_module = compiled_module
|
||||||
.downcast_ref::<NativeModule>()
|
.downcast_ref::<NativeArtifact>()
|
||||||
.expect("The provided module is not a Native compiled module");
|
.expect("The provided module is not a Native compiled module");
|
||||||
let serialized = compiled_module.serialize()?;
|
let serialized = compiled_module.serialize()?;
|
||||||
Ok(serialized)
|
Ok(serialized)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deserializes a WebAssembly module (binary content of a Shared Object file)
|
/// Deserializes a WebAssembly module (binary content of a Shared Object file)
|
||||||
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn BaseCompiledModule>, DeserializeError> {
|
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn Artifact>, DeserializeError> {
|
||||||
if !Self::is_deserializable(&bytes) {
|
if !Self::is_deserializable(&bytes) {
|
||||||
return Err(DeserializeError::Incompatible(
|
return Err(DeserializeError::Incompatible(
|
||||||
"The provided bytes are not in any native format Wasmer can understand".to_string(),
|
"The provided bytes are not in any native format Wasmer can understand".to_string(),
|
||||||
@@ -217,7 +213,7 @@ impl Engine for NativeEngine {
|
|||||||
fn deserialize_from_file(
|
fn deserialize_from_file(
|
||||||
&self,
|
&self,
|
||||||
file_ref: &Path,
|
file_ref: &Path,
|
||||||
) -> Result<Arc<dyn BaseCompiledModule>, DeserializeError> {
|
) -> Result<Arc<dyn Artifact>, DeserializeError> {
|
||||||
let mut file = File::open(&file_ref)?;
|
let mut file = File::open(&file_ref)?;
|
||||||
let mut buffer = [0; 5];
|
let mut buffer = [0; 5];
|
||||||
// read up to 5 bytes
|
// read up to 5 bytes
|
||||||
@@ -228,7 +224,7 @@ impl Engine for NativeEngine {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
unsafe {
|
unsafe {
|
||||||
Ok(Arc::new(NativeModule::deserialize_from_file(
|
Ok(Arc::new(NativeArtifact::deserialize_from_file(
|
||||||
&self, &file_ref,
|
&self, &file_ref,
|
||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,12 @@
|
|||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
mod artifact;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod module;
|
|
||||||
mod serialize;
|
mod serialize;
|
||||||
|
|
||||||
|
pub use crate::artifact::NativeArtifact;
|
||||||
pub use crate::engine::NativeEngine;
|
pub use crate::engine::NativeEngine;
|
||||||
pub use crate::module::NativeModule;
|
|
||||||
|
|
||||||
/// Version number of this crate.
|
/// Version number of this crate.
|
||||||
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||||
|
|||||||
@@ -2,9 +2,9 @@ use wasmer_runtime::ModuleInfo;
|
|||||||
|
|
||||||
use downcast_rs::{impl_downcast, Downcast};
|
use downcast_rs::{impl_downcast, Downcast};
|
||||||
|
|
||||||
/// The `CompiledModule` trait is used by engine implementors, such
|
/// The `Artifact` trait is used by engine implementors, such
|
||||||
/// as a JIT or Native execution.
|
/// as a JIT or Native execution.
|
||||||
pub trait CompiledModule: Downcast {
|
pub trait Artifact: Downcast {
|
||||||
/// Return a pointer to a module.
|
/// Return a pointer to a module.
|
||||||
fn module(&self) -> &ModuleInfo;
|
fn module(&self) -> &ModuleInfo;
|
||||||
|
|
||||||
@@ -12,4 +12,4 @@ pub trait CompiledModule: Downcast {
|
|||||||
fn module_mut(&mut self) -> &mut ModuleInfo;
|
fn module_mut(&mut self) -> &mut ModuleInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_downcast!(CompiledModule);
|
impl_downcast!(Artifact);
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
use crate::error::InstantiationError;
|
use crate::error::InstantiationError;
|
||||||
use crate::resolver::Resolver;
|
use crate::resolver::Resolver;
|
||||||
use crate::tunables::Tunables;
|
use crate::tunables::Tunables;
|
||||||
use crate::{CompiledModule, DeserializeError, SerializeError};
|
use crate::{Artifact, DeserializeError, SerializeError};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use wasm_common::FunctionType;
|
use wasm_common::FunctionType;
|
||||||
@@ -30,33 +30,33 @@ pub trait Engine {
|
|||||||
fn validate(&self, binary: &[u8]) -> Result<(), CompileError>;
|
fn validate(&self, binary: &[u8]) -> Result<(), CompileError>;
|
||||||
|
|
||||||
/// Compile a WebAssembly binary
|
/// Compile a WebAssembly binary
|
||||||
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn CompiledModule>, CompileError>;
|
fn compile(&self, binary: &[u8]) -> Result<Arc<dyn Artifact>, CompileError>;
|
||||||
|
|
||||||
/// Instantiates a WebAssembly module
|
/// Instantiates a WebAssembly module
|
||||||
unsafe fn instantiate(
|
unsafe fn instantiate(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn CompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
resolver: &dyn Resolver,
|
resolver: &dyn Resolver,
|
||||||
) -> Result<InstanceHandle, InstantiationError>;
|
) -> Result<InstanceHandle, InstantiationError>;
|
||||||
|
|
||||||
/// Finish the instantiation of a WebAssembly module
|
/// Finish the instantiation of a WebAssembly module
|
||||||
unsafe fn finish_instantiation(
|
unsafe fn finish_instantiation(
|
||||||
&self,
|
&self,
|
||||||
compiled_module: &dyn CompiledModule,
|
compiled_module: &dyn Artifact,
|
||||||
handle: &InstanceHandle,
|
handle: &InstanceHandle,
|
||||||
) -> Result<(), InstantiationError>;
|
) -> Result<(), InstantiationError>;
|
||||||
|
|
||||||
/// Serializes a WebAssembly module
|
/// Serializes a WebAssembly module
|
||||||
fn serialize(&self, compiled_module: &dyn CompiledModule) -> Result<Vec<u8>, SerializeError>;
|
fn serialize(&self, compiled_module: &dyn Artifact) -> Result<Vec<u8>, SerializeError>;
|
||||||
|
|
||||||
/// Deserializes a WebAssembly module
|
/// Deserializes a WebAssembly module
|
||||||
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn CompiledModule>, DeserializeError>;
|
fn deserialize(&self, bytes: &[u8]) -> Result<Arc<dyn Artifact>, DeserializeError>;
|
||||||
|
|
||||||
/// Deserializes a WebAssembly module from a path
|
/// Deserializes a WebAssembly module from a path
|
||||||
fn deserialize_from_file(
|
fn deserialize_from_file(
|
||||||
&self,
|
&self,
|
||||||
file_ref: &Path,
|
file_ref: &Path,
|
||||||
) -> Result<Arc<dyn CompiledModule>, DeserializeError> {
|
) -> Result<Arc<dyn Artifact>, DeserializeError> {
|
||||||
let bytes = std::fs::read(file_ref)?;
|
let bytes = std::fs::read(file_ref)?;
|
||||||
self.deserialize(&bytes)
|
self.deserialize(&bytes)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,19 +21,19 @@
|
|||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
|
|
||||||
|
mod artifact;
|
||||||
mod engine;
|
mod engine;
|
||||||
mod error;
|
mod error;
|
||||||
mod module;
|
|
||||||
mod resolver;
|
mod resolver;
|
||||||
mod serialize;
|
mod serialize;
|
||||||
mod trap;
|
mod trap;
|
||||||
mod tunables;
|
mod tunables;
|
||||||
|
|
||||||
|
pub use crate::artifact::Artifact;
|
||||||
pub use crate::engine::Engine;
|
pub use crate::engine::Engine;
|
||||||
pub use crate::error::{
|
pub use crate::error::{
|
||||||
DeserializeError, ImportError, InstantiationError, LinkError, SerializeError,
|
DeserializeError, ImportError, InstantiationError, LinkError, SerializeError,
|
||||||
};
|
};
|
||||||
pub use crate::module::CompiledModule;
|
|
||||||
pub use crate::resolver::{resolve_imports, NullResolver, Resolver};
|
pub use crate::resolver::{resolve_imports, NullResolver, Resolver};
|
||||||
pub use crate::serialize::SerializableFunctionFrameInfo;
|
pub use crate::serialize::SerializableFunctionFrameInfo;
|
||||||
pub use crate::trap::*;
|
pub use crate::trap::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user