Closed Okm165 closed 1 month ago
The PanicResult
is not implicit it the code itself, so we decided to handle the panic wrapper when fetching return values, and either return the decodified error in the case of a Panic::Error
or return the wrapped result in the case of Panic::Ok
, consuming the wrapper in the process. This makes it more clear for users of the CLI to see when a run panicked, and avoids possible confusion with the panic wrapper being returned, as otherwise you would have to look at all the corelib functions used by the program to know if the first return value will be a panic wrapper or not.
This is also compatible with how cairo's cairo-run
handles its outputs. If you run the program (removing the input arg and declaring it inside the program instead), it will yield: Run completed successfully, returning [9, 16, 25]
In order to make it work with existing impl on branch main commit: https://github.com/lambdaclass/cairo-vm/commit/3f1f9ecfffcc52e6d30ce3254b55a7b7cb94eb7f i need to define program like this:
#[derive(Drop, Serde)]
struct Input {
a: felt252,
b: felt252,
c: felt252,
}
struct Output {
a_2: felt252,
b_2: felt252,
c_2: felt252,
}
fn main() -> Output {
let input = Input {
a: 3,
b: 4,
c: 5,
};
let a_2 = input.a * input.a;
let b_2 = input.b * input.b;
let c_2 = input.c * input.c;
// assert (a_2 + b_2 == c_2, 'invalid value');
Output {
a_2,
b_2,
c_2,
}
}
But this is kinda obvious because this prog is not returning PanicResult(Output)
but just Output
struct.
What i wanna understand:
Given PanicResult(u32)
= [Int(0), Int(0), Int(9)]
why PanicResult(Output)
= [Int(0), Int(9), Int(16), Int(25)]
(with just single Int(0)
element), this line suggests
me that code expects it to be a list of [Int(0), Int(0), Int(9), Int(16), Int(25)]
.
In order to make it work with existing impl on branch main commit: 3f1f9ec i need to define program like this:
#[derive(Drop, Serde)] struct Input { a: felt252, b: felt252, c: felt252, } struct Output { a_2: felt252, b_2: felt252, c_2: felt252, } fn main() -> Output { let input = Input { a: 3, b: 4, c: 5, }; let a_2 = input.a * input.a; let b_2 = input.b * input.b; let c_2 = input.c * input.c; // assert (a_2 + b_2 == c_2, 'invalid value'); Output { a_2, b_2, c_2, } }
But this is kinda obvious because this prog is not returning
PanicResult(Output)
but justOutput
struct.What i wanna understand: Given
PanicResult(u32)
=[Int(0), Int(0), Int(9)]
whyPanicResult(Output)
=[Int(0), Int(9), Int(16), Int(25)]
(with just singleInt(0)
element), this linesuggests
me that code expects it to be a list of[Int(0), Int(0), Int(9), Int(16), Int(25)]
.
This program doesn't call function that can cause a panic, and therefore doesn't return a PanicResult<Output>
, it only returns Output
This program when finished returns object PanicResult(Output). When i print return_values i see
[Int(0), Int(9), Int(16), Int(25)]
so in order to successfully serialize output i needed to change line to the code below:because otherwise it is removing
a_2=Int(9)
element ofOutput
struct and fails on serialization step.Program behaves differently when return value is of type for ex.
u32
This program when finished returns object PanicResult(u32) When i print return_values i see
[Int(0), Int(0), Int(9)]
so line does remove[Int(0), Int(0)]
part and it works well.Do u know the reason why struct was not represented by
[Int(0), Int(0), Int(9), Int(16), Int(25)]
as code expects it to be? Do u know what causes this behavior? Tell me wdyt i may misunderstand smth?branch main commit: 3f1f9ecfffcc52e6d30ce3254b55a7b7cb94eb7f