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:
@@ -12,7 +12,7 @@ use wasmtime_runtime::Export;
|
||||
/// WebAssembly.
|
||||
/// # Example
|
||||
/// ```
|
||||
/// use wasmtime::{HostRef, Val};
|
||||
/// use wasmtime::Val;
|
||||
///
|
||||
/// struct TimesTwo;
|
||||
///
|
||||
@@ -54,9 +54,8 @@ use wasmtime_runtime::Export;
|
||||
/// );
|
||||
///
|
||||
/// // Build a reference to the "times_two" function that can be used.
|
||||
/// let times_two_function = HostRef::new(
|
||||
/// wasmtime::Func::new(&store, times_two_type, std::rc::Rc::new(TimesTwo))
|
||||
/// );
|
||||
/// let times_two_function =
|
||||
/// wasmtime::Func::new(&store, times_two_type, std::rc::Rc::new(TimesTwo));
|
||||
///
|
||||
/// // Create module instance that imports our function
|
||||
/// let instance = wasmtime::Instance::new(
|
||||
@@ -71,7 +70,6 @@ use wasmtime_runtime::Export;
|
||||
/// // Borrow and call "run". Returning any error message from Wasm as a string.
|
||||
/// let original = 5i32;
|
||||
/// let results = run_function
|
||||
/// .borrow()
|
||||
/// .call(&[original.into()])
|
||||
/// .map_err(|trap| trap.to_string())?;
|
||||
///
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::callable::{Callable, NativeCallable, WasmtimeFn, WrappedCallable};
|
||||
use crate::r#ref::{AnyRef, HostRef};
|
||||
use crate::r#ref::AnyRef;
|
||||
use crate::runtime::Store;
|
||||
use crate::trampoline::{generate_global_export, generate_memory_export, generate_table_export};
|
||||
use crate::trap::Trap;
|
||||
@@ -15,53 +15,53 @@ use wasmtime_runtime::InstanceHandle;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub enum Extern {
|
||||
Func(HostRef<Func>),
|
||||
Global(HostRef<Global>),
|
||||
Table(HostRef<Table>),
|
||||
Memory(HostRef<Memory>),
|
||||
Func(Func),
|
||||
Global(Global),
|
||||
Table(Table),
|
||||
Memory(Memory),
|
||||
}
|
||||
|
||||
impl Extern {
|
||||
pub fn func(&self) -> Option<&HostRef<Func>> {
|
||||
pub fn func(&self) -> Option<&Func> {
|
||||
match self {
|
||||
Extern::Func(func) => Some(func),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn global(&self) -> Option<&HostRef<Global>> {
|
||||
pub fn global(&self) -> Option<&Global> {
|
||||
match self {
|
||||
Extern::Global(global) => Some(global),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn table(&self) -> Option<&HostRef<Table>> {
|
||||
pub fn table(&self) -> Option<&Table> {
|
||||
match self {
|
||||
Extern::Table(table) => Some(table),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
pub fn memory(&self) -> Option<&HostRef<Memory>> {
|
||||
pub fn memory(&self) -> Option<&Memory> {
|
||||
match self {
|
||||
Extern::Memory(memory) => Some(memory),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> ExternType {
|
||||
pub fn ty(&self) -> ExternType {
|
||||
match self {
|
||||
Extern::Func(ft) => ExternType::Func(ft.borrow().r#type().clone()),
|
||||
Extern::Memory(ft) => ExternType::Memory(ft.borrow().r#type().clone()),
|
||||
Extern::Table(tt) => ExternType::Table(tt.borrow().r#type().clone()),
|
||||
Extern::Global(gt) => ExternType::Global(gt.borrow().r#type().clone()),
|
||||
Extern::Func(ft) => ExternType::Func(ft.ty().clone()),
|
||||
Extern::Memory(ft) => ExternType::Memory(ft.ty().clone()),
|
||||
Extern::Table(tt) => ExternType::Table(tt.ty().clone()),
|
||||
Extern::Global(gt) => ExternType::Global(gt.ty().clone()),
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_wasmtime_export(&self) -> wasmtime_runtime::Export {
|
||||
match self {
|
||||
Extern::Func(f) => f.borrow().wasmtime_export().clone(),
|
||||
Extern::Global(g) => g.borrow().wasmtime_export().clone(),
|
||||
Extern::Memory(m) => m.borrow().wasmtime_export().clone(),
|
||||
Extern::Table(t) => t.borrow().wasmtime_export().clone(),
|
||||
Extern::Func(f) => f.wasmtime_export().clone(),
|
||||
Extern::Global(g) => g.wasmtime_export().clone(),
|
||||
Extern::Memory(m) => m.wasmtime_export().clone(),
|
||||
Extern::Table(t) => t.wasmtime_export().clone(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,50 +71,51 @@ impl Extern {
|
||||
export: wasmtime_runtime::Export,
|
||||
) -> Extern {
|
||||
match export {
|
||||
wasmtime_runtime::Export::Function { .. } => Extern::Func(HostRef::new(
|
||||
Func::from_wasmtime_function(export, store, instance_handle),
|
||||
)),
|
||||
wasmtime_runtime::Export::Memory { .. } => Extern::Memory(HostRef::new(
|
||||
Memory::from_wasmtime_memory(export, store, instance_handle),
|
||||
)),
|
||||
wasmtime_runtime::Export::Global { .. } => {
|
||||
Extern::Global(HostRef::new(Global::from_wasmtime_global(export, store)))
|
||||
wasmtime_runtime::Export::Function { .. } => {
|
||||
Extern::Func(Func::from_wasmtime_function(export, store, instance_handle))
|
||||
}
|
||||
wasmtime_runtime::Export::Memory { .. } => {
|
||||
Extern::Memory(Memory::from_wasmtime_memory(export, store, instance_handle))
|
||||
}
|
||||
wasmtime_runtime::Export::Global { .. } => {
|
||||
Extern::Global(Global::from_wasmtime_global(export, store))
|
||||
}
|
||||
wasmtime_runtime::Export::Table { .. } => {
|
||||
Extern::Table(Table::from_wasmtime_table(export, store, instance_handle))
|
||||
}
|
||||
wasmtime_runtime::Export::Table { .. } => Extern::Table(HostRef::new(
|
||||
Table::from_wasmtime_table(export, store, instance_handle),
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HostRef<Func>> for Extern {
|
||||
fn from(r: HostRef<Func>) -> Self {
|
||||
impl From<Func> for Extern {
|
||||
fn from(r: Func) -> Self {
|
||||
Extern::Func(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HostRef<Global>> for Extern {
|
||||
fn from(r: HostRef<Global>) -> Self {
|
||||
impl From<Global> for Extern {
|
||||
fn from(r: Global) -> Self {
|
||||
Extern::Global(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HostRef<Memory>> for Extern {
|
||||
fn from(r: HostRef<Memory>) -> Self {
|
||||
impl From<Memory> for Extern {
|
||||
fn from(r: Memory) -> Self {
|
||||
Extern::Memory(r)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HostRef<Table>> for Extern {
|
||||
fn from(r: HostRef<Table>) -> Self {
|
||||
impl From<Table> for Extern {
|
||||
fn from(r: Table) -> Self {
|
||||
Extern::Table(r)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Func {
|
||||
_store: Store,
|
||||
callable: Rc<dyn WrappedCallable + 'static>,
|
||||
r#type: FuncType,
|
||||
ty: FuncType,
|
||||
}
|
||||
|
||||
impl Func {
|
||||
@@ -125,26 +126,26 @@ impl Func {
|
||||
|
||||
fn from_wrapped(
|
||||
store: &Store,
|
||||
r#type: FuncType,
|
||||
ty: FuncType,
|
||||
callable: Rc<dyn WrappedCallable + 'static>,
|
||||
) -> Func {
|
||||
Func {
|
||||
_store: store.clone(),
|
||||
callable,
|
||||
r#type,
|
||||
ty,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> &FuncType {
|
||||
&self.r#type
|
||||
pub fn ty(&self) -> &FuncType {
|
||||
&self.ty
|
||||
}
|
||||
|
||||
pub fn param_arity(&self) -> usize {
|
||||
self.r#type.params().len()
|
||||
self.ty.params().len()
|
||||
}
|
||||
|
||||
pub fn result_arity(&self) -> usize {
|
||||
self.r#type.results().len()
|
||||
self.ty.results().len()
|
||||
}
|
||||
|
||||
pub fn call(&self, params: &[Val]) -> Result<Box<[Val]>, Trap> {
|
||||
@@ -178,32 +179,39 @@ impl fmt::Debug for Func {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Global {
|
||||
inner: Rc<GlobalInner>,
|
||||
}
|
||||
|
||||
struct GlobalInner {
|
||||
_store: Store,
|
||||
r#type: GlobalType,
|
||||
ty: GlobalType,
|
||||
wasmtime_export: wasmtime_runtime::Export,
|
||||
#[allow(dead_code)]
|
||||
wasmtime_state: Option<crate::trampoline::GlobalState>,
|
||||
}
|
||||
|
||||
impl Global {
|
||||
pub fn new(store: &Store, r#type: GlobalType, val: Val) -> Global {
|
||||
pub fn new(store: &Store, ty: GlobalType, val: Val) -> Global {
|
||||
let (wasmtime_export, wasmtime_state) =
|
||||
generate_global_export(&r#type, val).expect("generated global");
|
||||
generate_global_export(&ty, val).expect("generated global");
|
||||
Global {
|
||||
_store: store.clone(),
|
||||
r#type,
|
||||
wasmtime_export,
|
||||
wasmtime_state: Some(wasmtime_state),
|
||||
inner: Rc::new(GlobalInner {
|
||||
_store: store.clone(),
|
||||
ty,
|
||||
wasmtime_export,
|
||||
wasmtime_state: Some(wasmtime_state),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> &GlobalType {
|
||||
&self.r#type
|
||||
pub fn ty(&self) -> &GlobalType {
|
||||
&self.inner.ty
|
||||
}
|
||||
|
||||
fn wasmtime_global_definition(&self) -> *mut wasmtime_runtime::VMGlobalDefinition {
|
||||
match self.wasmtime_export {
|
||||
match self.inner.wasmtime_export {
|
||||
wasmtime_runtime::Export::Global { definition, .. } => definition,
|
||||
_ => panic!("global definition not found"),
|
||||
}
|
||||
@@ -212,22 +220,22 @@ impl Global {
|
||||
pub fn get(&self) -> Val {
|
||||
let definition = unsafe { &mut *self.wasmtime_global_definition() };
|
||||
unsafe {
|
||||
match self.r#type().content() {
|
||||
match self.ty().content() {
|
||||
ValType::I32 => Val::from(*definition.as_i32()),
|
||||
ValType::I64 => Val::from(*definition.as_i64()),
|
||||
ValType::F32 => Val::F32(*definition.as_u32()),
|
||||
ValType::F64 => Val::F64(*definition.as_u64()),
|
||||
_ => unimplemented!("Global::get for {:?}", self.r#type().content()),
|
||||
_ => unimplemented!("Global::get for {:?}", self.ty().content()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set(&mut self, val: Val) {
|
||||
if val.r#type() != *self.r#type().content() {
|
||||
pub fn set(&self, val: Val) {
|
||||
if val.ty() != *self.ty().content() {
|
||||
panic!(
|
||||
"global of type {:?} cannot be set to {:?}",
|
||||
self.r#type().content(),
|
||||
val.r#type()
|
||||
self.ty().content(),
|
||||
val.ty()
|
||||
);
|
||||
}
|
||||
let definition = unsafe { &mut *self.wasmtime_global_definition() };
|
||||
@@ -237,13 +245,13 @@ impl Global {
|
||||
Val::I64(i) => *definition.as_i64_mut() = i,
|
||||
Val::F32(f) => *definition.as_u32_mut() = f,
|
||||
Val::F64(f) => *definition.as_u64_mut() = f,
|
||||
_ => unimplemented!("Global::set for {:?}", val.r#type()),
|
||||
_ => unimplemented!("Global::set for {:?}", val.ty()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn wasmtime_export(&self) -> &wasmtime_runtime::Export {
|
||||
&self.wasmtime_export
|
||||
&self.inner.wasmtime_export
|
||||
}
|
||||
|
||||
pub(crate) fn from_wasmtime_global(export: wasmtime_runtime::Export, store: &Store) -> Global {
|
||||
@@ -254,17 +262,20 @@ impl Global {
|
||||
};
|
||||
let ty = GlobalType::from_wasmtime_global(&global);
|
||||
Global {
|
||||
_store: store.clone(),
|
||||
r#type: ty,
|
||||
wasmtime_export: export,
|
||||
wasmtime_state: None,
|
||||
inner: Rc::new(GlobalInner {
|
||||
_store: store.clone(),
|
||||
ty: ty,
|
||||
wasmtime_export: export,
|
||||
wasmtime_state: None,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Table {
|
||||
store: Store,
|
||||
r#type: TableType,
|
||||
ty: TableType,
|
||||
wasmtime_handle: InstanceHandle,
|
||||
wasmtime_export: wasmtime_runtime::Export,
|
||||
}
|
||||
@@ -299,13 +310,13 @@ fn set_table_item(
|
||||
}
|
||||
|
||||
impl Table {
|
||||
pub fn new(store: &Store, r#type: TableType, init: Val) -> Table {
|
||||
match r#type.element() {
|
||||
pub fn new(store: &Store, ty: TableType, init: Val) -> Table {
|
||||
match ty.element() {
|
||||
ValType::FuncRef => (),
|
||||
_ => panic!("table is not for funcref"),
|
||||
}
|
||||
let (mut wasmtime_handle, wasmtime_export) =
|
||||
generate_table_export(&r#type).expect("generated table");
|
||||
generate_table_export(&ty).expect("generated table");
|
||||
|
||||
// Initialize entries with the init value.
|
||||
match wasmtime_export {
|
||||
@@ -323,14 +334,14 @@ impl Table {
|
||||
|
||||
Table {
|
||||
store: store.clone(),
|
||||
r#type,
|
||||
ty,
|
||||
wasmtime_handle,
|
||||
wasmtime_export,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> &TableType {
|
||||
&self.r#type
|
||||
pub fn ty(&self) -> &TableType {
|
||||
&self.ty
|
||||
}
|
||||
|
||||
fn wasmtime_table_index(&self) -> wasm::DefinedTableIndex {
|
||||
@@ -362,9 +373,9 @@ impl Table {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, delta: u32, init: Val) -> bool {
|
||||
pub fn grow(&self, delta: u32, init: Val) -> bool {
|
||||
let index = self.wasmtime_table_index();
|
||||
if let Some(len) = self.wasmtime_handle.table_grow(index, delta) {
|
||||
if let Some(len) = self.wasmtime_handle.clone().table_grow(index, delta) {
|
||||
let mut wasmtime_handle = self.wasmtime_handle.clone();
|
||||
for i in 0..delta {
|
||||
let i = len - (delta - i);
|
||||
@@ -395,34 +406,35 @@ impl Table {
|
||||
let ty = TableType::from_wasmtime_table(&table.table);
|
||||
Table {
|
||||
store: store.clone(),
|
||||
r#type: ty,
|
||||
ty: ty,
|
||||
wasmtime_handle: instance_handle,
|
||||
wasmtime_export: export,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Memory {
|
||||
_store: Store,
|
||||
r#type: MemoryType,
|
||||
ty: MemoryType,
|
||||
wasmtime_handle: InstanceHandle,
|
||||
wasmtime_export: wasmtime_runtime::Export,
|
||||
}
|
||||
|
||||
impl Memory {
|
||||
pub fn new(store: &Store, r#type: MemoryType) -> Memory {
|
||||
pub fn new(store: &Store, ty: MemoryType) -> Memory {
|
||||
let (wasmtime_handle, wasmtime_export) =
|
||||
generate_memory_export(&r#type).expect("generated memory");
|
||||
generate_memory_export(&ty).expect("generated memory");
|
||||
Memory {
|
||||
_store: store.clone(),
|
||||
r#type,
|
||||
ty,
|
||||
wasmtime_handle,
|
||||
wasmtime_export,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn r#type(&self) -> &MemoryType {
|
||||
&self.r#type
|
||||
pub fn ty(&self) -> &MemoryType {
|
||||
&self.ty
|
||||
}
|
||||
|
||||
fn wasmtime_memory_definition(&self) -> *mut wasmtime_runtime::VMMemoryDefinition {
|
||||
@@ -453,12 +465,15 @@ impl Memory {
|
||||
(self.data_size() / wasmtime_environ::WASM_PAGE_SIZE as usize) as u32
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, delta: u32) -> bool {
|
||||
pub fn grow(&self, delta: u32) -> bool {
|
||||
match self.wasmtime_export {
|
||||
wasmtime_runtime::Export::Memory { definition, .. } => {
|
||||
let definition = unsafe { &(*definition) };
|
||||
let index = self.wasmtime_handle.memory_index(definition);
|
||||
self.wasmtime_handle.memory_grow(index, delta).is_some()
|
||||
self.wasmtime_handle
|
||||
.clone()
|
||||
.memory_grow(index, delta)
|
||||
.is_some()
|
||||
}
|
||||
_ => panic!("memory definition not found"),
|
||||
}
|
||||
@@ -481,7 +496,7 @@ impl Memory {
|
||||
let ty = MemoryType::from_wasmtime_memory(&memory.memory);
|
||||
Memory {
|
||||
_store: store.clone(),
|
||||
r#type: ty,
|
||||
ty: ty,
|
||||
wasmtime_handle: instance_handle,
|
||||
wasmtime_export: export,
|
||||
}
|
||||
|
||||
@@ -167,29 +167,29 @@ cfg_if::cfg_if! {
|
||||
impl Instance {
|
||||
/// The signal handler must be
|
||||
/// [async-signal-safe](http://man7.org/linux/man-pages/man7/signal-safety.7.html).
|
||||
pub fn set_signal_handler<H>(&mut self, handler: H)
|
||||
pub fn set_signal_handler<H>(&self, handler: H)
|
||||
where
|
||||
H: 'static + Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool,
|
||||
{
|
||||
self.instance_handle.set_signal_handler(handler);
|
||||
self.instance_handle.clone().set_signal_handler(handler);
|
||||
}
|
||||
}
|
||||
} else if #[cfg(target_os = "windows")] {
|
||||
impl Instance {
|
||||
pub fn set_signal_handler<H>(&mut self, handler: H)
|
||||
pub fn set_signal_handler<H>(&self, handler: H)
|
||||
where
|
||||
H: 'static + Fn(winapi::um::winnt::EXCEPTION_POINTERS) -> bool,
|
||||
{
|
||||
self.instance_handle.set_signal_handler(handler);
|
||||
self.instance_handle.clone().set_signal_handler(handler);
|
||||
}
|
||||
}
|
||||
} else if #[cfg(target_os = "macos")] {
|
||||
impl Instance {
|
||||
pub fn set_signal_handler<H>(&mut self, handler: H)
|
||||
pub fn set_signal_handler<H>(&self, handler: H)
|
||||
where
|
||||
H: 'static + Fn(libc::c_int, *const libc::siginfo_t, *const libc::c_void) -> bool,
|
||||
{
|
||||
self.instance_handle.set_signal_handler(handler);
|
||||
self.instance_handle.clone().set_signal_handler(handler);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::externals::Func;
|
||||
use crate::r#ref::{AnyRef, HostRef};
|
||||
use crate::r#ref::AnyRef;
|
||||
use crate::runtime::Store;
|
||||
use crate::types::ValType;
|
||||
use std::ptr;
|
||||
@@ -34,7 +34,7 @@ pub enum Val {
|
||||
AnyRef(AnyRef),
|
||||
|
||||
/// A first-class reference to a WebAssembly function.
|
||||
FuncRef(HostRef<Func>),
|
||||
FuncRef(Func),
|
||||
|
||||
/// A 128-bit number
|
||||
V128(u128),
|
||||
@@ -71,7 +71,7 @@ impl Val {
|
||||
}
|
||||
|
||||
/// Returns the corresponding [`ValType`] for this `Val`.
|
||||
pub fn r#type(&self) -> ValType {
|
||||
pub fn ty(&self) -> ValType {
|
||||
match self {
|
||||
Val::I32(_) => ValType::I32,
|
||||
Val::I64(_) => ValType::I64,
|
||||
@@ -111,7 +111,7 @@ impl Val {
|
||||
(I64(i64) i64 unwrap_i64 *e)
|
||||
(F32(f32) f32 unwrap_f32 f32::from_bits(*e))
|
||||
(F64(f64) f64 unwrap_f64 f64::from_bits(*e))
|
||||
(FuncRef(&HostRef<Func>) funcref unwrap_funcref e)
|
||||
(FuncRef(&Func) funcref unwrap_funcref e)
|
||||
(V128(u128) v128 unwrap_v128 *e)
|
||||
}
|
||||
|
||||
@@ -122,7 +122,6 @@ impl Val {
|
||||
pub fn anyref(&self) -> Option<AnyRef> {
|
||||
match self {
|
||||
Val::AnyRef(e) => Some(e.clone()),
|
||||
Val::FuncRef(e) => Some(e.anyref()),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
@@ -164,21 +163,12 @@ impl From<f64> for Val {
|
||||
|
||||
impl From<AnyRef> for Val {
|
||||
fn from(val: AnyRef) -> Val {
|
||||
match &val {
|
||||
AnyRef::Ref(r) => {
|
||||
if r.is_ref::<Func>() {
|
||||
Val::FuncRef(r.get_ref())
|
||||
} else {
|
||||
Val::AnyRef(val)
|
||||
}
|
||||
}
|
||||
_ => unimplemented!("AnyRef::Other"),
|
||||
}
|
||||
Val::AnyRef(val)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HostRef<Func>> for Val {
|
||||
fn from(val: HostRef<Func>) -> Val {
|
||||
impl From<Func> for Val {
|
||||
fn from(val: Func) -> Val {
|
||||
Val::FuncRef(val)
|
||||
}
|
||||
}
|
||||
@@ -206,7 +196,6 @@ pub(crate) fn into_checked_anyfunc(
|
||||
vmctx: ptr::null_mut(),
|
||||
},
|
||||
Val::FuncRef(f) => {
|
||||
let f = f.borrow();
|
||||
let (vmctx, func_ptr, signature) = match f.wasmtime_export() {
|
||||
wasmtime_runtime::Export::Function {
|
||||
vmctx,
|
||||
@@ -243,5 +232,5 @@ pub(crate) fn from_checked_anyfunc(
|
||||
vmctx: item.vmctx,
|
||||
};
|
||||
let f = Func::from_wasmtime_function(export, store, instance_handle);
|
||||
Val::FuncRef(HostRef::new(f))
|
||||
Val::FuncRef(f)
|
||||
}
|
||||
|
||||
@@ -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