diff --git a/common/src/utils.rs b/common/src/utils.rs index db083aa4..13ba0e27 100644 --- a/common/src/utils.rs +++ b/common/src/utils.rs @@ -1,25 +1,14 @@ //! Various CLAP-related utilities. -mod panic { - #[cfg(not(test))] - #[allow(unused)] - pub use std::panic::catch_unwind; - - #[cfg(test)] - #[inline] - #[allow(unused)] - pub fn catch_unwind R + std::panic::UnwindSafe, R>( - f: F, - ) -> std::thread::Result { - Ok(f()) - } -} +#[cfg(not(test))] +#[allow(unused)] +pub(crate) use std::panic::catch_unwind as handle_panic; +#[cfg(test)] #[inline] -pub(crate) fn handle_panic R + std::panic::UnwindSafe, R>( - f: F, -) -> std::thread::Result { - panic::catch_unwind(f) +#[allow(unused)] +pub(crate) fn handle_panic R, R>(f: F) -> std::thread::Result { + Ok(f()) } mod fixed_point; diff --git a/host/src/extensions/wrapper.rs b/host/src/extensions/wrapper.rs index 231f183d..049ef54b 100644 --- a/host/src/extensions/wrapper.rs +++ b/host/src/extensions/wrapper.rs @@ -14,19 +14,15 @@ use std::ptr::NonNull; use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Once, OnceLock}; -mod panic { - #[cfg(not(test))] - #[allow(unused)] - pub use std::panic::catch_unwind; - - #[cfg(test)] - #[inline] - #[allow(unused)] - pub fn catch_unwind R + std::panic::UnwindSafe, R>( - f: F, - ) -> std::thread::Result { - Ok(f()) - } +#[cfg(not(test))] +#[allow(unused)] +use std::panic::catch_unwind as handle_panic; + +#[cfg(test)] +#[inline] +#[allow(unused)] +fn handle_panic R, R>(f: F) -> std::thread::Result { + Ok(f()) } pub(crate) mod descriptor; @@ -250,8 +246,7 @@ impl HostWrapper { where F: FnOnce(Pa) -> Result, { - panic::catch_unwind(AssertUnwindSafe(|| handler(param))) - .map_err(|_| HostWrapperError::Panic)? + handle_panic(AssertUnwindSafe(|| handler(param))).map_err(|_| HostWrapperError::Panic)? } /// # Safety diff --git a/host/src/factory.rs b/host/src/factory.rs index fdd6ed9f..e7d06608 100644 --- a/host/src/factory.rs +++ b/host/src/factory.rs @@ -58,9 +58,9 @@ pub unsafe trait FactoryPointer<'a>: Sized + 'a { /// /// # mod diva { include!("./bundle/diva_stub.rs"); } /// # let bundle = unsafe { PluginBundle::load_from_raw(&diva::DIVA_STUB_ENTRY, "/home/user/.clap/u-he/libdiva.so").unwrap() }; -/// # #[cfg(never)] +/// # /* /// let bundle = PluginBundle::load("/home/user/.clap/u-he/libdiva.so")?; -/// +/// # */ /// // Fetch the PluginFactory from the bundle, if present /// let plugin_factory = bundle.get_plugin_factory().unwrap(); /// diff --git a/host/src/host.rs b/host/src/host.rs index dad7487a..b941295f 100644 --- a/host/src/host.rs +++ b/host/src/host.rs @@ -160,8 +160,9 @@ //! //! # mod diva { include!("./bundle/diva_stub.rs"); } //! # let bundle = unsafe { PluginBundle::load_from_raw(&diva::DIVA_STUB_ENTRY, "/home/user/.clap/u-he/libdiva.so")? }; -//! # #[cfg(never)] +//! # /* //! let bundle = PluginBundle::load("/home/user/.clap/u-he/libdiva.so")?; +//! # */ //! //! let mut plugin_instance = PluginInstance::::new( //! |_| MyHostShared::default(), diff --git a/host/src/lib.rs b/host/src/lib.rs index 87c6490e..a9dacc11 100644 --- a/host/src/lib.rs +++ b/host/src/lib.rs @@ -144,8 +144,9 @@ //! // Step 1: Load the bundle in memory. //! # mod diva { include!("./bundle/diva_stub.rs"); } //! # let bundle = unsafe { PluginBundle::load_from_raw(&diva::DIVA_STUB_ENTRY, "/home/user/.clap/u-he/libdiva.so")? }; -//! # #[cfg(never)] +//! # /* //! let bundle = PluginBundle::load("/home/user/.clap/u-he/libdiva.so")?; +//! # */ //! //! // Step 2: Get the Plugin factory of this bundle. //! let plugin_factory = bundle.get_plugin_factory().unwrap(); diff --git a/host/tests/instance.rs b/host/tests/instance.rs index 620988c2..92f703c4 100644 --- a/host/tests/instance.rs +++ b/host/tests/instance.rs @@ -1,3 +1,4 @@ +use clack_extensions::log::{HostLog, HostLogImpl, LogSeverity}; use clack_host::factory::PluginFactory; use clack_plugin::prelude::*; use std::ffi::CStr; @@ -71,12 +72,25 @@ impl<'a> SharedHandler<'a> for MyHostShared { } } +impl HostLogImpl for MyHostShared { + fn log(&self, severity: LogSeverity, message: &str) { + // This is the error we're expecting + if message != "Some error" { + eprintln!("[{severity}] {message}"); + } + } +} + struct MyHost; impl HostHandlers for MyHost { type Shared<'a> = MyHostShared; type MainThread<'a> = (); type AudioProcessor<'a> = (); + + fn declare_extensions(builder: &mut HostExtensions, _: &Self::Shared<'_>) { + builder.register::(); + } } #[test] diff --git a/plugin/src/entry.rs b/plugin/src/entry.rs index 27f2977b..77e75993 100644 --- a/plugin/src/entry.rs +++ b/plugin/src/entry.rs @@ -20,7 +20,7 @@ //! entry type, or see the provided [`SinglePluginEntry`] convenience type if you only need to //! expose a single plugin type to the host. -use crate::extensions::wrapper::panic::catch_unwind; +use crate::extensions::wrapper::handle_panic; use crate::factory::Factory; use std::error::Error; use std::ffi::{c_void, CStr}; @@ -400,7 +400,7 @@ impl EntryHolder { plugin_path: *const core::ffi::c_char, entry_factory: impl FnOnce(&CStr) -> Result + UnwindSafe, ) -> bool { - let Ok(Ok(mut inner)) = catch_unwind(|| self.inner.lock()) else { + let Ok(Ok(mut inner)) = handle_panic(|| self.inner.lock()) else { // A poisoned lock means init() panicked, so we consider the entry unusable. // Same if lock() itself panicked. return false; @@ -415,7 +415,7 @@ impl EntryHolder { } Uninitialized => { let plugin_path = CStr::from_ptr(plugin_path); - let entry = catch_unwind(|| entry_factory(plugin_path)); + let entry = handle_panic(|| entry_factory(plugin_path)); if let Ok(Ok(entry)) = entry { *inner = Initialized { @@ -445,7 +445,7 @@ impl EntryHolder { if *reference_count > 1 { *reference_count -= 1; } else { - let _ = catch_unwind(AssertUnwindSafe(|| *inner = Uninitialized)); + let _ = handle_panic(AssertUnwindSafe(|| *inner = Uninitialized)); } } } @@ -469,7 +469,7 @@ impl EntryHolder { let identifier = CStr::from_ptr(identifier); - catch_unwind(AssertUnwindSafe(|| { + handle_panic(AssertUnwindSafe(|| { let mut builder = EntryFactories::new(identifier); entry.declare_factories(&mut builder); builder.found() diff --git a/plugin/src/extensions/wrapper.rs b/plugin/src/extensions/wrapper.rs index 4f96a1b1..60766517 100644 --- a/plugin/src/extensions/wrapper.rs +++ b/plugin/src/extensions/wrapper.rs @@ -16,19 +16,15 @@ use std::panic::AssertUnwindSafe; use std::pin::Pin; use std::ptr::NonNull; -pub(crate) mod panic { - #[cfg(not(test))] - #[allow(unused)] - pub use std::panic::catch_unwind; +#[cfg(not(test))] +#[allow(unused)] +pub(crate) use std::panic::catch_unwind as handle_panic; - #[cfg(test)] - #[inline] - #[allow(unused)] - pub fn catch_unwind R + std::panic::UnwindSafe, R>( - f: F, - ) -> std::thread::Result { - Ok(f()) - } +#[cfg(test)] +#[inline] +#[allow(unused)] +pub(crate) fn handle_panic R, R>(f: F) -> std::thread::Result { + Ok(f()) } /// A wrapper around a `clack` plugin of a given type. @@ -276,7 +272,7 @@ impl<'a, P: Plugin> PluginWrapper<'a, P> { where F: FnOnce(Pa) -> Result, { - panic::catch_unwind(AssertUnwindSafe(|| handler(parameter))) + handle_panic(AssertUnwindSafe(|| handler(parameter))) .map_err(|_| PluginWrapperError::Panic)? } } diff --git a/plugin/src/factory/plugin.rs b/plugin/src/factory/plugin.rs index 5859b2e9..98064cca 100644 --- a/plugin/src/factory/plugin.rs +++ b/plugin/src/factory/plugin.rs @@ -8,7 +8,7 @@ //! //! See the [`factory` module documentation](crate::factory) to learn more about factories. -use crate::extensions::wrapper::panic; +use crate::extensions::wrapper::handle_panic; use crate::factory::Factory; use crate::host::HostInfo; use crate::plugin::{PluginDescriptor, PluginInstance}; @@ -64,7 +64,7 @@ impl PluginFactoryWrapper { ) -> Option { let factory = Self::from_raw(raw); let result = factory.and_then(|factory| { - match panic::catch_unwind(AssertUnwindSafe(|| handler(factory.factory()))) { + match handle_panic(AssertUnwindSafe(|| handler(factory.factory()))) { Err(_) => Err(PluginFactoryError::Panic), Ok(Err(e)) => Err(e), Ok(Ok(val)) => Ok(val), diff --git a/plugin/src/plugin/instance.rs b/plugin/src/plugin/instance.rs index 453f7ad9..cd437900 100644 --- a/plugin/src/plugin/instance.rs +++ b/plugin/src/plugin/instance.rs @@ -1,4 +1,4 @@ -use crate::extensions::wrapper::{panic, PluginWrapper, PluginWrapperError}; +use crate::extensions::wrapper::{handle_panic, PluginWrapper, PluginWrapperError}; use crate::extensions::PluginExtensions; use crate::host::{HostInfo, HostMainThreadHandle, HostSharedHandle}; use crate::plugin::instance::WrapperData::*; @@ -239,7 +239,7 @@ impl<'a, P: Plugin> PluginBoxInner<'a, P> { // We could use direct &mut access for the swap, but let's use atomic operations just in case... if plugin_data.state.swap(DESTROYING, Ordering::SeqCst) != DESTROYING { - let _ = panic::catch_unwind(|| { + let _ = handle_panic(|| { let _ = Box::>::from_raw( plugin.plugin_data as *const c_void as *mut _, );