Skip to content

Commit

Permalink
Merge pull request wasmerio#4085 from wasmerio/load-webc-from-disk
Browse files Browse the repository at this point in the history
Switch to lazily loading a Wasmer package directly from disk
  • Loading branch information
Michael Bryan authored Jul 20, 2023
2 parents aaa34d2 + 8e83546 commit 4380bb3
Show file tree
Hide file tree
Showing 8 changed files with 516 additions and 538 deletions.
788 changes: 355 additions & 433 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,7 @@ rust-version = "1.67"
version = "4.0.0"

[workspace.dependencies]
webc = { version = "5.0.4", default-features = false }
wapm-targz-to-pirita = "0.3.1"
webc = { version = "5.1.1", default-features = false, features = ["package"] }
wasmer-toml = "0.6.0"

[build-dependencies]
Expand Down Expand Up @@ -291,3 +290,4 @@ required-features = ["backend"]
name = "features"
path = "examples/features.rs"
required-features = ["cranelift"]

2 changes: 0 additions & 2 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ confidence-threshold = 0.8
exceptions = [
# Each entry is the crate and version constraint, and its specific allow
# list
{ name = "webc", allow = ["BUSL-1.1"] },
{ name = "wapm-targz-to-pirita", allow = ["BUSL-1.1"] },
]


Expand Down
1 change: 0 additions & 1 deletion lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ virtual-net = { version = "0.3.0", path = "../virtual-net" }

# Wasmer-owned dependencies.
webc = { workspace = true }
wapm-targz-to-pirita = { workspace = true }
wasmer-deploy-cli = { version = "0.1.17", default-features = false }

# Third-party dependencies.
Expand Down
52 changes: 5 additions & 47 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use sha2::{Digest, Sha256};
use tempfile::NamedTempFile;
use tokio::runtime::Handle;
use url::Url;
use wapm_targz_to_pirita::{webc::v1::DirOrFile, FileMap, TransformManifestFunctions};
use wasmer::{
DeserializeError, Engine, Function, Imports, Instance, Module, Store, Type, TypedFunction,
Value,
Expand Down Expand Up @@ -544,11 +543,13 @@ enum ExecutableTarget {
impl ExecutableTarget {
/// Try to load a Wasmer package from a directory containing a `wasmer.toml`
/// file.
#[tracing::instrument(level = "debug", skip_all)]
fn from_dir(dir: &Path, runtime: &dyn Runtime, pb: &ProgressBar) -> Result<Self, Error> {
pb.set_message(format!("Loading \"{}\" into memory", dir.display()));

let webc = construct_webc_in_memory(dir)?;
let container = Container::from_bytes(webc)?;
let manifest_path = dir.join("wasmer.toml");
let webc = webc::wasmer_package::Package::from_manifest(manifest_path)?;
let container = Container::from(webc);

pb.set_message("Resolving dependencies");
let pkg = runtime
Expand All @@ -559,7 +560,7 @@ impl ExecutableTarget {
}

/// Try to load a file into something that can be used to run it.
#[tracing::instrument(skip_all)]
#[tracing::instrument(level = "debug", skip_all)]
fn from_file(path: &Path, runtime: &dyn Runtime, pb: &ProgressBar) -> Result<Self, Error> {
pb.set_message(format!("Loading from \"{}\"", path.display()));

Expand Down Expand Up @@ -622,49 +623,6 @@ impl ExecutableTarget {
}
}

#[tracing::instrument(level = "debug", skip_all)]
fn construct_webc_in_memory(dir: &Path) -> Result<Vec<u8>, Error> {
let mut files = BTreeMap::new();
load_files_from_disk(&mut files, dir, dir)?;

let wasmer_toml = DirOrFile::File("wasmer.toml".into());
if let Some(toml_data) = files.remove(&wasmer_toml) {
// HACK(Michael-F-Bryan): The version of wapm-targz-to-pirita we are
// using doesn't know we renamed "wapm.toml" to "wasmer.toml", so we
// manually patch things up if people have already migrated their
// projects.
files
.entry(DirOrFile::File("wapm.toml".into()))
.or_insert(toml_data);
}

let functions = TransformManifestFunctions::default();
let webc = wapm_targz_to_pirita::generate_webc_file(files, dir, &functions)?;

Ok(webc)
}

fn load_files_from_disk(files: &mut FileMap, dir: &Path, base: &Path) -> Result<(), Error> {
let entries = dir
.read_dir()
.with_context(|| format!("Unable to read the contents of \"{}\"", dir.display()))?;

for entry in entries {
let path = entry?.path();
let relative_path = path.strip_prefix(base)?.to_path_buf();

if path.is_dir() {
load_files_from_disk(files, &path, base)?;
files.insert(DirOrFile::Dir(relative_path), Vec::new());
} else if path.is_file() {
let data = std::fs::read(&path)
.with_context(|| format!("Unable to read \"{}\"", path.display()))?;
files.insert(DirOrFile::File(relative_path), data);
}
}
Ok(())
}

#[cfg(feature = "coredump")]
fn generate_coredump(err: &Error, source_name: String, coredump_path: &Path) -> Result<(), Error> {
let err: &wasmer::RuntimeError = match err.downcast_ref() {
Expand Down
Loading

0 comments on commit 4380bb3

Please sign in to comment.