2018-10-15 03:03:00 +02:00
|
|
|
pub mod utils;
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-01-23 22:29:51 +01:00
|
|
|
use std::{mem::size_of, panic, slice};
|
2019-01-11 20:47:41 +01:00
|
|
|
use wasmer_runtime::{
|
2019-01-18 10:54:16 -08:00
|
|
|
self as runtime,
|
2019-01-23 22:29:51 +01:00
|
|
|
error::CallError,
|
2019-01-18 11:15:13 -08:00
|
|
|
error::{CallResult, Result},
|
2019-01-21 14:43:04 -08:00
|
|
|
import::ImportObject,
|
2019-01-11 20:47:41 +01:00
|
|
|
instance::Instance,
|
2019-01-19 00:28:41 -06:00
|
|
|
module::Module,
|
2019-01-23 22:29:51 +01:00
|
|
|
types::{FuncSig, Type, Value},
|
2019-01-11 20:47:41 +01:00
|
|
|
};
|
2019-01-17 17:43:58 -08:00
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
use wasmer_emscripten::is_emscripten_module;
|
2018-12-10 19:19:46 -08:00
|
|
|
|
2018-10-14 22:23:48 +02:00
|
|
|
pub struct ResultObject {
|
|
|
|
/// A webassembly::Module object representing the compiled WebAssembly module.
|
|
|
|
/// This Module can be instantiated again
|
2019-01-18 10:54:16 -08:00
|
|
|
pub module: Module,
|
2018-10-14 22:23:48 +02:00
|
|
|
/// A webassembly::Instance object that contains all the Exported WebAssembly
|
|
|
|
/// functions.
|
2018-12-30 11:23:16 +01:00
|
|
|
pub instance: Box<Instance>,
|
2018-10-14 22:23:48 +02:00
|
|
|
}
|
2018-10-11 21:29:36 +02:00
|
|
|
|
2019-01-09 19:45:48 -06:00
|
|
|
#[derive(PartialEq)]
|
|
|
|
pub enum InstanceABI {
|
|
|
|
Emscripten,
|
|
|
|
None,
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:10:53 +02:00
|
|
|
/// The webassembly::instantiate() function allows you to compile and
|
2018-10-11 21:29:36 +02:00
|
|
|
/// instantiate WebAssembly code
|
2018-10-14 23:48:59 +02:00
|
|
|
/// Params:
|
2018-10-11 21:29:36 +02:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// * `import_object`: An object containing the values to be imported
|
|
|
|
/// into the newly-created Instance, such as functions or
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::Memory objects. There must be one matching property
|
2018-10-11 21:29:36 +02:00
|
|
|
/// for each declared import of the compiled module or else a
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::LinkError is thrown.
|
2018-10-11 21:29:36 +02:00
|
|
|
/// Errors:
|
2018-10-14 23:48:59 +02:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 22:10:53 +02:00
|
|
|
/// webassembly::CompileError, webassembly::LinkError, or
|
|
|
|
/// webassembly::RuntimeError, depending on the cause of the failure.
|
2019-01-21 14:43:04 -08:00
|
|
|
pub fn instantiate(buffer_source: &[u8], import_object: ImportObject) -> Result<ResultObject> {
|
2019-01-19 00:28:41 -06:00
|
|
|
debug!("webassembly - compiling module");
|
|
|
|
let module = compile(&buffer_source[..])?;
|
|
|
|
|
|
|
|
debug!("webassembly - instantiating");
|
|
|
|
let instance = module.instantiate(import_object)?;
|
|
|
|
|
|
|
|
debug!("webassembly - instance created");
|
|
|
|
Ok(ResultObject {
|
|
|
|
module,
|
|
|
|
instance: Box::new(instance),
|
|
|
|
})
|
2018-10-14 22:23:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-24 12:36:43 +02:00
|
|
|
/// The webassembly::instantiate_streaming() function compiles and instantiates
|
2018-10-24 11:56:42 +02:00
|
|
|
/// a WebAssembly module directly from a streamed underlying source.
|
|
|
|
/// This is the most efficient, optimized way to load wasm code.
|
2018-10-24 12:36:43 +02:00
|
|
|
pub fn instantiate_streaming(
|
2018-11-06 15:51:01 +01:00
|
|
|
_buffer_source: Vec<u8>,
|
2019-01-21 14:43:04 -08:00
|
|
|
_import_object: ImportObject,
|
2019-01-18 10:54:16 -08:00
|
|
|
) -> Result<ResultObject> {
|
2018-10-24 11:56:42 +02:00
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
2018-10-14 22:23:48 +02:00
|
|
|
/// The webassembly::compile() function compiles a webassembly::Module
|
|
|
|
/// from WebAssembly binary code. This function is useful if it
|
|
|
|
/// is necessary to a compile a module before it can be instantiated
|
|
|
|
/// (otherwise, the webassembly::instantiate() function should be used).
|
2018-10-14 23:48:59 +02:00
|
|
|
/// Params:
|
2018-10-14 22:23:48 +02:00
|
|
|
/// * `buffer_source`: A `Vec<u8>` containing the
|
|
|
|
/// binary code of the .wasm module you want to compile.
|
|
|
|
/// Errors:
|
2018-10-14 23:48:59 +02:00
|
|
|
/// If the operation fails, the Result rejects with a
|
2018-10-14 22:23:48 +02:00
|
|
|
/// webassembly::CompileError.
|
2019-01-18 10:54:16 -08:00
|
|
|
pub fn compile(buffer_source: &[u8]) -> Result<Module> {
|
2019-01-22 13:02:06 -06:00
|
|
|
let module = runtime::compile(buffer_source)?;
|
2019-01-18 10:54:16 -08:00
|
|
|
Ok(module)
|
2018-10-11 21:29:36 +02:00
|
|
|
}
|
2018-12-10 16:23:14 -08:00
|
|
|
|
2019-01-19 00:28:41 -06:00
|
|
|
/// Performs common instance operations needed when an instance is first run
|
|
|
|
/// including data setup, handling arguments and calling a main function
|
|
|
|
pub fn run_instance(
|
2019-01-18 10:54:16 -08:00
|
|
|
module: &Module,
|
2018-12-15 00:46:11 -06:00
|
|
|
instance: &mut Instance,
|
2019-01-19 00:28:41 -06:00
|
|
|
_path: &str,
|
2019-01-23 22:29:51 +01:00
|
|
|
args: Vec<&str>,
|
2019-01-18 10:54:16 -08:00
|
|
|
) -> CallResult<()> {
|
|
|
|
let main_name = if is_emscripten_module(module) {
|
2018-12-30 11:23:16 +01:00
|
|
|
"_main"
|
2018-12-10 16:23:14 -08:00
|
|
|
} else {
|
2018-12-30 11:23:16 +01:00
|
|
|
"main"
|
|
|
|
};
|
|
|
|
|
2019-01-23 22:29:51 +01:00
|
|
|
// Get main arguments.
|
|
|
|
let main_args = get_main_args(main_name, args, instance).unwrap();
|
|
|
|
|
|
|
|
// Call main function with the arguments.
|
|
|
|
instance.call(main_name, &main_args)?;
|
|
|
|
|
2018-12-30 11:23:16 +01:00
|
|
|
// TODO atinit and atexit for emscripten
|
|
|
|
|
2019-01-18 10:54:16 -08:00
|
|
|
Ok(())
|
2018-12-10 16:23:14 -08:00
|
|
|
}
|
2019-01-23 22:29:51 +01:00
|
|
|
|
|
|
|
/// Passes arguments from the host to the WebAssembly instance.
|
|
|
|
fn get_main_args(
|
|
|
|
main_name: &str,
|
|
|
|
args: Vec<&str>,
|
|
|
|
instance: &mut Instance,
|
|
|
|
) -> CallResult<Vec<Value>> {
|
|
|
|
// Getting main function signature.
|
|
|
|
let func_sig = instance.get_signature(main_name)?;
|
|
|
|
let params = &func_sig.params;
|
|
|
|
|
|
|
|
// Check for a () or (i32, i32) sig.
|
|
|
|
match params.as_slice() {
|
|
|
|
&[Type::I32, Type::I32] => {
|
|
|
|
// Copy strings into wasm memory and get addresses to them.
|
|
|
|
let string_addresses = args
|
|
|
|
.iter()
|
|
|
|
.map(|string| copy_string_into_wasm(instance, (*string).to_string()).unwrap())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
// Create a wasm array to the strings.
|
|
|
|
let array = create_wasm_array(instance, string_addresses).unwrap();
|
|
|
|
|
|
|
|
Ok(vec![
|
|
|
|
Value::I32(array as i32),
|
|
|
|
Value::I32(args.len() as i32),
|
|
|
|
])
|
|
|
|
}
|
|
|
|
&[] => Ok(vec![]),
|
|
|
|
_ => Err(CallError::Signature {
|
|
|
|
expected: FuncSig {
|
|
|
|
params: vec![Type::I32, Type::I32],
|
|
|
|
returns: vec![],
|
|
|
|
},
|
|
|
|
found: params.to_vec(),
|
|
|
|
}
|
|
|
|
.into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy rust string to wasm instance.
|
|
|
|
fn copy_string_into_wasm(instance: &mut Instance, string: String) -> CallResult<u32> {
|
|
|
|
let string_len = string.len();
|
|
|
|
|
|
|
|
let space_offset = instance
|
|
|
|
.call("_malloc", &[Value::I32((string_len as i32) + 1)])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let space_offset = match space_offset.as_slice() {
|
|
|
|
&[Value::I32(res)] => Some(res as u32),
|
|
|
|
_ => None,
|
|
|
|
}.unwrap();
|
|
|
|
|
|
|
|
let raw_memory = instance.inner.vmctx.memory(0)[space_offset as usize] as *mut u8;
|
|
|
|
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(raw_memory, string_len) };
|
|
|
|
|
|
|
|
for (byte, loc) in string.bytes().zip(slice.iter_mut()) {
|
|
|
|
*loc = byte;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe { *raw_memory.add(string_len) = 0 };
|
|
|
|
|
|
|
|
Ok(space_offset)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a pointer to an array of items in a wasm memory
|
|
|
|
fn create_wasm_array(instance: &mut Instance, values: Vec<u32>) -> CallResult<u32> {
|
|
|
|
let values_len = values.len();
|
|
|
|
|
|
|
|
// Space to store pointers to values
|
|
|
|
let values_offset = instance
|
|
|
|
.call(
|
|
|
|
"_malloc",
|
|
|
|
&[Value::I32((size_of::<u32>() * values.len()) as i32)],
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let values_offset = match values_offset.as_slice() {
|
|
|
|
&[Value::I32(res)] => Some(res as u32),
|
|
|
|
_ => None,
|
|
|
|
}.unwrap();
|
|
|
|
|
|
|
|
let raw_memory = instance.inner.vmctx.memory(0)[values_offset as usize] as *mut u32;
|
|
|
|
|
|
|
|
let slice = unsafe { slice::from_raw_parts_mut(raw_memory, values_len) };
|
|
|
|
|
|
|
|
for (value, loc) in values.iter().zip(slice.iter_mut()) {
|
|
|
|
*loc = value.clone();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Space to store pointer to array
|
|
|
|
let array_offset = instance
|
|
|
|
.call("_malloc", &[Value::I32(size_of::<u32>() as i32)])
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let array_offset = match array_offset.as_slice() {
|
|
|
|
&[Value::I32(res)] => Some(res as u32),
|
|
|
|
_ => None,
|
|
|
|
}.unwrap();
|
|
|
|
|
|
|
|
let raw_memory = instance.inner.vmctx.memory(0)[values_offset as usize] as *mut u32;
|
|
|
|
|
|
|
|
unsafe { *raw_memory = values_offset };
|
|
|
|
|
|
|
|
Ok(array_offset)
|
|
|
|
}
|