2019-03-29 16:22:38 +01:00
|
|
|
//! Create, read, destroy import definitions (function, global, memory
|
|
|
|
//! and table) on an instance.
|
2019-03-29 15:50:16 +01:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
error::{update_last_error, CApiError},
|
|
|
|
export::{wasmer_import_export_kind, wasmer_import_export_value},
|
2020-01-10 15:13:30 +01:00
|
|
|
instance::wasmer_instance_context_t,
|
2019-03-29 15:50:16 +01:00
|
|
|
module::wasmer_module_t,
|
|
|
|
value::wasmer_value_tag,
|
|
|
|
wasmer_byte_array, wasmer_result_t,
|
|
|
|
};
|
2019-12-16 18:06:37 -05:00
|
|
|
use libc::c_uint;
|
2020-01-10 15:13:30 +01:00
|
|
|
use std::{
|
|
|
|
convert::TryFrom,
|
|
|
|
ffi::{c_void, CStr},
|
|
|
|
os::raw::c_char,
|
|
|
|
ptr, slice,
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2020-04-06 17:09:50 -07:00
|
|
|
use wasmer::import::{ImportObject, ImportObjectIterator};
|
2020-04-10 12:12:36 -07:00
|
|
|
use wasmer::vm::Ctx;
|
|
|
|
use wasmer::wasm::{Export, FuncSig, Global, Memory, Module, Table, Type};
|
2019-03-29 15:50:16 +01:00
|
|
|
use wasmer_runtime_core::{
|
2020-04-24 14:30:14 -07:00
|
|
|
error::RuntimeError,
|
2020-04-10 12:12:36 -07:00
|
|
|
export::{Context, FuncPointer},
|
2019-03-29 15:50:16 +01:00
|
|
|
module::ImportName,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct wasmer_import_t {
|
|
|
|
pub module_name: wasmer_byte_array,
|
|
|
|
pub import_name: wasmer_byte_array,
|
|
|
|
pub tag: wasmer_import_export_kind,
|
|
|
|
pub value: wasmer_import_export_value,
|
|
|
|
}
|
|
|
|
|
2019-08-01 10:48:03 +03:00
|
|
|
#[repr(C)]
|
|
|
|
pub struct wasmer_import_object_t;
|
|
|
|
|
2019-03-29 15:50:16 +01:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_import_func_t;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_import_descriptor_t;
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_import_descriptors_t;
|
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct wasmer_import_object_iter_t;
|
|
|
|
|
2019-08-01 14:06:25 +03:00
|
|
|
/// Creates a new empty import object.
|
|
|
|
/// See also `wasmer_import_object_append`
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_object_new() -> *mut wasmer_import_object_t {
|
|
|
|
let import_object = Box::new(ImportObject::new());
|
|
|
|
|
|
|
|
Box::into_raw(import_object) as *mut wasmer_import_object_t
|
|
|
|
}
|
|
|
|
|
2019-10-01 12:08:45 -07:00
|
|
|
#[cfg(feature = "wasi")]
|
2019-10-11 09:19:46 +02:00
|
|
|
mod wasi;
|
2019-10-01 12:08:45 -07:00
|
|
|
|
|
|
|
#[cfg(feature = "wasi")]
|
|
|
|
pub use self::wasi::*;
|
|
|
|
|
2019-12-13 13:04:54 -08:00
|
|
|
#[cfg(feature = "emscripten")]
|
|
|
|
mod emscripten;
|
|
|
|
|
|
|
|
#[cfg(feature = "emscripten")]
|
|
|
|
pub use self::emscripten::*;
|
|
|
|
|
2019-10-09 17:29:27 -07:00
|
|
|
/// Gets an entry from an ImportObject at the name and namespace.
|
2019-10-29 14:55:14 -07:00
|
|
|
/// Stores `name`, `namespace`, and `import_export_value` in `import`.
|
|
|
|
/// Thus these must remain valid for the lifetime of `import`.
|
2019-10-09 17:29:27 -07:00
|
|
|
///
|
|
|
|
/// The caller owns all data involved.
|
2019-10-29 14:55:14 -07:00
|
|
|
/// `import_export_value` will be written to based on `tag`.
|
2019-10-09 17:29:27 -07:00
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_object_get_import(
|
|
|
|
import_object: *const wasmer_import_object_t,
|
|
|
|
namespace: wasmer_byte_array,
|
|
|
|
name: wasmer_byte_array,
|
|
|
|
import: *mut wasmer_import_t,
|
|
|
|
import_export_value: *mut wasmer_import_export_value,
|
|
|
|
tag: u32,
|
|
|
|
) -> wasmer_result_t {
|
|
|
|
let tag: wasmer_import_export_kind = if let Ok(t) = TryFrom::try_from(tag) {
|
|
|
|
t
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "wasmer_import_export_tag out of range".to_string(),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
};
|
|
|
|
let import_object: &mut ImportObject = &mut *(import_object as *mut ImportObject);
|
|
|
|
let namespace_str = if let Ok(ns) = namespace.as_str() {
|
|
|
|
ns
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "error converting namespace to UTF-8 string".to_string(),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
};
|
|
|
|
let name_str = if let Ok(name) = name.as_str() {
|
|
|
|
name
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "error converting name to UTF-8 string".to_string(),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
};
|
|
|
|
if import.is_null() || import_export_value.is_null() {
|
|
|
|
update_last_error(CApiError {
|
2019-10-24 11:50:43 -07:00
|
|
|
msg: "pointers to import and import_export_value must not be null".to_string(),
|
2019-10-09 17:29:27 -07:00
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
let import_out = &mut *import;
|
|
|
|
let import_export_value_out = &mut *import_export_value;
|
|
|
|
if let Some(export) =
|
|
|
|
import_object.maybe_with_namespace(namespace_str, |ns| ns.get_export(name_str))
|
|
|
|
{
|
|
|
|
match export {
|
|
|
|
Export::Function { .. } => {
|
|
|
|
if tag != wasmer_import_export_kind::WASM_FUNCTION {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: format!("Found function, expected {}", tag.to_str()),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
import_out.tag = wasmer_import_export_kind::WASM_FUNCTION;
|
|
|
|
let writer = import_export_value_out.func as *mut Export;
|
|
|
|
*writer = export.clone();
|
|
|
|
}
|
|
|
|
Export::Memory(memory) => {
|
|
|
|
if tag != wasmer_import_export_kind::WASM_MEMORY {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: format!("Found memory, expected {}", tag.to_str()),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
import_out.tag = wasmer_import_export_kind::WASM_MEMORY;
|
|
|
|
let writer = import_export_value_out.func as *mut Memory;
|
|
|
|
*writer = memory.clone();
|
|
|
|
}
|
|
|
|
Export::Table(table) => {
|
|
|
|
if tag != wasmer_import_export_kind::WASM_TABLE {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: format!("Found table, expected {}", tag.to_str()),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
import_out.tag = wasmer_import_export_kind::WASM_TABLE;
|
|
|
|
let writer = import_export_value_out.func as *mut Table;
|
|
|
|
*writer = table.clone();
|
|
|
|
}
|
|
|
|
Export::Global(global) => {
|
|
|
|
if tag != wasmer_import_export_kind::WASM_GLOBAL {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: format!("Found global, expected {}", tag.to_str()),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
import_out.tag = wasmer_import_export_kind::WASM_GLOBAL;
|
|
|
|
let writer = import_export_value_out.func as *mut Global;
|
|
|
|
*writer = global.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
import_out.value = *import_export_value;
|
|
|
|
import_out.module_name = namespace;
|
|
|
|
import_out.import_name = name;
|
|
|
|
|
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: format!("Export {} {} not found", namespace_str, name_str),
|
|
|
|
});
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
/// private wrapper data type used for casting
|
|
|
|
#[repr(C)]
|
|
|
|
struct WasmerImportObjectIterator(
|
|
|
|
std::iter::Peekable<Box<dyn Iterator<Item = <ImportObjectIterator as Iterator>::Item>>>,
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Create an iterator over the functions in the import object.
|
|
|
|
/// Get the next import with `wasmer_import_object_iter_next`
|
|
|
|
/// Free the iterator with `wasmer_import_object_iter_destroy`
|
2019-10-10 16:07:56 -07:00
|
|
|
#[no_mangle]
|
2019-10-29 14:55:14 -07:00
|
|
|
pub unsafe extern "C" fn wasmer_import_object_iterate_functions(
|
2019-10-25 14:24:22 -07:00
|
|
|
import_object: *const wasmer_import_object_t,
|
2019-10-29 14:55:14 -07:00
|
|
|
) -> *mut wasmer_import_object_iter_t {
|
2019-10-25 14:24:22 -07:00
|
|
|
if import_object.is_null() {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "import_object must not be null".to_owned(),
|
|
|
|
});
|
2019-10-29 14:55:14 -07:00
|
|
|
return std::ptr::null_mut();
|
2019-10-25 14:24:22 -07:00
|
|
|
}
|
|
|
|
let import_object: &ImportObject = &*(import_object as *const ImportObject);
|
2020-04-29 23:15:38 +03:00
|
|
|
let iter_inner = Box::new(import_object.clone().into_iter().filter(|(_, _, e)| {
|
2019-10-29 14:55:14 -07:00
|
|
|
if let Export::Function { .. } = e {
|
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
})) as Box<dyn Iterator<Item = <ImportObjectIterator as Iterator>::Item>>;
|
|
|
|
let iterator = Box::new(WasmerImportObjectIterator(iter_inner.peekable()));
|
|
|
|
|
|
|
|
Box::into_raw(iterator) as *mut wasmer_import_object_iter_t
|
2019-10-25 14:24:22 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
/// Writes the next value to `import`. `WASMER_ERROR` is returned if there
|
|
|
|
/// was an error or there's nothing left to return.
|
|
|
|
///
|
|
|
|
/// To free the memory allocated here, pass the import to `wasmer_import_object_imports_destroy`.
|
|
|
|
/// To check if the iterator is done, use `wasmer_import_object_iter_at_end`.
|
2019-10-25 14:24:22 -07:00
|
|
|
#[no_mangle]
|
2019-10-29 14:55:14 -07:00
|
|
|
pub unsafe extern "C" fn wasmer_import_object_iter_next(
|
|
|
|
import_object_iter: *mut wasmer_import_object_iter_t,
|
|
|
|
import: *mut wasmer_import_t,
|
|
|
|
) -> wasmer_result_t {
|
|
|
|
if import_object_iter.is_null() || import.is_null() {
|
2019-10-10 16:07:56 -07:00
|
|
|
update_last_error(CApiError {
|
2019-10-29 14:55:14 -07:00
|
|
|
msg: "import_object_iter and import must not be null".to_owned(),
|
2019-10-10 16:07:56 -07:00
|
|
|
});
|
2019-10-29 14:55:14 -07:00
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
2019-10-10 16:07:56 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
let iter = &mut *(import_object_iter as *mut WasmerImportObjectIterator);
|
|
|
|
let out = &mut *import;
|
|
|
|
// TODO: the copying here can be optimized away, we just need to use a different type of
|
|
|
|
// iterator internally
|
|
|
|
if let Some((namespace, name, export)) = iter.0.next() {
|
|
|
|
let ns = {
|
|
|
|
let mut n = namespace.clone();
|
|
|
|
n.shrink_to_fit();
|
|
|
|
n.into_bytes()
|
|
|
|
};
|
|
|
|
let ns_bytes = wasmer_byte_array {
|
|
|
|
bytes: ns.as_ptr(),
|
|
|
|
bytes_len: ns.len() as u32,
|
|
|
|
};
|
|
|
|
|
|
|
|
let name = {
|
|
|
|
let mut n = name.clone();
|
|
|
|
n.shrink_to_fit();
|
|
|
|
n.into_bytes()
|
|
|
|
};
|
|
|
|
let name_bytes = wasmer_byte_array {
|
|
|
|
bytes: name.as_ptr(),
|
|
|
|
bytes_len: name.len() as u32,
|
|
|
|
};
|
|
|
|
|
|
|
|
out.module_name = ns_bytes;
|
|
|
|
out.import_name = name_bytes;
|
|
|
|
|
|
|
|
std::mem::forget(ns);
|
|
|
|
std::mem::forget(name);
|
|
|
|
|
2019-10-10 16:07:56 -07:00
|
|
|
match export {
|
|
|
|
Export::Function { .. } => {
|
2019-10-29 14:55:14 -07:00
|
|
|
let func = Box::new(export.clone());
|
|
|
|
|
|
|
|
out.tag = wasmer_import_export_kind::WASM_FUNCTION;
|
|
|
|
out.value = wasmer_import_export_value {
|
|
|
|
func: Box::into_raw(func) as *mut _ as *const _,
|
2019-10-10 16:07:56 -07:00
|
|
|
};
|
2019-10-29 14:55:14 -07:00
|
|
|
}
|
|
|
|
Export::Global(global) => {
|
|
|
|
let glbl = Box::new(global.clone());
|
2019-10-10 16:07:56 -07:00
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
out.tag = wasmer_import_export_kind::WASM_GLOBAL;
|
|
|
|
out.value = wasmer_import_export_value {
|
|
|
|
global: Box::into_raw(glbl) as *mut _ as *const _,
|
2019-10-10 16:07:56 -07:00
|
|
|
};
|
2019-10-29 14:55:14 -07:00
|
|
|
}
|
|
|
|
Export::Memory(memory) => {
|
|
|
|
let mem = Box::new(memory.clone());
|
2019-10-10 16:07:56 -07:00
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
out.tag = wasmer_import_export_kind::WASM_MEMORY;
|
|
|
|
out.value = wasmer_import_export_value {
|
|
|
|
memory: Box::into_raw(mem) as *mut _ as *const _,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Export::Table(table) => {
|
|
|
|
let tbl = Box::new(table.clone());
|
2019-10-10 16:07:56 -07:00
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
out.tag = wasmer_import_export_kind::WASM_TABLE;
|
|
|
|
out.value = wasmer_import_export_value {
|
|
|
|
memory: Box::into_raw(tbl) as *mut _ as *const _,
|
2019-10-10 16:07:56 -07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 14:55:14 -07:00
|
|
|
|
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
2019-10-10 16:07:56 -07:00
|
|
|
}
|
2019-10-29 14:55:14 -07:00
|
|
|
}
|
2019-10-10 16:07:56 -07:00
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
/// Returns true if further calls to `wasmer_import_object_iter_next` will
|
|
|
|
/// not return any new data
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_object_iter_at_end(
|
|
|
|
import_object_iter: *mut wasmer_import_object_iter_t,
|
|
|
|
) -> bool {
|
|
|
|
if import_object_iter.is_null() {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "import_object_iter must not be null".to_owned(),
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let iter = &mut *(import_object_iter as *mut WasmerImportObjectIterator);
|
|
|
|
|
|
|
|
iter.0.peek().is_none()
|
2019-10-10 16:07:56 -07:00
|
|
|
}
|
|
|
|
|
2019-10-29 14:55:14 -07:00
|
|
|
/// Frees the memory allocated by `wasmer_import_object_iterate_functions`
|
2019-10-10 16:07:56 -07:00
|
|
|
#[no_mangle]
|
2019-10-29 14:55:14 -07:00
|
|
|
pub unsafe extern "C" fn wasmer_import_object_iter_destroy(
|
|
|
|
import_object_iter: *mut wasmer_import_object_iter_t,
|
|
|
|
) {
|
2019-10-29 15:18:45 -07:00
|
|
|
if !import_object_iter.is_null() {
|
|
|
|
let _ = Box::from_raw(import_object_iter as *mut WasmerImportObjectIterator);
|
|
|
|
}
|
2019-10-29 14:55:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Frees the memory allocated in `wasmer_import_object_iter_next`
|
2019-10-24 11:50:43 -07:00
|
|
|
///
|
|
|
|
/// This function does not free the memory in `wasmer_import_object_t`;
|
|
|
|
/// it only frees memory allocated while querying a `wasmer_import_object_t`.
|
2019-10-29 14:55:14 -07:00
|
|
|
#[no_mangle]
|
2019-10-10 16:07:56 -07:00
|
|
|
pub unsafe extern "C" fn wasmer_import_object_imports_destroy(
|
|
|
|
imports: *mut wasmer_import_t,
|
|
|
|
imports_len: u32,
|
|
|
|
) {
|
2019-10-29 15:18:45 -07:00
|
|
|
if imports.is_null() {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-10 16:07:56 -07:00
|
|
|
let imports: &[wasmer_import_t] = &*slice::from_raw_parts_mut(imports, imports_len as usize);
|
|
|
|
for import in imports {
|
|
|
|
let _namespace: Vec<u8> = Vec::from_raw_parts(
|
|
|
|
import.module_name.bytes as *mut u8,
|
|
|
|
import.module_name.bytes_len as usize,
|
|
|
|
import.module_name.bytes_len as usize,
|
|
|
|
);
|
|
|
|
let _name: Vec<u8> = Vec::from_raw_parts(
|
|
|
|
import.import_name.bytes as *mut u8,
|
|
|
|
import.import_name.bytes_len as usize,
|
|
|
|
import.import_name.bytes_len as usize,
|
|
|
|
);
|
|
|
|
match import.tag {
|
|
|
|
wasmer_import_export_kind::WASM_FUNCTION => {
|
|
|
|
let _: Box<Export> = Box::from_raw(import.value.func as *mut _);
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_GLOBAL => {
|
|
|
|
let _: Box<Global> = Box::from_raw(import.value.global as *mut _);
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_MEMORY => {
|
|
|
|
let _: Box<Memory> = Box::from_raw(import.value.memory as *mut _);
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_TABLE => {
|
|
|
|
let _: Box<Table> = Box::from_raw(import.value.table as *mut _);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 14:06:25 +03:00
|
|
|
/// Extends an existing import object with new imports
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_object_extend(
|
|
|
|
import_object: *mut wasmer_import_object_t,
|
2019-10-10 11:22:45 -07:00
|
|
|
imports: *const wasmer_import_t,
|
2019-08-01 14:06:25 +03:00
|
|
|
imports_len: c_uint,
|
|
|
|
) -> wasmer_result_t {
|
|
|
|
let import_object: &mut ImportObject = &mut *(import_object as *mut ImportObject);
|
|
|
|
|
|
|
|
let mut extensions: Vec<(String, String, Export)> = Vec::new();
|
|
|
|
|
|
|
|
let imports: &[wasmer_import_t] = slice::from_raw_parts(imports, imports_len as usize);
|
|
|
|
for import in imports {
|
|
|
|
let module_name = slice::from_raw_parts(
|
|
|
|
import.module_name.bytes,
|
|
|
|
import.module_name.bytes_len as usize,
|
|
|
|
);
|
|
|
|
let module_name = if let Ok(s) = std::str::from_utf8(module_name) {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "error converting module name to string".to_string(),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
};
|
|
|
|
let import_name = slice::from_raw_parts(
|
|
|
|
import.import_name.bytes,
|
|
|
|
import.import_name.bytes_len as usize,
|
|
|
|
);
|
|
|
|
let import_name = if let Ok(s) = std::str::from_utf8(import_name) {
|
|
|
|
s
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "error converting import_name to string".to_string(),
|
|
|
|
});
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
};
|
|
|
|
|
|
|
|
let export = match import.tag {
|
|
|
|
wasmer_import_export_kind::WASM_MEMORY => {
|
|
|
|
let mem = import.value.memory as *mut Memory;
|
|
|
|
Export::Memory((&*mem).clone())
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_FUNCTION => {
|
|
|
|
let func_export = import.value.func as *mut Export;
|
|
|
|
(&*func_export).clone()
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_GLOBAL => {
|
|
|
|
let global = import.value.global as *mut Global;
|
|
|
|
Export::Global((&*global).clone())
|
|
|
|
}
|
|
|
|
wasmer_import_export_kind::WASM_TABLE => {
|
|
|
|
let table = import.value.table as *mut Table;
|
|
|
|
Export::Table((&*table).clone())
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let extension = (module_name.to_string(), import_name.to_string(), export);
|
|
|
|
extensions.push(extension)
|
|
|
|
}
|
|
|
|
|
|
|
|
import_object.extend(extensions);
|
|
|
|
|
|
|
|
return wasmer_result_t::WASMER_OK;
|
|
|
|
}
|
|
|
|
|
2019-03-29 15:50:16 +01:00
|
|
|
/// Gets import descriptors for the given module
|
|
|
|
///
|
|
|
|
/// The caller owns the object and should call `wasmer_import_descriptors_destroy` to free it.
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptors(
|
|
|
|
module: *const wasmer_module_t,
|
|
|
|
import_descriptors: *mut *mut wasmer_import_descriptors_t,
|
|
|
|
) {
|
2019-12-13 13:04:54 -08:00
|
|
|
if module.is_null() {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-29 15:50:16 +01:00
|
|
|
let module = &*(module as *const Module);
|
|
|
|
let total_imports = module.info().imported_functions.len()
|
|
|
|
+ module.info().imported_tables.len()
|
|
|
|
+ module.info().imported_globals.len()
|
|
|
|
+ module.info().imported_memories.len();
|
|
|
|
let mut descriptors: Vec<NamedImportDescriptor> = Vec::with_capacity(total_imports);
|
|
|
|
|
|
|
|
for (
|
|
|
|
_index,
|
|
|
|
ImportName {
|
|
|
|
namespace_index,
|
|
|
|
name_index,
|
|
|
|
},
|
|
|
|
) in &module.info().imported_functions
|
|
|
|
{
|
|
|
|
let namespace = module.info().namespace_table.get(*namespace_index);
|
|
|
|
let name = module.info().name_table.get(*name_index);
|
|
|
|
descriptors.push(NamedImportDescriptor {
|
|
|
|
module: namespace.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
kind: wasmer_import_export_kind::WASM_FUNCTION,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (
|
|
|
|
_index,
|
|
|
|
(
|
|
|
|
ImportName {
|
|
|
|
namespace_index,
|
|
|
|
name_index,
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
),
|
|
|
|
) in &module.info().imported_tables
|
|
|
|
{
|
|
|
|
let namespace = module.info().namespace_table.get(*namespace_index);
|
|
|
|
let name = module.info().name_table.get(*name_index);
|
|
|
|
descriptors.push(NamedImportDescriptor {
|
|
|
|
module: namespace.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
kind: wasmer_import_export_kind::WASM_TABLE,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (
|
|
|
|
_index,
|
|
|
|
(
|
|
|
|
ImportName {
|
|
|
|
namespace_index,
|
|
|
|
name_index,
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
),
|
|
|
|
) in &module.info().imported_globals
|
|
|
|
{
|
|
|
|
let namespace = module.info().namespace_table.get(*namespace_index);
|
|
|
|
let name = module.info().name_table.get(*name_index);
|
|
|
|
descriptors.push(NamedImportDescriptor {
|
|
|
|
module: namespace.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
kind: wasmer_import_export_kind::WASM_GLOBAL,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (
|
|
|
|
_index,
|
|
|
|
(
|
|
|
|
ImportName {
|
|
|
|
namespace_index,
|
|
|
|
name_index,
|
|
|
|
},
|
|
|
|
_,
|
|
|
|
),
|
|
|
|
) in &module.info().imported_memories
|
|
|
|
{
|
|
|
|
let namespace = module.info().namespace_table.get(*namespace_index);
|
|
|
|
let name = module.info().name_table.get(*name_index);
|
|
|
|
descriptors.push(NamedImportDescriptor {
|
|
|
|
module: namespace.to_string(),
|
|
|
|
name: name.to_string(),
|
|
|
|
kind: wasmer_import_export_kind::WASM_MEMORY,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let named_import_descriptors: Box<NamedImportDescriptors> =
|
|
|
|
Box::new(NamedImportDescriptors(descriptors));
|
|
|
|
*import_descriptors =
|
|
|
|
Box::into_raw(named_import_descriptors) as *mut wasmer_import_descriptors_t;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NamedImportDescriptors(Vec<NamedImportDescriptor>);
|
|
|
|
|
|
|
|
/// Frees the memory for the given import descriptors
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn wasmer_import_descriptors_destroy(
|
|
|
|
import_descriptors: *mut wasmer_import_descriptors_t,
|
|
|
|
) {
|
|
|
|
if !import_descriptors.is_null() {
|
|
|
|
unsafe { Box::from_raw(import_descriptors as *mut NamedImportDescriptors) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the length of the import descriptors
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptors_len(
|
|
|
|
exports: *mut wasmer_import_descriptors_t,
|
2019-05-22 16:44:03 +02:00
|
|
|
) -> c_uint {
|
2019-03-29 15:50:16 +01:00
|
|
|
if exports.is_null() {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-05-22 16:44:03 +02:00
|
|
|
(*(exports as *mut NamedImportDescriptors)).0.len() as c_uint
|
2019-03-29 15:50:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets import descriptor by index
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptors_get(
|
|
|
|
import_descriptors: *mut wasmer_import_descriptors_t,
|
2019-05-22 16:44:03 +02:00
|
|
|
idx: c_uint,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> *mut wasmer_import_descriptor_t {
|
|
|
|
if import_descriptors.is_null() {
|
|
|
|
return ptr::null_mut();
|
|
|
|
}
|
|
|
|
let named_import_descriptors = &mut *(import_descriptors as *mut NamedImportDescriptors);
|
|
|
|
&mut (*named_import_descriptors).0[idx as usize] as *mut NamedImportDescriptor
|
|
|
|
as *mut wasmer_import_descriptor_t
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets name for the import descriptor
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptor_name(
|
|
|
|
import_descriptor: *mut wasmer_import_descriptor_t,
|
|
|
|
) -> wasmer_byte_array {
|
|
|
|
let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor);
|
|
|
|
wasmer_byte_array {
|
|
|
|
bytes: named_import_descriptor.name.as_ptr(),
|
|
|
|
bytes_len: named_import_descriptor.name.len() as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets module name for the import descriptor
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptor_module_name(
|
|
|
|
import_descriptor: *mut wasmer_import_descriptor_t,
|
|
|
|
) -> wasmer_byte_array {
|
|
|
|
let named_import_descriptor = &*(import_descriptor as *mut NamedImportDescriptor);
|
|
|
|
wasmer_byte_array {
|
|
|
|
bytes: named_import_descriptor.module.as_ptr(),
|
|
|
|
bytes_len: named_import_descriptor.module.len() as u32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets export descriptor kind
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_descriptor_kind(
|
|
|
|
export: *mut wasmer_import_descriptor_t,
|
|
|
|
) -> wasmer_import_export_kind {
|
|
|
|
let named_import_descriptor = &*(export as *mut NamedImportDescriptor);
|
|
|
|
named_import_descriptor.kind.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the result parameter to the arity of the params of the wasmer_import_func_t
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
|
|
/// and `wasmer_last_error_message` to get an error message.
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_func_params_arity(
|
|
|
|
func: *const wasmer_import_func_t,
|
2019-06-12 12:10:49 +02:00
|
|
|
result: *mut u32,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> wasmer_result_t {
|
|
|
|
let export = &*(func as *const Export);
|
|
|
|
if let Export::Function { ref signature, .. } = *export {
|
2019-06-12 12:10:49 +02:00
|
|
|
*result = signature.params().len() as u32;
|
2019-03-29 15:50:16 +01:00
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "func ptr error in wasmer_import_func_params_arity".to_string(),
|
|
|
|
});
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-13 07:35:57 +01:00
|
|
|
/// Creates new host function, aka imported function. `func` is a
|
|
|
|
/// function pointer, where the first argument is the famous `vm::Ctx`
|
|
|
|
/// (in Rust), or `wasmer_instance_context_t` (in C). All arguments
|
|
|
|
/// must be typed with compatible WebAssembly native types:
|
2019-03-29 15:50:16 +01:00
|
|
|
///
|
2020-01-13 07:35:57 +01:00
|
|
|
/// | WebAssembly type | C/C++ type |
|
|
|
|
/// | ---------------- | ---------- |
|
|
|
|
/// | `i32` | `int32_t` |
|
|
|
|
/// | `i64` | `int64_t` |
|
|
|
|
/// | `f32` | `float` |
|
|
|
|
/// | `f64` | `double` |
|
|
|
|
///
|
|
|
|
/// The function pointer must have a lifetime greater than the
|
|
|
|
/// WebAssembly instance lifetime.
|
|
|
|
///
|
|
|
|
/// The caller owns the object and should call
|
|
|
|
/// `wasmer_import_func_destroy` to free it.
|
2019-03-29 15:50:16 +01:00
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_func_new(
|
|
|
|
func: extern "C" fn(data: *mut c_void),
|
|
|
|
params: *const wasmer_value_tag,
|
2019-05-22 16:44:03 +02:00
|
|
|
params_len: c_uint,
|
2019-03-29 15:50:16 +01:00
|
|
|
returns: *const wasmer_value_tag,
|
2019-05-22 16:44:03 +02:00
|
|
|
returns_len: c_uint,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> *mut wasmer_import_func_t {
|
|
|
|
let params: &[wasmer_value_tag] = slice::from_raw_parts(params, params_len as usize);
|
|
|
|
let params: Vec<Type> = params.iter().cloned().map(|x| x.into()).collect();
|
|
|
|
let returns: &[wasmer_value_tag] = slice::from_raw_parts(returns, returns_len as usize);
|
|
|
|
let returns: Vec<Type> = returns.iter().cloned().map(|x| x.into()).collect();
|
|
|
|
|
|
|
|
let export = Box::new(Export::Function {
|
|
|
|
func: FuncPointer::new(func as _),
|
|
|
|
ctx: Context::Internal,
|
|
|
|
signature: Arc::new(FuncSig::new(params, returns)),
|
|
|
|
});
|
|
|
|
Box::into_raw(export) as *mut wasmer_import_func_t
|
|
|
|
}
|
|
|
|
|
2020-01-13 07:35:37 +01:00
|
|
|
/// Stop the execution of a host function, aka imported function. The
|
|
|
|
/// function must be used _only_ inside a host function.
|
|
|
|
///
|
|
|
|
/// The pointer to `wasmer_instance_context_t` is received by the host
|
|
|
|
/// function as its first argument. Just passing it to `ctx` is fine.
|
|
|
|
///
|
|
|
|
/// The error message must have a greater lifetime than the host
|
|
|
|
/// function itself since the error is read outside the host function
|
|
|
|
/// with `wasmer_last_error_message`.
|
|
|
|
///
|
|
|
|
/// This function returns `wasmer_result_t::WASMER_ERROR` if `ctx` or
|
|
|
|
/// `error_message` are null.
|
|
|
|
///
|
|
|
|
/// This function never returns otherwise.
|
2020-01-10 15:13:30 +01:00
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
2020-01-15 08:37:20 +01:00
|
|
|
pub unsafe extern "C" fn wasmer_trap(
|
2020-01-10 15:13:30 +01:00
|
|
|
ctx: *const wasmer_instance_context_t,
|
|
|
|
error_message: *const c_char,
|
2020-01-13 07:20:06 +01:00
|
|
|
) -> wasmer_result_t {
|
|
|
|
if ctx.is_null() {
|
|
|
|
update_last_error(CApiError {
|
2020-01-15 12:50:18 +01:00
|
|
|
msg: "ctx ptr is null in wasmer_trap".to_string(),
|
2020-01-13 07:20:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if error_message.is_null() {
|
|
|
|
update_last_error(CApiError {
|
2020-01-15 12:50:18 +01:00
|
|
|
msg: "error_message is null in wasmer_trap".to_string(),
|
2020-01-13 07:20:06 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return wasmer_result_t::WASMER_ERROR;
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:13:30 +01:00
|
|
|
let ctx = &*(ctx as *const Ctx);
|
|
|
|
let error_message = CStr::from_ptr(error_message).to_str().unwrap();
|
|
|
|
|
|
|
|
(&*ctx.module)
|
|
|
|
.runnable_module
|
2020-04-24 14:30:14 -07:00
|
|
|
.do_early_trap(RuntimeError::User(Box::new(error_message))); // never returns
|
2020-01-10 15:13:30 +01:00
|
|
|
|
2020-01-15 08:40:36 +01:00
|
|
|
// cbindgen does not generate a binding for a function that
|
|
|
|
// returns `!`. Since we also need to error in some cases, the
|
|
|
|
// output type of `wasmer_trap` is `wasmer_result_t`. But the OK
|
|
|
|
// case is not reachable because `do_early_trap` never
|
|
|
|
// returns. That's a compromise to satisfy the Rust type system,
|
|
|
|
// cbindgen, and get an acceptable clean code.
|
2020-01-10 15:13:30 +01:00
|
|
|
#[allow(unreachable_code)]
|
2020-01-13 07:20:06 +01:00
|
|
|
wasmer_result_t::WASMER_OK
|
2020-01-10 15:13:30 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:50:16 +01:00
|
|
|
/// Sets the params buffer to the parameter types of the given wasmer_import_func_t
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
|
|
/// and `wasmer_last_error_message` to get an error message.
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_func_params(
|
|
|
|
func: *const wasmer_import_func_t,
|
|
|
|
params: *mut wasmer_value_tag,
|
2019-05-22 16:44:03 +02:00
|
|
|
params_len: c_uint,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> wasmer_result_t {
|
|
|
|
let export = &*(func as *const Export);
|
|
|
|
if let Export::Function { ref signature, .. } = *export {
|
|
|
|
let params: &mut [wasmer_value_tag] =
|
|
|
|
slice::from_raw_parts_mut(params, params_len as usize);
|
|
|
|
for (i, item) in signature.params().iter().enumerate() {
|
|
|
|
params[i] = item.into();
|
|
|
|
}
|
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "func ptr error in wasmer_import_func_params".to_string(),
|
|
|
|
});
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the returns buffer to the parameter types of the given wasmer_import_func_t
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
|
|
/// and `wasmer_last_error_message` to get an error message.
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_func_returns(
|
|
|
|
func: *const wasmer_import_func_t,
|
|
|
|
returns: *mut wasmer_value_tag,
|
2019-05-22 16:44:03 +02:00
|
|
|
returns_len: c_uint,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> wasmer_result_t {
|
|
|
|
let export = &*(func as *const Export);
|
|
|
|
if let Export::Function { ref signature, .. } = *export {
|
|
|
|
let returns: &mut [wasmer_value_tag] =
|
|
|
|
slice::from_raw_parts_mut(returns, returns_len as usize);
|
|
|
|
for (i, item) in signature.returns().iter().enumerate() {
|
|
|
|
returns[i] = item.into();
|
|
|
|
}
|
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "func ptr error in wasmer_import_func_returns".to_string(),
|
|
|
|
});
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the result parameter to the arity of the returns of the wasmer_import_func_t
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_OK` upon success.
|
|
|
|
///
|
|
|
|
/// Returns `wasmer_result_t::WASMER_ERROR` upon failure. Use `wasmer_last_error_length`
|
|
|
|
/// and `wasmer_last_error_message` to get an error message.
|
|
|
|
#[no_mangle]
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
pub unsafe extern "C" fn wasmer_import_func_returns_arity(
|
|
|
|
func: *const wasmer_import_func_t,
|
2019-06-12 12:10:49 +02:00
|
|
|
result: *mut u32,
|
2019-03-29 15:50:16 +01:00
|
|
|
) -> wasmer_result_t {
|
|
|
|
let export = &*(func as *const Export);
|
|
|
|
if let Export::Function { ref signature, .. } = *export {
|
2019-06-12 12:10:49 +02:00
|
|
|
*result = signature.returns().len() as u32;
|
2019-03-29 15:50:16 +01:00
|
|
|
wasmer_result_t::WASMER_OK
|
|
|
|
} else {
|
|
|
|
update_last_error(CApiError {
|
|
|
|
msg: "func ptr error in wasmer_import_func_results_arity".to_string(),
|
|
|
|
});
|
|
|
|
wasmer_result_t::WASMER_ERROR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Frees memory for the given Func
|
|
|
|
#[allow(clippy::cast_ptr_alignment)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn wasmer_import_func_destroy(func: *mut wasmer_import_func_t) {
|
|
|
|
if !func.is_null() {
|
|
|
|
unsafe { Box::from_raw(func as *mut Export) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-01 10:48:03 +03:00
|
|
|
/// Frees memory of the given ImportObject
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn wasmer_import_object_destroy(import_object: *mut wasmer_import_object_t) {
|
|
|
|
if !import_object.is_null() {
|
|
|
|
unsafe { Box::from_raw(import_object as *mut ImportObject) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-29 15:50:16 +01:00
|
|
|
struct NamedImportDescriptor {
|
|
|
|
module: String,
|
|
|
|
name: String,
|
|
|
|
kind: wasmer_import_export_kind,
|
|
|
|
}
|