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
use derivative::Derivative;
use std::any::Any;
use wasmer_types::RawValue;

use crate::store::InternalStoreHandle;

/// Underlying object referenced by a `VMExternRef`.
#[derive(Derivative)]
#[derivative(Debug)]
pub struct VMExternObj {
    #[derivative(Debug = "ignore")]
    contents: Box<dyn Any + Send + Sync + 'static>,
}

impl VMExternObj {
    /// Wraps the given value to expose it to Wasm code as an externref.
    pub fn new(val: impl Any + Send + Sync + 'static) -> Self {
        Self {
            contents: Box::new(val),
        }
    }

    #[allow(clippy::should_implement_trait)]
    /// Returns a reference to the underlying value.
    pub fn as_ref(&self) -> &(dyn Any + Send + Sync + 'static) {
        &*self.contents
    }
}

/// Represents an opaque reference to any data within WebAssembly.
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct VMExternRef(pub InternalStoreHandle<VMExternObj>);

impl VMExternRef {
    /// Converts the `VMExternRef` into a `RawValue`.
    pub fn into_raw(self) -> RawValue {
        RawValue {
            funcref: self.0.index(),
        }
    }

    /// Extracts a `VMExternRef` from a `RawValue`.
    ///
    /// # Safety
    /// `raw` must be a valid `VMExternRef` instance.
    pub unsafe fn from_raw(raw: RawValue) -> Option<Self> {
        InternalStoreHandle::from_index(raw.externref).map(Self)
    }
}