use crate::collections::{MutSpan, NonNullConst, Result};
use crate::library::library_loader::LibraryLoaderInterface;
use crate::library::{
Error, InternalHandle, LibraryHandle, LibraryType, LoaderHandle, OSPathChar, Symbol,
};
use crate::{Bool, CBase, CBaseFn, CBaseInterface, TypeWrapper};
use std::ffi::c_void;
use std::ptr::NonNull;
pub type RegisterLoaderFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
loader: NonNullConst<LibraryLoaderInterface>,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error>,
>;
pub type UnregisterLoaderFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LoaderHandle,
) -> Result<i8, Error>,
>;
pub type GetLoaderInterfaceFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LoaderHandle,
) -> Result<NonNullConst<LibraryLoaderInterface>, Error>,
>;
pub type GetLoaderHandleFromTypeFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error>,
>;
pub type GetLoaderHandleFromLibraryFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
) -> Result<LoaderHandle, Error>,
>;
pub type GetNumLoadersFn =
TypeWrapper<unsafe extern "C-unwind" fn(base_module: Option<NonNull<CBase>>) -> usize>;
pub type LibraryExistsFn = TypeWrapper<
unsafe extern "C-unwind" fn(base_module: Option<NonNull<CBase>>, handle: LibraryHandle) -> Bool,
>;
pub type TypeExistsFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
lib_type: NonNullConst<LibraryType>,
) -> Bool,
>;
pub type GetLibraryTypesFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
buffer: NonNull<MutSpan<LibraryType>>,
) -> Result<usize, Error>,
>;
pub type CreateLibraryHandleFn =
TypeWrapper<unsafe extern "C-unwind" fn(base_module: Option<NonNull<CBase>>) -> LibraryHandle>;
pub type RemoveLibraryHandleFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
) -> Result<i8, Error>,
>;
pub type LinkLibraryFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
loader: LoaderHandle,
internal: InternalHandle,
) -> Result<i8, Error>,
>;
pub type GetInternalLibraryHandleFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
) -> Result<InternalHandle, Error>,
>;
pub type LoadFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
loader: LoaderHandle,
path: NonNullConst<OSPathChar>,
) -> Result<LibraryHandle, Error>,
>;
pub type UnloadFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
) -> Result<i8, Error>,
>;
pub type GetDataSymbolFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<NonNullConst<c_void>>, Error>,
>;
pub type GetFunctionSymbolFn = TypeWrapper<
unsafe extern "C-unwind" fn(
base_module: Option<NonNull<CBase>>,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<CBaseFn>, Error>,
>;
pub trait LibraryBinding {
unsafe fn register_loader(
&mut self,
loader: NonNullConst<LibraryLoaderInterface>,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error>;
unsafe fn unregister_loader(&mut self, handle: LoaderHandle) -> Result<i8, Error>;
unsafe fn get_loader_interface(
&mut self,
handle: LoaderHandle,
) -> Result<NonNullConst<LibraryLoaderInterface>, Error>;
unsafe fn get_loader_handle_from_type(
&self,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error>;
unsafe fn get_loader_handle_from_library(
&self,
handle: LibraryHandle,
) -> Result<LoaderHandle, Error>;
unsafe fn get_num_loaders(&self) -> usize;
unsafe fn library_exists(&self, handle: LibraryHandle) -> Bool;
unsafe fn type_exists(&self, lib_type: NonNullConst<LibraryType>) -> Bool;
unsafe fn get_library_types(
&self,
buffer: NonNull<MutSpan<LibraryType>>,
) -> Result<usize, Error>;
unsafe fn create_library_handle(&mut self) -> LibraryHandle;
unsafe fn remove_library_handle(&mut self, handle: LibraryHandle) -> Result<i8, Error>;
unsafe fn link_library(
&mut self,
handle: LibraryHandle,
loader: LoaderHandle,
internal: InternalHandle,
) -> Result<i8, Error>;
unsafe fn get_internal_library_handle(
&self,
handle: LibraryHandle,
) -> Result<InternalHandle, Error>;
unsafe fn load(
&mut self,
loader: LoaderHandle,
path: NonNullConst<OSPathChar>,
) -> Result<LibraryHandle, Error>;
unsafe fn unload(&mut self, handle: LibraryHandle) -> Result<i8, Error>;
unsafe fn get_data_symbol(
&self,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<NonNullConst<c_void>>, Error>;
unsafe fn get_function_symbol(
&self,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<CBaseFn>, Error>;
}
impl LibraryBinding for CBaseInterface {
#[inline]
unsafe fn register_loader(
&mut self,
loader: NonNullConst<LibraryLoaderInterface>,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error> {
(self.library_register_loader_fn)(self.base_module, loader, lib_type)
}
#[inline]
unsafe fn unregister_loader(&mut self, handle: LoaderHandle) -> Result<i8, Error> {
(self.library_unregister_loader_fn)(self.base_module, handle)
}
#[inline]
unsafe fn get_loader_interface(
&mut self,
handle: LoaderHandle,
) -> Result<NonNullConst<LibraryLoaderInterface>, Error> {
(self.library_get_loader_interface_fn)(self.base_module, handle)
}
#[inline]
unsafe fn get_loader_handle_from_type(
&self,
lib_type: NonNullConst<LibraryType>,
) -> Result<LoaderHandle, Error> {
(self.library_get_loader_handle_from_type_fn)(self.base_module, lib_type)
}
#[inline]
unsafe fn get_loader_handle_from_library(
&self,
handle: LibraryHandle,
) -> Result<LoaderHandle, Error> {
(self.library_get_loader_handle_from_library_fn)(self.base_module, handle)
}
#[inline]
unsafe fn get_num_loaders(&self) -> usize {
(self.library_get_num_loaders_fn)(self.base_module)
}
#[inline]
unsafe fn library_exists(&self, handle: LibraryHandle) -> Bool {
(self.library_library_exists_fn)(self.base_module, handle)
}
#[inline]
unsafe fn type_exists(&self, lib_type: NonNullConst<LibraryType>) -> Bool {
(self.library_type_exists_fn)(self.base_module, lib_type)
}
#[inline]
unsafe fn get_library_types(
&self,
buffer: NonNull<MutSpan<LibraryType>>,
) -> Result<usize, Error> {
(self.library_get_library_types_fn)(self.base_module, buffer)
}
#[inline]
unsafe fn create_library_handle(&mut self) -> LibraryHandle {
(self.library_create_library_handle_fn)(self.base_module)
}
#[inline]
unsafe fn remove_library_handle(&mut self, handle: LibraryHandle) -> Result<i8, Error> {
(self.library_remove_library_handle_fn)(self.base_module, handle)
}
#[inline]
unsafe fn link_library(
&mut self,
handle: LibraryHandle,
loader: LoaderHandle,
internal: InternalHandle,
) -> Result<i8, Error> {
(self.library_link_library_fn)(self.base_module, handle, loader, internal)
}
#[inline]
unsafe fn get_internal_library_handle(
&self,
handle: LibraryHandle,
) -> Result<InternalHandle, Error> {
(self.library_get_internal_library_handle_fn)(self.base_module, handle)
}
#[inline]
unsafe fn load(
&mut self,
loader: LoaderHandle,
path: NonNullConst<OSPathChar>,
) -> Result<LibraryHandle, Error> {
(self.library_load_fn)(self.base_module, loader, path)
}
#[inline]
unsafe fn unload(&mut self, handle: LibraryHandle) -> Result<i8, Error> {
(self.library_unload_fn)(self.base_module, handle)
}
#[inline]
unsafe fn get_data_symbol(
&self,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<NonNullConst<c_void>>, Error> {
(self.library_get_data_symbol_fn)(self.base_module, handle, symbol)
}
#[inline]
unsafe fn get_function_symbol(
&self,
handle: LibraryHandle,
symbol: NonNullConst<u8>,
) -> Result<Symbol<CBaseFn>, Error> {
(self.library_get_function_symbol_fn)(self.base_module, handle, symbol)
}
}