Skip to content

Add WASI p2 support #488

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

Merged
merged 9 commits into from
Aug 11, 2025
Merged
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
9 changes: 5 additions & 4 deletions examples/linking.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

engine = Wasmtime::Engine.new

# Create a linker to link modules together. We want to use WASI with
# the linker, so we pass in `wasi: true`.
linker = Wasmtime::Linker.new(engine, wasi: true)
# Create a linker to link modules together.
linker = Wasmtime::Linker.new(engine)
# We want to use WASI with # the linker, so we call add_to_linker_sync.
Wasmtime::WASI::P1.add_to_linker_sync(linker)

mod1 = Wasmtime::Module.from_file(engine, "examples/linking1.wat")
mod2 = Wasmtime::Module.from_file(engine, "examples/linking2.wat")
Expand All @@ -13,7 +14,7 @@
.inherit_stdin
.inherit_stdout

store = Wasmtime::Store.new(engine, wasi_config: wasi_config)
store = Wasmtime::Store.new(engine, wasi_p1_config: wasi_config)

# Instantiate `mod2` which only uses WASI, then register
# that instance with the linker so `mod1` can use it.
Expand Down
17 changes: 17 additions & 0 deletions examples/wasi-p2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require "wasmtime"

engine = Wasmtime::Engine.new
component = Wasmtime::Component::Component.from_file(engine, "spec/fixtures/wasi-debug-p2.wasm")

linker = Wasmtime::Component::Linker.new(engine)
Wasmtime::WASI::P2.add_to_linker_sync(linker)

wasi_config = Wasmtime::WasiConfig.new
.set_stdin_string("hi!")
.inherit_stdout
.inherit_stderr
.set_argv(ARGV)
.set_env(ENV)
store = Wasmtime::Store.new(engine, wasi_config: wasi_config)

Wasmtime::Component::WasiCommand.new(store, component, linker).call_run(store)
5 changes: 3 additions & 2 deletions examples/wasi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
engine = Wasmtime::Engine.new
mod = Wasmtime::Module.from_file(engine, "spec/fixtures/wasi-debug.wasm")

linker = Wasmtime::Linker.new(engine, wasi: true)
linker = Wasmtime::Linker.new(engine)
Wasmtime::WASI::P1.add_to_linker_sync(linker)

wasi_config = Wasmtime::WasiConfig.new
.set_stdin_string("hi!")
.inherit_stdout
.inherit_stderr
.set_argv(ARGV)
.set_env(ENV)
store = Wasmtime::Store.new(engine, wasi_config: wasi_config)
store = Wasmtime::Store.new(engine, wasi_p1_config: wasi_config)

instance = linker.instantiate(store, mod)
instance.invoke("_start")
4 changes: 4 additions & 0 deletions ext/src/ruby_api/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod convert;
mod func;
mod instance;
mod linker;
mod wasi_command;

use super::root;
use magnus::{
Expand All @@ -13,6 +14,8 @@ use wasmtime::component::Component as ComponentImpl;

pub use func::Func;
pub use instance::Instance;
pub use linker::Linker;
pub use wasi_command::WasiCommand;

pub fn component_namespace(ruby: &Ruby) -> RModule {
static COMPONENT_NAMESPACE: Lazy<RModule> =
Expand Down Expand Up @@ -162,6 +165,7 @@ pub fn init(ruby: &Ruby) -> Result<(), Error> {
instance::init(ruby, &namespace)?;
func::init(ruby, &namespace)?;
convert::init(ruby)?;
wasi_command::init(ruby, &namespace)?;

Ok(())
}
33 changes: 31 additions & 2 deletions ext/src/ruby_api/component/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ use super::{Component, Instance};
use crate::{
err,
ruby_api::{
errors,
store::{StoreContextValue, StoreData},
Engine, Module, Store,
},
};
use std::{borrow::BorrowMut, cell::RefCell};
use std::{
borrow::BorrowMut,
cell::{RefCell, RefMut},
};

use crate::error;
use magnus::{
class, function, gc::Marker, method, r_string::RString, scan_args, typed_data::Obj,
DataTypeFunctions, Error, Module as _, Object, RModule, Ruby, TryConvert, TypedData, Value,
};
use wasmtime::component::{Linker as LinkerImpl, LinkerInstance as LinkerInstanceImpl};
use wasmtime_wasi::{
p2::{IoView, WasiCtx, WasiView},
ResourceTable,
};

/// @yard
/// @rename Wasmtime::Component::Linker
Expand All @@ -23,6 +31,7 @@ use wasmtime::component::{Linker as LinkerImpl, LinkerInstance as LinkerInstance
pub struct Linker {
inner: RefCell<LinkerImpl<StoreData>>,
refs: RefCell<Vec<Value>>,
has_wasi: RefCell<bool>,
}
unsafe impl Send for Linker {}

Expand All @@ -38,13 +47,23 @@ impl Linker {
/// @param engine [Engine]
/// @return [Linker]
pub fn new(engine: &Engine) -> Result<Self, Error> {
let linker = LinkerImpl::new(engine.get());
let linker: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());

Ok(Linker {
inner: RefCell::new(linker),
refs: RefCell::new(Vec::new()),
has_wasi: RefCell::new(false),
})
}

pub(crate) fn inner_mut(&self) -> RefMut<'_, LinkerImpl<StoreData>> {
self.inner.borrow_mut()
}

pub(crate) fn has_wasi(&self) -> bool {
*self.has_wasi.borrow()
}

/// @yard
/// @def root
/// Define items in the root of this {Linker}.
Expand Down Expand Up @@ -105,6 +124,10 @@ impl Linker {
store: Obj<Store>,
component: &Component,
) -> Result<Instance, Error> {
if *rb_self.has_wasi.borrow() && !store.context().data().has_wasi_ctx() {
return err!("{}", errors::missing_wasi_ctx_error("linker.instantiate"));
}

let inner = rb_self.inner.borrow();
inner
.instantiate(store.context_mut(), component.get())
Expand All @@ -119,6 +142,12 @@ impl Linker {
})
.map_err(|e| error!("{}", e))
}

pub(crate) fn add_wasi_p2(&self) -> Result<(), Error> {
*self.has_wasi.borrow_mut() = true;
let mut inner = self.inner.borrow_mut();
wasmtime_wasi::p2::add_to_linker_sync(&mut inner).map_err(|e| error!("{e}"))
}
}

