1
0

feat: fix LazyLock issue and add something in cdylib

This commit is contained in:
2026-05-11 13:33:01 +08:00
parent f0e610f8c8
commit 76ddddb6cd
4 changed files with 134 additions and 69 deletions

View File

@@ -12,6 +12,9 @@ use std::cell::RefCell;
use std::ffi::{CStr, CString, c_char}; use std::ffi::{CStr, CString, c_char};
use thiserror::Error as TeError; use thiserror::Error as TeError;
/// The type representing the raw pointer to immutable C-style NUL-terminated string.
pub type CStyleString = *const c_char;
// region: Error // region: Error
/// Error occurs in this crate. /// Error occurs in this crate.
@@ -31,13 +34,13 @@ type Result<T> = std::result::Result<T, Error>;
// endregion // endregion
// region: FFI String // region: String Cache for Exposing
struct FfiString { struct StringCache {
msg: CString, msg: CString,
} }
impl FfiString { impl StringCache {
fn new() -> Self { fn new() -> Self {
Self { Self {
msg: CString::new("").expect("empty string must be valid for CString"), msg: CString::new("").expect("empty string must be valid for CString"),
@@ -49,7 +52,7 @@ impl FfiString {
Ok(()) Ok(())
} }
pub fn get_msg(&self) -> *const c_char { pub fn get_msg(&self) -> CStyleString {
self.msg.as_ptr() self.msg.as_ptr()
} }
@@ -63,7 +66,7 @@ impl FfiString {
// region: Exposed Functions // region: Exposed Functions
thread_local! { thread_local! {
static STRING_CACHE: RefCell<FfiString> = RefCell::new(FfiString::new()); static STRING_CACHE: RefCell<StringCache> = RefCell::new(StringCache::new());
} }
/// Set thread local string exposed for C code. /// Set thread local string exposed for C code.
@@ -74,7 +77,7 @@ pub fn set_ffi_string(msg: &str) -> Result<()> {
} }
/// Get const pointer to thread local string exposed for C code. /// Get const pointer to thread local string exposed for C code.
pub fn get_ffi_string() -> *const c_char { pub fn get_ffi_string() -> CStyleString {
STRING_CACHE.with(|e| e.borrow().get_msg()) STRING_CACHE.with(|e| e.borrow().get_msg())
} }
@@ -88,7 +91,7 @@ pub fn clear_ffi_string() {
} }
/// Parse string given by C code into Rust string. /// Parse string given by C code into Rust string.
pub fn parse_ffi_string<'a>(ptr: *const c_char) -> Result<&'a str> { pub fn parse_ffi_string<'a>(ptr: CStyleString) -> Result<&'a str> {
if ptr.is_null() { if ptr.is_null() {
Err(Error::NullPtr) Err(Error::NullPtr)
} else { } else {

View File

@@ -3,11 +3,11 @@ mod last_error;
mod object_pool; mod object_pool;
use object_pool::ObjectPool; use object_pool::ObjectPool;
use std::ffi::{CString, c_char}; use std::sync::{LazyLock, RwLock};
use std::sync::{LazyLock, PoisonError, RwLock};
use thiserror::Error as TeError; use thiserror::Error as TeError;
use wfassoc::highlevel::{Program, Schema}; use wfassoc::highlevel::{Program, Schema};
pub use cstr_ffi::CStyleString;
pub use object_pool::Token; pub use object_pool::Token;
// region: Error // region: Error
@@ -44,6 +44,36 @@ type Result<T> = std::result::Result<T, Error>;
// region: Macros // region: Macros
macro_rules! in_param_ty {
($t:ty) => {
$t
};
}
macro_rules! out_param_ty {
($t:ty) => {
*mut $t
};
}
macro_rules! set_out_param {
($lhs:expr, $rhs:expr) => {
unsafe { *$lhs = $rhs };
};
}
macro_rules! pull_reader {
($pool:expr) => {
$pool.read().map_err(|_| Error::PoisonRwLock)
};
}
macro_rules! pull_writer {
($pool:expr) => {
$pool.write().map_err(|_| Error::PoisonRwLock)
};
}
// endregion // endregion
// region: Exposed Functions // region: Exposed Functions
@@ -52,16 +82,18 @@ type Result<T> = std::result::Result<T, Error>;
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFStartup() -> bool { pub extern "C" fn WFStartup() -> bool {
// TODO: Initialize all pool by fetching writer from them
true true
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFShutdown() -> bool { pub extern "C" fn WFShutdown() -> bool {
// TODO: Free all pool stored objects
true true
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFGetLastError() -> *const c_char { pub extern "C" fn WFGetLastError() -> CStyleString {
last_error::get_last_error() last_error::get_last_error()
} }
@@ -70,32 +102,24 @@ pub extern "C" fn WFHasPrivilege() -> bool {
wfassoc::win32::utilities::has_privilege() wfassoc::win32::utilities::has_privilege()
} }
#[unsafe(no_mangle)]
pub extern "C" fn WFAdd(left: u32, right: u32, rv: *mut u32) -> bool {
unsafe {
*rv = left + right;
}
return true;
}
// endregion // endregion
// region: Schema // region: Schema
const SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> = static SCHEMA_POOL: LazyLock<RwLock<ObjectPool<Schema>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new())); LazyLock::new(|| RwLock::new(ObjectPool::new()));
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFSchemaCreate(out_token: *mut Token) -> bool { pub extern "C" fn WFSchemaCreate(out_schema: out_param_ty!(Token)) -> bool {
fn inner() -> Result<Token> { fn inner() -> Result<Token> {
let binding = &SCHEMA_POOL; let mut pool = pull_writer!(SCHEMA_POOL)?;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
Ok(pool.allocate(Schema::new())?) Ok(pool.allocate(Schema::new())?)
} }
match inner() { match inner() {
Ok(rv) => { Ok(rv) => {
unsafe { *out_token = rv }; set_out_param!(out_schema, rv);
last_error::clear_last_error();
true true
} }
Err(e) => { Err(e) => {
@@ -105,23 +129,48 @@ pub extern "C" fn WFSchemaCreate(out_token: *mut Token) -> bool {
} }
} }
#[unsafe(no_mangle)]
pub extern "C" fn WFSchemaSetIdentifier(
in_schema: in_param_ty!(Token),
in_value: in_param_ty!(CStyleString),
) -> bool {
fn inner(in_schema: Token, in_value: CStyleString) -> Result<()> {
let mut pool = pull_writer!(SCHEMA_POOL)?;
let schema = pool.get_mut(in_schema)?;
schema.set_identifier(cstr_ffi::parse_ffi_string(in_value)?);
Ok(())
}
match inner(in_schema, in_value) {
Ok(_) => {
last_error::clear_last_error();
true
}
Err(e) => {
last_error::set_last_error(e.to_string().as_str());
false
}
}
}
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFSchemaIntoProgram(in_token: Token, out_token: *mut Token) -> bool { pub extern "C" fn WFSchemaIntoProgram(
in_schema: in_param_ty!(Token),
out_program: out_param_ty!(Token),
) -> bool {
fn inner(in_token: Token) -> Result<Token> { fn inner(in_token: Token) -> Result<Token> {
let binding = &SCHEMA_POOL; let mut pool = pull_writer!(SCHEMA_POOL)?;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let schema = pool.pop(in_token)?; let schema = pool.pop(in_token)?;
let binding = &PROGRAM_POOL; let mut pool = pull_writer!(PROGRAM_POOL)?;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
let program = schema.into_program()?; let program = schema.into_program()?;
Ok(pool.allocate(program)?) Ok(pool.allocate(program)?)
} }
match inner(in_token) { match inner(in_schema) {
Ok(rv) => { Ok(rv) => {
unsafe { *out_token = rv }; set_out_param!(out_program, rv);
last_error::clear_last_error();
true true
} }
Err(e) => { Err(e) => {
@@ -132,15 +181,17 @@ pub extern "C" fn WFSchemaIntoProgram(in_token: Token, out_token: *mut Token) ->
} }
#[unsafe(no_mangle)] #[unsafe(no_mangle)]
pub extern "C" fn WFSchemaDestroy(in_token: Token) -> bool { pub extern "C" fn WFSchemaDestroy(in_schema: in_param_ty!(Token)) -> bool {
fn inner(in_token: Token) -> Result<()> { fn inner(in_token: Token) -> Result<()> {
let binding = &SCHEMA_POOL; let mut pool = pull_writer!(SCHEMA_POOL)?;
let mut pool = binding.write().map_err(|_| Error::PoisonRwLock)?;
Ok(pool.free(in_token)?) Ok(pool.free(in_token)?)
} }
match inner(in_token) { match inner(in_schema) {
Ok(_) => true, Ok(_) => {
last_error::clear_last_error();
true
}
Err(e) => { Err(e) => {
last_error::set_last_error(e.to_string().as_str()); last_error::set_last_error(e.to_string().as_str());
false false
@@ -152,7 +203,7 @@ pub extern "C" fn WFSchemaDestroy(in_token: Token) -> bool {
// region: Program // region: Program
const PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> = static PROGRAM_POOL: LazyLock<RwLock<ObjectPool<Program>>> =
LazyLock::new(|| RwLock::new(ObjectPool::new())); LazyLock::new(|| RwLock::new(ObjectPool::new()));
// endregion // endregion

View File

@@ -6,7 +6,7 @@ use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::ffi::OsStr; use std::ffi::OsStr;
use std::path::Path; use std::path::Path;
use std::rc::Rc; use std::sync::Arc;
use std::sync::LazyLock; use std::sync::LazyLock;
use thiserror::Error as TeError; use thiserror::Error as TeError;
@@ -234,16 +234,16 @@ pub struct Program {
applications_key: lowlevel::ApplicationsKey, applications_key: lowlevel::ApplicationsKey,
app_path: String, app_path: String,
app_dir_path: String, app_dir_path: String,
name: Option<Rc<ProgramStr>>, name: Option<Arc<ProgramStr>>,
icon: Option<Rc<ProgramIcon>>, icon: Option<Arc<ProgramIcon>>,
behavior: Option<Rc<ProgramBehavior>>, behavior: Option<Arc<ProgramBehavior>>,
#[allow(dead_code)] #[allow(dead_code)]
strs: Vec<Rc<ProgramStr>>, strs: Vec<Arc<ProgramStr>>,
#[allow(dead_code)] #[allow(dead_code)]
icons: Vec<Rc<ProgramIcon>>, icons: Vec<Arc<ProgramIcon>>,
#[allow(dead_code)] #[allow(dead_code)]
behaviors: Vec<Rc<ProgramBehavior>>, behaviors: Vec<Arc<ProgramBehavior>>,
ext_keys: Vec<ProgramProgIdExtKey>, ext_keys: Vec<ProgramProgIdExtKey>,
ext_keys_map: HashMap<String, usize>, ext_keys_map: HashMap<String, usize>,
@@ -293,9 +293,9 @@ impl Program {
fn resolve_index<T>( fn resolve_index<T>(
key: &String, key: &String,
vector: &Vec<Rc<T>>, vector: &Vec<Arc<T>>,
index_map: &HashMap<String, usize>, index_map: &HashMap<String, usize>,
) -> Result<Rc<T>, ParseProgramError> { ) -> Result<Arc<T>, ParseProgramError> {
match index_map.get(key) { match index_map.get(key) {
Some(index) => Ok(vector Some(index) => Ok(vector
.get(*index) .get(*index)
@@ -355,22 +355,22 @@ impl Program {
let program_str = ProgramStr { let program_str = ProgramStr {
inner: str_res_variant, inner: str_res_variant,
}; };
Ok(Rc::new(program_str)) Ok(Arc::new(program_str))
})?; })?;
let (icons, icons_index_map) = Self::flat_hashmap(&schema.icons, |entry| { let (icons, icons_index_map) = Self::flat_hashmap(&schema.icons, |entry| {
let icon_res_variant: lowlevel::IconResVariant = entry.as_str().into(); let icon_res_variant: lowlevel::IconResVariant = entry.as_str().into();
let program_icon = ProgramIcon { let program_icon = ProgramIcon {
inner: icon_res_variant, inner: icon_res_variant,
}; };
Ok(Rc::new(program_icon)) Ok(Arc::new(program_icon))
})?; })?;
let (behaviors, behaviors_index_map) = Self::flat_hashmap(&schema.behaviors, |entry| { let (behaviors, behaviors_index_map) = Self::flat_hashmap(&schema.behaviors, |entry| {
// We simply always use "Open" verb. // We simply always use "Open" verb.
let cmdline: concept::CmdLine = entry.as_str().parse()?; let cmdline: concept::CmdLine = entry.as_str().parse()?;
let verb = concept::Verb::OPEN.clone(); let verb = concept::Verb::OPEN();
let shell_verb = lowlevel::ShellVerb::new(verb, cmdline); let shell_verb = lowlevel::ShellVerb::new(verb, cmdline);
let program_behavior = ProgramBehavior { inner: shell_verb }; let program_behavior = ProgramBehavior { inner: shell_verb };
Ok(Rc::new(program_behavior)) Ok(Arc::new(program_behavior))
})?; })?;
// Setup default name, icon and behavior // Setup default name, icon and behavior
@@ -764,9 +764,9 @@ struct ProgramProgIdExtKey {
ext_key: lowlevel::ExtKey, ext_key: lowlevel::ExtKey,
progid_key: lowlevel::ProgIdKey, progid_key: lowlevel::ProgIdKey,
name: Rc<ProgramStr>, name: Arc<ProgramStr>,
icon: Rc<ProgramIcon>, icon: Arc<ProgramIcon>,
behavior: Rc<ProgramBehavior>, behavior: Arc<ProgramBehavior>,
} }
// endregion // endregion

View File

@@ -699,13 +699,10 @@ pub struct ExpandString {
inner: String, inner: String,
} }
impl ExpandString {
/// Internal shared compiled regex pattern matching Variable, /// Internal shared compiled regex pattern matching Variable,
/// the `%` braced string like `%SystemRoot%`. /// the `%` braced string like `%SystemRoot%`.
const VAR_RE: LazyLock<Regex> = LazyLock::new(|| { static WINVAR_RE: LazyLock<Regex> =
Regex::new(r"%[a-zA-Z0-9_]+%").expect("unexpected bad regex pattern string") LazyLock::new(|| Regex::new(r"%[a-zA-Z0-9_]+%").expect("unexpected bad regex pattern string"));
});
}
impl ExpandString { impl ExpandString {
/// Create a new expand string /// Create a new expand string
@@ -743,7 +740,7 @@ impl ExpandString {
// If the final string still has environment variable, // If the final string still has environment variable,
// we think we fail to expand it. // we think we fail to expand it.
if Self::VAR_RE.is_match(rv.as_str()) { if WINVAR_RE.is_match(rv.as_str()) {
Err(ExpandEnvVarError::NoEnvVar) Err(ExpandEnvVarError::NoEnvVar)
} else { } else {
Ok(rv) Ok(rv)
@@ -761,7 +758,7 @@ impl FromStr for ExpandString {
type Err = ParseExpandStrError; type Err = ParseExpandStrError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
if Self::VAR_RE.is_match(s) { if WINVAR_RE.is_match(s) {
Ok(Self { Ok(Self {
inner: s.to_string(), inner: s.to_string(),
}) })
@@ -858,9 +855,20 @@ pub struct Verb {
} }
impl Verb { impl Verb {
pub const OPEN: LazyLock<Verb> = LazyLock::new(|| Verb::new("open").expect("unexpected bad verb")); #[allow(non_snake_case)]
pub const EDIT: LazyLock<Verb> = LazyLock::new(|| Verb::new("edit").expect("unexpected bad verb")); pub fn OPEN() -> Self {
pub const PLAY: LazyLock<Verb> = LazyLock::new(|| Verb::new("play").expect("unexpected bad verb")); Verb::new("open").expect("unexpected bad verb")
}
#[allow(non_snake_case)]
pub fn EDIT() -> Self {
Verb::new("edit").expect("unexpected bad verb")
}
#[allow(non_snake_case)]
pub fn PLAY() -> Self {
Verb::new("play").expect("unexpected bad verb")
}
} }
impl Verb { impl Verb {
@@ -941,7 +949,9 @@ pub struct CmdLine {
impl CmdLine { impl CmdLine {
/// Create a new command line. /// Create a new command line.
pub fn new<S: AsRef<str>>(args: &[S]) -> Result<Self, BadCmdLineError> { pub fn new<S: AsRef<str>>(args: &[S]) -> Result<Self, BadCmdLineError> {
Ok(Self { inner: args.iter().map(|s| s.as_ref().to_string()).collect() }) Ok(Self {
inner: args.iter().map(|s| s.as_ref().to_string()).collect(),
})
} }
/// Get the full command line. /// Get the full command line.
@@ -981,7 +991,8 @@ impl FromStr for CmdLine {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
// We simply split it by space for rough result. // We simply split it by space for rough result.
Ok(Self { inner: s.split(' ').map(|s| s.to_string()).collect() }) Ok(Self {
inner: s.split(' ').map(|s| s.to_string()).collect(),
})
} }
} }