Improved code based on comments

This commit is contained in:
Syrus
2020-05-11 09:55:24 -07:00
parent 29e8adaa86
commit 598d90b746
7 changed files with 37 additions and 8 deletions

View File

@@ -26,6 +26,7 @@ pub use crate::types::{
MemoryType, Mutability, TableType, Val, ValType, MemoryType, Mutability, TableType, Val, ValType,
}; };
pub use target_lexicon::{Architecture, OperatingSystem, Triple, HOST};
pub use wasm_common::{Bytes, Pages, ValueType, WasmExternType, WasmTypeList}; pub use wasm_common::{Bytes, Pages, ValueType, WasmExternType, WasmTypeList};
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
pub use wasmer_compiler::CompilerConfig; pub use wasmer_compiler::CompilerConfig;

View File

@@ -1,7 +1,9 @@
# Wasmer Native # Wasmer Native
The Wasmer Native is usable with any compiler implementation The Wasmer Native is usable with any compiler implementation
based on `wasmer-compiler`. based on `wasmer-compiler` that is able to emit Position Independent
Code (PIC).
After the compiler process the result, the Native Engine generates After the compiler process the result, the Native Engine generates
a shared object file and links it via `dlsym` so it can be usable by the a shared object file and links it via `dlsym` so it can be usable by the
`wasmer` api. `wasmer` api.

View File

@@ -231,7 +231,10 @@ impl NativeEngineInner {
if self.compiler.is_none() { if self.compiler.is_none() {
return Err(CompileError::Codegen("The NativeEngine is operating in headless mode, so it can only execute already compiled Modules.".to_string())); return Err(CompileError::Codegen("The NativeEngine is operating in headless mode, so it can only execute already compiled Modules.".to_string()));
} }
Ok(&**self.compiler.as_ref().expect("Can't get compiler reference")) Ok(&**self
.compiler
.as_ref()
.expect("Can't get compiler reference"))
} }
/// Validate the module /// Validate the module

View File

@@ -4,6 +4,8 @@
//! it generates a shared object file (`.so` or `.dylib` depending on //! it generates a shared object file (`.so` or `.dylib` depending on
//! the target), saves it temporarily to disk and uses it natively //! the target), saves it temporarily to disk and uses it natively
//! via `dlopen` and `dlsym` (using the `libloading` library). //! via `dlopen` and `dlsym` (using the `libloading` library).
//!
//! Note: `.dll` generation for Windows is not yet supported
#![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)] #![deny(missing_docs, trivial_numeric_casts, unused_extern_crates)]
#![warn(unused_import_braces)] #![warn(unused_import_braces)]

View File

@@ -3,7 +3,7 @@
use crate::engine::{NativeEngine, NativeEngineInner}; use crate::engine::{NativeEngine, NativeEngineInner};
use crate::serialize::ModuleMetadata; use crate::serialize::ModuleMetadata;
use faerie::{ArtifactBuilder, Decl, Link, Reloc, SectionKind}; use faerie::{ArtifactBuilder, ArtifactError, Decl, Link, Reloc, SectionKind};
use libloading::{Library, Symbol}; use libloading::{Library, Symbol};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::any::Any; use std::any::Any;
@@ -344,7 +344,6 @@ impl NativeModule {
use std::slice::from_raw_parts; use std::slice::from_raw_parts;
let mut size = &mut **symbol.deref(); let mut size = &mut **symbol.deref();
// println!("Size {:?}", size.to_vec());
let mut readable = &size[..]; let mut readable = &size[..];
let metadata_len = leb128::read::unsigned(&mut readable).map_err(|e| { let metadata_len = leb128::read::unsigned(&mut readable).map_err(|e| {
DeserializeError::CorruptedBinary("Can't read metadata size".to_string()) DeserializeError::CorruptedBinary("Can't read metadata size".to_string())

View File

@@ -35,7 +35,17 @@ impl Compile {
.map(|osstr| osstr.to_string_lossy().to_string()) .map(|osstr| osstr.to_string_lossy().to_string())
.unwrap_or_default(); .unwrap_or_default();
let recommended_extension = match engine_name.as_ref() { let recommended_extension = match engine_name.as_ref() {
"native" => "so", "native" => {
// TODO: Match it depending on the `BinaryFormat` instead of the
// `OperatingSystem`.
let target = self.compiler.get_target()?;
match target.triple().operating_system {
OperatingSystem::Darwin
| OperatingSystem::Ios
| OperatingSystem::MacOSX { .. } => "dylib",
_ => "so",
}
}
"jit" => "wjit", "jit" => "wjit",
_ => "?", _ => "?",
}; };

View File

@@ -101,23 +101,35 @@ impl StoreOptions {
} }
} }
/// Get the Target architecture
pub fn get_features(&self) -> Result<Features> {
Ok(Features::default())
}
/// Get the Target architecture
pub fn get_target(&self) -> Result<Target> {
Ok(Target::default())
}
/// Get the Compiler Config for the current options /// Get the Compiler Config for the current options
#[allow(unused_variables)] #[allow(unused_variables)]
fn get_config(&self, compiler: Compiler) -> Result<Box<dyn CompilerConfig>> { fn get_config(&self, compiler: Compiler) -> Result<Box<dyn CompilerConfig>> {
let features = self.get_features()?;
let target = self.get_target()?;
let config: Box<dyn CompilerConfig> = match compiler { let config: Box<dyn CompilerConfig> = match compiler {
#[cfg(feature = "singlepass")] #[cfg(feature = "singlepass")]
Compiler::Singlepass => { Compiler::Singlepass => {
let config = wasmer_compiler_singlepass::SinglepassConfig::default(); let config = wasmer_compiler_singlepass::SinglepassConfig::new(features, target);
Box::new(config) Box::new(config)
} }
#[cfg(feature = "cranelift")] #[cfg(feature = "cranelift")]
Compiler::Cranelift => { Compiler::Cranelift => {
let config = wasmer_compiler_cranelift::CraneliftConfig::default(); let config = wasmer_compiler_cranelift::CraneliftConfig::new(features, target);
Box::new(config) Box::new(config)
} }
#[cfg(feature = "llvm")] #[cfg(feature = "llvm")]
Compiler::LLVM => { Compiler::LLVM => {
let config = wasmer_compiler_llvm::LLVMConfig::default(); let config = wasmer_compiler_llvm::LLVMConfig::new(features, target);
Box::new(config) Box::new(config)
} }
#[cfg(not(all(feature = "singlepass", feature = "cranelift", feature = "llvm",)))] #[cfg(not(all(feature = "singlepass", feature = "cranelift", feature = "llvm",)))]