JuliaPluto / PlutoUI.jl

https://featured.plutojl.org/basic/plutoui.jl
The Unlicense
299 stars 54 forks source link

Invalid Index Error with High Resolution Slider #215

Closed Firionus closed 1 year ago

Firionus commented 2 years ago

When the HTML value of a slider exceeds 2^31 - 1 = 2147483647, the bound value is assigned an invalid index error.

Steps to Reproduce

  1. Create a new Pluto notebook with two cells and execute them:
    begin
    using PlutoUI
    @bind x Slider(0:2^32)
    end
    x
  2. Move the slider
  3. Observe that when the slider value is moved above 2147483647, instead of a number, the second cell shows an error:
(
    ❌ AbstractPlutoDingetjes: Bond value transformation errored., 
    ArgumentError("invalid index: 2.151284505e9 of type Float64"), 
    Base.StackTraces.StackFrame[
        to_index(i::Float64) at indices.jl:300, 
        to_index(A::UnitRange{Int64}, i::Float64) at indices.jl:277, 
        to_indices at indices.jl:333 [inlined], 
        to_indices at indices.jl:325 [inlined], 
        getindex(A::UnitRange{Int64}, I::Float64) at abstractarray.jl:1218, 
        transform_value(slider::PlutoUI.BuiltinsNotebook.Slider{Int64}, val_from_js::Float64) at Builtins.jl:213, 
        transform_bond_value(s::Symbol, value_from_js::Float64) at PlutoRunner.jl:1730, 
        top-level scope at none:1, 
        eval(m::Module, e::Any) at boot.jl:373, 
        top-level scope at none:1, 
        eval(m::Module, e::Any) at boot.jl:373, 
        (::Distributed.var"#103#104"{Distributed.CallMsg{:call}})() at process_messages.jl:274, 
        run_work_thunk(thunk::Distributed.var"#103#104"{Distributed.CallMsg{:call}}, print_error::Bool) at process_messages.jl:63, 
        run_work_thunk(rv::Distributed.RemoteValue, thunk::Function) at process_messages.jl:72, 
        (::Distributed.var"#96#98"{Distributed.RemoteValue, Distributed.var"#103#104"{Distributed.CallMsg{:call}}})() at task.jl:423
    ], 
    2.151284505e9
)

Expected Behavior The bound variable should not be assigned an error value.

Since 2^31 should be enough possible values for any slider, throwing an error when constructing a Slider with too many possible values would seem reasonable to me. This way, the user can be given a precise and helpful error, instead of the cryptic one shown currently.

Alternatively, can the underlying code be changed to interpret the higher numbers properly? The index seems to normally be interpreted as Int32, but in the case of big numbers as Float64 instead of Int64. Could there be an issue there?