54 lines
1.2 KiB
Rust
54 lines
1.2 KiB
Rust
use std::rc::Rc;
|
|
|
|
use futures::future::LocalBoxFuture;
|
|
use orchid_base::binary::future_to_vt;
|
|
|
|
use crate::api;
|
|
use crate::entrypoint::ExtensionBuilder;
|
|
use crate::ext_port::ExtPort;
|
|
|
|
pub type ExtCx = api::binary::ExtensionContext;
|
|
|
|
struct Spawner(api::binary::SpawnerBin);
|
|
impl Drop for Spawner {
|
|
fn drop(&mut self) { (self.0.drop)(self.0.data) }
|
|
}
|
|
impl Spawner {
|
|
pub fn spawn(&self, fut: LocalBoxFuture<'static, ()>) {
|
|
(self.0.spawn)(self.0.data, future_to_vt(fut))
|
|
}
|
|
}
|
|
|
|
pub fn orchid_extension_main_body(cx: ExtCx, builder: ExtensionBuilder) {
|
|
let spawner = Spawner(cx.spawner);
|
|
builder.build(ExtPort {
|
|
input: Box::pin(cx.input),
|
|
output: Box::pin(cx.output),
|
|
log: Box::pin(cx.log),
|
|
spawn: Rc::new(move |fut| spawner.spawn(fut)),
|
|
});
|
|
}
|
|
|
|
/// Generate entrypoint for the dylib extension loader
|
|
///
|
|
/// # Usage
|
|
///
|
|
/// ```
|
|
/// #[macro_use]
|
|
/// use orchid_extension::dylib_main;
|
|
/// use orchid_extension::entrypoint::ExtensionBuilder;
|
|
///
|
|
/// dylib_main! {
|
|
/// ExtensionBuilder::new("orchid-std::main")
|
|
/// }
|
|
/// ```
|
|
#[macro_export]
|
|
macro_rules! dylib_main {
|
|
($builder:expr) => {
|
|
#[unsafe(no_mangle)]
|
|
pub extern "C" fn orchid_extension_main(cx: ::orchid_api::binary::ExtensionContext) {
|
|
$crate::binary::orchid_extension_main_body(cx, $builder);
|
|
}
|
|
};
|
|
}
|