#![allow(clippy::missing_safety_doc)]
use std::ffi::{c_char, CStr};
use std::panic::{catch_unwind, AssertUnwindSafe};
use std::sync::Arc;
use crate::context::Context;
use crate::graph::GraphInner;
use crate::kernel::Contract;
use crate::runtime::last_error;
use crate::status::{code, Error};
pub const ABI_VERSION: u32 = 4;
pub const INVALID_ID: usize = usize::MAX;
pub const LMFLOW_POLLER_BLOCK: i32 = 0;
pub const LMFLOW_POLLER_DROP_OLDEST: i32 = 1;
pub const LMFLOW_POLLER_DROP_NEWEST: i32 = 2;
pub const LMFLOW_POLLER_LATEST: i32 = 3;
fn guard<F: FnOnce() -> i32>(f: F) -> i32 {
match catch_unwind(AssertUnwindSafe(f)) {
Ok(v) => v,
Err(_) => {
last_error::set("internal engine panic (caught at the FFI boundary)");
code::PANIC
}
}
}
fn guard_val<T, F: FnOnce() -> T>(default: T, f: F) -> T {
match catch_unwind(AssertUnwindSafe(f)) {
Ok(v) => v,
Err(_) => {
last_error::set("internal engine panic (caught at the FFI boundary)");
default
}
}
}
fn fail(e: Error) -> i32 {
last_error::set(&e.to_string());
e.code()
}
fn to_status(r: crate::status::Result<()>) -> i32 {
match r {
Ok(()) => code::OK,
Err(e) => fail(e),
}
}
unsafe fn cstr<'a>(p: *const c_char) -> Option<&'a str> {
if p.is_null() {
return None;
}
CStr::from_ptr(p).to_str().ok()
}
unsafe fn ctx_ref<'a>(c: *const LMFlowContext) -> Option<&'a Context> {
if c.is_null() {
None
} else {
Some(&*(c as *const Context))
}
}
unsafe fn ctx_mut<'a>(c: *mut LMFlowContext) -> Option<&'a mut Context> {
if c.is_null() {
None
} else {
Some(&mut *(c as *mut Context))
}
}
unsafe fn contract_mut<'a>(c: *mut LMFlowContract) -> Option<&'a mut Contract> {
if c.is_null() {
None
} else {
Some(&mut *(c as *mut Contract))
}
}
#[repr(C)]
pub struct LMFlowGraph {
_private: [u8; 0],
}
#[repr(C)]
pub struct LMFlowInput {
_private: [u8; 0],
}
#[repr(C)]
pub struct LMFlowPoller {
_private: [u8; 0],
}
#[repr(C)]
pub struct LMFlowContext {
_private: [u8; 0],
}
#[repr(C)]
pub struct LMFlowContract {
_private: [u8; 0],
}
pub struct InputHandle {
graph: Arc<GraphInner>,
edge: usize,
}
mod context;
mod graph;
mod kernel;
mod packet;
pub use context::*;
pub use graph::*;
pub use kernel::*;
pub use packet::*;