Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions selene-core/rust/runtime/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
//! plugins as rust crates.
//!
//! See `selene-simple-runtime-plugin` for a fully worked example.
use std::{ffi, mem, sync::Arc};
use anyhow::{Result, anyhow};
use std::{
ffi, mem,
panic::{self, UnwindSafe},
sync::Arc,
};

use crate::utils::{convert_cargs_to_strings, result_of_errno_to_errno, result_to_errno};

Expand All @@ -27,15 +32,18 @@ impl<F: RuntimeInterfaceFactory> Helper<F> {
unsafe { Box::from_raw(instance as *mut F::Interface) }
}

fn with_runtime_instance<T>(
fn with_runtime_instance<T: UnwindSafe>(
instance: RuntimeInstance,
mut go: impl FnMut(&mut F::Interface) -> T,
) -> T {
assert!(!instance.is_null());
let mut r = unsafe { Self::from_runtime_instance(instance) };
let t = go(&mut r);
mem::forget(r);
t
mut go: impl FnMut(&mut F::Interface) -> Result<T> + UnwindSafe,
) -> Result<T> {
panic::catch_unwind(move || {
assert!(!instance.is_null());
let mut r = unsafe { Self::from_runtime_instance(instance) };
let t = go(&mut r);
mem::forget(r);
t
})
.map_err(|_| anyhow!("runtime panicked"))?
}

fn factory(&self) -> Arc<F> {
Expand Down
Loading