Skip to content

Commit

Permalink
conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Nov 25, 2024
2 parents ba72f77 + fb28495 commit ed79976
Show file tree
Hide file tree
Showing 23 changed files with 87 additions and 111 deletions.
2 changes: 1 addition & 1 deletion CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* @edg-l @igaray @azteca1998 @jrchatruc @entropidelic @fmoletta @Oppen @pefontana
* @edg-l @igaray @azteca1998 @jrchatruc @entropidelic @fmoletta @Oppen @pefontana @gabrielbosio
14 changes: 9 additions & 5 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub unsafe extern "C" fn cairo_native__dict_get(
key[31] &= 0x0F; // Filter out first 4 bits (they're outside an i252).

dict.count += 1;
dict.inner.entry(key).or_insert(std::ptr::null_mut()) as *mut _ as *mut c_void
std::ptr::from_mut(dict.inner.entry(key).or_insert(std::ptr::null_mut())).cast::<c_void>()
}

/// Compute the total gas refund for the dictionary at squash time.
Expand Down Expand Up @@ -502,7 +502,7 @@ pub extern "C" fn cairo_native__get_costs_builtin() -> *const u64 {
BUILTIN_COSTS.get()
}

/// Utility methods for the print runtime function
// Utility methods for the print runtime function

/// Formats the given felts as a debug string.
fn format_for_debug(mut felts: IntoIter<Felt>) -> String {
Expand Down Expand Up @@ -536,10 +536,12 @@ pub struct FormattedItem {
}
impl FormattedItem {
/// Returns the formatted item as is.
#[must_use]
pub fn get(self) -> String {
self.item
}
/// Wraps the formatted item with quote, if it's a string. Otherwise returns it as is.
#[must_use]
pub fn quote_if_string(self) -> String {
if self.is_string {
format!("\"{}\"", self.item)
Expand Down Expand Up @@ -585,7 +587,7 @@ fn format_short_string(value: &Felt) -> String {
}

/// Tries to format a string, represented as a sequence of `Felt252`s.
/// If the sequence is not a valid serialization of a ByteArray, returns None and doesn't change the
/// If the sequence is not a valid serialization of a `ByteArray`, returns None and doesn't change the
/// given iterator (`values`).
fn try_format_string<T>(values: &mut T) -> Option<String>
where
Expand Down Expand Up @@ -618,6 +620,7 @@ where
}

/// Converts a bigint representing a felt252 to a Cairo short-string.
#[must_use]
pub fn as_cairo_short_string(value: &Felt) -> Option<String> {
let mut as_string = String::default();
let mut is_end = false;
Expand All @@ -637,10 +640,11 @@ pub fn as_cairo_short_string(value: &Felt) -> Option<String> {

/// Converts a bigint representing a felt252 to a Cairo short-string of the given length.
/// Nulls are allowed and length must be <= 31.
#[must_use]
pub fn as_cairo_short_string_ex(value: &Felt, length: usize) -> Option<String> {
if length == 0 {
return if value.is_zero() {
Some("".to_string())
Some(String::new())
} else {
None
};
Expand All @@ -658,7 +662,7 @@ pub fn as_cairo_short_string_ex(value: &Felt, length: usize) -> Option<String> {
return None;
}

let mut as_string = "".to_string();
let mut as_string = String::new();
for byte in bytes {
if byte == 0 {
as_string.push_str(r"\0");
Expand Down
39 changes: 13 additions & 26 deletions src/arch/aarch64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,60 +104,50 @@ impl AbiArgument for i32 {

impl AbiArgument for u64 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 64 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 64 {
align_to(buffer, get_integer_layout(64).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for i64 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 64 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 64 {
align_to(buffer, get_integer_layout(64).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for u128 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 56 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 56 {
align_to(buffer, get_integer_layout(128).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for i128 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 56 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 56 {
align_to(buffer, get_integer_layout(128).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for Felt {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 56 {
buffer.extend_from_slice(&self.to_bytes_le());
} else {
if buffer.len() >= 56 {
align_to(buffer, get_integer_layout(252).align());
buffer.extend_from_slice(&self.to_bytes_le());
}
buffer.extend_from_slice(&self.to_bytes_le());
Ok(())
}
}
Expand All @@ -173,14 +163,11 @@ impl AbiArgument for [u8; 31] {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
// The `bytes31` type is treated as a 248-bit integer, therefore it follows the same
// splitting rules as them.
if buffer.len() < 56 {
buffer.extend_from_slice(self);
buffer.push(0);
} else {
if buffer.len() >= 56 {
align_to(buffer, get_integer_layout(252).align());
buffer.extend_from_slice(self);
buffer.push(0);
}
buffer.extend_from_slice(self);
buffer.push(0);
Ok(())
}
}
Expand Down
41 changes: 15 additions & 26 deletions src/arch/x86_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,60 +104,51 @@ impl AbiArgument for i32 {

impl AbiArgument for u64 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 48 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 48 {
align_to(buffer, get_integer_layout(64).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for i64 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 48 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 48 {
align_to(buffer, get_integer_layout(64).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for u128 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 40 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 40 {
align_to(buffer, get_integer_layout(128).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for i128 {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 40 {
buffer.extend_from_slice(&self.to_ne_bytes());
} else {
if buffer.len() >= 40 {
align_to(buffer, get_integer_layout(128).align());
buffer.extend_from_slice(&self.to_ne_bytes());
}
buffer.extend_from_slice(&self.to_ne_bytes());
Ok(())
}
}

impl AbiArgument for Felt {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
if buffer.len() < 40 {
buffer.extend_from_slice(&self.to_bytes_le());
} else {
if buffer.len() >= 40 {
align_to(buffer, get_integer_layout(252).align());
buffer.extend_from_slice(&self.to_bytes_le());
}

buffer.extend_from_slice(&self.to_bytes_le());
Ok(())
}
}
Expand All @@ -173,15 +164,13 @@ impl AbiArgument for [u8; 31] {
fn to_bytes(&self, buffer: &mut Vec<u8>) -> Result<(), Error> {
// The `bytes31` type is treated as a 248-bit integer, therefore it follows the same
// splitting rules as them.
if buffer.len() < 40 {
buffer.extend_from_slice(self);
buffer.push(0);
} else {
if buffer.len() >= 40 {
align_to(buffer, get_integer_layout(252).align());
buffer.extend_from_slice(self);
buffer.push(0);
}

buffer.extend_from_slice(self);
buffer.push(0);

Ok(())
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ fn compile_func(
}

// Store the return value in the return pointer, if there's one.
if let Some(true) = has_return_ptr {
if Some(true) == has_return_ptr {
let (_ret_type_id, ret_type_info) = return_type_infos[0];
let ret_layout = ret_type_info.layout(registry)?;

Expand Down
2 changes: 1 addition & 1 deletion src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl NativeContext {
Self { context }
}

pub fn context(&self) -> &Context {
pub const fn context(&self) -> &Context {
&self.context
}

Expand Down
2 changes: 1 addition & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use cairo_lang_sierra::extensions::{
structure::StructConcreteLibfunc,
};

pub fn libfunc_to_name(value: &CoreConcreteLibfunc) -> &'static str {
pub const fn libfunc_to_name(value: &CoreConcreteLibfunc) -> &'static str {
match value {
CoreConcreteLibfunc::ApTracking(value) => match value {
cairo_lang_sierra::extensions::ap_tracking::ApTrackingConcreteLibfunc::Revoke(_) => {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ pub mod panic {
impl NativeAssertError {
pub fn new(msg: String) -> Self {
let backtrace = Backtrace::capture();
let info = if let BacktraceStatus::Captured = backtrace.status() {
let info = if BacktraceStatus::Captured == backtrace.status() {
BacktraceOrLocation::Backtrace(backtrace)
} else {
BacktraceOrLocation::Location(std::panic::Location::caller())
Expand Down
14 changes: 5 additions & 9 deletions src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ use cairo_lang_sierra::{
use libc::c_void;
use num_bigint::BigInt;
use num_traits::One;
use std::{
alloc::Layout,
arch::global_asm,
ptr::{addr_of_mut, NonNull},
};
use std::{alloc::Layout, arch::global_asm, ptr::NonNull};

mod aot;
mod contract;
Expand Down Expand Up @@ -379,7 +375,8 @@ fn parse_result(
registry,
)?),
CoreTypeConcrete::Box(info) => unsafe {
let ptr = return_ptr.unwrap_or(NonNull::new_unchecked(ret_registers[0] as *mut ()));
let ptr =
return_ptr.unwrap_or_else(|| NonNull::new_unchecked(ret_registers[0] as *mut ()));
let value = Value::from_ptr(ptr, &info.ty, registry)?;
libc_free(ptr.cast().as_ptr());
Ok(value)
Expand Down Expand Up @@ -591,9 +588,8 @@ fn parse_result(
}
}
CoreTypeConcrete::Felt252Dict(_) | CoreTypeConcrete::SquashedFelt252Dict(_) => unsafe {
let ptr = return_ptr.unwrap_or(NonNull::new_unchecked(
addr_of_mut!(ret_registers[0]) as *mut ()
));
let ptr = return_ptr
.unwrap_or_else(|| NonNull::new_unchecked((&raw mut ret_registers[0]) as *mut ()));
Ok(Value::from_ptr(ptr, type_id, registry)?)
},

Expand Down
2 changes: 1 addition & 1 deletion src/executor/aot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ unsafe impl Send for AotNativeExecutor {}
unsafe impl Sync for AotNativeExecutor {}

impl AotNativeExecutor {
pub fn new(
pub const fn new(
library: Library,
registry: ProgramRegistry<CoreType, CoreLibfunc>,
gas_metadata: GasMetadata,
Expand Down
14 changes: 7 additions & 7 deletions src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ pub struct AotContractExecutor {
contract_info: NativeContractInfo,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct NativeContractInfo {
pub version: ContractInfoVersion,
pub entry_points_info: BTreeMap<u64, EntryPointInfo>,
Expand Down Expand Up @@ -118,7 +118,7 @@ pub enum BuiltinType {
}

impl BuiltinType {
pub fn size_in_bytes(&self) -> usize {
pub const fn size_in_bytes(&self) -> usize {
match self {
BuiltinType::Bitwise => 8,
BuiltinType::EcOp => 8,
Expand Down Expand Up @@ -530,9 +530,7 @@ impl AotContractExecutor {
unsafe { libc_free(array_ptr.cast()) };
}

let mut error_msg = None;

if tag != 0 {
let error_msg = if tag != 0 {
let bytes_err: Vec<_> = array_value
.iter()
.flat_map(|felt| felt.to_bytes_be().to_vec())
Expand All @@ -541,8 +539,10 @@ impl AotContractExecutor {
.collect();
let str_error = decode_error_message(&bytes_err);

error_msg = Some(str_error);
}
Some(str_error)
} else {
None
};

// Restore the old ptr and get back our builtincost box and free it.
let our_builtincosts_ptr = set_costs_builtin(old_builtincosts_ptr);
Expand Down
4 changes: 2 additions & 2 deletions src/executor/jit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ impl<'m> JitNativeExecutor<'m> {
})
}

pub fn program_registry(&self) -> &ProgramRegistry<CoreType, CoreLibfunc> {
pub const fn program_registry(&self) -> &ProgramRegistry<CoreType, CoreLibfunc> {
&self.registry
}

pub fn module(&self) -> &Module<'m> {
pub const fn module(&self) -> &Module<'m> {
&self.module
}

Expand Down
Loading

0 comments on commit ed79976

Please sign in to comment.