use crate::app_interface::{DataLibrary, MoveData}; use alloc::rc::Rc; pub trait MoveLibraryTrait: DataLibrary {} pub type MoveLibrary = Rc; #[cfg(not(feature = "mock_data"))] mod implementation { use super::*; use crate::app_interface::{MoveDataImpl, StringKey}; use crate::handling::extern_ref::{ExternRef, ExternalReferenceType}; use spin::RwLock; struct MoveLibraryInner { ptr: ExternRef, cache: RwLock>, } #[derive(Clone)] pub struct MoveLibraryImpl { inner: Rc, } impl MoveLibraryImpl { #[cfg(not(feature = "mock_data"))] pub(crate) fn new(ptr: ExternRef) -> Self { Self { inner: Rc::new(MoveLibraryInner { ptr, cache: Default::default(), }), } } #[cfg(feature = "mock_data")] pub fn mock() -> Self { Self { inner: Rc::new(MoveLibraryInner { ptr: ExternRef::mock(), cache: Default::default(), }), } } } impl MoveLibraryTrait for MoveLibraryImpl {} impl DataLibrary for MoveLibraryImpl { fn get_cache(&self) -> &spin::rwlock::RwLock> { &self.inner.cache } fn get_self_ref(&self) -> ExternRef { self.inner.ptr } #[cfg(not(feature = "mock_data"))] fn _get_ref_by_name(ptr: ExternRef, name: ExternRef) -> u32 { unsafe { move_library_get_move(ptr, name).get_internal_index() } } #[cfg(not(feature = "mock_data"))] fn _get_ref_by_hash(ptr: ExternRef, hash: u32) -> u32 { unsafe { move_library_get_move_by_hash(ptr, hash).get_internal_index() } } fn _from_external_ref_index(index: u32) -> Option { let v = ExternRef::::from(index).get_value(); if let Some(v) = v { Some(Rc::new(v)) } else { None } } } crate::handling::cacheable::cacheable!(MoveLibraryImpl); #[cfg(not(feature = "mock_data"))] impl ExternalReferenceType for MoveLibraryImpl { fn from_extern_value(reference: ExternRef) -> Self { Self::new(reference) } } #[cfg(not(feature = "mock_data"))] extern "wasm" { fn move_library_get_move( ptr: ExternRef, name: ExternRef, ) -> ExternRef; fn move_library_get_move_by_hash( ptr: ExternRef, hash: u32, ) -> ExternRef; } } #[cfg(not(feature = "mock_data"))] pub use implementation::*;