2022-07-18 08:16:47 +00:00
|
|
|
use std::marker::PhantomData;
|
2022-08-20 10:22:12 +00:00
|
|
|
use std::mem::transmute;
|
2022-08-27 16:04:56 +00:00
|
|
|
use std::sync::Arc;
|
2022-07-18 08:16:47 +00:00
|
|
|
|
2022-07-18 13:36:03 +00:00
|
|
|
use crate::script_implementations::wasm::script_resolver::{
|
|
|
|
WebAssemblyEnv, WebAssemblyEnvironmentData, WebAssemblyScriptResolver,
|
|
|
|
};
|
2022-08-27 12:14:58 +00:00
|
|
|
use unique_type_id::UniqueTypeId;
|
|
|
|
use wasmer::FromToNativeWasmType;
|
|
|
|
use wasmer::FunctionEnvMut;
|
2022-07-18 08:16:47 +00:00
|
|
|
|
2022-08-26 16:23:35 +00:00
|
|
|
/// An Extern Ref allows us to pass objects to WASM without actually passing raw memory, or
|
|
|
|
/// requiring us to make copies. Instead, we pass a simple increment index, that we can then use
|
|
|
|
/// to find the relevant data.
|
2022-08-27 16:04:56 +00:00
|
|
|
#[derive(Copy, Clone)]
|
2022-08-20 10:22:12 +00:00
|
|
|
pub(crate) struct ExternRef<T: UniqueTypeId<u64>> {
|
2022-08-26 16:23:35 +00:00
|
|
|
/// The lookup index we can use to find the data.
|
2022-07-18 08:16:47 +00:00
|
|
|
index: u32,
|
2022-08-26 16:23:35 +00:00
|
|
|
/// Phantom data so we can get a type generic.
|
2022-07-18 08:16:47 +00:00
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: UniqueTypeId<u64>> ExternRef<T> {
|
2022-08-26 16:23:35 +00:00
|
|
|
/// Instantiates a new ExternRef for a bit of data. If we already have made an Extern Ref for
|
|
|
|
/// this data and type, we use that instead.
|
2022-07-18 13:36:03 +00:00
|
|
|
pub fn new(env: &WebAssemblyEnvironmentData, value: &T) -> Self {
|
2022-07-18 08:16:47 +00:00
|
|
|
Self {
|
2022-07-18 13:36:03 +00:00
|
|
|
index: env.get_extern_ref_index(value),
|
2022-07-18 08:16:47 +00:00
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-27 12:14:58 +00:00
|
|
|
/// Instantiates a new ExternRef for a bit of data using the function environment. If we already
|
|
|
|
/// have made an Extern Ref for this data and type, we use that instead.
|
|
|
|
pub fn func_new(env: &FunctionEnvMut<WebAssemblyEnv>, value: &T) -> Self {
|
|
|
|
Self {
|
|
|
|
index: env.data().data().get_extern_ref_index(value),
|
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:18:11 +00:00
|
|
|
/// Creates an ExternRef with a given resolver. This can be used in cases where we do not have an environment variable.
|
|
|
|
pub(crate) fn new_with_resolver(resolver: &WebAssemblyScriptResolver, value: &T) -> Self {
|
2022-07-18 08:16:47 +00:00
|
|
|
Self {
|
2022-07-18 11:18:11 +00:00
|
|
|
index: resolver.environment_data().get_extern_ref_index(value),
|
2022-07-18 08:16:47 +00:00
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:18:11 +00:00
|
|
|
/// An empty value ExternRef.
|
2022-07-18 08:16:47 +00:00
|
|
|
pub fn null() -> Self {
|
|
|
|
Self {
|
|
|
|
index: 0,
|
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-18 11:18:11 +00:00
|
|
|
/// Returns the real value for a given ExternRef. Note that the requested type must be the same as the type of the
|
|
|
|
/// value when it was passed before. If these types do not match, this will panic.
|
2022-08-27 16:04:56 +00:00
|
|
|
pub fn value_func<'a, 'b>(&'a self, env: &'b FunctionEnvMut<WebAssemblyEnv>) -> Option<&'b T> {
|
|
|
|
self.value(&env.data().data())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the real value for a given ExternRef. Note that the requested type must be the same as the type of the
|
|
|
|
/// value when it was passed before. If these types do not match, this will panic.
|
|
|
|
pub fn value<'a, 'b, 'c>(&'a self, env: &'b Arc<WebAssemblyEnvironmentData>) -> Option<&'c T> {
|
|
|
|
let ptr = env.get_extern_ref_value(self.index) as *const T;
|
2022-07-18 11:18:11 +00:00
|
|
|
unsafe { ptr.as_ref() }
|
2022-07-18 08:16:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: UniqueTypeId<u64>> FromToNativeWasmType for ExternRef<T> {
|
|
|
|
type Native = i32;
|
|
|
|
|
|
|
|
fn from_native(native: Self::Native) -> Self {
|
|
|
|
Self {
|
|
|
|
index: native as u32,
|
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_native(self) -> Self::Native {
|
|
|
|
self.index as i32
|
|
|
|
}
|
|
|
|
}
|
2022-08-20 10:22:12 +00:00
|
|
|
|
2022-08-26 16:23:35 +00:00
|
|
|
/// A VecExternRef is an Extern Ref for vector types. This allows us to pass Vecs to WASM without
|
|
|
|
/// actually passing raw memory, or requiring us to make copies.
|
2022-08-20 10:22:12 +00:00
|
|
|
pub(crate) struct VecExternRef<T> {
|
2022-08-26 16:23:35 +00:00
|
|
|
/// The lookup index we can use to find the data.
|
2022-08-20 10:22:12 +00:00
|
|
|
index: u32,
|
2022-08-26 16:23:35 +00:00
|
|
|
/// The number of items in the vec.
|
2022-08-20 10:22:12 +00:00
|
|
|
size: u32,
|
2022-08-26 16:23:35 +00:00
|
|
|
/// Phantom data so we can get a type generic.
|
2022-08-20 10:22:12 +00:00
|
|
|
_phantom: PhantomData<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: UniqueTypeId<u64>> VecExternRef<T> {
|
2022-08-26 16:23:35 +00:00
|
|
|
/// Instantiates a new VecExternRef for a given slice.
|
2022-08-20 10:22:12 +00:00
|
|
|
pub fn new(env: &WebAssemblyEnvironmentData, value: &[T]) -> Self {
|
|
|
|
Self {
|
|
|
|
index: env.get_extern_vec_ref_index(value),
|
|
|
|
size: value.len() as u32,
|
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl<T: UniqueTypeId<u64>> FromToNativeWasmType for VecExternRef<T> {
|
|
|
|
type Native = i64;
|
|
|
|
|
|
|
|
fn from_native(native: Self::Native) -> Self {
|
|
|
|
let split: (u32, u32) = unsafe { transmute(native) };
|
|
|
|
Self {
|
|
|
|
index: split.0,
|
|
|
|
size: split.1,
|
|
|
|
_phantom: Default::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_native(self) -> Self::Native {
|
|
|
|
let v: i64 = unsafe { transmute((self.index, self.size)) };
|
|
|
|
v
|
|
|
|
}
|
|
|
|
}
|