use crate::any::AnyView;
use crate::collections::array::Array;
use crate::derive::Object;
use crate::error::{Error, Result};
use crate::function::Function;
use crate::object::{self, Object, ObjectArc, ObjectRef, ObjectRefCore};
use crate::reflection::{AccessPath, TypeAttrColumn};
use crate::tvm_ffi_sys::{TVMFFIAny, TVMFFIObject, TVMFFITypeIndex};
use crate::type_traits::AnyCompatible;
#[repr(C)]
#[derive(Object)]
#[object(type_key = "ffi.VisitErrorContext", final)]
pub(crate) struct VisitErrorContextObj {
base_: Object,
reverse_visit_pattern: Array<Option<ObjectRef>>,
prev_error_context: Option<ObjectRef>,
}
const ENRICH_VISIT_ERROR_ATTR: &str = "__ffi_enrich_visit_error__";
fn as_context(context: &ObjectRef) -> Option<&VisitErrorContextObj> {
let object = ObjectArc::as_ptr(ObjectRef::data(context));
unsafe {
object::is_instance_of::<VisitErrorContextObj>((*object.cast::<TVMFFIObject>()).type_index)
.then(|| &*object.cast::<VisitErrorContextObj>())
}
}
pub fn find_access_paths<R>(root: &R, error: &Error) -> Result<Array<AccessPath>>
where
R: ObjectRefCore + AnyCompatible,
{
let Some(context) = error
.extra_context()
.filter(|context| as_context(context).is_some())
else {
return Ok(Array::default());
};
Function::get_global("ffi.VisitErrorContext.FindAccessPaths")?
.call_packed(&[
AnyView::from(root),
AnyView::from(context),
AnyView::from(&false),
])?
.try_into()
}
#[cold]
#[inline(never)]
pub(crate) fn enrich_visit_error(error: Error, root: TVMFFIAny) -> Error {
if root.type_index < TVMFFITypeIndex::kTVMFFIStaticObjectBegin as i32
|| error.extra_context().and_then(as_context).is_none()
{
return error;
}
let Some(hook) = TypeAttrColumn::lookup(ENRICH_VISIT_ERROR_ATTR)
.and_then(|column| column.get(root.type_index))
.and_then(|hook| Function::try_from(hook).ok())
else {
return error;
};
let root = unsafe { AnyView::from_raw_ffi_any(root) };
match hook
.call_packed(&[AnyView::from(&error), root])
.and_then(Error::try_from)
{
Ok(enriched) => enriched,
Err(_) => error,
}
}
#[cold]
pub(crate) fn update_visit_error_context(mut error: Error, raw: TVMFFIAny) -> Error {
if raw.type_index < TVMFFITypeIndex::kTVMFFIStaticObjectBegin as i32 {
return error;
}
let Ok(node) = ObjectRef::try_from(unsafe { AnyView::from_raw_ffi_any(raw) }) else {
return error;
};
if let Some(context) = error.extra_context().and_then(as_context)
&& context
.reverse_visit_pattern
.iter()
.next_back()
.and_then(Option::as_ref)
.is_some_and(|last| last.same_as(&node))
{
return error;
}
let slot = error.extra_context_mut();
let previous = slot.take();
let context = match previous {
Some(previous) if as_context(&previous).is_some() => {
let mut context = unsafe {
ObjectArc::from_raw(
ObjectArc::into_raw(ObjectRef::into_data(previous))
.cast::<VisitErrorContextObj>(),
)
};
if !ObjectArc::is_unique(&context) {
context = ObjectArc::new(VisitErrorContextObj {
base_: Object::new(),
reverse_visit_pattern: context.reverse_visit_pattern.clone(),
prev_error_context: context.prev_error_context.clone(),
});
}
unsafe {
(*ObjectArc::as_mut_ptr(&mut context))
.reverse_visit_pattern
.push(Some(node))
};
context
}
previous => ObjectArc::new(VisitErrorContextObj {
base_: Object::new(),
reverse_visit_pattern: Array::from_vec(vec![Some(node)]),
prev_error_context: previous,
}),
};
*slot = Some(unsafe {
ObjectRef::from_data(ObjectArc::from_raw(
ObjectArc::into_raw(context).cast::<Object>(),
))
});
error
}