Update main argument handling

This commit is contained in:
Brandon Fish
2019-01-26 14:17:17 -06:00
parent 26d29049d9
commit f273823abd
2 changed files with 37 additions and 7 deletions

View File

@ -153,24 +153,54 @@ impl fmt::Debug for EmscriptenData {
pub fn run_emscripten_instance(
_module: &Module,
instance: &mut Instance,
_path: &str,
path: &str,
args: Vec<&str>,
) -> CallResult<()> {
let mut data = EmscriptenData::new(instance);
let data_ptr = &mut data as *mut _ as *mut c_void;
instance.ctx().data = data_ptr;
// Get main arguments.
let main_args = get_main_args("_main", args, instance).unwrap();
// Call main function with the arguments.
instance.call("_main", &main_args)?;
let main_func = instance.func("_main")?;
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());
instance.call("_main", &[Value::I32(argc as i32), Value::I32(argv as i32)])?;
}
0 => {
instance.call("_main", &[])?;
}
_ => panic!(
"The emscripten main function has received an incorrect number of params {}",
num_params
),
};
// TODO atinit and atexit for emscripten
println!("{:?}", data);
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)
//}
/// Passes arguments from the host to the WebAssembly instance.
fn get_main_args(
main_name: &str,