/// @yard
Expand Down
58 changes: 58 additions & 0 deletions ext/src/ruby_api/component/wasi_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use magnus::{
class, function, method, module::Module, typed_data::Obj, DataTypeFunctions, Error, Object,
RModule, Ruby,
};
use wasmtime_wasi::p2::bindings::sync::Command;

use crate::{
err, error,
ruby_api::{
component::{linker::Linker, Component},
errors,
},
Store,
};

#[magnus::wrap(class = "Wasmtime::Component::WasiCommand", size, free_immediately)]
pub struct WasiCommand {
command: Command,
}

impl WasiCommand {
/// @yard
/// @def new(store, component, linker)
/// @param store [Store]
/// @param component [Component]
/// @param linker [Linker]
/// @return [WasiCommand]
pub fn new(store: &Store, component: &Component, linker: &Linker) -> Result<Self, Error> {
if linker.has_wasi() && !store.context().data().has_wasi_ctx() {
return err!("{}", errors::missing_wasi_ctx_error("WasiCommand.new"));
}
let command =
Command::instantiate(store.context_mut(), component.get(), &linker.inner_mut())
.map_err(|e| error!("{e}"))?;
Ok(Self { command })
}

/// @yard
/// @def call_run(store)
/// @param store [Store]
/// @return [nil]
pub fn call_run(_ruby: &Ruby, rb_self: Obj<Self>, store: &Store) -> Result<(), Error> {
rb_self
.command
.wasi_cli_run()
.call_run(store.context_mut())
.map_err(|err| error!("{err}"))?
.map_err(|_| error!("Error running `run`"))
}
}

pub fn init(_ruby: &Ruby, namespace: &RModule) -> Result<(), Error> {
let linker = namespace.define_class("WasiCommand", class::object())?;
linker.define_singleton_method("new", function!(WasiCommand::new, 3))?;
linker.define_method("call_run", method!(WasiCommand::call_run, 1))?;

Ok(())
}
23 changes: 23 additions & 0 deletions ext/src/ruby_api/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ impl ExceptionMessage for magnus::Error {
}
}

pub(crate) fn missing_wasi_ctx_error(callee: &str) -> String {
missing_wasi_error(callee, "WASI", "P2", "wasi_config")
}

pub(crate) fn missing_wasi_p1_ctx_error() -> String {
missing_wasi_error("linker.instantiate", "WASI p1", "P1", "wasi_p1_config")
}

fn missing_wasi_error(
callee: &str,
wasi_text: &str,
add_wasi_call: &str,
option_name: &str,
) -> String {
format!(
"Store is missing {wasi_text} configuration.\n\n\
When using `WASI::{add_wasi_call}::add_to_linker_sync(linker)`, the Store given to\n\
`{callee}` must have a {wasi_text} configuration.\n\
To fix this, provide the `{option_name}` when creating the Store:\n\
Wasmtime::Store.new(engine, {option_name}: WasiConfig.new)"
)
}

mod bundled {
include!(concat!(env!("OUT_DIR"), "/bundled/error.rs"));
}
Expand Down
45 changes: 16 additions & 29 deletions ext/src/ruby_api/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
root,
store::{Store, StoreContextValue, StoreData},
};
use crate::{define_rb_intern, err, error};
use crate::{err, error, ruby_api::errors};
use magnus::{
block::Proc, class, function, gc::Marker, method, prelude::*, scan_args, scan_args::scan_args,
typed_data::Obj, DataTypeFunctions, Error, Object, RArray, RHash, RString, Ruby, TypedData,
Expand All @@ -18,18 +18,14 @@ use magnus::{
use std::cell::RefCell;
use wasmtime::Linker as LinkerImpl;

define_rb_intern!(
WASI=> "wasi",
);

/// @yard
/// @see https://docs.rs/wasmtime/latest/wasmtime/struct.Linker.html Wasmtime's Rust doc
#[derive(TypedData)]
#[magnus(class = "Wasmtime::Linker", size, mark, free_immediately)]
pub struct Linker {
inner: RefCell<LinkerImpl<StoreData>>,
refs: RefCell<Vec<Value>>,
has_wasi: bool,
has_wasi: RefCell<bool>,
}

unsafe impl Send for Linker {}
Expand All @@ -42,25 +38,15 @@ impl DataTypeFunctions for Linker {

impl Linker {
/// @yard
/// @def new(engine, wasi: false)
/// @def new(engine)
/// @param engine [Engine]
/// @param wasi [Boolean] Whether WASI should be defined in this Linker. Defaults to false.
/// @return [Linker]
pub fn new(args: &[Value]) -> Result<Self, Error> {
let args = scan_args::scan_args::<(&Engine,), (), (), (), _, ()>(args)?;
let kw = scan_args::get_kwargs::<_, (), (Option<bool>,), ()>(args.keywords, &[], &[*WASI])?;
let (engine,) = args.required;
let wasi = kw.optional.0.unwrap_or(false);

let mut inner: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());
if wasi {
wasmtime_wasi::preview1::add_to_linker_sync(&mut inner, |s| s.wasi_ctx_mut())
.map_err(|e| error!("{}", e))?
}
pub fn new(engine: &Engine) -> Result<Self, Error> {
let inner: LinkerImpl<StoreData> = LinkerImpl::new(engine.get());
Ok(Self {
inner: RefCell::new(inner),
refs: Default::default(),
has_wasi: wasi,
has_wasi: RefCell::new(false),
})
}

Expand Down Expand Up @@ -280,14 +266,8 @@ impl Linker {
/// @param mod [Module]
/// @return [Instance]
pub fn instantiate(&self, store: Obj<Store>, module: &Module) -> Result<Instance, Error> {
if self.has_wasi && !store.context().data().has_wasi_ctx() {
return err!(
"Store is missing WASI configuration.\n\n\
When using `wasi: true`, the Store given to\n\
`Linker#instantiate` must have a WASI configuration.\n\
To fix this, provide the `wasi_config` when creating the Store:\n\
Wasmtime::Store.new(engine, wasi_config: WasiConfig.new)"
);
if *self.has_wasi.borrow() && !store.context().data().has_wasi_p1_ctx() {
return err!("{}", errors::missing_wasi_p1_ctx_error());
}

self.inner
Expand Down Expand Up @@ -322,11 +302,18 @@ impl Linker {
let mut inner = self.inner.borrow_mut();
deterministic_wasi_ctx::replace_scheduling_functions(&mut inner).map_err(|e| error!("{e}"))
}

pub(crate) fn add_wasi_p1(&self) -> Result<(), Error> {
*self.has_wasi.borrow_mut() = true;
let mut inner = self.inner.borrow_mut();
wasmtime_wasi::preview1::add_to_linker_sync(&mut inner, |s| s.wasi_p1_ctx_mut())
.map_err(|e| error!("{e}"))
}
}

pub fn init() -> Result<(), Error> {
let class = root().define_class("Linker", class::object())?;
class.define_singleton_method("new", function!(Linker::new, -1))?;
class.define_singleton_method("new", function!(Linker::new, 1))?;
class.define_method("allow_shadowing=", method!(Linker::set_allow_shadowing, 1))?;
class.define_method(
"allow_unknown_exports=",
Expand Down
2 changes: 2 additions & 0 deletions ext/src/ruby_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod pooling_allocation_config;
mod store;
mod table;
mod trap;
mod wasi;
mod wasi_config;

pub use caller::Caller;
Expand Down Expand Up @@ -83,6 +84,7 @@ pub fn init(ruby: &Ruby) -> Result<(), Error> {
memory::init(ruby)?;
linker::init()?;
externals::init()?;
wasi::init()?;
wasi_config::init()?;
table::init()?;
global::init()?;
Expand Down
Loading
Loading