mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 05:49:08 +00:00
wasm_interp: support WASI argc/argv, and writing to stdout/stderr
This commit is contained in:
parent
736630b53f
commit
284dc6fa51
4 changed files with 386 additions and 69 deletions
|
@ -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 {}.{}",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue