Skip to content

Commit

Permalink
Merge pull request #3852 from wasmerio/updated-runners
Browse files Browse the repository at this point in the history
Re-work package resolution and make runners use it
  • Loading branch information
syrusakbary authored May 25, 2023
2 parents c77166e + 2f58a25 commit e89b313
Show file tree
Hide file tree
Showing 58 changed files with 4,746 additions and 2,805 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ api-docs-repo/
.xwin-cache
wapm.toml
wasmer.toml
*.snap.new
# Generated by tests on Android
/avd
/core
Expand All @@ -25,4 +26,4 @@ build-capi.tar.gz
build-wasmer.tar.gz
lcov.info
link/
link.tar.gz
link.tar.gz
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions lib/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ wasm-coredump-builder = { version = "0.1.11", optional = true }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = [ "env-filter", "fmt" ] }
clap-verbosity-flag = "2"
async-trait = "0.1.68"
tokio = { version = "1.28.1", features = ["macros", "rt-multi-thread"] }
once_cell = "1.17.1"

# NOTE: Must use different features for clap because the "color" feature does not
# work on wasi due to the anstream dependency not compiling.
Expand Down
82 changes: 56 additions & 26 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ use anyhow::{anyhow, Context, Result};
use clap::Parser;
#[cfg(feature = "coredump")]
use std::fs::File;
use std::io::Write;
use std::net::SocketAddr;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::{io::Write, sync::Arc};
use tokio::runtime::Handle;
use wasmer::FunctionEnv;
use wasmer::*;
use wasmer_cache::{Cache, FileSystemCache, Hash};
use wasmer_registry::WasmerConfig;
use wasmer_types::Type as ValueType;
use wasmer_wasix::runners::Runner;

Expand Down Expand Up @@ -246,10 +248,15 @@ impl RunWithPathBuf {
}

fn inner_execute(&self) -> Result<()> {
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
let handle = runtime.handle().clone();

#[cfg(feature = "webc_runner")]
{
if let Ok(pf) = webc::Container::from_disk(self.path.clone()) {
return self.run_container(pf, self.command_name.as_deref(), &self.args);
return self.run_container(pf, self.command_name.as_deref(), &self.args, handle);
}
}
let (mut store, module) = self.get_store_module()?;
Expand Down Expand Up @@ -342,9 +349,13 @@ impl RunWithPathBuf {
.map(|f| f.to_string_lossy().to_string())
})
.unwrap_or_default();

let wasmer_dir = WasmerConfig::get_wasmer_dir().map_err(anyhow::Error::msg)?;
let runtime = Arc::new(self.wasi.prepare_runtime(store.engine().clone(), &wasmer_dir, handle)?);

let (ctx, instance) = self
.wasi
.instantiate(&mut store, &module, program_name, self.args.clone())
.instantiate(&module, program_name, self.args.clone(), runtime, &mut store)
.with_context(|| "failed to instantiate WASI module")?;

let capable_of_deep_sleep = unsafe { ctx.data(&store).capable_of_deep_sleep() };
Expand Down Expand Up @@ -415,7 +426,16 @@ impl RunWithPathBuf {
container: webc::Container,
id: Option<&str>,
args: &[String],
handle: Handle,
) -> Result<(), anyhow::Error> {
use wasmer_wasix::{
bin_factory::BinaryPackage,
runners::{emscripten::EmscriptenRunner, wasi::WasiRunner, wcgi::WcgiRunner},
WasiRuntime,
};

let wasmer_dir = WasmerConfig::get_wasmer_dir().map_err(anyhow::Error::msg)?;

let id = id
.or_else(|| container.manifest().entrypoint.as_deref())
.context("No command specified")?;
Expand All @@ -426,35 +446,45 @@ impl RunWithPathBuf {
.with_context(|| format!("No metadata found for the command, \"{id}\""))?;

let (store, _compiler_type) = self.store.get_store()?;
let mut runner = wasmer_wasix::runners::wasi::WasiRunner::new(store);
runner.set_args(args.to_vec());
if runner.can_run_command(id, command).unwrap_or(false) {
return runner.run_cmd(&container, id).context("WASI runner failed");
let runtime = self
.wasi
.prepare_runtime(store.engine().clone(), &wasmer_dir, handle)?;
let runtime = Arc::new(runtime);
let pkg = runtime
.task_manager()
.block_on(BinaryPackage::from_webc(&container, &*runtime))?;

if WasiRunner::can_run_command(command).unwrap_or(false) {
let mut runner = WasiRunner::new();
runner.set_args(args.to_vec());
return runner
.run_command(id, &pkg, runtime)
.context("WASI runner failed");
}

let (store, _compiler_type) = self.store.get_store()?;
let mut runner = wasmer_wasix::runners::emscripten::EmscriptenRunner::new(store);
runner.set_args(args.to_vec());
if runner.can_run_command(id, command).unwrap_or(false) {
if EmscriptenRunner::can_run_command(command).unwrap_or(false) {
let mut runner = EmscriptenRunner::new();
runner.set_args(args.to_vec());
return runner
.run_cmd(&container, id)
.run_command(id, &pkg, runtime)
.context("Emscripten runner failed");
}

let mut runner = wasmer_wasix::runners::wcgi::WcgiRunner::new(id);
let (store, _compiler_type) = self.store.get_store()?;
runner
.config()
.args(args)
.store(store)
.addr(self.wcgi.addr)
.envs(self.wasi.env_vars.clone())
.map_directories(self.wasi.mapped_dirs.clone());
if self.wasi.forward_host_env {
runner.config().forward_host_env();
}
if runner.can_run_command(id, command).unwrap_or(false) {
return runner.run_cmd(&container, id).context("WCGI runner failed");
if WcgiRunner::can_run_command(command).unwrap_or(false) {
let mut runner = WcgiRunner::new();
runner
.config()
.args(args)
.addr(self.wcgi.addr)
.envs(self.wasi.env_vars.clone())
.map_directories(self.wasi.mapped_dirs.clone());
if self.wasi.forward_host_env {
runner.config().forward_host_env();
}

return runner
.run_command(id, &pkg, runtime)
.context("WCGI runner failed");
}

anyhow::bail!(
Expand Down
Loading

0 comments on commit e89b313

Please sign in to comment.