use crate::{
global::{
operators::ModificationOperator,
protocol_structures::injected_values::InjectedValueDeclaration,
},
instruction::type_instruction_codes::{
TypeLocalOrShared, TypeMutabilityCode, TypeOwnershipCode,
},
prelude::*,
shared_values::{
PointerAddress, ReferenceMutability, RemotePointerAddress,
SelfOwnedPointerAddress, SharedContainerMutability,
},
types::{
type_definition::callable::CallableKind,
type_definition_with_metadata::TypeMetadata,
},
values::core_values::{decimal::Decimal, integer::Integer},
};
use alloc::string::FromUtf8Error;
use binrw::{
BinRead, BinResult, BinWrite, Endian,
io::{Read, Seek, Write},
meta::{EndianKind, ReadEndian},
};
use core::{fmt::Display, ops::Deref};
use itertools::Itertools;
use modular_bitfield::{bitfield, prelude::B4};
#[cfg(feature = "disassembler")]
mod disassembler_instruction_data;
use crate::{
libs::core::{
core_lib_id::CoreLibIdIndex,
type_id::{CoreLibBaseTypeId, CoreLibTypeId, CoreLibVariantTypeId},
},
values::core_values::integer::typed_integer::IntegerTypeVariant,
};
#[cfg(feature = "disassembler")]
pub use disassembler_instruction_data::*;
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Int8Data(pub i8);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Int16Data(pub i16);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Int32Data(pub i32);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Int64Data(pub i64);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Int128Data(pub i128);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UInt8Data(pub u8);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UInt16Data(pub u16);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UInt32Data(pub u32);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UInt64Data(pub u64);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UInt128Data(pub u128);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Float32Data(pub f32);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct Float64Data(pub f64);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct FloatAsInt16Data(pub i16);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct FloatAsInt32Data(pub i32);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct DecimalData(pub Decimal);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct IntegerData(pub Integer);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct InstantData(pub i128);
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ShortTextDataRaw {
pub length: u8,
#[br(count = length)]
pub text: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct ShortTextData(pub String);
impl ShortTextData {
pub fn new(text: impl Into<String>) -> Self {
ShortTextData(text.into())
}
}
impl Deref for ShortTextData {
type Target = String;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl From<&ShortTextData> for ShortTextDataRaw {
fn from(value: &ShortTextData) -> Self {
let bytes = value.0.as_bytes();
Self {
length: bytes.len() as u8,
text: bytes.to_vec(),
}
}
}
impl TryFrom<ShortTextDataRaw> for ShortTextData {
type Error = FromUtf8Error;
fn try_from(raw: ShortTextDataRaw) -> Result<Self, Self::Error> {
let string = String::from_utf8(raw.text)?;
Ok(ShortTextData(string))
}
}
impl BinWrite for ShortTextData {
type Args<'a> = ();
fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<()> {
let raw = ShortTextDataRaw::from(self);
raw.write_options(writer, endian, ())
}
}
impl BinRead for ShortTextData {
type Args<'a> = ();
fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<Self> {
let raw = ShortTextDataRaw::read_options(reader, endian, ())?;
raw.try_into().map_err(|_| binrw::Error::AssertFail {
pos: reader.stream_position().unwrap_or(0),
message: "Invalid UTF-8 string".to_string(),
})
}
}
impl ReadEndian for ShortTextData {
const ENDIAN: EndianKind = EndianKind::Endian(Endian::Little);
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CallableSignatureData {
pub name: ShortTextData, pub kind: CallableKind,
pub parameter_count: u8,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub requires_async: bool,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub has_rest_parameter: bool,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub has_return_type: bool,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub has_yeet_type: bool,
#[br(count = parameter_count)]
pub parameter_names: Vec<ShortTextData>,
#[br(if(has_rest_parameter))]
pub rest_parameter_name: Option<ShortTextData>,
}
impl Display for CallableSignatureData {
fn fmt(
&self,
formatter: &mut core::fmt::Formatter,
) -> Result<(), core::fmt::Error> {
write!(formatter, "[")?;
write!(formatter, "kind: {}, ", self.kind)?;
write!(formatter, "requires_async: {}, ", self.requires_async)?;
write!(
formatter,
"parameters: [{}], ",
self.parameter_names.iter().map(|n| &n.0).join(", ")
)?;
if let Some(rest) = &self.rest_parameter_name {
write!(formatter, ", rest_parameter: {}, ", rest.0)?;
}
write!(formatter, "has_return_type: {}, ", self.has_return_type)?;
write!(formatter, "has_yeet_type: {}", self.has_yeet_type)?;
write!(formatter, "]")?;
Ok(())
}
}
impl CallableSignatureData {
pub fn total_type_count(&self) -> u32 {
let mut count = self.parameter_count as u32;
if self.has_rest_parameter {
count += 1;
}
if self.has_return_type {
count += 1;
}
if self.has_yeet_type {
count += 1;
}
count
}
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CallableDeclarationData {
pub signature: CallableSignatureData,
pub body: InstructionBlockData,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct EntityTypeDefinitionData {
pub name: ShortTextData,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ClassifiedValueData {
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub has_tag: bool,
#[br(if(has_tag))]
pub(crate) tag: Option<ShortTextData>,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub(crate) tag_value_is_empty: bool,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub(crate) is_entity: bool,
pub impl_count: u32,
#[br(count = impl_count)]
pub(crate) impls: Vec<PointerAddress>,
}
impl ClassifiedValueData {
pub fn new_tagged(tag: String, tag_value_is_empty: bool) -> Self {
Self {
has_tag: true,
tag: Some(ShortTextData(tag)),
tag_value_is_empty,
is_entity: false,
impl_count: 0,
impls: Vec::new(),
}
}
pub fn new_entity() -> Self {
Self {
has_tag: false,
tag: None,
tag_value_is_empty: false,
is_entity: true,
impl_count: 0,
impls: Vec::new(),
}
}
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CallableData {
pub signature: CallableSignatureData,
pub body: CallableDataBody,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CallableDataBody {
pub injected_value_count: u32,
pub length: u32, #[br(count = length)]
pub body: Vec<u8>,
}
impl Display for CallableDataBody {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[length: {}, injected_value_count: {}]",
self.length, self.injected_value_count,
)
}
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct TextDataRaw {
pub length: u32,
#[br(count = length)]
pub text: Vec<u8>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct TextData(pub String);
impl From<String> for TextData {
fn from(value: String) -> Self {
TextData(value)
}
}
impl From<&str> for TextData {
fn from(value: &str) -> Self {
TextData(value.to_string())
}
}
impl From<&TextData> for TextDataRaw {
fn from(value: &TextData) -> Self {
let bytes = value.0.as_bytes();
Self {
length: bytes.len() as u32,
text: bytes.to_vec(),
}
}
}
impl TryFrom<TextDataRaw> for TextData {
type Error = FromUtf8Error;
fn try_from(raw: TextDataRaw) -> Result<Self, Self::Error> {
let string = String::from_utf8(raw.text)?;
Ok(TextData(string))
}
}
impl BinWrite for TextData {
type Args<'a> = ();
fn write_options<W: Write + Seek>(
&self,
writer: &mut W,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<()> {
let raw = TextDataRaw::from(self);
raw.write_options(writer, endian, ())
}
}
impl BinRead for TextData {
type Args<'a> = ();
fn read_options<R: Read + Seek>(
reader: &mut R,
endian: Endian,
_: Self::Args<'_>,
) -> BinResult<Self> {
let raw = TextDataRaw::read_options(reader, endian, ())?;
raw.try_into().map_err(|_| binrw::Error::AssertFail {
pos: reader.stream_position().unwrap_or(0),
message: "Invalid UTF-8 string".to_string(),
})
}
}
impl ReadEndian for TextData {
const ENDIAN: EndianKind = EndianKind::Endian(Endian::Little);
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ShortListData {
pub element_count: u8,
}
pub fn element_size(core_lib_type_id: CoreLibIdIndex) -> u32 {
match CoreLibTypeId::try_from(core_lib_type_id) {
Ok(id) => match id {
CoreLibTypeId::Base(base_id) => match base_id {
CoreLibBaseTypeId::Null => 0,
CoreLibBaseTypeId::Boolean => 1,
_ => unimplemented!(
"Core lib type does not support element size: {}",
core_lib_type_id
),
},
CoreLibTypeId::Variant(variant_id) => match variant_id {
CoreLibVariantTypeId::Integer(integer_variant) => {
match integer_variant {
IntegerTypeVariant::U8 => 1,
IntegerTypeVariant::U16 => 2,
IntegerTypeVariant::U32 => 4,
IntegerTypeVariant::U64 => 8,
IntegerTypeVariant::U128 => 16,
IntegerTypeVariant::I8 => 1,
IntegerTypeVariant::I16 => 2,
IntegerTypeVariant::I32 => 4,
IntegerTypeVariant::I64 => 8,
IntegerTypeVariant::I128 => 16,
_ => unimplemented!(
"Core lib type does not support element size: {}",
core_lib_type_id
),
}
}
_ => unimplemented!(
"Core lib type does not support element size: {}",
core_lib_type_id
),
},
},
Err(_) => unimplemented!(
"Core lib type does not support element size: {}",
core_lib_type_id
),
}
}
pub fn calculate_list_size(
core_lib_type_id: CoreLibIdIndex,
element_count: u32,
) -> u32 {
let element_size = element_size(core_lib_type_id);
element_size * element_count
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CompactListData {
pub element_count: u32,
pub core_lib_type: CoreLibIdIndex,
#[br(count = calculate_list_size(core_lib_type, element_count))]
pub elements: Vec<u8>,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct StatementsData {
pub statements_count: u32,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub terminated: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UnboundedStatementsData {
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub terminated: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ShortStatementsData {
pub statements_count: u8,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub terminated: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ListData {
pub element_count: u32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ShortMapData {
pub element_count: u8,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub allow_only_text_keys: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct MapData {
pub element_count: u32,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub allow_only_text_keys: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct UnionData {
pub element_count: u32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct InstructionCloseAndStore {
pub instruction: Int8Data,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct GetOrCreateRemoteRefData {
pub address: RemotePointerAddress,
pub create_block_size: u64,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct MoveWithValue {
pub mutability: SharedContainerMutability,
pub previous_address: SelfOwnedPointerAddress,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct SharedRef {
pub address: PointerAddress,
pub ref_mutability: ReferenceMutability,
pub container_mutability: SharedContainerMutability,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub is_debug: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct SharedRefWithValue {
pub address: SelfOwnedPointerAddress, pub ref_mutability: ReferenceMutability,
pub container_mutability: SharedContainerMutability,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub is_debug: bool,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ModifySharedContainerValue {
pub operator: ModificationOperator,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct InstructionBlockData {
pub length: u32,
pub injected_value_count: u32,
#[br(count = injected_value_count)]
pub injected_values: Vec<InjectedValueDeclaration>,
#[br(count = length)]
pub body: Vec<u8>,
}
impl Display for InstructionBlockData {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[length: {}, injected_value_count: {}, injected_values: [{}]]",
self.length,
self.injected_value_count,
self.injected_values
.iter()
.map(|v| format!("{}", v))
.join(", "),
)
}
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct SpliceData {
pub start_index: u32,
pub delete_count: u32,
pub insert_count: u32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct JumpData {
pub offset: i32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct JumpWithValueData {
pub offset: i32,
pub value: i32, }
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ApplyData {
pub arg_count: u8,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct CallMethodData {
pub method_name: ShortTextData,
pub arg_count: u8,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ImplMarkersData {
pub impl_count: u8,
#[br(count = impl_count)]
pub impls: Vec<PointerAddress>,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct TaggedTypeData {
pub(crate) tag: ShortTextData,
#[br(map = |x: u8| x != 0)]
#[bw(map = |b: &bool| if *b { 1u8 } else { 0u8 })]
pub(crate) has_type: bool,
}
impl TaggedTypeData {
pub fn new(tag: String, has_type: bool) -> Self {
Self {
tag: ShortTextData(tag),
has_type,
}
}
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct IntersectionData {
pub(crate) element_count: u32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct ListSliceCollectionData {
pub(crate) element_count: u32,
}
#[derive(BinRead, BinWrite, Clone, Debug, PartialEq)]
#[brw(little)]
pub struct TypeReferenceData {
pub address: PointerAddress,
}
#[bitfield]
#[derive(BinWrite, BinRead, Clone, Copy, Debug, PartialEq)]
#[bw(map = |&x| Self::into_bytes(x))]
#[br(map = Self::from_bytes)]
#[brw(little)]
pub struct TypeMetadataBin {
pub ownership: TypeOwnershipCode,
pub mutability: TypeMutabilityCode,
pub type_local_or_shared: TypeLocalOrShared,
#[skip]
_unused: B4,
}
impl From<&TypeMetadataBin> for TypeMetadata {
fn from(value: &TypeMetadataBin) -> Self {
match value.type_local_or_shared() {
TypeLocalOrShared::Local => TypeMetadata::Local {
mutability: (&value.mutability()).into(),
ownership: (&value.ownership()).into(),
},
TypeLocalOrShared::Shared => TypeMetadata::Shared {
mutability: (&value.mutability()).into(),
ownership: (&value.ownership()).into(),
},
}
}
}
impl From<&TypeMetadata> for TypeMetadataBin {
fn from(value: &TypeMetadata) -> Self {
match value {
TypeMetadata::Local {
mutability,
ownership: reference_mutability,
} => Self::new()
.with_type_local_or_shared(TypeLocalOrShared::Local)
.with_mutability(mutability.into())
.with_ownership(reference_mutability.into()),
TypeMetadata::Shared {
mutability,
ownership,
} => Self::new()
.with_type_local_or_shared(TypeLocalOrShared::Shared)
.with_mutability(mutability.into())
.with_ownership(ownership.into()),
}
}
}