Remove HostRef from the wasmtime public API (#788)
* Remove `HostRef` from the `wasmtime` public API
This commit removes all remaining usages of `HostRef` in the public API
of the `wasmtime` crate. This involved a number of API decisions such
as:
* None of `Func`, `Global`, `Table`, or `Memory` are wrapped in `HostRef`
* All of `Func`, `Global`, `Table`, and `Memory` implement `Clone` now.
* Methods called `type` are renamed to `ty` to avoid typing `r#type`.
* Methods requiring mutability for external items now no longer require
mutability. The mutable reference here is sort of a lie anyway since
the internals are aliased by the underlying module anyway. This
affects:
* `Table::set`
* `Table::grow`
* `Memory::grow`
* `Instance::set_signal_handler`
* The `Val::FuncRef` type is now no longer automatically coerced to
`AnyRef`. This is technically a breaking change which is pretty bad,
but I'm hoping that we can live with this interim state while we sort
out the `AnyRef` story in general.
* The implementation of the C API was refactored and updated in a few
locations to account for these changes:
* Accessing the exports of an instance are now cached to ensure we
always hand out the same `HostRef` values.
* `wasm_*_t` for external values no longer have internal cache,
instead they all wrap `wasm_external_t` and have an unchecked
accessor for the underlying variant (since the type is proof that
it's there). This makes casting back and forth much more trivial.
This is all related to #708 and while there's still more work to be done
in terms of documentation, this is the major bulk of the rest of the
implementation work on #708 I believe.
* More API updates
* Run rustfmt
* Fix a doc test
* More test updates
This commit is contained in:
@@ -10,6 +10,7 @@ use super::{
|
||||
HostInfo, HostRef, ImportType, Instance, Limits, Memory, MemoryType, Module, Store, Table,
|
||||
TableType, Trap, Val, ValType,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::{mem, ptr, slice};
|
||||
|
||||
@@ -243,6 +244,12 @@ enum wasm_externtype_t_type_cache {
|
||||
declare_vec!(wasm_externtype_vec_t, *mut wasm_externtype_t);
|
||||
|
||||
pub type wasm_externkind_t = u8;
|
||||
|
||||
const WASM_EXTERN_FUNC: wasm_externkind_t = 0;
|
||||
const WASM_EXTERN_GLOBAL: wasm_externkind_t = 1;
|
||||
const WASM_EXTERN_TABLE: wasm_externkind_t = 2;
|
||||
const WASM_EXTERN_MEMORY: wasm_externkind_t = 3;
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_importtype_t {
|
||||
@@ -312,6 +319,7 @@ declare_vec!(wasm_frame_vec_t, *mut wasm_frame_t);
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_instance_t {
|
||||
instance: HostRef<Instance>,
|
||||
exports_cache: RefCell<Option<Vec<ExternHost>>>,
|
||||
}
|
||||
pub type wasm_message_t = wasm_name_t;
|
||||
#[repr(C)]
|
||||
@@ -336,12 +344,22 @@ pub struct wasm_module_t {
|
||||
pub struct wasm_shared_module_t {
|
||||
_unused: [u8; 0],
|
||||
}
|
||||
#[repr(C)]
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_func_t {
|
||||
func: HostRef<Func>,
|
||||
ext: Option<Box<wasm_extern_t>>,
|
||||
ext: wasm_extern_t,
|
||||
}
|
||||
|
||||
impl wasm_func_t {
|
||||
fn func(&self) -> &HostRef<Func> {
|
||||
match &self.ext.which {
|
||||
ExternHost::Func(f) => f,
|
||||
_ => unsafe { std::hint::unreachable_unchecked() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type wasm_func_callback_t = std::option::Option<
|
||||
unsafe extern "C" fn(args: *const wasm_val_t, results: *mut wasm_val_t) -> *mut wasm_trap_t,
|
||||
>;
|
||||
@@ -352,40 +370,67 @@ pub type wasm_func_callback_with_env_t = std::option::Option<
|
||||
results: *mut wasm_val_t,
|
||||
) -> *mut wasm_trap_t,
|
||||
>;
|
||||
#[repr(C)]
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_global_t {
|
||||
global: HostRef<Global>,
|
||||
ext: Option<Box<wasm_extern_t>>,
|
||||
ext: wasm_extern_t,
|
||||
}
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_table_t {
|
||||
table: HostRef<Table>,
|
||||
ext: Option<Box<wasm_extern_t>>,
|
||||
}
|
||||
pub type wasm_table_size_t = u32;
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_memory_t {
|
||||
memory: HostRef<Memory>,
|
||||
ext: Option<Box<wasm_extern_t>>,
|
||||
}
|
||||
pub type wasm_memory_pages_t = u32;
|
||||
#[repr(C)]
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_extern_t {
|
||||
ext: Extern,
|
||||
cache: wasm_extern_t_type_cache,
|
||||
|
||||
impl wasm_global_t {
|
||||
fn global(&self) -> &HostRef<Global> {
|
||||
match &self.ext.which {
|
||||
ExternHost::Global(g) => g,
|
||||
_ => unsafe { std::hint::unreachable_unchecked() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum wasm_extern_t_type_cache {
|
||||
Empty,
|
||||
Func(wasm_func_t),
|
||||
Global(wasm_global_t),
|
||||
Memory(wasm_memory_t),
|
||||
Table(wasm_table_t),
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_table_t {
|
||||
ext: wasm_extern_t,
|
||||
}
|
||||
|
||||
impl wasm_table_t {
|
||||
fn table(&self) -> &HostRef<Table> {
|
||||
match &self.ext.which {
|
||||
ExternHost::Table(t) => t,
|
||||
_ => unsafe { std::hint::unreachable_unchecked() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type wasm_table_size_t = u32;
|
||||
|
||||
#[derive(Clone)]
|
||||
#[repr(transparent)]
|
||||
pub struct wasm_memory_t {
|
||||
ext: wasm_extern_t,
|
||||
}
|
||||
|
||||
impl wasm_memory_t {
|
||||
fn memory(&self) -> &HostRef<Memory> {
|
||||
match &self.ext.which {
|
||||
ExternHost::Memory(m) => m,
|
||||
_ => unsafe { std::hint::unreachable_unchecked() },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub type wasm_memory_pages_t = u32;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct wasm_extern_t {
|
||||
which: ExternHost,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
enum ExternHost {
|
||||
Func(HostRef<Func>),
|
||||
Global(HostRef<Global>),
|
||||
Memory(HostRef<Memory>),
|
||||
Table(HostRef<Table>),
|
||||
}
|
||||
|
||||
declare_vec!(wasm_extern_vec_t, *mut wasm_extern_t);
|
||||
@@ -415,16 +460,9 @@ pub unsafe extern "C" fn wasm_engine_new() -> *mut wasm_engine_t {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_func(e: *mut wasm_extern_t) -> *mut wasm_func_t {
|
||||
if let wasm_extern_t_type_cache::Empty = (*e).cache {
|
||||
(*e).cache = wasm_extern_t_type_cache::Func(wasm_func_t {
|
||||
func: (*e).ext.func().unwrap().clone(),
|
||||
ext: None,
|
||||
});
|
||||
}
|
||||
|
||||
match &mut (*e).cache {
|
||||
wasm_extern_t_type_cache::Func(f) => f,
|
||||
_ => panic!("wasm_extern_as_func"),
|
||||
match &(*e).which {
|
||||
ExternHost::Func(_) => e.cast(),
|
||||
_ => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -435,17 +473,7 @@ pub unsafe extern "C" fn wasm_extern_vec_delete(v: *mut wasm_extern_vec_t) {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_as_extern(f: *mut wasm_func_t) -> *mut wasm_extern_t {
|
||||
if (*f).ext.is_none() {
|
||||
(*f).ext = Some(Box::new(wasm_extern_t {
|
||||
ext: Extern::Func((*f).func.clone()),
|
||||
cache: wasm_extern_t_type_cache::Empty,
|
||||
}));
|
||||
}
|
||||
|
||||
match &mut (*f).ext {
|
||||
Some(e) => e.as_mut(),
|
||||
_ => panic!("wasm_func_as_extern"),
|
||||
}
|
||||
&mut (*f).ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -454,7 +482,7 @@ pub unsafe extern "C" fn wasm_func_call(
|
||||
args: *const wasm_val_t,
|
||||
results: *mut wasm_val_t,
|
||||
) -> *mut wasm_trap_t {
|
||||
let func = (*func).func.borrow();
|
||||
let func = (*func).func().borrow();
|
||||
let mut params = Vec::with_capacity(func.param_arity());
|
||||
for i in 0..func.param_arity() {
|
||||
let val = &(*args.add(i));
|
||||
@@ -606,8 +634,9 @@ pub unsafe extern "C" fn wasm_func_new(
|
||||
let ty = (*ty).functype.clone();
|
||||
let callback = Rc::new(callback);
|
||||
let func = Box::new(wasm_func_t {
|
||||
func: HostRef::new(Func::new(store, ty, callback)),
|
||||
ext: None,
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Func(HostRef::new(Func::new(store, ty, callback))),
|
||||
},
|
||||
});
|
||||
Box::into_raw(func)
|
||||
}
|
||||
@@ -660,13 +689,19 @@ pub unsafe extern "C" fn wasm_instance_new(
|
||||
let mut externs: Vec<Extern> = Vec::with_capacity((*module).imports.len());
|
||||
for i in 0..(*module).imports.len() {
|
||||
let import = *imports.add(i);
|
||||
externs.push((*import).ext.clone());
|
||||
externs.push(match &(*import).which {
|
||||
ExternHost::Func(e) => Extern::Func(e.borrow().clone()),
|
||||
ExternHost::Table(e) => Extern::Table(e.borrow().clone()),
|
||||
ExternHost::Global(e) => Extern::Global(e.borrow().clone()),
|
||||
ExternHost::Memory(e) => Extern::Memory(e.borrow().clone()),
|
||||
});
|
||||
}
|
||||
let module = &(*module).module.borrow();
|
||||
match Instance::new(store, module, &externs) {
|
||||
Ok(instance) => {
|
||||
let instance = Box::new(wasm_instance_t {
|
||||
instance: HostRef::new(instance),
|
||||
exports_cache: RefCell::new(None),
|
||||
});
|
||||
if !result.is_null() {
|
||||
(*result) = ptr::null_mut();
|
||||
@@ -694,14 +729,23 @@ pub unsafe extern "C" fn wasm_instance_exports(
|
||||
instance: *const wasm_instance_t,
|
||||
out: *mut wasm_extern_vec_t,
|
||||
) {
|
||||
let instance = &(*instance).instance.borrow();
|
||||
let exports = instance.exports();
|
||||
let mut cache = (*instance).exports_cache.borrow_mut();
|
||||
let exports = cache.get_or_insert_with(|| {
|
||||
let instance = &(*instance).instance.borrow();
|
||||
instance
|
||||
.exports()
|
||||
.iter()
|
||||
.map(|e| match e {
|
||||
Extern::Func(f) => ExternHost::Func(HostRef::new(f.clone())),
|
||||
Extern::Global(f) => ExternHost::Global(HostRef::new(f.clone())),
|
||||
Extern::Memory(f) => ExternHost::Memory(HostRef::new(f.clone())),
|
||||
Extern::Table(f) => ExternHost::Table(HostRef::new(f.clone())),
|
||||
})
|
||||
.collect()
|
||||
});
|
||||
let mut buffer = Vec::with_capacity(exports.len());
|
||||
for e in exports.iter() {
|
||||
let ext = Box::new(wasm_extern_t {
|
||||
ext: e.clone(),
|
||||
cache: wasm_extern_t_type_cache::Empty,
|
||||
});
|
||||
for e in exports {
|
||||
let ext = Box::new(wasm_extern_t { which: e.clone() });
|
||||
buffer.push(Box::into_raw(ext));
|
||||
}
|
||||
(*out).set_buffer(buffer);
|
||||
@@ -817,8 +861,9 @@ pub unsafe extern "C" fn wasm_func_new_with_env(
|
||||
finalizer,
|
||||
});
|
||||
let func = Box::new(wasm_func_t {
|
||||
func: HostRef::new(Func::new(store, ty, callback)),
|
||||
ext: None,
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Func(HostRef::new(Func::new(store, ty, callback))),
|
||||
},
|
||||
});
|
||||
Box::into_raw(func)
|
||||
}
|
||||
@@ -1012,24 +1057,25 @@ pub unsafe extern "C" fn wasm_exporttype_vec_delete(et: *mut wasm_exporttype_vec
|
||||
(*et).uninitialize();
|
||||
}
|
||||
|
||||
fn from_externtype(ty: &ExternType) -> wasm_externkind_t {
|
||||
match ty {
|
||||
ExternType::Func(_) => 0,
|
||||
ExternType::Global(_) => 1,
|
||||
ExternType::Table(_) => 2,
|
||||
ExternType::Memory(_) => 3,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_kind(e: *const wasm_extern_t) -> wasm_externkind_t {
|
||||
from_externtype(&(*e).ext.r#type())
|
||||
match (*e).which {
|
||||
ExternHost::Func(_) => WASM_EXTERN_FUNC,
|
||||
ExternHost::Global(_) => WASM_EXTERN_GLOBAL,
|
||||
ExternHost::Table(_) => WASM_EXTERN_TABLE,
|
||||
ExternHost::Memory(_) => WASM_EXTERN_MEMORY,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_type(e: *const wasm_extern_t) -> *mut wasm_externtype_t {
|
||||
let et = Box::new(wasm_externtype_t {
|
||||
ty: (*e).ext.r#type(),
|
||||
ty: match &(*e).which {
|
||||
ExternHost::Func(f) => ExternType::Func(f.borrow().ty().clone()),
|
||||
ExternHost::Global(f) => ExternType::Global(f.borrow().ty().clone()),
|
||||
ExternHost::Table(f) => ExternType::Table(f.borrow().ty().clone()),
|
||||
ExternHost::Memory(f) => ExternType::Memory(f.borrow().ty().clone()),
|
||||
},
|
||||
cache: wasm_externtype_t_type_cache::Empty,
|
||||
});
|
||||
Box::into_raw(et)
|
||||
@@ -1120,17 +1166,22 @@ pub unsafe extern "C" fn wasm_externtype_delete(et: *mut wasm_externtype_t) {
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_externtype_kind(et: *const wasm_externtype_t) -> wasm_externkind_t {
|
||||
from_externtype(&(*et).ty)
|
||||
match &(*et).ty {
|
||||
ExternType::Func(_) => WASM_EXTERN_FUNC,
|
||||
ExternType::Table(_) => WASM_EXTERN_TABLE,
|
||||
ExternType::Global(_) => WASM_EXTERN_GLOBAL,
|
||||
ExternType::Memory(_) => WASM_EXTERN_MEMORY,
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_param_arity(f: *const wasm_func_t) -> usize {
|
||||
(*f).func.borrow().param_arity()
|
||||
(*f).func().borrow().param_arity()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_result_arity(f: *const wasm_func_t) -> usize {
|
||||
(*f).func.borrow().result_arity()
|
||||
(*f).func().borrow().result_arity()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1279,32 +1330,15 @@ pub unsafe extern "C" fn wasm_valtype_kind(vt: *const wasm_valtype_t) -> wasm_va
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_global(e: *mut wasm_extern_t) -> *mut wasm_global_t {
|
||||
if let wasm_extern_t_type_cache::Empty = (*e).cache {
|
||||
(*e).cache = wasm_extern_t_type_cache::Global(wasm_global_t {
|
||||
global: (*e).ext.global().unwrap().clone(),
|
||||
ext: None,
|
||||
});
|
||||
}
|
||||
|
||||
match &mut (*e).cache {
|
||||
wasm_extern_t_type_cache::Global(g) => g,
|
||||
_ => panic!("wasm_extern_as_global"),
|
||||
match &(*e).which {
|
||||
ExternHost::Global(_) => e.cast(),
|
||||
_ => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_global_as_extern(g: *mut wasm_global_t) -> *mut wasm_extern_t {
|
||||
if (*g).ext.is_none() {
|
||||
(*g).ext = Some(Box::new(wasm_extern_t {
|
||||
ext: Extern::Global((*g).global.clone()),
|
||||
cache: wasm_extern_t_type_cache::Empty,
|
||||
}));
|
||||
}
|
||||
|
||||
match &mut (*g).ext {
|
||||
Some(e) => e.as_mut(),
|
||||
_ => panic!("wasm_global_as_extern"),
|
||||
}
|
||||
&mut (*g).ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1322,7 +1356,7 @@ pub unsafe extern "C" fn wasm_global_same(
|
||||
g1: *const wasm_global_t,
|
||||
g2: *const wasm_global_t,
|
||||
) -> bool {
|
||||
(*g1).global.ptr_eq(&(*g2).global)
|
||||
(*g1).global().ptr_eq(&(*g2).global())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1336,18 +1370,22 @@ pub unsafe extern "C" fn wasm_global_new(
|
||||
(*gt).globaltype.clone(),
|
||||
(*val).val(),
|
||||
));
|
||||
let g = Box::new(wasm_global_t { global, ext: None });
|
||||
let g = Box::new(wasm_global_t {
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Global(global),
|
||||
},
|
||||
});
|
||||
Box::into_raw(g)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_global_get(g: *const wasm_global_t, out: *mut wasm_val_t) {
|
||||
(*out).set((*g).global.borrow_mut().get());
|
||||
(*out).set((*g).global().borrow().get());
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_global_set(g: *mut wasm_global_t, val: *const wasm_val_t) {
|
||||
(*g).global.borrow_mut().set((*val).val())
|
||||
(*g).global().borrow().set((*val).val())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1377,32 +1415,15 @@ pub unsafe extern "C" fn wasm_globaltype_new(
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_memory(e: *mut wasm_extern_t) -> *mut wasm_memory_t {
|
||||
if let wasm_extern_t_type_cache::Empty = (*e).cache {
|
||||
(*e).cache = wasm_extern_t_type_cache::Memory(wasm_memory_t {
|
||||
memory: (*e).ext.memory().unwrap().clone(),
|
||||
ext: None,
|
||||
});
|
||||
}
|
||||
|
||||
match &mut (*e).cache {
|
||||
wasm_extern_t_type_cache::Memory(m) => m,
|
||||
_ => panic!("wasm_extern_as_memory"),
|
||||
match &(*e).which {
|
||||
ExternHost::Memory(_) => e.cast(),
|
||||
_ => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_as_extern(m: *mut wasm_memory_t) -> *mut wasm_extern_t {
|
||||
if (*m).ext.is_none() {
|
||||
(*m).ext = Some(Box::new(wasm_extern_t {
|
||||
ext: Extern::Memory((*m).memory.clone()),
|
||||
cache: wasm_extern_t_type_cache::Empty,
|
||||
}));
|
||||
}
|
||||
|
||||
match &mut (*m).ext {
|
||||
Some(e) => e.as_mut(),
|
||||
_ => panic!("wasm_global_as_extern"),
|
||||
}
|
||||
&mut (*m).ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1420,22 +1441,22 @@ pub unsafe extern "C" fn wasm_memory_same(
|
||||
m1: *const wasm_memory_t,
|
||||
m2: *const wasm_memory_t,
|
||||
) -> bool {
|
||||
(*m1).memory.ptr_eq(&(*m2).memory)
|
||||
(*m1).memory().ptr_eq(&(*m2).memory())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_data(m: *mut wasm_memory_t) -> *mut u8 {
|
||||
(*m).memory.borrow().data_ptr()
|
||||
(*m).memory().borrow().data_ptr()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_data_size(m: *const wasm_memory_t) -> usize {
|
||||
(*m).memory.borrow().data_size()
|
||||
(*m).memory().borrow().data_size()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_memory_size(m: *const wasm_memory_t) -> wasm_memory_pages_t {
|
||||
(*m).memory.borrow().size()
|
||||
(*m).memory().borrow().size()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1443,7 +1464,7 @@ pub unsafe extern "C" fn wasm_memory_grow(
|
||||
m: *mut wasm_memory_t,
|
||||
delta: wasm_memory_pages_t,
|
||||
) -> bool {
|
||||
(*m).memory.borrow_mut().grow(delta)
|
||||
(*m).memory().borrow().grow(delta)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1455,7 +1476,11 @@ pub unsafe extern "C" fn wasm_memory_new(
|
||||
&(*store).store.borrow(),
|
||||
(*mt).memorytype.clone(),
|
||||
));
|
||||
let m = Box::new(wasm_memory_t { memory, ext: None });
|
||||
let m = Box::new(wasm_memory_t {
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Memory(memory),
|
||||
},
|
||||
});
|
||||
Box::into_raw(m)
|
||||
}
|
||||
|
||||
@@ -1483,37 +1508,20 @@ pub unsafe extern "C" fn wasm_memorytype_new(
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_extern_as_table(e: *mut wasm_extern_t) -> *mut wasm_table_t {
|
||||
if let wasm_extern_t_type_cache::Empty = (*e).cache {
|
||||
(*e).cache = wasm_extern_t_type_cache::Table(wasm_table_t {
|
||||
table: (*e).ext.table().unwrap().clone(),
|
||||
ext: None,
|
||||
});
|
||||
}
|
||||
|
||||
match &mut (*e).cache {
|
||||
wasm_extern_t_type_cache::Table(t) => t,
|
||||
_ => panic!("wasm_extern_as_table"),
|
||||
match &(*e).which {
|
||||
ExternHost::Table(_) => e.cast(),
|
||||
_ => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_table_as_extern(t: *mut wasm_table_t) -> *mut wasm_extern_t {
|
||||
if (*t).ext.is_none() {
|
||||
(*t).ext = Some(Box::new(wasm_extern_t {
|
||||
ext: Extern::Table((*t).table.clone()),
|
||||
cache: wasm_extern_t_type_cache::Empty,
|
||||
}));
|
||||
}
|
||||
|
||||
match &mut (*t).ext {
|
||||
Some(e) => e.as_mut(),
|
||||
_ => panic!("wasm_table_as_extern"),
|
||||
}
|
||||
&mut (*t).ext
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_func_as_ref(f: *mut wasm_func_t) -> *mut wasm_ref_t {
|
||||
let r = (*f).func.anyref();
|
||||
let r = (*f).func().anyref();
|
||||
let f = Box::new(wasm_ref_t { r });
|
||||
Box::into_raw(f)
|
||||
}
|
||||
@@ -1547,12 +1555,13 @@ pub unsafe extern "C" fn wasm_table_new(
|
||||
Val::AnyRef(AnyRef::Null)
|
||||
};
|
||||
let t = Box::new(wasm_table_t {
|
||||
table: HostRef::new(Table::new(
|
||||
&(*store).store.borrow(),
|
||||
(*tt).tabletype.clone(),
|
||||
init,
|
||||
)),
|
||||
ext: None,
|
||||
ext: wasm_extern_t {
|
||||
which: ExternHost::Table(HostRef::new(Table::new(
|
||||
&(*store).store.borrow(),
|
||||
(*tt).tabletype.clone(),
|
||||
init,
|
||||
))),
|
||||
},
|
||||
});
|
||||
Box::into_raw(t)
|
||||
}
|
||||
@@ -1582,7 +1591,7 @@ pub unsafe extern "C" fn wasm_table_get(
|
||||
t: *const wasm_table_t,
|
||||
index: wasm_table_size_t,
|
||||
) -> *mut wasm_ref_t {
|
||||
let val = (*t).table.borrow().get(index);
|
||||
let val = (*t).table().borrow().get(index);
|
||||
into_funcref(val)
|
||||
}
|
||||
|
||||
@@ -1593,12 +1602,12 @@ pub unsafe extern "C" fn wasm_table_set(
|
||||
r: *mut wasm_ref_t,
|
||||
) -> bool {
|
||||
let val = from_funcref(r);
|
||||
(*t).table.borrow().set(index, val)
|
||||
(*t).table().borrow().set(index, val)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_table_size(t: *const wasm_table_t) -> wasm_table_size_t {
|
||||
(*t).table.borrow().size()
|
||||
(*t).table().borrow().size()
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
@@ -1608,12 +1617,12 @@ pub unsafe extern "C" fn wasm_table_grow(
|
||||
init: *mut wasm_ref_t,
|
||||
) -> bool {
|
||||
let init = from_funcref(init);
|
||||
(*t).table.borrow_mut().grow(delta, init)
|
||||
(*t).table().borrow().grow(delta, init)
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wasm_table_same(t1: *const wasm_table_t, t2: *const wasm_table_t) -> bool {
|
||||
(*t1).table.ptr_eq(&(*t2).table)
|
||||
(*t1).table().ptr_eq((*t2).table())
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
|
||||
Reference in New Issue
Block a user