mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-25 04:52:32 +00:00
cli: Convert run_wasm to work with roc_wasm_interp
This commit is contained in:
parent
ca0f159386
commit
09fee71084
1 changed files with 20 additions and 25 deletions
|
@ -865,7 +865,7 @@ fn roc_run<'a, I: IntoIterator<Item = &'a OsStr>>(
|
|||
generated_filename,
|
||||
args.into_iter().map(|os_str| {
|
||||
os_str.to_str().expect(
|
||||
"Roc does not currently support passing non-UTF8 arguments to Wasmer.",
|
||||
"Roc does not currently support passing non-UTF8 arguments to Wasm.",
|
||||
)
|
||||
}),
|
||||
);
|
||||
|
@ -1240,33 +1240,28 @@ fn roc_run_native<I: IntoIterator<Item = S>, S: AsRef<OsStr>>(
|
|||
|
||||
#[cfg(feature = "run-wasm32")]
|
||||
fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(wasm_path: &std::path::Path, args: I) {
|
||||
use wasmer::{Instance, Module, Store};
|
||||
use bumpalo::collections::Vec;
|
||||
use roc_wasm_interp::{DefaultImportDispatcher, Instance};
|
||||
|
||||
let store = Store::default();
|
||||
let module = Module::from_file(&store, &wasm_path).unwrap();
|
||||
let bytes = std::fs::read(wasm_path).unwrap();
|
||||
let arena = Bump::new();
|
||||
|
||||
// First, we create the `WasiEnv`
|
||||
use wasmer_wasi::WasiState;
|
||||
let mut wasi_env = WasiState::new("hello").args(args).finalize().unwrap();
|
||||
|
||||
// Then, we get the import object related to our WASI
|
||||
// and attach it to the Wasm instance.
|
||||
let import_object = wasi_env.import_object(&module).unwrap();
|
||||
|
||||
let instance = Instance::new(&module, &import_object).unwrap();
|
||||
|
||||
let start = instance.exports.get_function("_start").unwrap();
|
||||
|
||||
use wasmer_wasi::WasiError;
|
||||
match start.call(&[]) {
|
||||
Ok(_) => {}
|
||||
Err(e) => match e.downcast::<WasiError>() {
|
||||
Ok(WasiError::Exit(0)) => {
|
||||
// we run the `_start` function, so exit(0) is expected
|
||||
}
|
||||
other => panic!("Wasmer error: {:?}", other),
|
||||
},
|
||||
let mut argv = Vec::<&[u8]>::new_in(&arena);
|
||||
for arg in args {
|
||||
let mut arg_copy = Vec::<u8>::new_in(&arena);
|
||||
arg_copy.extend_from_slice(arg.as_ref());
|
||||
argv.push(arg_copy.into_bump_slice());
|
||||
}
|
||||
let import_dispatcher = DefaultImportDispatcher::new(&argv);
|
||||
|
||||
let mut instance = Instance::from_bytes(&arena, &bytes, import_dispatcher, false).unwrap();
|
||||
|
||||
instance
|
||||
.call_export("_start", [])
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.expect_i32()
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "run-wasm32"))]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue