wasm_interp: support WASI argc/argv, and writing to stdout/stderr

This commit is contained in:
Brian Carroll 2022-12-03 12:28:20 +00:00
parent 736630b53f
commit 284dc6fa51
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0
4 changed files with 386 additions and 69 deletions

View file

@ -9,6 +9,7 @@ pub use instance::Instance;
use roc_wasm_module::Value;
use value_stack::ValueStack;
use wasi::WasiDispatcher;
pub trait ImportDispatcher {
/// Dispatch a call from WebAssembly to your own code, based on module and function name.
@ -21,11 +22,23 @@ pub trait ImportDispatcher {
) -> Option<Value>;
}
pub const DEFAULT_IMPORTS: DefaultImportDispatcher = DefaultImportDispatcher {};
pub const DEFAULT_IMPORTS: DefaultImportDispatcher = DefaultImportDispatcher {
wasi: WasiDispatcher { args: &[] },
};
pub struct DefaultImportDispatcher {}
pub struct DefaultImportDispatcher<'a> {
wasi: WasiDispatcher<'a>,
}
impl ImportDispatcher for DefaultImportDispatcher {
impl<'a> DefaultImportDispatcher<'a> {
pub fn new(args: &'a [&'a String]) -> Self {
DefaultImportDispatcher {
wasi: WasiDispatcher { args },
}
}
}
impl<'a> ImportDispatcher for DefaultImportDispatcher<'a> {
fn dispatch(
&mut self,
module_name: &str,
@ -34,7 +47,7 @@ impl ImportDispatcher for DefaultImportDispatcher {
memory: &mut [u8],
) -> Option<Value> {
if module_name == wasi::MODULE_NAME {
wasi::dispatch(function_name, arguments, memory)
self.wasi.dispatch(function_name, arguments, memory)
} else {
panic!(
"DefaultImportDispatcher does not implement {}.{}",