-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add reentrant init test, implement initializing method on clack-host
- Loading branch information
Showing
5 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
use clack_extensions::timer::{ | ||
HostTimer, HostTimerImpl, PluginTimer, PluginTimerImpl, TimerError, TimerId, | ||
}; | ||
use clack_host::prelude::*; | ||
use clack_plugin::clack_entry; | ||
use clack_plugin::prelude::*; | ||
use std::sync::OnceLock; | ||
|
||
struct MyPlugin; | ||
|
||
impl Plugin for MyPlugin { | ||
type AudioProcessor<'a> = (); | ||
type Shared<'a> = (); | ||
type MainThread<'a> = MyPluginMainThread; | ||
|
||
fn declare_extensions(builder: &mut PluginExtensions<Self>, shared: Option<&Self::Shared<'_>>) { | ||
assert!(shared.is_none()); // Host will only query extensions from within. | ||
builder.register::<PluginTimer>(); | ||
} | ||
} | ||
|
||
struct MyPluginMainThread; | ||
|
||
impl<'a> PluginMainThread<'a, ()> for MyPluginMainThread {} | ||
|
||
impl PluginTimerImpl for MyPluginMainThread { | ||
fn on_timer(&mut self, timer_id: TimerId) { | ||
assert_eq!(timer_id, TimerId(5)); | ||
} | ||
} | ||
|
||
impl DefaultPluginFactory for MyPlugin { | ||
fn get_descriptor() -> PluginDescriptor { | ||
PluginDescriptor::new("my.plugin", "My plugin") | ||
} | ||
|
||
fn new_shared(_host: HostHandle) -> Result<Self::Shared<'_>, PluginError> { | ||
Ok(()) | ||
} | ||
|
||
fn new_main_thread( | ||
mut host: HostMainThreadHandle, | ||
_shared: &(), | ||
) -> Result<MyPluginMainThread, PluginError> { | ||
let timer: &HostTimer = host.shared().extension().unwrap(); | ||
let timer_id = timer.register_timer(&mut host, 1_000).unwrap(); | ||
assert_eq!(timer_id, TimerId(5)); | ||
Ok(MyPluginMainThread) | ||
} | ||
} | ||
|
||
static MY_PLUGIN_ENTRY: EntryDescriptor = clack_entry!(SinglePluginEntry<MyPlugin>); | ||
|
||
struct MyHost; | ||
|
||
impl Host for MyHost { | ||
type Shared<'a> = MyHostShared<'a>; | ||
type MainThread<'a> = MyHostMainThread<'a>; | ||
type AudioProcessor<'a> = (); | ||
|
||
fn declare_extensions(builder: &mut HostExtensions<Self>, _shared: &Self::Shared<'_>) { | ||
builder.register::<HostTimer>(); | ||
} | ||
} | ||
|
||
struct MyHostShared<'a> { | ||
init: OnceLock<PluginInitializingHandle<'a>>, | ||
} | ||
|
||
impl<'a> HostShared<'a> for MyHostShared<'a> { | ||
fn initializing(&self, instance: PluginInitializingHandle<'a>) { | ||
let _ = self.init.set(instance); | ||
} | ||
|
||
fn request_restart(&self) { | ||
unimplemented!() | ||
} | ||
fn request_process(&self) { | ||
unimplemented!() | ||
} | ||
fn request_callback(&self) { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
struct MyHostMainThread<'a> { | ||
shared: &'a MyHostShared<'a>, | ||
timer_registered: bool, | ||
} | ||
|
||
impl<'a> HostMainThread<'a> for MyHostMainThread<'a> { | ||
fn instantiated(&mut self, _instance: PluginMainThreadHandle<'a>) {} | ||
} | ||
|
||
impl<'a> HostTimerImpl for MyHostMainThread<'a> { | ||
fn register_timer(&mut self, period_ms: u32) -> Result<TimerId, TimerError> { | ||
assert_eq!(period_ms, 1000); | ||
|
||
let handle = self | ||
.shared | ||
.init | ||
.get() | ||
.expect("Initializing should have been called already!"); | ||
|
||
handle | ||
.get_extension::<PluginTimer>() | ||
.expect("Plugin should implement Timer extension!"); | ||
|
||
self.timer_registered = true; | ||
Ok(TimerId(5)) | ||
} | ||
|
||
fn unregister_timer(&mut self, _timer_id: TimerId) -> Result<(), TimerError> { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
#[test] | ||
fn can_call_host_methods_during_init() { | ||
let host = HostInfo::new("host", "host", "host", "1.0").unwrap(); | ||
|
||
let bundle = unsafe { PluginBundle::load_from_raw(&MY_PLUGIN_ENTRY, "/my/plugin") }.unwrap(); | ||
let instance = PluginInstance::<MyHost>::new( | ||
|_| MyHostShared { | ||
init: OnceLock::new(), | ||
}, | ||
|shared| MyHostMainThread { | ||
shared, | ||
timer_registered: false, | ||
}, | ||
&bundle, | ||
c"my.plugin", | ||
&host, | ||
) | ||
.unwrap(); | ||
|
||
// Timer should have already been registered by the plugin during init(). | ||
assert!(instance.main_thread_host_data().timer_registered); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters