Taaitaaiger / jlrs

Julia bindings for Rust
MIT License
408 stars 21 forks source link

function call with arbitary arguments fail #105

Closed nabeel99 closed 11 months ago

nabeel99 commented 11 months ago

So the issue is say i want to call the following function in a julia code

function find_arb!(Δ::VT, Λ::VT, cfmm::ProductTwoCoin{T}, v::VT) where {T, VT<:AbstractVector{T}}
    R, γ = cfmm.R, cfmm.γ
    k = R[1]*R[2]

    Δ[1] = prod_arb_δ(v[2]/v[1], R[1], k, γ)
    Δ[2] = prod_arb_δ(v[1]/v[2], R[2], k, γ)

    Λ[1] = prod_arb_λ(v[1]/v[2], R[1], k, γ)
    Λ[2] = prod_arb_λ(v[2]/v[1], R[2], k, γ)
    return nothing
end

Problem #1 the function takes 4 arguments where call0,1,2,3 is implemented for a maximum of 3 arguments, so naturally i opted for the call function which seems to take arbitary args problem is, when i pass my argument as call(&mut frame, (arg0,arg1,arg2,arg3) it gives the following compiler error :

error[E0277]: the trait bound `jlrs::prelude::Value<'_, '_>: jlrs::args::Values<'_, '_, _>` is not satisfied
   --> src/main.rs:86:39
    |
86  |                     .call(&mut frame, (jl_type_deltas.as_value()))
    |                      ----   

Problem#2 I use JL Core Reflect to generate the following rust equivalent of the julia type :

#[repr(C)]
#[derive(Clone, Debug, Unbox, ValidLayout, Typecheck, ConstructType)]
#[jlrs(julia_type = "CFMMRouter.UniV3")]
pub struct UniV3<'scope, 'data, T> {
    pub current_price: T,
    pub current_tick: i64,
    pub lower_ticks: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
    pub liquidity: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
    pub γ: T,
    pub Ai: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
}

issue is when i try to pass it to the call function it obv rejects because it only accepts arguments of type value but as you can see into julia is not derived for struct if so after instantiating it how do i pass it to a julia function ? do i have to manually impl that trait for while jl reflect does not?

Taaitaaiger commented 11 months ago

You're using call which takes an arbitrary number of arguments, which have to be passed as an array or slice of values: .call(&mut frame, [jl_type_deltas.as_value()]). (The technical reason why it doesn't take AsRef<[Value]> is that in some cases extra arguments have to be added at the start, and Values enables doing that without dynamic allocations)

Because the function only takes a single argument, though, you can use call1 instead.

nabeel99 commented 11 months ago

Any pointers for the problem#2, how can we pass custom steucts generated via jl reflect but they still dont impl intojulia trait

Taaitaaiger commented 11 months ago

You can use DataType::instantiate and pass all fields as Values

Op za 12 aug. 2023 00:07 schreef Nabeel @.***>:

Any pointers for the problem#2, how can we pass custom steucts generated via jl reflect but they still dont impl intojulia trait

— Reply to this email directly, view it on GitHub https://github.com/Taaitaaiger/jlrs/issues/105#issuecomment-1675455945, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQBHH7FQQGJ3VZXFIPLZ7DXU2ULVANCNFSM6AAAAAA3NIFVFY . You are receiving this because you commented.Message ID: @.***>

Taaitaaiger commented 11 months ago

Constructors can also be called by converting a DataType to a Value and calling it.

Op za 12 aug. 2023 00:11 schreef Thomas van Doornmalen < @.***>:

You can use DataType::instantiate and pass all fields as Values

Op za 12 aug. 2023 00:07 schreef Nabeel @.***>:

Any pointers for the problem#2, how can we pass custom steucts generated via jl reflect but they still dont impl intojulia trait

— Reply to this email directly, view it on GitHub https://github.com/Taaitaaiger/jlrs/issues/105#issuecomment-1675455945, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOQBHH7FQQGJ3VZXFIPLZ7DXU2ULVANCNFSM6AAAAAA3NIFVFY . You are receiving this because you commented.Message ID: @.***>

nabeel99 commented 11 months ago

tried what u asked, fixed the compilation errors but getting the following error on runtime , this is the julia fn am trying to call :

function find_arb!(Δ::VT, Λ::VT, cfmm::UniV3, v::VT) where {T, VT<:AbstractVector{T}}

Error :

