use crate::{
dxb_parser::next_instructions_stack::{
NextInstructionType, NextInstructionsStack,
NotInUnboundedRegularScopeError,
},
runtime::execution::macros::yield_unwrap,
};
use crate::{
global::protocol_structures::{
instructions::{Instruction, NestedInstructionResolutionStrategy},
regular_instructions::RegularInstruction,
type_instructions::TypeInstruction,
},
libs::core::core_lib_id::CoreLibIdIndex,
prelude::*,
};
use alloc::string::FromUtf8Error;
use binrw::{BinRead, io::Cursor};
use core::{cell::RefCell, fmt, fmt::Display, result::Result};
#[derive(Debug)]
pub enum DXBParserError {
InvalidEndpoint(String),
InvalidBinaryCode(u8),
FailedToReadInstructionCode,
InvalidInstructionCode(u8),
ExpectingMoreInstructions,
UnexpectedBytesAfterEndOfInstructions,
FmtError(fmt::Error),
BinRwError(binrw::Error),
FromUtf8Error(FromUtf8Error),
NotInUnboundedRegularScopeError,
InvalidCoreLibId(CoreLibIdIndex),
}
impl PartialEq for DXBParserError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(
DXBParserError::InvalidEndpoint(a),
DXBParserError::InvalidEndpoint(b),
) => a == b,
(
DXBParserError::InvalidBinaryCode(a),
DXBParserError::InvalidBinaryCode(b),
) => a == b,
(
DXBParserError::FailedToReadInstructionCode,
DXBParserError::FailedToReadInstructionCode,
) => true,
(
DXBParserError::InvalidInstructionCode(a),
DXBParserError::InvalidInstructionCode(b),
) => a == b,
(
DXBParserError::ExpectingMoreInstructions,
DXBParserError::ExpectingMoreInstructions,
) => true,
(
DXBParserError::UnexpectedBytesAfterEndOfInstructions,
DXBParserError::UnexpectedBytesAfterEndOfInstructions,
) => true,
(DXBParserError::FmtError(a), DXBParserError::FmtError(b)) => {
a.to_string() == b.to_string()
}
(DXBParserError::BinRwError(a), DXBParserError::BinRwError(b)) => {
a.to_string() == b.to_string()
}
(
DXBParserError::FromUtf8Error(a),
DXBParserError::FromUtf8Error(b),
) => a.to_string() == b.to_string(),
(
DXBParserError::NotInUnboundedRegularScopeError,
DXBParserError::NotInUnboundedRegularScopeError,
) => true,
_ => false,
}
}
}
impl From<fmt::Error> for DXBParserError {
fn from(error: fmt::Error) -> Self {
DXBParserError::FmtError(error)
}
}
impl From<binrw::Error> for DXBParserError {
fn from(error: binrw::Error) -> Self {
DXBParserError::BinRwError(error)
}
}
impl From<FromUtf8Error> for DXBParserError {
fn from(error: FromUtf8Error) -> Self {
DXBParserError::FromUtf8Error(error)
}
}
impl From<NotInUnboundedRegularScopeError> for DXBParserError {
fn from(_: NotInUnboundedRegularScopeError) -> Self {
DXBParserError::NotInUnboundedRegularScopeError
}
}
impl Display for DXBParserError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
DXBParserError::InvalidBinaryCode(code) => {
core::write!(f, "Invalid binary code: {code}")
}
DXBParserError::InvalidEndpoint(endpoint) => {
core::write!(f, "Invalid endpoint: {endpoint}")
}
DXBParserError::FailedToReadInstructionCode => {
core::write!(f, "Failed to read instruction code")
}
DXBParserError::InvalidInstructionCode(code) => {
core::write!(
f,
"Encountered an invalid instruction code: {:2X}",
code
)
}
DXBParserError::FmtError(err) => {
core::write!(f, "Formatting error: {err}")
}
DXBParserError::BinRwError(err) => {
core::write!(f, "Binary read/write error: {err}")
}
DXBParserError::FromUtf8Error(err) => {
core::write!(f, "UTF-8 conversion error: {err}")
}
DXBParserError::ExpectingMoreInstructions => {
core::write!(f, "Expecting more instructions")
}
DXBParserError::UnexpectedBytesAfterEndOfInstructions => {
core::write!(f, "Unexpected bytes after end of instructions")
}
DXBParserError::NotInUnboundedRegularScopeError => {
core::write!(f, "Not in unbounded regular scope error")
}
DXBParserError::InvalidCoreLibId(id) => {
core::write!(f, "Invalid Core Lib Id: {}", id.0)
}
}
}
}
pub fn iterate_instructions(
dxb_body_ref: Rc<RefCell<Vec<u8>>>,
nested_instruction_resolution_strategy: NestedInstructionResolutionStrategy,
) -> impl Iterator<Item = Result<Instruction, DXBParserError>> {
gen move {
let mut next_instructions_stack = NextInstructionsStack::default();
let mut dxb_body = core::mem::take(&mut *dxb_body_ref.borrow_mut());
let mut len = dxb_body.len();
let mut reader = Cursor::new(dxb_body);
loop {
if reader.position() as usize >= len {
if !next_instructions_stack.is_end() {
yield Err(DXBParserError::ExpectingMoreInstructions);
dxb_body = core::mem::take(&mut *dxb_body_ref.borrow_mut());
len = dxb_body.len();
reader = Cursor::new(dxb_body);
continue;
}
return;
}
let next_instruction_type = next_instructions_stack.pop();
let instruction = match next_instruction_type {
NextInstructionType::End => {
if len > reader.position() as usize {
yield Err(DXBParserError::UnexpectedBytesAfterEndOfInstructions);
}
return
},
NextInstructionType::Regular => {
let instruction = yield_unwrap!(RegularInstruction::read(&mut reader));
let instruction = if let RegularInstruction::RemoteExecution(instruction_block_data) = instruction {
match nested_instruction_resolution_strategy {
#[cfg(feature = "disassembler")]
NestedInstructionResolutionStrategy::ResolveNestedScopesFlat | NestedInstructionResolutionStrategy::ResolveNestedScopesTree => {
use crate::global::protocol_structures::instruction_data::{InstructionBlockDataDebugFlat, InstructionBlockDataDebugTree};
let (inner_instructions, err) = crate::disassembler::disassemble_body(
&instruction_block_data.body,
nested_instruction_resolution_strategy
);
if let Some(err) = err {
return yield Err(err);
}
if nested_instruction_resolution_strategy == NestedInstructionResolutionStrategy::ResolveNestedScopesFlat {
RegularInstruction::_RemoteExecutionDebugFlat(InstructionBlockDataDebugFlat {
length: instruction_block_data.length,
injected_variable_count: instruction_block_data.injected_value_count,
injected_values: instruction_block_data.injected_values,
body: inner_instructions.flatten(),
})
}
else {
RegularInstruction::_RemoteExecutionDebugTree(InstructionBlockDataDebugTree {
length: instruction_block_data.length,
injected_variable_count: instruction_block_data.injected_value_count,
injected_values: instruction_block_data.injected_values,
body: inner_instructions,
})
}
}
_ => RegularInstruction::RemoteExecution(instruction_block_data)
}
}
else {
instruction
};
yield_unwrap!(next_instructions_stack.handle_next_expected_instructions(
instruction.get_next_expected_instructions()
));
instruction
}
.into(),
NextInstructionType::Type => {
let instruction = yield_unwrap!(TypeInstruction::read(&mut reader));
yield_unwrap!(next_instructions_stack.handle_next_expected_instructions(
instruction.get_next_expected_instructions()
));
instruction
}
.into(),
};
yield Ok(instruction);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::global::{
instruction_codes::InstructionCode,
protocol_structures::instruction_data::UInt8Data,
};
use core::assert_matches;
fn iterate_dxb(
data: Vec<u8>,
) -> impl Iterator<Item = Result<Instruction, DXBParserError>> {
iterate_instructions(
Rc::new(RefCell::new(data)),
NestedInstructionResolutionStrategy::default(),
)
}
#[test]
fn invalid_instruction_code() {
let data = vec![0xFF]; let mut iterator = iterate_dxb(data);
let result = iterator.next().unwrap();
assert_matches!(
result,
Err(err @ DXBParserError::BinRwError(_)) if err.to_string().contains("invalid instruction code")
);
}
#[test]
fn empty_expect_more_instructions() {
let data = vec![]; let mut iterator = iterate_dxb(data);
let result = iterator.next().unwrap();
assert_matches!(result, Err(DXBParserError::ExpectingMoreInstructions));
}
#[test]
fn valid_uint8_instruction() {
let data = vec![InstructionCode::UINT_8 as u8, 42];
let mut iterator = iterate_dxb(data);
let result = iterator.next().unwrap();
match result {
Ok(Instruction::Regular(RegularInstruction::UInt8(value))) => {
assert_eq!(value.0, 42);
}
_ => panic!("Expected UINT_8 instruction"),
}
assert!(iterator.next().is_none());
}
#[test]
fn valid_short_text_instruction() {
let text = "Hello";
let text_bytes = text.as_bytes();
let mut data =
vec![InstructionCode::SHORT_TEXT as u8, text_bytes.len() as u8];
data.extend_from_slice(text_bytes);
let mut iterator = iterate_dxb(data);
let result = iterator.next().unwrap();
match result {
Ok(Instruction::Regular(RegularInstruction::ShortText(value))) => {
assert_eq!(value.0, "Hello");
}
_ => panic!("Expected SHORT_TEXT instruction"),
}
assert!(iterator.next().is_none());
}
#[test]
fn valid_add_instruction() {
let data = vec![
InstructionCode::ADD as u8,
InstructionCode::UINT_8 as u8,
10,
InstructionCode::UINT_8 as u8,
20,
];
let mut iterator = iterate_dxb(data);
assert!(matches!(
iterator.next().unwrap(),
Ok(Instruction::Regular(RegularInstruction::Add))
));
assert!(matches!(
iterator.next().unwrap(),
Ok(Instruction::Regular(RegularInstruction::UInt8(UInt8Data(
10
))))
));
assert!(matches!(
iterator.next().unwrap(),
Ok(Instruction::Regular(RegularInstruction::UInt8(UInt8Data(
20
))))
));
assert!(iterator.next().is_none());
}
#[test]
fn error_for_partial_instruction() {
let data = vec![InstructionCode::UINT_16 as u8, 0x34]; let mut iterator = iterate_dxb(data);
let result = iterator.next().unwrap();
assert!(matches!(result, Err(DXBParserError::BinRwError(_))));
}
#[test]
fn expect_more_instructions_after_partial() {
let data = vec![InstructionCode::LIST as u8, 0x02, 0x00, 0x00, 0x00]; let data_ref = Rc::new(RefCell::new(data));
let mut iterator = iterate_instructions(
data_ref.clone(),
NestedInstructionResolutionStrategy::default(),
);
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(RegularInstruction::List(_)))
));
let result = iterator.next().unwrap();
assert!(matches!(
result,
Err(DXBParserError::ExpectingMoreInstructions)
));
let new_data = vec![
InstructionCode::UINT_8 as u8, 10,
InstructionCode::UINT_8 as u8, 20,
];
*data_ref.borrow_mut() = new_data;
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(RegularInstruction::UInt8(_)))
));
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(RegularInstruction::UInt8(_)))
));
assert!(iterator.next().is_none());
}
#[test]
fn unbounded_expect_more_instructions() {
let data = vec![InstructionCode::UNBOUNDED_STATEMENTS as u8]; let data_ref = Rc::new(RefCell::new(data));
let mut iterator = iterate_instructions(
data_ref.clone(),
NestedInstructionResolutionStrategy::default(),
);
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(
RegularInstruction::UnboundedStatements
))
));
let result = iterator.next().unwrap();
assert!(matches!(
result,
Err(DXBParserError::ExpectingMoreInstructions)
));
let new_data = vec![
InstructionCode::UINT_8 as u8, 42,
InstructionCode::UNBOUNDED_STATEMENTS_END as u8, 0x00,
];
*data_ref.borrow_mut() = new_data;
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(RegularInstruction::UInt8(_)))
));
let result = iterator.next().unwrap();
assert!(matches!(
result,
Ok(Instruction::Regular(
RegularInstruction::UnboundedStatementsEnd(_)
))
));
assert!(iterator.next().is_none());
}
}