I got very confused and started mucking about with "spawn" when in fact all I needed was the "inline" extension type in orcx that allows the interpreter to expose custom constants.
59 lines
1.4 KiB
Rust
59 lines
1.4 KiB
Rust
use std::rc::Rc;
|
|
use std::time::Duration;
|
|
|
|
use futures::future::LocalBoxFuture;
|
|
use orchid_base::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, delay: Duration, fut: LocalBoxFuture<'static, ()>) {
|
|
(self.0.spawn)(self.0.data, delay.as_millis().try_into().unwrap(), future_to_vt(fut))
|
|
}
|
|
}
|
|
|
|
pub fn orchid_extension_main_body(cx: ExtCx, builder: ExtensionBuilder) {
|
|
let spawner = Rc::new(Spawner(cx.spawner));
|
|
let spawner2 = spawner.clone();
|
|
spawner2.spawn(
|
|
Duration::ZERO,
|
|
Box::pin(builder.run(ExtPort {
|
|
input: Box::pin(cx.input),
|
|
output: Box::pin(cx.output),
|
|
log: Box::pin(cx.log),
|
|
spawn: Rc::new(move |delay, fut| spawner.spawn(delay, 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);
|
|
}
|
|
};
|
|
}
|