1
0

feat: update cdylib

This commit is contained in:
2026-05-15 16:00:54 +08:00
parent 5c182bb4d3
commit 684a24fcf8
5 changed files with 202 additions and 20 deletions

View File

@@ -13,6 +13,7 @@ crate-type = ["cdylib"]
thiserror = { workspace = true }
wfassoc = { path="../wfassoc" }
slotmap = "1.1.1"
num_enum = "0.7.6"
[build-dependencies]
cbindgen = "0.29.0"

View File

@@ -2,6 +2,7 @@ mod cstr_ffi;
mod last_error;
mod object_pool;
use num_enum::TryFromPrimitive;
use object_pool::ObjectPool;
use std::sync::{LazyLock, RwLock};
use thiserror::Error as TeError;
@@ -32,6 +33,9 @@ enum Error {
#[error("{0}")]
ObjectPool(#[from] object_pool::Error),
/// Error occurs when checking enum value
#[error("")]
EnumOutOfRange,
/// Error when manipulating with poison RwLock
#[error("RwLock is poisoning")]
PoisonRwLock,
@@ -62,6 +66,12 @@ macro_rules! set_out_param {
};
}
macro_rules! resolve_enum {
($t:ty, $v:expr) => {
<$t>::try_from($v).map_err(|_| Error::EnumOutOfRange)
};
}
macro_rules! pull_reader {
($pool:expr) => {
$pool.read().map_err(|_| Error::PoisonRwLock)
@@ -173,6 +183,54 @@ macro_rules! cffi_wrapper {
// endregion
// region: Object Pools
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
// endregion
// region: Exposed Types
#[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive)]
pub enum Scope {
User = 0,
System = 1,
}
impl From<Scope> for wfassoc::Scope {
fn from(value: Scope) -> Self {
match value {
Scope::User => wfassoc::Scope::User,
Scope::System => wfassoc::Scope::System,
}
}
}
#[repr(u32)]
#[derive(Debug, Copy, Clone, TryFromPrimitive)]
pub enum View {
User,
System,
Hybrid,
}
impl From<View> for wfassoc::View {
fn from(value: View) -> Self {
match value {
View::User => wfassoc::View::User,
View::System => wfassoc::View::System,
View::Hybrid => wfassoc::View::Hybrid,
}
}
}
// endregion
// region: Exposed Functions
// region: Facilities
@@ -213,9 +271,6 @@ pub extern "C" fn WFHasPrivilege() -> bool {
// region: Schema
static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
cffi_wrapper!(|| -> (out_schema: Token) {
@@ -224,6 +279,14 @@ pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
cffi_wrapper!(|in_schema: Token| {
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.free(in_schema)?)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaSetIdentifier(
in_schema: in_param_ty!(Token),
@@ -402,21 +465,10 @@ pub extern "C" fn WFSchemaAddExt(
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
cffi_wrapper!(|in_schema: Token| {
let mut pool = pull_writer!(SCHEMA_POOL)?;
Ok(pool.free(in_schema)?)
})
}
// endregion
// region: Program
static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramCreate(
in_schema: in_param_ty!(Token),
@@ -440,6 +492,78 @@ pub extern "C" fn WFProgramDestroy(in_program: in_param_ty!(Token)) -> bool {
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramRegister(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.register(scope.into())?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramUnregister(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.unregister(scope.into())?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramIsRegistered(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
out_is_registered: out_param_ty!(bool),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32| -> (out_is_registered: bool) {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
Ok(program.is_registered(scope.into())?)
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramLinkExt(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
in_index: in_param_ty!(usize),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32, in_index: usize| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.link_ext(scope.into(), in_index)?;
Ok(())
})
}
#[unsafe(no_mangle)]
pub extern "C" fn WFProgramUnlinkExt(
in_program: in_param_ty!(Token),
in_scope: in_param_ty!(u32),
in_index: in_param_ty!(usize),
) -> bool {
cffi_wrapper!(|in_program: Token, in_scope: u32, in_index: usize| {
let mut pool = pull_writer!(PROGRAM_POOL)?;
let program = pool.get_mut(in_program)?;
let scope = resolve_enum!(Scope, in_scope)?;
program.unlink_ext(scope.into(), in_index)?;
Ok(())
})
}
// endregion
// endregion

View File

@@ -25,34 +25,40 @@ pub struct ObjectPool<T> {
}
impl<T> ObjectPool<T> {
/// Create a new [ObjectPool].
pub fn new() -> Self {
Self {
objs: SlotMap::new(),
}
}
/// Convert [slotmap] crate's [DefaultKey] to our [Token].
fn key_to_token(key: &DefaultKey) -> Token {
key.data().as_ffi()
}
/// Convert our [Token] to [slotmap] crate's [DefaultKey].
fn token_to_key(token: Token) -> DefaultKey {
DefaultKey::from(KeyData::from_ffi(token))
}
/// Put given object into the pool and return its token.
///
/// The ownership of given object is transferred to the pool.
pub fn allocate(&mut self, value: T) -> Result<Token, Error> {
let key = self.objs.insert(value);
Ok(Self::key_to_token(&key))
}
/// Free the object in the pool corresponding to the given token.
pub fn free(&mut self, token: Token) -> Result<(), Error> {
let _ = self.pop(token)?;
Ok(())
}
pub fn clear(&mut self) -> () {
self.objs.clear();
}
/// Remove the object from the pool corresponding to the given token and return it.
///
/// The ownership of the object is transferred to the caller.
pub fn pop(&mut self, token: Token) -> Result<T, Error> {
match self.objs.remove(Self::token_to_key(token)) {
Some(obj) => Ok(obj),
@@ -60,12 +66,19 @@ impl<T> ObjectPool<T> {
}
}
/// Clear all objects in the pool.
pub fn clear(&mut self) -> () {
self.objs.clear();
}
/// Get a reference to the object in the pool corresponding to the given token.
pub fn get(&self, token: Token) -> Result<&T, Error> {
self.objs
.get(Self::token_to_key(token))
.ok_or(Error::NoSuchToken)
}
/// Get a mutable reference to the object in the pool corresponding to the given token.
pub fn get_mut(&mut self, token: Token) -> Result<&mut T, Error> {
self.objs
.get_mut(Self::token_to_key(token))