use bstack::{BStack, BStackRange};
use crate::primitives::{EightCC, OwnershipKind};
use crate::types::traits::{BStackBlock, BStackCast};
use super::RttiResult;
use super::{HEADER_TAG_OFFSET, Shape, add_off};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct AnyRef {
tag: EightCC,
offset: u64,
}
impl AnyRef {
#[inline(always)]
pub unsafe fn new(tag: EightCC, offset: u64) -> Self {
Self { tag, offset }
}
pub fn from_block(data: &BStack, offset: u64) -> RttiResult<Self> {
let mut tag = [0u8; 8];
data.get_into(add_off(offset, HEADER_TAG_OFFSET)?, &mut tag)?;
Ok(Self {
tag: EightCC(tag),
offset,
})
}
#[inline(always)]
pub fn tag(&self) -> EightCC {
self.tag
}
#[inline(always)]
pub fn offset(&self) -> u64 {
self.offset
}
#[inline(always)]
pub fn is<T: BStackBlock>(&self) -> bool {
self.tag == <T as BStackCast>::eightcc()
}
pub fn downcast<T: BStackBlock>(&self) -> Option<T> {
self.is::<T>().then(|| unsafe {
T::from_range(BStackRange::new(
self.offset,
core::mem::size_of::<T::OnDisk>() as u64,
))
})
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
Pod(Box<[u8]>),
Block {
tag: EightCC,
fields: Box<[(String, Value)]>,
},
Enum {
tag: EightCC,
variant: String,
fields: Box<[(String, Value)]>,
},
Ref { tag: EightCC, offset: u64 },
Foreign {
tag: EightCC,
kind: OwnershipKind,
file_id: u64,
offset: u64,
},
Null,
Some(Box<Value>),
Array(Box<[Value]>),
Vec(Box<[Value]>),
Tuple(Box<[Value]>),
Class(Box<[u8]>),
}
#[derive(Clone, Debug, PartialEq)]
pub struct VecRef {
pub data_off: u64,
pub data_size: u64,
pub elem: Shape,
}
#[derive(Clone, Debug, PartialEq)]
pub enum Moved {
Pod(Box<[u8]>),
Ref(Option<AnyRef>),
Weak(Option<AnyRef>),
Vec(Option<VecRef>),
List(Box<[Option<AnyRef>]>),
WeakList(Box<[Option<AnyRef>]>),
Foreign {
tag: EightCC,
kind: OwnershipKind,
file_id: u64,
offset: u64,
},
ForeignList(Box<[ForeignPtr]>),
Tuple(Box<[Moved]>),
Array(Box<[Moved]>),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ForeignPtr {
pub tag: EightCC,
pub kind: OwnershipKind,
pub file_id: u64,
pub offset: u64,
}
pub(in crate::rtti) enum Resolved {
Instance { offset: u64, shape: Shape },
Class { tag: EightCC, name: String },
}