Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build(wasmer/cache): opt 'sys' into default feature #2821

Merged
merged 6 commits into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,7 @@ js-default = ["js", "std", "wasm-types-polyfill"]

wasm-types-polyfill = ["js", "wasmparser"]

js-serializable-module = []

[package.metadata.docs.rs]
features = ["compiler", "core", "cranelift", "default-compiler", "default-dylib", "default-engine", "dylib", "engine", "jit", "native", "singlepass", "sys", "sys-default", "universal"]
37 changes: 37 additions & 0 deletions lib/api/src/js/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,40 @@ pub enum WasmError {
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}

/// The Serialize error can occur when serializing a
/// compiled Module into a binary.
/// Copied from wasmer_engine::SerializeError
#[derive(Debug)]
#[cfg_attr(feature = "std", derive(Error))]
pub enum SerializeError {
/// An IO error
#[cfg_attr(feature = "std", error(transparent))]
Io(#[from] std::io::Error),
/// A generic serialization error
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
}

/// The Deserialize error can occur when loading a
/// compiled Module from a binary.
/// Copied from wasmer_engine::DeSerializeError
#[derive(Error, Debug)]
pub enum DeserializeError {
/// An IO error
#[cfg_attr(feature = "std", error(transparent))]
Io(#[from] std::io::Error),
/// A generic deserialization error
#[cfg_attr(feature = "std", error("{0}"))]
Generic(String),
/// Incompatible serialized binary
#[cfg_attr(feature = "std", error("incompatible binary: {0}"))]
Incompatible(String),
/// The provided binary is corrupted
#[cfg_attr(feature = "std", error("corrupted binary: {0}"))]
CorruptedBinary(String),
/// The binary was valid, but we got an error when
/// trying to allocate the required resources.
#[cfg_attr(feature = "std", error(transparent))]
Compiler(CompileError),
}
1 change: 1 addition & 0 deletions lib/api/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub use wasmer_derive::WasmerEnv;

pub use crate::js::cell::WasmCell;
pub use crate::js::env::{HostEnvInitError, LazyInit, WasmerEnv};
pub use crate::js::error::{DeserializeError, SerializeError};
pub use crate::js::export::Export;
pub use crate::js::exports::{ExportError, Exportable, Exports, ExportsIterator};
pub use crate::js::externals::{
Expand Down
25 changes: 24 additions & 1 deletion lib/api/src/js/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::js::resolver::Resolver;
use crate::js::store::Store;
use crate::js::types::{ExportType, ImportType};
// use crate::js::InstantiationError;
use crate::js::error::CompileError;
use crate::js::error::{CompileError, SerializeError, DeserializeError};
#[cfg(feature = "wat")]
use crate::js::error::WasmError;
use crate::js::RuntimeError;
Expand Down Expand Up @@ -62,6 +62,8 @@ pub struct Module {
name: Option<String>,
// WebAssembly type hints
type_hints: Option<ModuleTypeHints>,
#[cfg(feature = "js-serializable-module")]
raw_bytes: Option<Vec<u8>>
}

impl Module {
Expand Down Expand Up @@ -194,6 +196,8 @@ impl Module {
module,
type_hints,
name,
#[cfg(feature = "js-serializable-module")]
raw_bytes: Some(binary.to_vec())
})
}

Expand Down Expand Up @@ -273,6 +277,23 @@ impl Module {
// self.artifact.module_ref().name.as_deref()
}

/// Serializes a module into a binary representation that the `Engine`
/// can later process via [`Module::deserialize`].
///
#[cfg(feature = "js-serializable-module")]
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
self.raw_bytes.clone().ok_or(SerializeError::Generic("Not able to serialize module".to_string()))
}

/// Deserializes a serialized Module binary into a `Module`.
///
/// This is safe since deserialization under `js` is essentially same as reconstructing `Module`.
/// We maintain the `unsafe` to preserve the same API as Wasmer
#[cfg(feature = "js-serializable-module")]
pub unsafe fn deserialize(store: &Store, bytes: &[u8]) -> Result<Self, DeserializeError> {
Self::new(store, bytes).map_err(|e| DeserializeError::Compiler(e))
}

/// Sets the name of the current module.
/// This is normally useful for stacktraces and debugging.
///
Expand Down Expand Up @@ -512,6 +533,8 @@ impl From<WebAssembly::Module> for Module {
module,
name: None,
type_hints: None,
#[cfg(feature = "js-serializable-module")]
raw_bytes: None
}
}
}
5 changes: 4 additions & 1 deletion lib/cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
edition = "2018"

[dependencies]
wasmer = { path = "../api", version = "=2.2.1", default-features = false, features = ["sys"] }
wasmer = { path = "../api", version = "=2.2.1", default-features = false}
hex = "0.4"
thiserror = "1"
blake3 = "1.0"
Expand All @@ -25,6 +25,9 @@ wasmer-engine-universal = { path = "../engine-universal", version = "=2.2.1" }
wasmer-engine-dylib = { path = "../engine-dylib", version = "=2.2.1" }

[features]
default = ["sys"]
sys = ["wasmer/sys-default"]
js = ["wasmer/js-default", "wasmer/js-serializable-module"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should enforce sys or js by default. We can simplify this such as:

Suggested change
default = ["sys"]
sys = ["wasmer/sys-default"]
js = ["wasmer/js-default", "wasmer/js-serializable-module"]
default = ["wasmer/js-serializable-module"]

This should work without adding any feature dependency on wasmer (that is, that the wasmer crate will not automatically use js or sys, it will just use the wasmer/js-serializable-module feature when using js but do nothing on sys, which is good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was curious about this

wasmer crate will not automatically use js or sys

Wouldn't it make existing build as breaking, that upper deps having wasmer-cache or transitive deps need to manually opt in sys if they're using default? Or it'll be fine since any pkg having wasmer already may opt in js / sys?

Copy link
Member

@syrusakbary syrusakbary Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I think I was not super clear. I think the filesystem should be enabled by default as well (that way there are no breaking changes).
However, we should not enforce sys or js by default (wasmer-cache should remain agnostic).
In any case, I did applied a small suggestion and the PR looks good!

blake3-pure = ["blake3/pure"]

[[bench]]
Expand Down
3 changes: 3 additions & 0 deletions lib/cache/src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg_attr(not(feature = "sys"), allow(unused))]
kwonoj marked this conversation as resolved.
Show resolved Hide resolved
use crate::cache::Cache;
use crate::hash::Hash;
use std::fs::{create_dir_all, File};
Expand Down Expand Up @@ -35,6 +36,7 @@ pub struct FileSystemCache {
ext: Option<String>,
}

#[cfg(feature = "sys")]
impl FileSystemCache {
/// Construct a new `FileSystemCache` around the specified directory.
pub fn new<P: Into<PathBuf>>(path: P) -> io::Result<Self> {
Expand Down Expand Up @@ -84,6 +86,7 @@ impl FileSystemCache {
}
}

#[cfg(feature = "sys")]
impl Cache for FileSystemCache {
type DeserializeError = DeserializeError;
type SerializeError = SerializeError;
Expand Down
1 change: 1 addition & 0 deletions lib/cache/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod filesystem;
mod hash;

pub use crate::cache::Cache;
#[cfg(feature = "sys")]
pub use crate::filesystem::FileSystemCache;
pub use crate::hash::Hash;

Expand Down