Skip to content

Commit

Permalink
Finish buf refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Wuerker committed Aug 25, 2023
1 parent 79cc5c2 commit 9896787
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ error: the struct `OutOfReachMarker` is private
6fn emit(self, _ val: OutOfReachMarker) {
^^^^^^^^^^^^^^^^ this struct is not `pub`
┌─ src/context.fe:13:8
┌─ src/context.fe:14:8
13struct OutOfReachMarker {}
14struct OutOfReachMarker {}
---------------- `OutOfReachMarker` is defined here
= `OutOfReachMarker` can only be used within `context`
Expand Down
30 changes: 18 additions & 12 deletions crates/library/std/src/buf.fe
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub struct MemoryBuffer {
}

/// The start of the buffer in EVM memory.
pub fn offset(self) -> u256 {
pub unsafe fn offset(self) -> u256 {
return self.offset
}

Expand Down Expand Up @@ -146,14 +146,16 @@ pub struct MemoryBufferWriter {
return self.cur.remainder()
}

pub fn write_offset(mut self, len: u256) -> u256 {
pub unsafe fn write_offset(mut self, len: u256) -> u256 {
return self.buf.offset() + self.cur.advance(len)
}

pub fn write_n(mut self, value: u256, len: u256) {
let offset: u256 = self.write_offset(len)
let shifted_value: u256 = evm::shl(bits: 256 - len * 8, value)
unsafe { evm::mstore(offset, value: shifted_value) }
unsafe {
let offset: u256 = self.write_offset(len)
evm::mstore(offset, value: shifted_value)
}
}

pub fn write_buf(mut self, buf: MemoryBuffer) {
Expand Down Expand Up @@ -183,8 +185,10 @@ pub trait MemoryBufferWrite {

impl MemoryBufferWrite for u256 {
fn write_buf(self, mut writer: MemoryBufferWriter) {
let offset: u256 = writer.write_offset(len: 32)
unsafe { evm::mstore(offset, value: self) }
unsafe {
let offset: u256 = writer.write_offset(len: 32)
evm::mstore(offset, value: self)
}
}
}

Expand Down Expand Up @@ -214,8 +218,10 @@ impl MemoryBufferWrite for u16 {

impl MemoryBufferWrite for u8 {
fn write_buf(self, mut writer: MemoryBufferWriter) {
let offset: u256 = writer.write_offset(len: 1)
unsafe { evm::mstore8(offset, value: self) }
unsafe {
let offset: u256 = writer.write_offset(len: 1)
evm::mstore8(offset, value: self)
}
}
}

Expand All @@ -239,13 +245,13 @@ pub struct MemoryBufferReader {
return self.cur.remainder()
}

fn read_offset(mut self, len: u256) -> u256 {
unsafe fn read_offset(mut self, len: u256) -> u256 {
return self.buf.offset() + self.cur.advance(len)
}

fn read_n(mut self, len: u256) -> u256 {
let offset: u256 = self.read_offset(len)
unsafe {
let offset: u256 = self.read_offset(len)
let value: u256 = evm::mload(offset)
return evm::shr(bits: 256 - len * 8, value)
}
Expand All @@ -272,8 +278,8 @@ pub struct MemoryBufferReader {
}

pub fn read_u256(mut self) -> u256 {
let offset: u256 = self.read_offset(len: 32)
unsafe {
let offset: u256 = self.read_offset(len: 32)
let value: u256 = evm::mload(offset)
return value
}
Expand Down Expand Up @@ -334,7 +340,7 @@ pub struct RawCallBuffer {
return self.output_len
}

pub fn offset(self) -> u256 {
pub unsafe fn offset(self) -> u256 {
return self.buf.offset()
}

Expand Down
1 change: 1 addition & 0 deletions crates/library/std/src/context.fe
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use ingot::error::{
}
use ingot::buf::{
RawCallBuffer,
MemoryBuffer,
MemoryBufferReader,
MemoryBufferWriter
}
Expand Down
Loading

0 comments on commit 9896787

Please sign in to comment.