cli: Convert run_wasm to work with roc_wasm_interp

This commit is contained in:
Brian Carroll 2022-12-16 00:50:30 +00:00
parent ca0f159386
commit 09fee71084
No known key found for this signature in database
GPG key ID: 5C7B2EC4101703C0

View file

@ -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"))]