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

Allow users to provide custom imports #406

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
68 changes: 17 additions & 51 deletions Cargo.lock

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

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ wasm-bindgen = { version = "0.2" }
wasm-bindgen-derive = "0.2.1"
wasm-bindgen-futures = "0.4"
wasm-bindgen-test = "0.3.37"
wasmer = { version = "4.2.5", default-features = false, features = ["js", "js-default", "tracing", "wasm-types-polyfill", "enable-serde"] }
wasmer = { version = "4.2.5", default-features = false, features = ["js", "js-default", "wasm-types-polyfill"] }
wasmer-wasix = { version = "0.18", default-features = false, features = ["js", "js-default"] }
webc = "5.3.0"

Expand Down Expand Up @@ -95,10 +95,10 @@ dwarf-debug-info = false
wasm-opt = ["--enable-threads", "--enable-bulk-memory", "-Oz"]

[patch.crates-io]
virtual-net = { git = "https://github.com/wasmerio/wasmer", branch = "master" }
virtual-fs = { git = "https://github.com/wasmerio/wasmer", branch = "master" }
wasmer-wasix = { git = "https://github.com/wasmerio/wasmer", branch = "master" }
wasmer = { git = "https://github.com/wasmerio/wasmer", branch = "master" }
virtual-net = { git = "https://github.com/wasmerio/wasmer", branch = "wasi-runner-custom-imports" }
virtual-fs = { git = "https://github.com/wasmerio/wasmer", branch = "wasi-runner-custom-imports" }
wasmer-wasix = { git = "https://github.com/wasmerio/wasmer", branch = "wasi-runner-custom-imports" }
wasmer = { git = "https://github.com/wasmerio/wasmer", branch = "wasi-runner-custom-imports" }
# virtual-net = { path = "../wasmer/lib/virtual-net" }
# virtual-fs = { path = "../wasmer/lib/virtual-fs" }
# wasmer-wasix = { path = "../wasmer/lib/wasix" }
Expand Down
37 changes: 37 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context;
use js_sys::Array;
use virtual_fs::TmpFileSystem;
use wasm_bindgen::{prelude::wasm_bindgen, JsCast, JsValue, UnwrapThrowExt};
use wasmer::{Extern, Imports};
use wasmer_wasix::WasiEnvBuilder;

use crate::{runtime::Runtime, utils::Error, Directory, DirectoryInit, JsRuntime, StringOrBytes};
Expand Down Expand Up @@ -41,6 +42,10 @@ type CommonOptions = {
* files.
*/
mount?: Record<string, DirectoryInit | Directory>;
/**
* Additional items that may be imported by the WebAssembly instance.
*/
imports?: Record<string, Record<string, any>>;
};

/**
Expand Down Expand Up @@ -82,6 +87,9 @@ extern "C" {
#[wasm_bindgen(method, getter)]
fn env(this: &CommonOptions) -> JsValue;

#[wasm_bindgen(method, getter)]
fn imports(this: &CommonOptions) -> JsValue;

#[wasm_bindgen(method, getter)]
fn stdin(this: &CommonOptions) -> Option<StringOrBytes>;

Expand Down Expand Up @@ -139,6 +147,31 @@ impl CommonOptions {

Ok(mounted_directories)
}

pub(crate) fn load_imports(&self) -> Result<Imports, Error> {
let mut imports = Imports::default();

if let Ok(obj) = self.imports().dyn_into() {
for (namespace, ns) in crate::utils::object_entries(&obj)? {
let namespace = String::from(namespace);
let ns = ns
.dyn_ref::<js_sys::Object>()
.context("Expected a record of records")?;

for (name, value) in crate::utils::object_entries(&ns)? {
let name = String::from(name);
let extern_value = interpret_import(value).ok_or_else(|| {
Error::js(js_sys::TypeError::new(&format!(
"Invalid type for {namespace}/{name}"
)))
})?;
imports.define(&namespace, &name, extern_value);
}
}
}

Ok(Imports::default())
}
}

impl Default for CommonOptions {
Expand All @@ -150,6 +183,10 @@ impl Default for CommonOptions {
}
}

fn interpret_import(_value: JsValue) -> Option<Extern> {
todo!()
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "RunOptions", extends = CommonOptions)]
Expand Down
3 changes: 3 additions & 0 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ async fn run_wasix_inner(wasm_module: WasmModule, config: RunOptions) -> Result<
let mut builder = WasiEnvBuilder::new(program_name).runtime(runtime.clone());
let (stdin, stdout, stderr) = config.configure_builder(&mut builder)?;

let imports = config.load_imports()?;
builder.add_imports(&imports);

let (exit_code_tx, exit_code_rx) = oneshot::channel();

let module: wasmer::Module = wasm_module.to_module(&*runtime).await?;
Expand Down
21 changes: 12 additions & 9 deletions src/wasmer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,26 @@ pub(crate) async fn configure_runner(
Error,
> {
let args = options.parse_args()?;
runner.set_args(args);
runner.with_args(args);

let env = options.parse_env()?;
runner.set_envs(env);
runner.with_envs(env);

let imports = options.load_imports()?;
runner.with_imports(&imports);

for (dest, dir) in options.mounted_directories()? {
runner.mount(dest, Arc::new(dir));
runner.with_mount(dest, Arc::new(dir));
}

if let Some(uses) = options.uses() {
let uses = crate::utils::js_string_array(uses)?;
let packages = load_injected_packages(uses, runtime).await?;
runner.add_injected_packages(packages);
runner.with_injected_packages(packages);
}

let (stderr_pipe, stderr_stream) = crate::streams::output_pipe();
runner.set_stderr(Box::new(stderr_pipe));
runner.with_stderr(Box::new(stderr_pipe));

let tty_options = runtime.tty_options().clone();
match setup_tty(options, tty_options) {
Expand All @@ -243,16 +246,16 @@ pub(crate) async fn configure_runner(
stdin_stream,
} => {
tracing::debug!("Setting up interactive TTY");
runner.set_stdin(Box::new(stdin_pipe));
runner.set_stdout(Box::new(stdout_pipe));
runner.with_stdin(Box::new(stdin_pipe));
runner.with_stdout(Box::new(stdout_pipe));
runtime.set_connected_to_tty(true);
Ok((Some(stdin_stream), stdout_stream, stderr_stream))
}
TerminalMode::NonInteractive { stdin } => {
tracing::debug!("Setting up non-interactive TTY");
let (stdout_pipe, stdout_stream) = crate::streams::output_pipe();
runner.set_stdin(Box::new(stdin));
runner.set_stdout(Box::new(stdout_pipe));
runner.with_stdin(Box::new(stdin));
runner.with_stdout(Box::new(stdout_pipe));

// HACK: Make sure we don't report stdin as interactive. This
// doesn't belong here because now it'll affect every other
Expand Down
Loading