mirror of
https://github.com/mii443/wasmer.git
synced 2025-12-07 21:28:21 +00:00
resolve conflict, replace two primary map to hash map
This commit is contained in:
35
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
35
lib/c-api/src/wasm_c_api/externals/function.rs
vendored
@@ -2,17 +2,27 @@ use super::super::store::wasm_store_t;
|
||||
use super::super::trap::wasm_trap_t;
|
||||
use super::super::types::{wasm_functype_t, wasm_valkind_enum};
|
||||
use super::super::value::{wasm_val_inner, wasm_val_t, wasm_val_vec_t};
|
||||
use super::CApiExternTag;
|
||||
use std::convert::TryInto;
|
||||
use std::ffi::c_void;
|
||||
use std::sync::Arc;
|
||||
use wasmer::{Function, Instance, RuntimeError, Val};
|
||||
use wasmer::{Function, RuntimeError, Val};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Debug, Clone)]
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
pub struct wasm_func_t {
|
||||
pub(crate) inner: Function,
|
||||
// this is how we ensure the instance stays alive
|
||||
pub(crate) instance: Option<Arc<Instance>>,
|
||||
pub(crate) tag: CApiExternTag,
|
||||
pub(crate) inner: Box<Function>,
|
||||
}
|
||||
|
||||
impl wasm_func_t {
|
||||
pub(crate) fn new(function: Function) -> Self {
|
||||
Self {
|
||||
tag: CApiExternTag::Function,
|
||||
inner: Box::new(function),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
@@ -80,10 +90,7 @@ pub unsafe extern "C" fn wasm_func_new(
|
||||
};
|
||||
let function = Function::new(&store.inner, func_sig, inner_callback);
|
||||
|
||||
Some(Box::new(wasm_func_t {
|
||||
instance: None,
|
||||
inner: function,
|
||||
}))
|
||||
Some(Box::new(wasm_func_t::new(function)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -115,9 +122,8 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
|
||||
impl Drop for WrapperEnv {
|
||||
fn drop(&mut self) {
|
||||
if let Some(env_finalizer) = Arc::get_mut(&mut self.env_finalizer)
|
||||
.map(Option::take)
|
||||
.flatten()
|
||||
if let Some(env_finalizer) =
|
||||
Arc::get_mut(&mut self.env_finalizer).and_then(Option::take)
|
||||
{
|
||||
if !self.env.is_null() {
|
||||
unsafe { (env_finalizer)(self.env as _) }
|
||||
@@ -172,10 +178,7 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
trampoline,
|
||||
);
|
||||
|
||||
Some(Box::new(wasm_func_t {
|
||||
instance: None,
|
||||
inner: function,
|
||||
}))
|
||||
Some(Box::new(wasm_func_t::new(function)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
22
lib/c-api/src/wasm_c_api/externals/global.rs
vendored
22
lib/c-api/src/wasm_c_api/externals/global.rs
vendored
@@ -1,14 +1,26 @@
|
||||
use super::super::store::wasm_store_t;
|
||||
use super::super::types::wasm_globaltype_t;
|
||||
use super::super::value::wasm_val_t;
|
||||
use super::CApiExternTag;
|
||||
use crate::error::update_last_error;
|
||||
use std::convert::TryInto;
|
||||
use wasmer::{Global, Val};
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct wasm_global_t {
|
||||
// maybe needs to hold onto instance
|
||||
pub(crate) inner: Global,
|
||||
pub(crate) tag: CApiExternTag,
|
||||
pub(crate) inner: Box<Global>,
|
||||
}
|
||||
|
||||
impl wasm_global_t {
|
||||
pub(crate) fn new(global: Global) -> Self {
|
||||
Self {
|
||||
tag: CApiExternTag::Global,
|
||||
inner: Box::new(global),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -30,7 +42,7 @@ pub unsafe extern "C" fn wasm_global_new(
|
||||
Global::new(store, wasm_val)
|
||||
};
|
||||
|
||||
Some(Box::new(wasm_global_t { inner: global }))
|
||||
Some(Box::new(wasm_global_t::new(global)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -40,9 +52,7 @@ pub unsafe extern "C" fn wasm_global_delete(_global: Option<Box<wasm_global_t>>)
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_global_copy(global: &wasm_global_t) -> Box<wasm_global_t> {
|
||||
// do shallow copy
|
||||
Box::new(wasm_global_t {
|
||||
inner: global.inner.clone(),
|
||||
})
|
||||
Box::new(wasm_global_t::new((&*global.inner).clone()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
22
lib/c-api/src/wasm_c_api/externals/memory.rs
vendored
22
lib/c-api/src/wasm_c_api/externals/memory.rs
vendored
@@ -1,12 +1,24 @@
|
||||
use super::super::store::wasm_store_t;
|
||||
use super::super::types::wasm_memorytype_t;
|
||||
use super::CApiExternTag;
|
||||
use std::mem;
|
||||
use wasmer::{Memory, Pages};
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct wasm_memory_t {
|
||||
// maybe needs to hold onto instance
|
||||
pub(crate) inner: Memory,
|
||||
pub(crate) tag: CApiExternTag,
|
||||
pub(crate) inner: Box<Memory>,
|
||||
}
|
||||
|
||||
impl wasm_memory_t {
|
||||
pub(crate) fn new(memory: Memory) -> Self {
|
||||
Self {
|
||||
tag: CApiExternTag::Memory,
|
||||
inner: Box::new(memory),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -20,7 +32,7 @@ pub unsafe extern "C" fn wasm_memory_new(
|
||||
let memory_type = memory_type.inner().memory_type.clone();
|
||||
let memory = c_try!(Memory::new(&store.inner, memory_type));
|
||||
|
||||
Some(Box::new(wasm_memory_t { inner: memory }))
|
||||
Some(Box::new(wasm_memory_t::new(memory)))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -30,9 +42,7 @@ pub unsafe extern "C" fn wasm_memory_delete(_memory: Option<Box<wasm_memory_t>>)
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_copy(memory: &wasm_memory_t) -> Box<wasm_memory_t> {
|
||||
// do shallow copy
|
||||
Box::new(wasm_memory_t {
|
||||
inner: memory.inner.clone(),
|
||||
})
|
||||
Box::new(wasm_memory_t::new((&*memory.inner).clone()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
245
lib/c-api/src/wasm_c_api/externals/mod.rs
vendored
245
lib/c-api/src/wasm_c_api/externals/mod.rs
vendored
@@ -6,16 +6,167 @@ mod table;
|
||||
pub use function::*;
|
||||
pub use global::*;
|
||||
pub use memory::*;
|
||||
use std::sync::Arc;
|
||||
use std::mem;
|
||||
pub use table::*;
|
||||
use wasmer::{Extern, Instance};
|
||||
use wasmer::{Extern, ExternType};
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_extern_t {
|
||||
// this is how we ensure the instance stays alive
|
||||
pub(crate) instance: Option<Arc<Instance>>,
|
||||
pub(crate) inner: Extern,
|
||||
pub(crate) inner: wasm_extern_inner,
|
||||
}
|
||||
|
||||
/// All elements in this union must be `repr(C)` and have a
|
||||
/// `CApiExternTag` as their first element.
|
||||
#[allow(non_camel_case_types)]
|
||||
pub(crate) union wasm_extern_inner {
|
||||
function: mem::ManuallyDrop<wasm_func_t>,
|
||||
memory: mem::ManuallyDrop<wasm_memory_t>,
|
||||
global: mem::ManuallyDrop<wasm_global_t>,
|
||||
table: mem::ManuallyDrop<wasm_table_t>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod extern_tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn externs_are_the_same_size() {
|
||||
use std::mem::{align_of, size_of};
|
||||
assert_eq!(size_of::<wasm_extern_t>(), size_of::<wasm_func_t>());
|
||||
assert_eq!(size_of::<wasm_extern_t>(), size_of::<wasm_memory_t>());
|
||||
assert_eq!(size_of::<wasm_extern_t>(), size_of::<wasm_global_t>());
|
||||
assert_eq!(size_of::<wasm_extern_t>(), size_of::<wasm_table_t>());
|
||||
|
||||
assert_eq!(align_of::<wasm_extern_t>(), align_of::<wasm_func_t>());
|
||||
assert_eq!(align_of::<wasm_extern_t>(), align_of::<wasm_memory_t>());
|
||||
assert_eq!(align_of::<wasm_extern_t>(), align_of::<wasm_global_t>());
|
||||
assert_eq!(align_of::<wasm_extern_t>(), align_of::<wasm_table_t>());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_are_the_same_offset_away() {
|
||||
use field_offset::offset_of;
|
||||
|
||||
let func_tag_offset = offset_of!(wasm_func_t => tag).get_byte_offset();
|
||||
let memory_tag_offset = offset_of!(wasm_memory_t => tag).get_byte_offset();
|
||||
let global_tag_offset = offset_of!(wasm_global_t => tag).get_byte_offset();
|
||||
let table_tag_offset = offset_of!(wasm_table_t => tag).get_byte_offset();
|
||||
|
||||
assert_eq!(func_tag_offset, memory_tag_offset);
|
||||
assert_eq!(global_tag_offset, table_tag_offset);
|
||||
assert_eq!(func_tag_offset, global_tag_offset);
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for wasm_extern_inner {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
match self.function.tag {
|
||||
CApiExternTag::Function => mem::ManuallyDrop::drop(&mut self.function),
|
||||
CApiExternTag::Global => mem::ManuallyDrop::drop(&mut self.global),
|
||||
CApiExternTag::Table => mem::ManuallyDrop::drop(&mut self.table),
|
||||
CApiExternTag::Memory => mem::ManuallyDrop::drop(&mut self.memory),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl wasm_extern_t {
|
||||
pub(crate) fn get_tag(&self) -> CApiExternTag {
|
||||
unsafe { self.inner.function.tag }
|
||||
}
|
||||
|
||||
pub(crate) fn ty(&self) -> ExternType {
|
||||
match self.get_tag() {
|
||||
CApiExternTag::Function => {
|
||||
ExternType::Function(unsafe { self.inner.function.inner.ty().clone() })
|
||||
}
|
||||
CApiExternTag::Memory => {
|
||||
ExternType::Memory(unsafe { self.inner.memory.inner.ty().clone() })
|
||||
}
|
||||
CApiExternTag::Global => {
|
||||
ExternType::Global(unsafe { self.inner.global.inner.ty().clone() })
|
||||
}
|
||||
CApiExternTag::Table => {
|
||||
ExternType::Table(unsafe { self.inner.table.inner.ty().clone() })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for wasm_extern_t {
|
||||
fn clone(&self) -> Self {
|
||||
match self.get_tag() {
|
||||
CApiExternTag::Function => Self {
|
||||
inner: wasm_extern_inner {
|
||||
function: unsafe { self.inner.function.clone() },
|
||||
},
|
||||
},
|
||||
CApiExternTag::Memory => Self {
|
||||
inner: wasm_extern_inner {
|
||||
memory: unsafe { self.inner.memory.clone() },
|
||||
},
|
||||
},
|
||||
CApiExternTag::Global => Self {
|
||||
inner: wasm_extern_inner {
|
||||
global: unsafe { self.inner.global.clone() },
|
||||
},
|
||||
},
|
||||
CApiExternTag::Table => Self {
|
||||
inner: wasm_extern_inner {
|
||||
table: unsafe { self.inner.table.clone() },
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Extern> for wasm_extern_t {
|
||||
fn from(other: Extern) -> Self {
|
||||
match other {
|
||||
Extern::Function(function) => Self {
|
||||
inner: wasm_extern_inner {
|
||||
function: mem::ManuallyDrop::new(wasm_func_t::new(function)),
|
||||
},
|
||||
},
|
||||
Extern::Memory(memory) => Self {
|
||||
inner: wasm_extern_inner {
|
||||
memory: mem::ManuallyDrop::new(wasm_memory_t::new(memory)),
|
||||
},
|
||||
},
|
||||
Extern::Table(table) => Self {
|
||||
inner: wasm_extern_inner {
|
||||
table: mem::ManuallyDrop::new(wasm_table_t::new(table)),
|
||||
},
|
||||
},
|
||||
Extern::Global(global) => Self {
|
||||
inner: wasm_extern_inner {
|
||||
global: mem::ManuallyDrop::new(wasm_global_t::new(global)),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<wasm_extern_t> for Extern {
|
||||
fn from(other: wasm_extern_t) -> Self {
|
||||
match other.get_tag() {
|
||||
CApiExternTag::Function => unsafe { (&*other.inner.function.inner).clone().into() },
|
||||
CApiExternTag::Memory => unsafe { (&*other.inner.memory.inner).clone().into() },
|
||||
CApiExternTag::Table => unsafe { (&*other.inner.table.inner).clone().into() },
|
||||
CApiExternTag::Global => unsafe { (&*other.inner.global.inner).clone().into() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
#[repr(C)]
|
||||
pub(crate) enum CApiExternTag {
|
||||
Function,
|
||||
Global,
|
||||
Table,
|
||||
Memory,
|
||||
}
|
||||
|
||||
wasm_declare_boxed_vec!(extern);
|
||||
@@ -31,106 +182,68 @@ pub unsafe extern "C" fn wasm_extern_copy(r#extern: &wasm_extern_t) -> Box<wasm_
|
||||
pub unsafe extern "C" fn wasm_extern_delete(_extern: Option<Box<wasm_extern_t>>) {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_as_extern(
|
||||
func: Option<&wasm_func_t>,
|
||||
) -> Option<Box<wasm_extern_t>> {
|
||||
let func = func?;
|
||||
|
||||
Some(Box::new(wasm_extern_t {
|
||||
instance: func.instance.clone(),
|
||||
inner: Extern::Function(func.inner.clone()),
|
||||
}))
|
||||
pub extern "C" fn wasm_func_as_extern(func: Option<&wasm_func_t>) -> Option<&wasm_extern_t> {
|
||||
unsafe { mem::transmute::<Option<&wasm_func_t>, Option<&wasm_extern_t>>(func) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_global_as_extern(
|
||||
global: Option<&wasm_global_t>,
|
||||
) -> Option<Box<wasm_extern_t>> {
|
||||
let global = global?;
|
||||
|
||||
Some(Box::new(wasm_extern_t {
|
||||
// TODO: update this if global does hold onto an `instance`
|
||||
instance: None,
|
||||
inner: Extern::Global(global.inner.clone()),
|
||||
}))
|
||||
pub extern "C" fn wasm_global_as_extern(global: Option<&wasm_global_t>) -> Option<&wasm_extern_t> {
|
||||
unsafe { mem::transmute::<Option<&wasm_global_t>, Option<&wasm_extern_t>>(global) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_as_extern(
|
||||
memory: Option<&wasm_memory_t>,
|
||||
) -> Option<Box<wasm_extern_t>> {
|
||||
let memory = memory?;
|
||||
|
||||
Some(Box::new(wasm_extern_t {
|
||||
// TODO: update this if global does hold onto an `instance`
|
||||
instance: None,
|
||||
inner: Extern::Memory(memory.inner.clone()),
|
||||
}))
|
||||
pub extern "C" fn wasm_memory_as_extern(memory: Option<&wasm_memory_t>) -> Option<&wasm_extern_t> {
|
||||
unsafe { mem::transmute::<Option<&wasm_memory_t>, Option<&wasm_extern_t>>(memory) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_table_as_extern(
|
||||
table: Option<&wasm_table_t>,
|
||||
) -> Option<Box<wasm_extern_t>> {
|
||||
let table = table?;
|
||||
|
||||
Some(Box::new(wasm_extern_t {
|
||||
// TODO: update this if global does hold onto an `instance`
|
||||
instance: None,
|
||||
inner: Extern::Table(table.inner.clone()),
|
||||
}))
|
||||
pub extern "C" fn wasm_table_as_extern(table: Option<&wasm_table_t>) -> Option<&wasm_extern_t> {
|
||||
unsafe { mem::transmute::<Option<&wasm_table_t>, Option<&wasm_extern_t>>(table) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_func(
|
||||
r#extern: Option<&wasm_extern_t>,
|
||||
) -> Option<Box<wasm_func_t>> {
|
||||
pub extern "C" fn wasm_extern_as_func(r#extern: Option<&wasm_extern_t>) -> Option<&wasm_func_t> {
|
||||
let r#extern = r#extern?;
|
||||
|
||||
if let Extern::Function(f) = &r#extern.inner {
|
||||
Some(Box::new(wasm_func_t {
|
||||
inner: f.clone(),
|
||||
instance: r#extern.instance.clone(),
|
||||
}))
|
||||
if r#extern.get_tag() == CApiExternTag::Function {
|
||||
Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_func_t>(r#extern) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_global(
|
||||
pub extern "C" fn wasm_extern_as_global(
|
||||
r#extern: Option<&wasm_extern_t>,
|
||||
) -> Option<Box<wasm_global_t>> {
|
||||
) -> Option<&wasm_global_t> {
|
||||
let r#extern = r#extern?;
|
||||
|
||||
if let Extern::Global(g) = &r#extern.inner {
|
||||
Some(Box::new(wasm_global_t { inner: g.clone() }))
|
||||
if r#extern.get_tag() == CApiExternTag::Global {
|
||||
Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_global_t>(r#extern) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_memory(
|
||||
pub extern "C" fn wasm_extern_as_memory(
|
||||
r#extern: Option<&wasm_extern_t>,
|
||||
) -> Option<Box<wasm_memory_t>> {
|
||||
) -> Option<&wasm_memory_t> {
|
||||
let r#extern = r#extern?;
|
||||
|
||||
if let Extern::Memory(m) = &r#extern.inner {
|
||||
Some(Box::new(wasm_memory_t { inner: m.clone() }))
|
||||
if r#extern.get_tag() == CApiExternTag::Memory {
|
||||
Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_memory_t>(r#extern) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_table(
|
||||
r#extern: Option<&wasm_extern_t>,
|
||||
) -> Option<Box<wasm_table_t>> {
|
||||
pub extern "C" fn wasm_extern_as_table(r#extern: Option<&wasm_extern_t>) -> Option<&wasm_table_t> {
|
||||
let r#extern = r#extern?;
|
||||
|
||||
if let Extern::Table(t) = &r#extern.inner {
|
||||
Some(Box::new(wasm_table_t { inner: t.clone() }))
|
||||
if r#extern.get_tag() == CApiExternTag::Table {
|
||||
Some(unsafe { mem::transmute::<&wasm_extern_t, &wasm_table_t>(r#extern) })
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
||||
20
lib/c-api/src/wasm_c_api/externals/table.rs
vendored
20
lib/c-api/src/wasm_c_api/externals/table.rs
vendored
@@ -1,11 +1,23 @@
|
||||
use super::super::store::wasm_store_t;
|
||||
use super::super::types::{wasm_ref_t, wasm_table_size_t, wasm_tabletype_t};
|
||||
use super::CApiExternTag;
|
||||
use wasmer::Table;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_table_t {
|
||||
// maybe needs to hold onto instance
|
||||
pub(crate) inner: Table,
|
||||
pub(crate) tag: CApiExternTag,
|
||||
pub(crate) inner: Box<Table>,
|
||||
}
|
||||
|
||||
impl wasm_table_t {
|
||||
pub(crate) fn new(table: Table) -> Self {
|
||||
Self {
|
||||
tag: CApiExternTag::Table,
|
||||
inner: Box::new(table),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -32,9 +44,7 @@ pub unsafe extern "C" fn wasm_table_delete(_table: Option<Box<wasm_table_t>>) {}
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_table_copy(table: &wasm_table_t) -> Box<wasm_table_t> {
|
||||
// do shallow copy
|
||||
Box::new(wasm_table_t {
|
||||
inner: table.inner.clone(),
|
||||
})
|
||||
Box::new(wasm_table_t::new((&*table.inner).clone()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -53,9 +53,8 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
.into_slice()
|
||||
.map(|imports| imports.iter())
|
||||
.unwrap_or_else(|| [].iter())
|
||||
.map(|imp| &imp.inner)
|
||||
.map(|imp| Extern::from((&**imp).clone()))
|
||||
.take(module_import_count)
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
let instance = match Instance::new(wasm_module, &resolver) {
|
||||
@@ -192,10 +191,7 @@ pub unsafe extern "C" fn wasm_instance_exports(
|
||||
None
|
||||
};
|
||||
|
||||
Box::into_raw(Box::new(wasm_extern_t {
|
||||
instance: Some(Arc::clone(instance)),
|
||||
inner: r#extern.clone(),
|
||||
}))
|
||||
Box::into_raw(Box::new(r#extern.clone().into()))
|
||||
})
|
||||
.collect::<Vec<*mut wasm_extern_t>>();
|
||||
extern_vec.shrink_to_fit();
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
use super::{wasm_externtype_t, wasm_name_t};
|
||||
use super::{owned_wasm_name_t, wasm_externtype_t, wasm_name_t};
|
||||
use wasmer::ExportType;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_exporttype_t {
|
||||
name: Box<wasm_name_t>,
|
||||
name: owned_wasm_name_t,
|
||||
extern_type: Box<wasm_externtype_t>,
|
||||
}
|
||||
|
||||
@@ -12,11 +12,12 @@ wasm_declare_boxed_vec!(exporttype);
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_exporttype_new(
|
||||
name: Option<Box<wasm_name_t>>,
|
||||
name: Option<&wasm_name_t>,
|
||||
extern_type: Option<Box<wasm_externtype_t>>,
|
||||
) -> Option<Box<wasm_exporttype_t>> {
|
||||
let name = unsafe { owned_wasm_name_t::new(name?) };
|
||||
Some(Box::new(wasm_exporttype_t {
|
||||
name: name?,
|
||||
name,
|
||||
extern_type: extern_type?,
|
||||
}))
|
||||
}
|
||||
@@ -42,7 +43,7 @@ impl From<ExportType> for wasm_exporttype_t {
|
||||
|
||||
impl From<&ExportType> for wasm_exporttype_t {
|
||||
fn from(other: &ExportType) -> Self {
|
||||
let name: Box<wasm_name_t> = Box::new(other.name().to_string().into());
|
||||
let name: owned_wasm_name_t = other.name().to_string().into();
|
||||
let extern_type: Box<wasm_externtype_t> = Box::new(other.ty().into());
|
||||
|
||||
wasm_exporttype_t { name, extern_type }
|
||||
|
||||
@@ -85,12 +85,12 @@ impl From<&ExternType> for wasm_externtype_t {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_type(r#extern: &wasm_extern_t) -> Box<wasm_externtype_t> {
|
||||
Box::new(wasm_externtype_t::new(r#extern.inner.ty()))
|
||||
Box::new(wasm_externtype_t::new(r#extern.ty()))
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_kind(r#extern: &wasm_extern_t) -> wasm_externkind_t {
|
||||
wasm_externkind_enum::from(r#extern.inner.ty()) as wasm_externkind_t
|
||||
wasm_externkind_enum::from(r#extern.ty()) as wasm_externkind_t
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use super::{wasm_externtype_t, wasm_name_t};
|
||||
use super::{owned_wasm_name_t, wasm_externtype_t, wasm_name_t};
|
||||
use wasmer::ImportType;
|
||||
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone)]
|
||||
#[repr(C)]
|
||||
pub struct wasm_importtype_t {
|
||||
module: Box<wasm_name_t>,
|
||||
name: Box<wasm_name_t>,
|
||||
module: owned_wasm_name_t,
|
||||
name: owned_wasm_name_t,
|
||||
extern_type: Box<wasm_externtype_t>,
|
||||
}
|
||||
|
||||
@@ -13,13 +14,19 @@ wasm_declare_boxed_vec!(importtype);
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wasm_importtype_new(
|
||||
module: Option<Box<wasm_name_t>>,
|
||||
name: Option<Box<wasm_name_t>>,
|
||||
module: Option<&wasm_name_t>,
|
||||
name: Option<&wasm_name_t>,
|
||||
extern_type: Option<Box<wasm_externtype_t>>,
|
||||
) -> Option<Box<wasm_importtype_t>> {
|
||||
let (module, name) = unsafe {
|
||||
(
|
||||
owned_wasm_name_t::new(module?),
|
||||
owned_wasm_name_t::new(name?),
|
||||
)
|
||||
};
|
||||
Some(Box::new(wasm_importtype_t {
|
||||
name: name?,
|
||||
module: module?,
|
||||
name,
|
||||
module,
|
||||
extern_type: extern_type?,
|
||||
}))
|
||||
}
|
||||
@@ -50,8 +57,8 @@ impl From<ImportType> for wasm_importtype_t {
|
||||
|
||||
impl From<&ImportType> for wasm_importtype_t {
|
||||
fn from(other: &ImportType) -> Self {
|
||||
let module: Box<wasm_name_t> = Box::new(other.module().to_string().into());
|
||||
let name: Box<wasm_name_t> = Box::new(other.name().to_string().into());
|
||||
let module: owned_wasm_name_t = other.module().to_string().into();
|
||||
let name: owned_wasm_name_t = other.name().to_string().into();
|
||||
let extern_type: Box<wasm_externtype_t> = Box::new(other.ty().into());
|
||||
|
||||
wasm_importtype_t {
|
||||
|
||||
@@ -28,16 +28,63 @@ wasm_declare_vec!(byte);
|
||||
#[allow(non_camel_case_types)]
|
||||
pub type wasm_name_t = wasm_byte_vec_t;
|
||||
|
||||
impl From<String> for wasm_name_t {
|
||||
impl AsRef<wasm_name_t> for wasm_name_t {
|
||||
fn as_ref(&self) -> &wasm_name_t {
|
||||
&self
|
||||
}
|
||||
}
|
||||
|
||||
/// An owned version of `wasm_name_t`.
|
||||
///
|
||||
/// Assumes that data is either valid host-owned or null.
|
||||
// NOTE: `wasm_name_t` already does a deep copy, so we just derive `Clone` here.
|
||||
#[derive(Debug, Clone)]
|
||||
#[repr(transparent)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct owned_wasm_name_t(wasm_name_t);
|
||||
|
||||
impl owned_wasm_name_t {
|
||||
/// Take ownership of some `wasm_name_t`
|
||||
///
|
||||
/// # Safety
|
||||
/// You must ensure that the data pointed to by `wasm_name_t` is valid and
|
||||
/// that it is not owned by anyone else.
|
||||
pub unsafe fn new(name: &wasm_name_t) -> Self {
|
||||
Self(wasm_name_t {
|
||||
size: name.size,
|
||||
data: name.data,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for owned_wasm_name_t {
|
||||
fn drop(&mut self) {
|
||||
if !self.0.data.is_null() {
|
||||
let _v = unsafe { Vec::from_raw_parts(self.0.data, self.0.size, self.0.size) };
|
||||
self.0.data = std::ptr::null_mut();
|
||||
self.0.size = 0;
|
||||
}
|
||||
// why can't we call this function?
|
||||
//unsafe { crate::wasm_c_api::macros::wasm_byte_vec_delete(Some(self.0)) }
|
||||
}
|
||||
}
|
||||
|
||||
impl AsRef<wasm_name_t> for owned_wasm_name_t {
|
||||
fn as_ref(&self) -> &wasm_name_t {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for owned_wasm_name_t {
|
||||
fn from(string: String) -> Self {
|
||||
let mut boxed_str: Box<str> = string.into_boxed_str();
|
||||
let data = boxed_str.as_mut_ptr();
|
||||
let size = boxed_str.bytes().len();
|
||||
let wasm_name = Self { data, size };
|
||||
let wasm_name = wasm_name_t { data, size };
|
||||
|
||||
Box::leak(boxed_str);
|
||||
|
||||
wasm_name
|
||||
Self(wasm_name)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,10 @@
|
||||
//! API.
|
||||
|
||||
use super::super::{
|
||||
externals::wasm_extern_t, module::wasm_module_t, store::wasm_store_t, types::wasm_name_t,
|
||||
externals::wasm_extern_t,
|
||||
module::wasm_module_t,
|
||||
store::wasm_store_t,
|
||||
types::{owned_wasm_name_t, wasm_name_t},
|
||||
wasi::wasi_env_t,
|
||||
};
|
||||
use crate::error::CApiError;
|
||||
@@ -19,8 +22,8 @@ use wasmer_wasi::{generate_import_object_from_env, get_wasi_version};
|
||||
#[allow(non_camel_case_types)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasmer_named_extern_t {
|
||||
module: Box<wasm_name_t>,
|
||||
name: Box<wasm_name_t>,
|
||||
module: owned_wasm_name_t,
|
||||
name: owned_wasm_name_t,
|
||||
r#extern: Box<wasm_extern_t>,
|
||||
}
|
||||
|
||||
@@ -179,17 +182,14 @@ fn wasi_get_unordered_imports_inner(
|
||||
*imports = import_object
|
||||
.into_iter()
|
||||
.map(|((module, name), export)| {
|
||||
let module = Box::new(module.into());
|
||||
let name = Box::new(name.into());
|
||||
let module = module.into();
|
||||
let name = name.into();
|
||||
let extern_inner = Extern::from_vm_export(store, export);
|
||||
|
||||
Box::new(wasmer_named_extern_t {
|
||||
module,
|
||||
name,
|
||||
r#extern: Box::new(wasm_extern_t {
|
||||
instance: None,
|
||||
inner: extern_inner,
|
||||
}),
|
||||
r#extern: Box::new(extern_inner.into()),
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
|
||||
@@ -6,7 +6,7 @@ mod capture_files;
|
||||
|
||||
pub use super::unstable::wasi::wasi_get_unordered_imports;
|
||||
use super::{
|
||||
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
|
||||
externals::{wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
|
||||
instance::wasm_instance_t,
|
||||
module::wasm_module_t,
|
||||
store::wasm_store_t,
|
||||
@@ -390,10 +390,7 @@ fn wasi_get_imports_inner(
|
||||
}));
|
||||
let inner = Extern::from_vm_export(store, export);
|
||||
|
||||
Some(Box::new(wasm_extern_t {
|
||||
instance: None,
|
||||
inner,
|
||||
}))
|
||||
Some(Box::new(inner.into()))
|
||||
})
|
||||
.collect::<Option<Vec<_>>>()?
|
||||
.into();
|
||||
@@ -407,10 +404,7 @@ pub unsafe extern "C" fn wasi_get_start_function(
|
||||
) -> Option<Box<wasm_func_t>> {
|
||||
let start = c_try!(instance.inner.exports.get_function("_start"));
|
||||
|
||||
Some(Box::new(wasm_func_t {
|
||||
inner: start.clone(),
|
||||
instance: Some(instance.inner.clone()),
|
||||
}))
|
||||
Some(Box::new(wasm_func_t::new(start.clone())))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user