Git Product home page Git Product logo

Comments (9)

eriktim avatar eriktim commented on May 20, 2024 1

Expanding the offset parameter on this line to bytes.byteOffset + offset (instead of offset only) seems to fix the issue. Both my previous example as my ProtoBuf Decoder run successfully with the change.
I think this is because the DataView.prototype.getUint8 and friends respect the offset of the original DataView, whereas Bytes.Decode.bytes currently ignores it. When using Bytes.Decode.bytes from the start of a Bytes sequence this obviously does not cause any issues.

from bytes.

eriktim avatar eriktim commented on May 20, 2024 1

I just started doing that ;-)

from bytes.

eriktim avatar eriktim commented on May 20, 2024

I am also running into this issue with my ProtoBuf decoder. However, I am not sure it has to do with Decode.loop, I think I've seen it with a custom recursion as well.
I tried creating a more basic example where replacing Decode.unsignedInt8 with Decode.bytes 1 causes things to break.

-- 10 bytes
data =
    Hex.toBytes "0102030405060708090A"
        |> Maybe.withDefault (Encode.encode <| Encode.sequence [])

-- 2x5 bytes, i.e. ["0102030405","060708090A"]
list =
    data
        |> Decode.decode
            (Decode.bytes 5
                |> Decode.andThen (\a -> Decode.map (\b -> [ a, b ]) (Decode.bytes 5))
            )
        |> Maybe.withDefault []

-- run the decoder for both items
mapped =
    list
        |> List.map (\bs -> Decode.decode (decoder <| Bytes.width bs) bs)
        |> Debug.log "mapped"

-- the loop decoder
decoder w =
    Decode.loop ( w, [] )
        (\( n, result ) ->
            if n <= 0 then
                Decode.succeed <| Decode.Done result

            else
                --Decode.map (\v -> Decode.Loop ( n - 1, v :: result )) Decode.unsignedInt8
                Decode.map (\v -> Decode.Loop ( n - 1, Hex.fromBytes v :: result )) (Decode.bytes 1)
        )

For the last two Decode.map (\v -> Decode.Loop ... lines, enabling

  • Decode.unsignedInt8 correctly results in mapped: [Just [5,4,3,2,1],Just [10,9,8,7,6]]
  • Decode.bytes 1 unfortunately results in mapped: [Just ["05","04","03","02","01"],Just ["05","04","03","02","01"]]

Could it have something to do with the fact that Decode.bytes "Copies a given number of bytes into a new Bytes sequence."?

from bytes.

icidasset avatar icidasset commented on May 20, 2024

Nice one @eriktim 👌 I think you're right.

The bytes decoder uses this function:
https://github.com/elm/bytes/blob/master/src/Elm/Kernel/Bytes.js#L149
Which uses the buffer from another DataView, and that buffer is a reference.
So if that reference changes, the copy changes as well.
It should be bytes.buffer.slice() if I'm not mistaken (see docs).

Will test out my theory soon when I have some free time.

from bytes.

eriktim avatar eriktim commented on May 20, 2024

Yes, I've been looking into that as well. But the referenced data should not change either, should it?
It looks as if the offset gets reset. When splitting the 10 bytes into 6 and 4 (instead of 5-5) you can see the lengths are correct.

from bytes.

icidasset avatar icidasset commented on May 20, 2024

@eriktim Awesome 🙌 Do you want to create a PR or should I?

from bytes.

icidasset avatar icidasset commented on May 20, 2024

@eriktim Could you tell me how you tested your fix? 🤔 It doesn't let me run the Elm Kernel code.

from bytes.

eriktim avatar eriktim commented on May 20, 2024

Sure @icidasset, I tested it by modifying my 1.0.7 installation:

  • Apply the patch in ~/.elm/0.19.0/package/elm/bytes/1.0.7/src/Elm/Kernel/Bytes.js;
  • Remove all .dat files in ~/.elm/0.19.0/package/elm/bytes/1.0.7;
  • Run your code.

Afterwards you can just delete the ~/.elm/0.19.0/package/elm/bytes/1.0.7 directory to restore the changes.

from bytes.

icidasset avatar icidasset commented on May 20, 2024

Thanks @eriktim ! I'm going to close this issue in favor of #12, where I explained the issue we have in greater detail and made a smaller SSCCE.

from bytes.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.