mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +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,
|
generated_filename,
|
||||||
args.into_iter().map(|os_str| {
|
args.into_iter().map(|os_str| {
|
||||||
os_str.to_str().expect(
|
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")]
|
#[cfg(feature = "run-wasm32")]
|
||||||
fn run_wasm<I: Iterator<Item = S>, S: AsRef<[u8]>>(wasm_path: &std::path::Path, args: I) {
|
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 bytes = std::fs::read(wasm_path).unwrap();
|
||||||
let module = Module::from_file(&store, &wasm_path).unwrap();
|
let arena = Bump::new();
|
||||||
|
|
||||||
// First, we create the `WasiEnv`
|
let mut argv = Vec::<&[u8]>::new_in(&arena);
|
||||||
use wasmer_wasi::WasiState;
|
for arg in args {
|
||||||
let mut wasi_env = WasiState::new("hello").args(args).finalize().unwrap();
|
let mut arg_copy = Vec::<u8>::new_in(&arena);
|
||||||
|
arg_copy.extend_from_slice(arg.as_ref());
|
||||||
// Then, we get the import object related to our WASI
|
argv.push(arg_copy.into_bump_slice());
|
||||||
// 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 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"))]
|
#[cfg(not(feature = "run-wasm32"))]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue