Skip to content

Commit 8155c8e

Browse files
authored
Merge branch 'main' into feat/compile-contracts
2 parents a0b9bda + 0e96dff commit 8155c8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+3211
-4821
lines changed

examples/easy_api.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cairo_native::{
2-
context::NativeContext, executor::JitNativeExecutor, utils::cairo_to_sierra, values::JitValue,
2+
context::NativeContext, executor::JitNativeExecutor, utils::cairo_to_sierra, Value,
33
};
44
use starknet_types_core::felt::Felt;
55
use std::path::Path;
@@ -18,7 +18,7 @@ fn main() {
1818
let native_program = native_context.compile(&sierra_program, false).unwrap();
1919

2020
// The parameters of the entry point.
21-
let params = &[JitValue::Felt252(Felt::from_bytes_be_slice(b"user"))];
21+
let params = &[Value::Felt252(Felt::from_bytes_be_slice(b"user"))];
2222

2323
// Find the entry point id by its name.
2424
let entry_point = "hello::hello::greet";

examples/invoke.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cairo_native::{
2-
context::NativeContext, executor::JitNativeExecutor, utils::find_entry_point, values::JitValue,
2+
context::NativeContext, executor::JitNativeExecutor, utils::find_entry_point, Value,
33
};
44
use std::path::Path;
55
use tracing_subscriber::{EnvFilter, FmtSubscriber};
@@ -30,7 +30,7 @@ fn main() {
3030

3131
let native_executor = JitNativeExecutor::from_native_module(native_program, Default::default());
3232

33-
let output = native_executor.invoke_dynamic(fn_id, &[JitValue::Felt252(1.into())], None);
33+
let output = native_executor.invoke_dynamic(fn_id, &[Value::Felt252(1.into())], None);
3434

3535
println!();
3636
println!("Cairo program was compiled and executed successfully.");

src/arch.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::{
22
error,
33
starknet::{ArrayAbi, U256},
44
types::TypeBuilder,
5-
values::JitValue,
5+
values::Value,
66
};
77
use bumpalo::Bump;
88
use cairo_lang_sierra::{
@@ -28,7 +28,7 @@ pub trait AbiArgument {
2828
/// A wrapper that implements `AbiArgument` for `JitValue`s. It contains all the required stuff to
2929
/// serialize all possible `JitValue`s.
3030
pub struct JitValueWithInfoWrapper<'a> {
31-
pub value: &'a JitValue,
31+
pub value: &'a Value,
3232
pub type_id: &'a ConcreteTypeId,
3333
pub info: &'a CoreTypeConcrete,
3434

@@ -39,7 +39,7 @@ pub struct JitValueWithInfoWrapper<'a> {
3939
impl<'a> JitValueWithInfoWrapper<'a> {
4040
fn map<'b>(
4141
&'b self,
42-
value: &'b JitValue,
42+
value: &'b Value,
4343
type_id: &'b ConcreteTypeId,
4444
) -> Result<JitValueWithInfoWrapper<'b>, error::Error>
4545
where
@@ -71,7 +71,7 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
7171
heap_ptr.to_bytes(buffer)?;
7272
}
7373
(value, CoreTypeConcrete::Nullable(info)) => {
74-
if matches!(value, JitValue::Null) {
74+
if matches!(value, Value::Null) {
7575
null::<()>().to_bytes(buffer)?;
7676
} else {
7777
let ptr = value.to_jit(self.arena, self.registry, self.type_id)?;
@@ -90,7 +90,7 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
9090
self.map(value, &info.ty)?.to_bytes(buffer)?
9191
}
9292

93-
(JitValue::Array(_), CoreTypeConcrete::Array(_)) => {
93+
(Value::Array(_), CoreTypeConcrete::Array(_)) => {
9494
// TODO: Assert that `info.ty` matches all the values' types.
9595

9696
let abi_ptr = self.value.to_jit(self.arena, self.registry, self.type_id)?;
@@ -101,19 +101,19 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
101101
abi.until.to_bytes(buffer)?;
102102
abi.capacity.to_bytes(buffer)?;
103103
}
104-
(JitValue::BoundedInt { .. }, CoreTypeConcrete::BoundedInt(_)) => todo!(),
105-
(JitValue::Bytes31(value), CoreTypeConcrete::Bytes31(_)) => value.to_bytes(buffer)?,
106-
(JitValue::EcPoint(x, y), CoreTypeConcrete::EcPoint(_)) => {
104+
(Value::BoundedInt { .. }, CoreTypeConcrete::BoundedInt(_)) => todo!(),
105+
(Value::Bytes31(value), CoreTypeConcrete::Bytes31(_)) => value.to_bytes(buffer)?,
106+
(Value::EcPoint(x, y), CoreTypeConcrete::EcPoint(_)) => {
107107
x.to_bytes(buffer)?;
108108
y.to_bytes(buffer)?;
109109
}
110-
(JitValue::EcState(x, y, x0, y0), CoreTypeConcrete::EcState(_)) => {
110+
(Value::EcState(x, y, x0, y0), CoreTypeConcrete::EcState(_)) => {
111111
x.to_bytes(buffer)?;
112112
y.to_bytes(buffer)?;
113113
x0.to_bytes(buffer)?;
114114
y0.to_bytes(buffer)?;
115115
}
116-
(JitValue::Enum { tag, value, .. }, CoreTypeConcrete::Enum(info)) => {
116+
(Value::Enum { tag, value, .. }, CoreTypeConcrete::Enum(info)) => {
117117
if self.info.is_memory_allocated(self.registry)? {
118118
let abi_ptr = self.value.to_jit(self.arena, self.registry, self.type_id)?;
119119

@@ -129,7 +129,7 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
129129
}
130130
}
131131
(
132-
JitValue::Felt252(value),
132+
Value::Felt252(value),
133133
CoreTypeConcrete::Felt252(_)
134134
| CoreTypeConcrete::StarkNet(
135135
StarkNetTypeConcrete::ClassHash(_)
@@ -138,7 +138,7 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
138138
| StarkNetTypeConcrete::StorageBaseAddress(_),
139139
),
140140
) => value.to_bytes(buffer)?,
141-
(JitValue::Felt252Dict { .. }, CoreTypeConcrete::Felt252Dict(_)) => {
141+
(Value::Felt252Dict { .. }, CoreTypeConcrete::Felt252Dict(_)) => {
142142
#[cfg(not(feature = "with-runtime"))]
143143
unimplemented!("enable the `with-runtime` feature to use felt252 dicts");
144144

@@ -150,13 +150,13 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
150150
.to_bytes(buffer)?
151151
}
152152
(
153-
JitValue::Secp256K1Point { x, y },
153+
Value::Secp256K1Point { x, y },
154154
CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::Secp256Point(
155155
Secp256PointTypeConcrete::K1(_),
156156
)),
157157
)
158158
| (
159-
JitValue::Secp256R1Point { x, y },
159+
Value::Secp256R1Point { x, y },
160160
CoreTypeConcrete::StarkNet(StarkNetTypeConcrete::Secp256Point(
161161
Secp256PointTypeConcrete::R1(_),
162162
)),
@@ -167,23 +167,23 @@ impl<'a> AbiArgument for JitValueWithInfoWrapper<'a> {
167167
x.to_bytes(buffer)?;
168168
y.to_bytes(buffer)?;
169169
}
170-
(JitValue::Sint128(value), CoreTypeConcrete::Sint128(_)) => value.to_bytes(buffer)?,
171-
(JitValue::Sint16(value), CoreTypeConcrete::Sint16(_)) => value.to_bytes(buffer)?,
172-
(JitValue::Sint32(value), CoreTypeConcrete::Sint32(_)) => value.to_bytes(buffer)?,
173-
(JitValue::Sint64(value), CoreTypeConcrete::Sint64(_)) => value.to_bytes(buffer)?,
174-
(JitValue::Sint8(value), CoreTypeConcrete::Sint8(_)) => value.to_bytes(buffer)?,
175-
(JitValue::Struct { fields, .. }, CoreTypeConcrete::Struct(info)) => {
170+
(Value::Sint128(value), CoreTypeConcrete::Sint128(_)) => value.to_bytes(buffer)?,
171+
(Value::Sint16(value), CoreTypeConcrete::Sint16(_)) => value.to_bytes(buffer)?,
172+
(Value::Sint32(value), CoreTypeConcrete::Sint32(_)) => value.to_bytes(buffer)?,
173+
(Value::Sint64(value), CoreTypeConcrete::Sint64(_)) => value.to_bytes(buffer)?,
174+
(Value::Sint8(value), CoreTypeConcrete::Sint8(_)) => value.to_bytes(buffer)?,
175+
(Value::Struct { fields, .. }, CoreTypeConcrete::Struct(info)) => {
176176
fields
177177
.iter()
178178
.zip(&info.members)
179179
.map(|(value, type_id)| self.map(value, type_id))
180180
.try_for_each(|wrapper| wrapper?.to_bytes(buffer))?;
181181
}
182-
(JitValue::Uint128(value), CoreTypeConcrete::Uint128(_)) => value.to_bytes(buffer)?,
183-
(JitValue::Uint16(value), CoreTypeConcrete::Uint16(_)) => value.to_bytes(buffer)?,
184-
(JitValue::Uint32(value), CoreTypeConcrete::Uint32(_)) => value.to_bytes(buffer)?,
185-
(JitValue::Uint64(value), CoreTypeConcrete::Uint64(_)) => value.to_bytes(buffer)?,
186-
(JitValue::Uint8(value), CoreTypeConcrete::Uint8(_)) => value.to_bytes(buffer)?,
182+
(Value::Uint128(value), CoreTypeConcrete::Uint128(_)) => value.to_bytes(buffer)?,
183+
(Value::Uint16(value), CoreTypeConcrete::Uint16(_)) => value.to_bytes(buffer)?,
184+
(Value::Uint32(value), CoreTypeConcrete::Uint32(_)) => value.to_bytes(buffer)?,
185+
(Value::Uint64(value), CoreTypeConcrete::Uint64(_)) => value.to_bytes(buffer)?,
186+
(Value::Uint8(value), CoreTypeConcrete::Uint8(_)) => value.to_bytes(buffer)?,
187187
_ => todo!(
188188
"abi argument unimplemented for ({:?}, {:?})",
189189
self.value,

src/bin/cairo-native-run.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
mod utils;
2-
31
use anyhow::Context;
42
use cairo_lang_compiler::{
53
compile_prepared_db, db::RootDatabase, project::setup_project, CompilerConfig,
64
};
75
use cairo_lang_runner::short_string::as_cairo_short_string;
86
use cairo_native::{
97
context::NativeContext,
10-
executor::{AotNativeExecutor, JitNativeExecutor, NativeExecutor},
8+
executor::{AotNativeExecutor, JitNativeExecutor},
119
metadata::gas::{GasMetadata, MetadataComputationConfig},
1210
starknet_stub::StubSyscallHandler,
1311
};
@@ -16,6 +14,8 @@ use std::path::PathBuf;
1614
use tracing_subscriber::{EnvFilter, FmtSubscriber};
1715
use utils::{find_function, result_to_runresult};
1816

17+
mod utils;
18+
1919
#[derive(Clone, Debug, ValueEnum)]
2020
enum RunMode {
2121
Aot,
@@ -74,12 +74,30 @@ fn main() -> anyhow::Result<()> {
7474
// Compile the sierra program into a MLIR module.
7575
let native_module = native_context.compile(&sierra_program, false).unwrap();
7676

77-
let native_executor: NativeExecutor = match args.run_mode {
77+
let native_executor: Box<dyn Fn(_, _, _, &mut StubSyscallHandler) -> _> = match args.run_mode {
7878
RunMode::Aot => {
79-
AotNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
79+
let executor =
80+
AotNativeExecutor::from_native_module(native_module, args.opt_level.into());
81+
Box::new(move |function_id, args, gas, syscall_handler| {
82+
executor.invoke_dynamic_with_syscall_handler(
83+
function_id,
84+
args,
85+
gas,
86+
syscall_handler,
87+
)
88+
})
8089
}
8190
RunMode::Jit => {
82-
JitNativeExecutor::from_native_module(native_module, args.opt_level.into()).into()
91+
let executor =
92+
JitNativeExecutor::from_native_module(native_module, args.opt_level.into());
93+
Box::new(move |function_id, args, gas, syscall_handler| {
94+
executor.invoke_dynamic_with_syscall_handler(
95+
function_id,
96+
args,
97+
gas,
98+
syscall_handler,
99+
)
100+
})
83101
}
84102
};
85103

@@ -94,8 +112,7 @@ fn main() -> anyhow::Result<()> {
94112

95113
let mut syscall_handler = StubSyscallHandler::default();
96114

97-
let result = native_executor
98-
.invoke_dynamic_with_syscall_handler(&func.id, &[], Some(initial_gas), &mut syscall_handler)
115+
let result = native_executor(&func.id, &[], Some(initial_gas), &mut syscall_handler)
99116
.with_context(|| "Failed to run the function.")?;
100117

101118
let run_result = result_to_runresult(&result)?;

src/bin/cairo-native-stress/main.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,38 @@
1313
//!
1414
//! For documentation on the specific cache used, see `NaiveAotCache`.
1515
16-
use cairo_lang_sierra::ids::FunctionId;
17-
use cairo_lang_sierra::program::{GenericArg, Program};
18-
use cairo_lang_sierra::program_registry::ProgramRegistry;
16+
use cairo_lang_sierra::{
17+
ids::FunctionId,
18+
program::{GenericArg, Program},
19+
program_registry::ProgramRegistry,
20+
};
1921
use cairo_lang_starknet::compile::compile_path;
20-
use cairo_native::metadata::gas::GasMetadata;
21-
use cairo_native::utils::SHARED_LIBRARY_EXT;
2222
use cairo_native::{
23-
context::NativeContext, executor::AotNativeExecutor, starknet::DummySyscallHandler,
24-
utils::find_entry_point_by_idx,
23+
context::NativeContext,
24+
executor::AotNativeExecutor,
25+
metadata::gas::GasMetadata,
26+
module_to_object, object_to_shared_lib,
27+
starknet::DummySyscallHandler,
28+
utils::{find_entry_point_by_idx, SHARED_LIBRARY_EXT},
29+
OptLevel,
2530
};
26-
use cairo_native::{module_to_object, object_to_shared_lib, OptLevel};
2731
use clap::Parser;
2832
use libloading::Library;
2933
use num_bigint::BigInt;
3034
use stats_alloc::{Region, StatsAlloc, INSTRUMENTED_SYSTEM};
31-
use std::alloc::System;
32-
use std::fmt::{Debug, Display};
33-
use std::fs::{create_dir_all, read_dir, OpenOptions};
34-
use std::hash::Hash;
35-
use std::io;
36-
use std::path::{Path, PathBuf};
37-
use std::sync::Arc;
38-
use std::{collections::HashMap, fs, time::Instant};
35+
use std::{
36+
alloc::System,
37+
collections::HashMap,
38+
fmt::{Debug, Display},
39+
fs::{self, create_dir_all, read_dir, OpenOptions},
40+
hash::Hash,
41+
io,
42+
path::{Path, PathBuf},
43+
sync::Arc,
44+
time::Instant,
45+
};
3946
use tracing::{debug, info, info_span, warn};
40-
use tracing_subscriber::layer::SubscriberExt;
41-
use tracing_subscriber::util::SubscriberInitExt;
42-
use tracing_subscriber::{EnvFilter, Layer};
47+
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
4348

4449
#[global_allocator]
4550
static GLOBAL_ALLOC: &StatsAlloc<System> = &INSTRUMENTED_SYSTEM;

src/bin/cairo-native-test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
mod utils;
2-
31
use anyhow::bail;
42
use cairo_lang_compiler::{
53
db::RootDatabase,
@@ -17,6 +15,8 @@ use utils::{
1715
RunArgs, RunMode,
1816
};
1917

18+
mod utils;
19+
2020
/// Compiles a Cairo project and runs all the functions marked as `#[test]`.
2121
/// Exits with 1 if the compilation or run fails, otherwise 0.
2222
#[derive(Parser, Debug)]

src/bin/scarb-native-dump.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
mod utils;
2-
3-
use std::{env, fs};
4-
51
use anyhow::Context;
62
use cairo_lang_sierra::program::VersionedProgram;
73
use cairo_native::context::NativeContext;
84
use melior::ir::operation::OperationPrintingFlags;
95
use scarb_metadata::{MetadataCommand, ScarbCommand};
6+
use std::{env, fs};
107

11-
/// Compiles all packages from a Scarb project on the current directory.
8+
mod utils;
129

10+
/// Compiles all packages from a Scarb project on the current directory.
1311
fn main() -> anyhow::Result<()> {
1412
let metadata = MetadataCommand::new().inherit_stderr().exec()?;
1513

src/bin/scarb-native-test.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
mod utils;
2-
3-
use std::collections::HashSet;
4-
use std::path::Path;
5-
use std::{env, fs};
6-
71
use anyhow::Context;
82
use cairo_lang_sierra::program::VersionedProgram;
93
use cairo_lang_test_plugin::{TestCompilation, TestCompilationMetadata};
104
use clap::{Parser, ValueEnum};
115
use scarb_metadata::{Metadata, MetadataCommand, ScarbCommand};
126
use scarb_ui::args::PackagesFilter;
13-
use utils::test::{display_tests_summary, filter_test_cases, find_testable_targets, run_tests};
14-
use utils::{RunArgs, RunMode};
7+
use std::{collections::HashSet, env, fs, path::Path};
8+
use utils::{
9+
test::{display_tests_summary, filter_test_cases, find_testable_targets, run_tests},
10+
RunArgs, RunMode,
11+
};
12+
13+
mod utils;
1514

1615
/// Compiles all packages from a Scarb project matching `packages_filter` and
1716
/// runs all functions marked with `#[test]`. Exits with 1 if the compilation

0 commit comments

Comments
 (0)