Skip to content

Commit

Permalink
Minor fixes for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
prokopyl committed Jun 5, 2024
1 parent 7b0d2ea commit 3280268
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 59 deletions.
25 changes: 7 additions & 18 deletions common/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
f: F,
) -> std::thread::Result<R> {
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<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
f: F,
) -> std::thread::Result<R> {
panic::catch_unwind(f)
#[allow(unused)]
pub(crate) fn handle_panic<F: FnOnce() -> R, R>(f: F) -> std::thread::Result<R> {
Ok(f())
}

mod fixed_point;
Expand Down
25 changes: 10 additions & 15 deletions host/src/extensions/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
f: F,
) -> std::thread::Result<R> {
Ok(f())
}
#[cfg(not(test))]
#[allow(unused)]
use std::panic::catch_unwind as handle_panic;

#[cfg(test)]
#[inline]
#[allow(unused)]
fn handle_panic<F: FnOnce() -> R, R>(f: F) -> std::thread::Result<R> {
Ok(f())
}

pub(crate) mod descriptor;
Expand Down Expand Up @@ -250,8 +246,7 @@ impl<H: HostHandlers> HostWrapper<H> {
where
F: FnOnce(Pa) -> Result<T, HostWrapperError>,
{
panic::catch_unwind(AssertUnwindSafe(|| handler(param)))
.map_err(|_| HostWrapperError::Panic)?
handle_panic(AssertUnwindSafe(|| handler(param))).map_err(|_| HostWrapperError::Panic)?
}

/// # Safety
Expand Down
4 changes: 2 additions & 2 deletions host/src/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
///
Expand Down
3 changes: 2 additions & 1 deletion host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<MyHost>::new(
//! |_| MyHostShared::default(),
Expand Down
3 changes: 2 additions & 1 deletion host/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
14 changes: 14 additions & 0 deletions host/tests/instance.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use clack_extensions::log::{HostLog, HostLogImpl, LogSeverity};
use clack_host::factory::PluginFactory;
use clack_plugin::prelude::*;
use std::ffi::CStr;
Expand Down Expand Up @@ -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>, _: &Self::Shared<'_>) {
builder.register::<HostLog>();
}
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions plugin/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -400,7 +400,7 @@ impl<E: Entry> EntryHolder<E> {
plugin_path: *const core::ffi::c_char,
entry_factory: impl FnOnce(&CStr) -> Result<E, EntryLoadError> + 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;
Expand All @@ -415,7 +415,7 @@ impl<E: Entry> EntryHolder<E> {
}
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 {
Expand Down Expand Up @@ -445,7 +445,7 @@ impl<E: Entry> EntryHolder<E> {
if *reference_count > 1 {
*reference_count -= 1;
} else {
let _ = catch_unwind(AssertUnwindSafe(|| *inner = Uninitialized));
let _ = handle_panic(AssertUnwindSafe(|| *inner = Uninitialized));
}
}
}
Expand All @@ -469,7 +469,7 @@ impl<E: Entry> EntryHolder<E> {

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()
Expand Down
22 changes: 9 additions & 13 deletions plugin/src/extensions/wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<F: FnOnce() -> R + std::panic::UnwindSafe, R>(
f: F,
) -> std::thread::Result<R> {
Ok(f())
}
#[cfg(test)]
#[inline]
#[allow(unused)]
pub(crate) fn handle_panic<F: FnOnce() -> R, R>(f: F) -> std::thread::Result<R> {
Ok(f())
}

/// A wrapper around a `clack` plugin of a given type.
Expand Down Expand Up @@ -276,7 +272,7 @@ impl<'a, P: Plugin> PluginWrapper<'a, P> {
where
F: FnOnce(Pa) -> Result<T, PluginWrapperError>,
{
panic::catch_unwind(AssertUnwindSafe(|| handler(parameter)))
handle_panic(AssertUnwindSafe(|| handler(parameter)))
.map_err(|_| PluginWrapperError::Panic)?
}
}
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/factory/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -64,7 +64,7 @@ impl<F: PluginFactory> PluginFactoryWrapper<F> {
) -> Option<T> {
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),
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/plugin/instance.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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::<PluginBoxInner<P>>::from_raw(
plugin.plugin_data as *const c_void as *mut _,
);
Expand Down

0 comments on commit 3280268

Please sign in to comment.