Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add BytecodeLocked::original_bytecode #1037

Merged
merged 1 commit into from
Feb 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions crates/interpreter/src/interpreter/analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ fn analyze(code: &[u8]) -> JumpMap {
#[derive(Clone)]
pub struct BytecodeLocked {
bytecode: Bytes,
len: usize,
original_len: usize,
jump_map: JumpMap,
}

impl fmt::Debug for BytecodeLocked {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BytecodeLocked")
.field("bytecode", &self.bytecode)
.field("len", &self.len)
.field("original_len", &self.original_len)
.field(
"jump_map",
&crate::primitives::hex::encode(self.jump_map.as_slice()),
Expand All @@ -96,7 +96,7 @@ impl TryFrom<Bytecode> for BytecodeLocked {
if let BytecodeState::Analysed { len, jump_map } = bytecode.state {
Ok(BytecodeLocked {
bytecode: bytecode.bytecode,
len,
original_len: len,
jump_map,
})
} else {
Expand All @@ -115,13 +115,13 @@ impl BytecodeLocked {
/// Returns the length of the bytecode.
#[inline]
pub fn len(&self) -> usize {
self.len
self.original_len
}

/// Returns whether the bytecode is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
self.original_len == 0
}

/// Calculate hash of the bytecode.
Expand All @@ -139,26 +139,33 @@ impl BytecodeLocked {
Bytecode {
bytecode: self.bytecode,
state: BytecodeState::Analysed {
len: self.len,
len: self.original_len,
jump_map: self.jump_map,
},
}
}

/// Returns the bytecode as a byte slice.
/// Returns a reference to the bytecode.
/// Note that this is the analyzed bytecode, which contains extra padding.
#[inline]
pub fn bytecode(&self) -> &[u8] {
pub fn bytecode(&self) -> &Bytes {
&self.bytecode
}

/// Returns the `Bytes` of the original bytecode by slicing the analyzed bytecode.
#[inline]
pub fn original_bytecode(&self) -> Bytes {
self.bytecode.slice(..self.original_len)
}

/// Returns the original bytecode as a byte slice.
#[inline]
pub fn original_bytecode_slice(&self) -> &[u8] {
match self.bytecode.get(..self.len) {
match self.bytecode.get(..self.original_len) {
Some(slice) => slice,
None => debug_unreachable!(
"original_bytecode_slice OOB: {} > {}",
self.len,
self.original_len,
self.bytecode.len()
),
}
Expand Down
Loading