Abstract arguments extraction into a function

This commit is contained in:
Steve Akinyemi 2018-12-06 16:26:27 +01:00
parent 5b8e4ed4a6
commit 266ced4311

View File

@ -91,37 +91,9 @@ fn execute_wasm(options: &Run) -> Result<(), String> {
let main: extern "C" fn(u32, u32, &webassembly::Instance) =
get_instance_function!(instance, func_index);
// Application Arguments
let mut arg_values: Vec<String> = Vec::new();
let mut arg_addrs: Vec<*const u8> = Vec::new();
let arg_length = options.args.len() + 1;
let (argc, argv) = get_module_arguments(options, &instance);
arg_values.reserve_exact(arg_length);
arg_addrs.reserve_exact(arg_length);
// Push name of wasm file
arg_values.push(format!("{}\0", options.path.to_str().unwrap()));
arg_addrs.push(arg_values[0].as_ptr());
// Push additional arguments
for (i, arg) in options.args.iter().enumerate() {
arg_values.push(format!("{}\0", arg));
arg_addrs.push(arg_values[i + 1].as_ptr());
}
// Get argument count and pointer to addresses
let argv = arg_addrs.as_ptr() as *mut *mut i8;
let argc = arg_length as u32;
// Copy the the arguments into the wasm memory and get offset
let argv_offset = unsafe {
copy_cstr_array_into_wasm(argc, argv, &instance)
};
debug!("argc = {:?}", argc);
debug!("argv = {:?}", arg_addrs);
return call_protected!(main(argc, argv_offset, &instance)).map_err(|err| format!("{}", err));
return call_protected!(main(argc, argv, &instance)).map_err(|err| format!("{}", err));
// TODO: We should implement emscripten __ATEXIT__
} else {
let func_index =
@ -155,3 +127,37 @@ fn main() {
CLIOptions::SelfUpdate => update::self_update(),
}
}
fn get_module_arguments(options: &Run, instance: &webassembly::Instance) -> (u32, u32) {
// Application Arguments
let mut arg_values: Vec<String> = Vec::new();
let mut arg_addrs: Vec<*const u8> = Vec::new();
let arg_length = options.args.len() + 1;
arg_values.reserve_exact(arg_length);
arg_addrs.reserve_exact(arg_length);
// Push name of wasm file
arg_values.push(format!("{}\0", options.path.to_str().unwrap()));
arg_addrs.push(arg_values[0].as_ptr());
// Push additional arguments
for (i, arg) in options.args.iter().enumerate() {
arg_values.push(format!("{}\0", arg));
arg_addrs.push(arg_values[i + 1].as_ptr());
}
// Get argument count and pointer to addresses
let argv = arg_addrs.as_ptr() as *mut *mut i8;
let argc = arg_length as u32;
// Copy the the arguments into the wasm memory and get offset
let argv_offset = unsafe {
copy_cstr_array_into_wasm(argc, argv, &instance)
};
debug!("argc = {:?}", argc);
debug!("argv = {:?}", arg_addrs);
(argc, argv_offset)
}