Fix main argument handling

This commit is contained in:
Brandon Fish
2019-01-27 12:18:58 -06:00
parent 384c27c4e3
commit 9cef5482ee
2 changed files with 27 additions and 21 deletions

View File

@ -11,7 +11,13 @@ use super::utils::{allocate_on_stack, copy_cstr_into_wasm, copy_terminated_array
use super::EmscriptenData;
use wasmer_runtime_core::vm::Ctx;
pub extern "C" fn _getaddrinfo(_one: i32, _two: i32, _three: i32, _four: i32, _ctx: &mut Ctx) -> i32 {
pub extern "C" fn _getaddrinfo(
_one: i32,
_two: i32,
_three: i32,
_four: i32,
_ctx: &mut Ctx,
) -> i32 {
debug!("emscripten::_getaddrinfo");
-1
}

View File

@ -165,9 +165,7 @@ pub fn run_emscripten_instance(
let num_params = main_func.signature().params.len();
let result = match num_params {
2 => {
let (argc, argv) = (5, 217920); // TODO Fix
// TODO store_module_arguments, cannot borrow `*ctx` as mutable more than once at a time
// store_module_arguments(path, args, instance.ctx());
let (argc, argv) = store_module_arguments(path, args, instance.ctx());
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
}
0 => {
@ -184,23 +182,25 @@ pub fn run_emscripten_instance(
Ok(())
}
//fn store_module_arguments(path: &str, args: Vec<&str>, ctx: &mut Ctx) -> (u32, u32) {
// let argc = args.len() + 1;
//
// let (argv_offset, argv_slice): (_, &mut [u32]) =
// unsafe { allocate_on_stack(((argc + 1) * 4) as u32, ctx) };
// assert!(!argv_slice.is_empty());
//
// argv_slice[0] = unsafe { allocate_cstr_on_stack(path, ctx).0 };
//
// for (slot, arg) in argv_slice[1..argc].iter_mut().zip(args.iter()) {
// *slot = unsafe { allocate_cstr_on_stack(&arg, ctx).0 };
// }
//
// argv_slice[argc] = 0;
//
// (argc as u32, argv_offset)
//}
fn store_module_arguments(path: &str, args: Vec<&str>, ctx: &mut Ctx) -> (u32, u32) {
let argc = args.len() + 1;
let mut args_slice = vec![0; argc];
args_slice[0] = unsafe { allocate_cstr_on_stack(path, ctx).0 };
for (slot, arg) in args_slice[1..argc].iter_mut().zip(args.iter()) {
*slot = unsafe { allocate_cstr_on_stack(&arg, ctx).0 };
}
let (argv_offset, argv_slice): (_, &mut [u32]) =
unsafe { allocate_on_stack(((argc + 1) * 4) as u32, ctx) };
assert!(!argv_slice.is_empty());
for (slot, arg) in argv_slice[0..argc].iter_mut().zip(args_slice.iter()) {
*slot = *arg
}
argv_slice[argc] = 0;
(argc as u32, argv_offset)
}
/// Passes arguments from the host to the WebAssembly instance.
fn get_main_args(