document and restrict the public interface

This commit is contained in:
Christof Petig 2023-06-24 16:05:35 +02:00
parent c9ff9e9841
commit 08d84291ab
3 changed files with 21 additions and 17 deletions

View File

@ -28,7 +28,7 @@ fn print_impl(
.map_err(|error| { .map_err(|error| {
tracing::error!( tracing::error!(
"Logging message from plugin {} failed with {:?}!", "Logging message from plugin {} failed with {:?}!",
env.name, env.name(),
error error
); );
wasmer_wasix_types::wasi::Errno::Memviolation wasmer_wasix_types::wasi::Errno::Memviolation
@ -38,12 +38,12 @@ fn print_impl(
.map_err(|error| { .map_err(|error| {
tracing::error!( tracing::error!(
"Logging message from plugin {} failed with {}!", "Logging message from plugin {} failed with {}!",
env.name, env.name(),
error error
); );
wasmer_wasix_types::wasi::Errno::Inval wasmer_wasix_types::wasi::Errno::Inval
}) })
.map(|msg| tracing::info!("[{}]: {}", env.name, msg)) .map(|msg| tracing::info!("[{}]: {}", env.name(), msg))
}) })
} }

View File

@ -65,7 +65,7 @@ impl PluginModule {
len: <MemoryModel as wasmer::MemorySize>::Offset, len: <MemoryModel as wasmer::MemorySize>::Offset,
) -> <MemoryModel as wasmer::MemorySize>::Offset { ) -> <MemoryModel as wasmer::MemorySize>::Offset {
let out = match env.data().read_serialized(&env.as_store_ref(), ptr, len) { let out = match env.data().read_serialized(&env.as_store_ref(), ptr, len) {
Ok(data) => retrieve_action(&env.data().ecs, data), Ok(data) => retrieve_action(&env.data().ecs(), data),
Err(e) => Err(RetrieveError::BincodeError(e.to_string())), Err(e) => Err(RetrieveError::BincodeError(e.to_string())),
}; };

View File

@ -11,14 +11,14 @@ use super::{
#[derive(Clone)] #[derive(Clone)]
pub struct HostFunctionEnvironment { pub struct HostFunctionEnvironment {
pub ecs: Arc<EcsAccessManager>, /* This represent the pointer to the ECS object (set to ecs: Arc<EcsAccessManager>, /* This represent the pointer to the ECS object (set to
* i32::MAX if to ECS is * i32::MAX if to ECS is
* availible) */ * availible) */
pub memory: Option<Memory>, // This object represent the WASM Memory memory: Option<Memory>, // This object represent the WASM Memory
pub allocator: Option< allocator: Option<
TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>, TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>>,
>, /* Linked to: wasm_prepare_buffer */ >, /* Linked to: wasm_prepare_buffer */
pub name: String, // This represent the plugin name name: String, // This represent the plugin name
} }
pub struct HostFunctionEnvironmentInit { pub struct HostFunctionEnvironmentInit {
@ -40,6 +40,7 @@ impl core::fmt::Display for HostFunctionException {
impl std::error::Error for HostFunctionException {} impl std::error::Error for HostFunctionException {}
impl HostFunctionEnvironment { impl HostFunctionEnvironment {
/// Create a new environment for functions providing functionality to WASM
pub fn new(name: String, ecs: Arc<EcsAccessManager>) -> Self { pub fn new(name: String, ecs: Arc<EcsAccessManager>) -> Self {
Self { Self {
ecs, ecs,
@ -50,24 +51,24 @@ impl HostFunctionEnvironment {
} }
#[inline] #[inline]
pub fn ecs(&self) -> &Arc<EcsAccessManager> { &self.ecs } pub(crate) fn ecs(&self) -> &Arc<EcsAccessManager> { &self.ecs }
#[inline] #[inline]
pub fn memory(&self) -> &Memory { self.memory.as_ref().unwrap() } pub(crate) fn memory(&self) -> &Memory { self.memory.as_ref().unwrap() }
#[inline] #[inline]
pub fn allocator( pub(crate) fn allocator(
&self, &self,
) -> &TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>> { ) -> &TypedFunction<<MemoryModel as wasmer::MemorySize>::Offset, WasmPtr<u8, MemoryModel>> {
self.allocator.as_ref().unwrap() self.allocator.as_ref().unwrap()
} }
#[inline] #[inline]
pub fn name(&self) -> &str { &self.name } pub(crate) fn name(&self) -> &str { &self.name }
/// This function is a safe interface to WASM memory that serializes and /// This function is a safe interface to WASM memory that serializes and
/// writes an object to linear memory returning a pointer /// writes an object to linear memory returning a pointer
pub fn write_serialized_with_length<T: Serialize>( pub(crate) fn write_serialized_with_length<T: Serialize>(
&self, &self,
store: &mut StoreMut, store: &mut StoreMut,
object: &T, object: &T,
@ -77,7 +78,7 @@ impl HostFunctionEnvironment {
/// This function is a safe interface to WASM memory that reads memory from /// This function is a safe interface to WASM memory that reads memory from
/// pointer and length returning an object /// pointer and length returning an object
pub fn read_serialized<T: DeserializeOwned>( pub(crate) fn read_serialized<T: DeserializeOwned>(
&self, &self,
store: &StoreRef, store: &StoreRef,
position: WasmPtr<u8, MemoryModel>, position: WasmPtr<u8, MemoryModel>,
@ -88,7 +89,7 @@ impl HostFunctionEnvironment {
/// This function is a safe interface to WASM memory that reads memory from /// This function is a safe interface to WASM memory that reads memory from
/// a pointer and a length and returns some bytes /// a pointer and a length and returns some bytes
pub fn read_bytes( pub(crate) fn read_bytes(
&self, &self,
store: &StoreRef, store: &StoreRef,
ptr: WasmPtr<u8, MemoryModel>, ptr: WasmPtr<u8, MemoryModel>,
@ -97,6 +98,8 @@ impl HostFunctionEnvironment {
memory_manager::read_bytes(self.memory(), store, ptr, len) memory_manager::read_bytes(self.memory(), store, ptr, len)
} }
/// This function creates the argument for init_with_instance() from
/// exported symbol lookup
pub fn args_from_instance( pub fn args_from_instance(
store: &Store, store: &Store,
instance: &Instance, instance: &Instance,
@ -108,6 +111,7 @@ impl HostFunctionEnvironment {
Ok(HostFunctionEnvironmentInit { memory, allocator }) Ok(HostFunctionEnvironmentInit { memory, allocator })
} }
/// Initialize the wasm exports in the environment
pub fn init_with_instance(&mut self, args: HostFunctionEnvironmentInit) { pub fn init_with_instance(&mut self, args: HostFunctionEnvironmentInit) {
self.memory = Some(args.memory); self.memory = Some(args.memory);
self.allocator = Some(args.allocator); self.allocator = Some(args.allocator);