use wasmer_vm::{
ImportInitializerFuncPtr, VMExport, VMExportFunction, VMExportGlobal, VMExportMemory,
VMExportTable,
};
use std::sync::Arc;
#[derive(Debug, Clone)]
pub enum Export {
Function(ExportFunction),
Table(ExportTable),
Memory(ExportMemory),
Global(ExportGlobal),
}
impl From<Export> for VMExport {
fn from(other: Export) -> Self {
match other {
Export::Function(ExportFunction { vm_function, .. }) => VMExport::Function(vm_function),
Export::Memory(ExportMemory { vm_memory }) => VMExport::Memory(vm_memory),
Export::Table(ExportTable { vm_table }) => VMExport::Table(vm_table),
Export::Global(ExportGlobal { vm_global }) => VMExport::Global(vm_global),
}
}
}
impl From<VMExport> for Export {
fn from(other: VMExport) -> Self {
match other {
VMExport::Function(vm_function) => Export::Function(ExportFunction {
vm_function,
metadata: None,
}),
VMExport::Memory(vm_memory) => Export::Memory(ExportMemory { vm_memory }),
VMExport::Table(vm_table) => Export::Table(ExportTable { vm_table }),
VMExport::Global(vm_global) => Export::Global(ExportGlobal { vm_global }),
}
}
}
#[derive(Debug, PartialEq)]
pub struct ExportFunctionMetadata {
pub(crate) host_env: *mut std::ffi::c_void,
pub(crate) import_init_function_ptr: Option<ImportInitializerFuncPtr>,
pub(crate) host_env_clone_fn: fn(*mut std::ffi::c_void) -> *mut std::ffi::c_void,
pub(crate) host_env_drop_fn: unsafe fn(*mut std::ffi::c_void),
}
unsafe impl Send for ExportFunctionMetadata {}
unsafe impl Sync for ExportFunctionMetadata {}
impl ExportFunctionMetadata {
pub unsafe fn new(
host_env: *mut std::ffi::c_void,
import_init_function_ptr: Option<ImportInitializerFuncPtr>,
host_env_clone_fn: fn(*mut std::ffi::c_void) -> *mut std::ffi::c_void,
host_env_drop_fn: fn(*mut std::ffi::c_void),
) -> Self {
Self {
host_env,
import_init_function_ptr,
host_env_clone_fn,
host_env_drop_fn,
}
}
}
impl Drop for ExportFunctionMetadata {
fn drop(&mut self) {
if !self.host_env.is_null() {
unsafe {
(self.host_env_drop_fn)(self.host_env);
}
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExportFunction {
pub vm_function: VMExportFunction,
pub metadata: Option<Arc<ExportFunctionMetadata>>,
}
impl From<ExportFunction> for Export {
fn from(func: ExportFunction) -> Self {
Self::Function(func)
}
}
#[derive(Debug, Clone)]
pub struct ExportTable {
pub vm_table: VMExportTable,
}
impl From<ExportTable> for Export {
fn from(table: ExportTable) -> Self {
Self::Table(table)
}
}
#[derive(Debug, Clone)]
pub struct ExportMemory {
pub vm_memory: VMExportMemory,
}
impl From<ExportMemory> for Export {
fn from(memory: ExportMemory) -> Self {
Self::Memory(memory)
}
}
#[derive(Debug, Clone)]
pub struct ExportGlobal {
pub vm_global: VMExportGlobal,
}
impl From<ExportGlobal> for Export {
fn from(global: ExportGlobal) -> Self {
Self::Global(global)
}
}