thread 'main' panicked at 'Result is an error: Exception(Exception { msg: "MethodError: no method matching find_arb!(::Vector{Float64}, ::Vector{Float64}, ::Type{Any}, ::Vector{Float64})\n\nClosest candidates are:\n  find_arb!(::VT, ::VT, !Matched::ProductTwoCoin{T}, ::VT) where {T, VT<:AbstractVector{T}}\n   @ Main.CFMMRouter ~/Desktop/CFMMRouter.jl/src/cfmms.jl:130\n  find_arb!(::VT, ::VT, !Matched::GeometricMeanTwoCoin{T}, ::VT) where {T, VT<:AbstractVector{T}}\n   @ Main.CFMMRouter ~/Desktop/CFMMRouter.jl/src/cfmms.jl:185\n  find_arb!(::VT, ::VT, !Matched::UniV3, ::VT) where {T, VT<:AbstractVector{T}}\n   @ Main.CFMMRouter ~/Desktop/CFMMRouter.jl/src/cfmms.jl:339\n  ...\n" })', src/main.rs:107:10
nabeel99 commented 11 months ago

Constructors can also be called by converting a DataType to a Value and calling it. Op za 12 aug. 2023 00:11 schreef Thomas van Doornmalen < @.>: You can use DataType::instantiate and pass all fields as Values Op za 12 aug. 2023 00:07 schreef Nabeel @.>: > Any pointers for the problem#2, how can we pass custom steucts generated > via jl reflect but they still dont impl intojulia trait > > — > Reply to this email directly, view it on GitHub > <#105 (comment)>, > or unsubscribe > https://github.com/notifications/unsubscribe-auth/AOQBHH7FQQGJ3VZXFIPLZ7DXU2ULVANCNFSM6AAAAAA3NIFVFY > . > You are receiving this because you commented.Message ID: > @.***> >

Additional info : This is how i created the data type as u guided :

let test = DataType::any_type(&frame);
            unsafe{
            test.instantiate(&frame, [value_cp,value_current_tick,jl_type_ticks.as_value(),jl_type_liquidity.as_value(),value_fees,jl_type_matrix.as_value()])};
            let test_value = test.as_value();
        and called it like this 
        ```

        .call(&mut frame, [jl_type_deltas.as_value(),jl_type_lambdas.as_value(),
                test_value,jl_type_prices.as_value()])
nabeel99 commented 11 months ago

this is the data type i got from julia reflect package which am trying to send into the function :

#[repr(C)]
#[derive(Clone, Debug, Unbox, ValidLayout, Typecheck, ConstructType)]
#[jlrs(julia_type = "CFMMRouter.UniV3")]
pub struct UniV3<'scope, 'data, T> {
    pub current_price: T,
    pub current_tick: i64,
    pub lower_ticks: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
    pub liquidity: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
    pub γ: T,
    pub Ai: ::std::option::Option<::jlrs::data::managed::array::ArrayRef<'scope, 'data>>,
}

am not quite sure how to construct it and send it to the julia function

nabeel99 commented 11 months ago

Constructors can also be called by converting a DataType to a Value and calling it. Op za 12 aug. 2023 00:11 schreef Thomas van Doornmalen < @.>: You can use DataType::instantiate and pass all fields as Values Op za 12 aug. 2023 00:07 schreef Nabeel @.>: > Any pointers for the problem#2, how can we pass custom steucts generated > via jl reflect but they still dont impl intojulia trait > > — > Reply to this email directly, view it on GitHub > <#105 (comment)>, > or unsubscribe > https://github.com/notifications/unsubscribe-auth/AOQBHH7FQQGJ3VZXFIPLZ7DXU2ULVANCNFSM6AAAAAA3NIFVFY > . > You are receiving this because you commented.Message ID: > @.***> >

not sure if this is what u meant :


  let e =cfmm_types::UniV3::<f64>::construct_type(&mut frame);
            let d= e.cast::<DataType>();```
if so not sure how do i populate the values of the struct since it returns a value.
nabeel99 commented 11 months ago

tried this as well

let e =cfmm_types::UniV3::<f64>::construct_type(&mut frame);
            let values =unsafe{ [value_cp,value_current_tick,jl_type_ticks.as_value(),jl_type_liquidity.as_value(),value_fees,jl_type_matrix.as_value()]};
            let populate = DataType::instantiate(e.datatype(), &mut frame, values).expect("failed 1").expect("failed 2");

but apparently getting a new error now : thread 'main' panicked at 'calledOption::unwrap()on aNonevalue', /Users/aaran/.cargo/registry/src/index.crates.io-6f17d22bba15001f/jlrs-0.19.0/src/data/static_data.rs:179:65

nabeel99 commented 11 months ago

@Taaitaaiger an update the above issues have been solved, but i ran into a segmentation fault, will be closing this and from now onwards be moving to the discussion tab as that seems a better forum to discuss such issues.