use core::ffi;
use dart_sys::{
Dart_CObject, Dart_DeletePersistentHandle_DL, Dart_FinalizableHandle,
Dart_GetError_DL, Dart_Handle, Dart_HandleFinalizer,
Dart_HandleFromPersistent_DL, Dart_InitializeApiDL, Dart_IsError_DL,
Dart_NewFinalizableHandle_DL, Dart_NewPersistentHandle_DL,
Dart_NewUnhandledExceptionError_DL, Dart_PersistentHandle, Dart_Port_DL,
Dart_PostCObject_DL, Dart_PropagateError_DL,
};
pub unsafe fn initialize_api(data: *mut ffi::c_void) -> isize {
unsafe { Dart_InitializeApiDL(data) }
}
pub unsafe fn new_persistent_handle(
object: Dart_Handle,
) -> Dart_PersistentHandle {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_NewPersistentHandle_DL
.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(object) }
}
pub unsafe fn handle_from_persistent(
object: Dart_PersistentHandle,
) -> Dart_Handle {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_HandleFromPersistent_DL
.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(object) }
}
pub unsafe fn delete_persistent_handle(object: Dart_Handle) {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_DeletePersistentHandle_DL
.expect("`dart_api_dl` has not been initialized")
};
unsafe {
func(object);
}
}
pub unsafe fn post_c_object(
port_id: Dart_Port_DL,
message: *mut Dart_CObject,
) -> bool {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_PostCObject_DL.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(port_id, message) }
}
pub unsafe fn new_finalizable_handle(
object: Dart_Handle,
peer: *mut ffi::c_void,
external_allocation_size: isize,
callback: Dart_HandleFinalizer,
) -> Dart_FinalizableHandle {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_NewFinalizableHandle_DL
.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(object, peer, external_allocation_size, callback) }
}
pub unsafe fn is_error(handle: Dart_Handle) -> bool {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_IsError_DL.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(handle) }
}
pub unsafe fn get_error(handle: Dart_Handle) -> *const ffi::c_char {
let func = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_GetError_DL.expect("`dart_api_dl` has not been initialized")
};
unsafe { func(handle) }
}
pub unsafe fn propagate_error(mut handle: Dart_Handle) {
let is_error = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_IsError_DL.expect("`dart_api_dl` has not been initialized")
};
let is_error = unsafe { is_error(handle) };
if !is_error {
let make_unhandled = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_NewUnhandledExceptionError_DL
.expect("`dart_api_dl` has not been initialized")
};
handle = unsafe { make_unhandled(handle) };
}
let propagate = unsafe {
#[expect(clippy::expect_used, reason = "FFI should be initialized")]
Dart_PropagateError_DL.expect("`dart_api_dl` has not been initialized")
};
unsafe {
propagate(handle);
}
}