use crate::store::AsStoreRef;
use rusty_jsc::{JSObject, JSObjectCallAsFunctionCallback, JSValue};
use std::any::Any;
use std::fmt;
#[cfg(feature = "tracing")]
use tracing::trace;
use wasmer_types::RawValue;
use wasmer_types::{
FunctionType, GlobalType, MemoryError, MemoryType, Pages, TableType, WASM_PAGE_SIZE,
};
#[derive(Clone, Debug, PartialEq)]
pub struct VMMemory {
pub(crate) memory: JSObject,
pub(crate) ty: MemoryType,
}
unsafe impl Send for VMMemory {}
unsafe impl Sync for VMMemory {}
impl VMMemory {
pub fn new(memory: JSObject, ty: MemoryType) -> Self {
Self { memory, ty }
}
pub fn get_runtime_size(&self) -> u32 {
unimplemented!();
}
pub(crate) fn try_clone(&self) -> Result<VMMemory, MemoryError> {
Ok(self.clone())
}
#[deprecated = "use `copy` instead"]
pub fn duplicate(
&mut self,
store: &impl AsStoreRef,
) -> Result<VMMemory, wasmer_types::MemoryError> {
self.copy(store)
}
pub fn copy(&self, store: &impl AsStoreRef) -> Result<VMMemory, wasmer_types::MemoryError> {
let new_memory =
crate::jsc::externals::memory::Memory::js_memory_from_type(&store, &self.ty)?;
#[cfg(feature = "tracing")]
trace!("memory copy started");
let src = crate::jsc::externals::memory_view::MemoryView::new_raw(&self.memory, store);
let amount = src.data_size() as usize;
let mut dst = crate::jsc::externals::memory_view::MemoryView::new_raw(&new_memory, store);
let dst_size = dst.data_size() as usize;
src.copy_to_memory(amount as u64, &dst).map_err(|err| {
wasmer_types::MemoryError::Generic(format!("failed to copy the memory - {}", err))
})?;
#[cfg(feature = "tracing")]
trace!("memory copy finished (size={})", dst.size().bytes().0);
Ok(Self {
memory: new_memory,
ty: self.ty.clone(),
})
}
}
pub type VMSharedMemory = VMMemory;
#[derive(Clone, Debug, PartialEq)]
pub struct VMGlobal {
pub(crate) global: JSObject,
pub(crate) ty: GlobalType,
}
impl VMGlobal {
pub(crate) fn new(global: JSObject, ty: GlobalType) -> Self {
Self { global, ty }
}
}
unsafe impl Send for VMGlobal {}
unsafe impl Sync for VMGlobal {}
#[derive(Clone, Debug, PartialEq)]
pub struct VMTable {
pub(crate) table: JSObject,
pub(crate) ty: TableType,
}
unsafe impl Send for VMTable {}
unsafe impl Sync for VMTable {}
impl VMTable {
pub(crate) fn new(table: JSObject, ty: TableType) -> Self {
Self { table, ty }
}
pub fn get_runtime_size(&self) -> u32 {
unimplemented!();
}
}
#[derive(Clone)]
pub struct VMFunction {
pub(crate) function: JSObject,
pub(crate) ty: FunctionType,
}
unsafe impl Send for VMFunction {}
unsafe impl Sync for VMFunction {}
impl VMFunction {
pub(crate) fn new(function: JSObject, ty: FunctionType) -> Self {
Self { function, ty }
}
}
impl PartialEq for VMFunction {
fn eq(&self, other: &Self) -> bool {
self.function == other.function
}
}
impl fmt::Debug for VMFunction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("VMFunction")
.field("function", &self.function)
.finish()
}
}
pub enum VMExtern {
Function(VMFunction),
Table(VMTable),
Memory(VMMemory),
Global(VMGlobal),
}
pub type VMInstance = JSObject;
#[derive(Debug)]
pub struct VMFunctionEnvironment {
contents: Box<dyn Any + Send + 'static>,
}
impl VMFunctionEnvironment {
pub fn new(val: impl Any + Send + 'static) -> Self {
Self {
contents: Box::new(val),
}
}
#[allow(clippy::should_implement_trait)]
pub fn as_ref(&self) -> &(dyn Any + Send + 'static) {
&*self.contents
}
#[allow(clippy::should_implement_trait)]
pub fn as_mut(&mut self) -> &mut (dyn Any + Send + 'static) {
&mut *self.contents
}
}
pub(crate) struct VMExternRef;
impl VMExternRef {
pub fn into_raw(self) -> RawValue {
unimplemented!();
}
pub unsafe fn from_raw(_raw: RawValue) -> Option<Self> {
unimplemented!();
}
}
#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub(crate) struct VMFuncRef;
impl VMFuncRef {
pub fn into_raw(self) -> RawValue {
unimplemented!();
}
pub unsafe fn from_raw(_raw: RawValue) -> Option<Self> {
unimplemented!();
}
}
pub struct VMTrampoline;
pub(crate) type VMExternTable = VMTable;
pub(crate) type VMExternMemory = VMMemory;
pub(crate) type VMExternGlobal = VMGlobal;
pub(crate) type VMExternFunction = VMFunction;
pub type VMFunctionCallback = JSObjectCallAsFunctionCallback;