1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use crate::{
    error::CreationError,
    instance::DynFunc,
    sig_registry::SigRegistry,
    structures::TypedIndex,
    types::{FuncSig, TableDescriptor},
    vm,
};

use std::convert::TryFrom;
use std::{ptr, sync::Arc};

enum AnyfuncInner<'a> {
    Host {
        ptr: *const vm::Func,
        signature: Arc<FuncSig>,
    },
    Managed(DynFunc<'a>),
}

/// Anyfunc data type.
pub struct Anyfunc<'a> {
    inner: AnyfuncInner<'a>,
}

impl<'a> Anyfunc<'a> {
    /// Create a new `Anyfunc`.
    pub unsafe fn new<Sig>(func: *const vm::Func, signature: Sig) -> Self
    where
        Sig: Into<Arc<FuncSig>>,
    {
        Self {
            inner: AnyfuncInner::Host {
                ptr: func as _,
                signature: signature.into(),
            },
        }
    }
}

impl<'a> From<DynFunc<'a>> for Anyfunc<'a> {
    fn from(function: DynFunc<'a>) -> Self {
        Anyfunc {
            inner: AnyfuncInner::Managed(function),
        }
    }
}

impl<'a> TryFrom<Anyfunc<'a>> for DynFunc<'a> {
    type Error = ();

    fn try_from(anyfunc: Anyfunc<'a>) -> Result<Self, Self::Error> {
        match anyfunc.inner {
            AnyfuncInner::Managed(df) => Ok(df),
            _ => Err(()),
        }
    }
}

/*
// TODO: implement this when `vm::Anyfunc` is updated (aka avoiding the linear scan in `wrap`)
impl<'a, Args: WasmTypeList, Rets: WasmTypeList> TryFrom<Anyfunc<'a>> for Func<'a, Args, Rets> {
    type Error = ();

    fn try_from(anyfunc: Anyfunc<'a>) -> Result<Self, Self::Error> {
        match anyfunc.inner {
            AnyfuncInner::Host {
                ptr,
                ctx,
                signature,
            } => {
                // TODO: return more specific error
                let ptr = NonNull::new(ptr as _).ok_or(())?;
                if signature.params() != Args::types() || signature.returns() != Rets::types() {
                    // TODO: return more specific error
                    return Err(());
                }
                let wasm = todo!("Figure out how to get typed_func::Wasm");
                // TODO: handle func_env
                let func_env = None;
                Ok(unsafe { Func::from_raw_parts(wasm, ptr, func_env, ctx) })
            }
            _ => Err(()),
        }
    }
}
*/

pub struct AnyfuncTable {
    pub(crate) backing: Vec<vm::Anyfunc>,
    max: Option<u32>,
}

impl AnyfuncTable {
    pub fn new(
        desc: TableDescriptor,
        local: &mut vm::LocalTable,
    ) -> Result<Box<Self>, CreationError> {
        let initial_table_backing_len = desc.minimum as usize;

        let mut storage = Box::new(AnyfuncTable {
            backing: vec![vm::Anyfunc::null(); initial_table_backing_len],
            max: desc.maximum,
        });

        let storage_ptr: *mut AnyfuncTable = &mut *storage;

        local.base = storage.backing.as_mut_ptr() as *mut u8;
        local.count = storage.backing.len();
        local.table = storage_ptr as *mut ();

        Ok(storage)
    }

    pub fn current_size(&self) -> u32 {
        self.backing.len() as u32
    }

    pub fn internal_buffer(&mut self) -> &mut [vm::Anyfunc] {
        &mut self.backing
    }

    pub fn grow(&mut self, delta: u32, local: &mut vm::LocalTable) -> Option<u32> {
        let starting_len = self.backing.len() as u32;

        let new_len = starting_len.checked_add(delta)?;

        if let Some(max) = self.max {
            if new_len > max {
                return None;
            }
        }

        self.backing.resize(new_len as usize, vm::Anyfunc::null());

        local.base = self.backing.as_mut_ptr() as *mut u8;
        local.count = self.backing.len();

        Some(starting_len)
    }

    // hidden and `pub(crate)` due to incomplete implementation (blocked on `wrap` issue)
    #[doc(hidden)]
    /// Get The vm::AnyFunc at the given index.
    pub(crate) fn get<'outer_table>(&self, index: u32) -> Option<Anyfunc<'outer_table>> {
        let vm_any_func = self.backing.get(index as usize)?;
        let signature = SigRegistry.lookup_signature(vm_any_func.sig_id.into());
        // TODO: this function should take a generic type param indicating what type of
        // anyfunc we want `host` or `managed` (or perhaps we should just return DynFunc/Func directly here).
        //
        // The issue with the current implementation is that through `StorableInTable`, we'll call
        // `TryFrom<Anyfuc> for Dynfunc` which will always fail because we always return a `Host` function here.
        Some(Anyfunc {
            inner: AnyfuncInner::Host {
                ptr: vm_any_func.func,
                signature,
            },
        })
    }

    pub fn set(&mut self, index: u32, element: Anyfunc) -> Result<(), ()> {
        if let Some(slot) = self.backing.get_mut(index as usize) {
            let anyfunc = match element.inner {
                AnyfuncInner::Host { ptr, signature } => {
                    let sig_index = SigRegistry.lookup_sig_index(signature);
                    let sig_id = vm::SigId(sig_index.index() as u32);

                    vm::Anyfunc {
                        func: ptr,
                        ctx: ptr::null_mut(),
                        sig_id,
                    }
                }
                AnyfuncInner::Managed(ref func) => {
                    let sig_index = SigRegistry.lookup_sig_index(Arc::clone(&func.signature));
                    let sig_id = vm::SigId(sig_index.index() as u32);

                    vm::Anyfunc {
                        func: func.raw(),
                        ctx: func.instance_inner.vmctx,
                        sig_id,
                    }
                }
            };

            *slot = anyfunc;

            Ok(())
        } else {
            Err(())
        }
    }
}