binf.cljc icon indicating copy to clipboard operation
binf.cljc copied to clipboard

Absolute read/write fails for offset views in java implementation

Open sh54 opened this issue 4 years ago • 1 comments

The following:

(let [view (-> (binf.buffer/alloc 64)
               (binf/view 32 16)
               (binf/endian-set :little-endian))]
  (binf/wa-b8 view 0 42)
  (t/is (= 42 (binf/ra-u8 view 0))))

Works in clojurescript but fails in clojure with the exception:

Unhandled java.lang.IndexOutOfBoundsException 32

       HeapByteBuffer.java:  171  java.nio.HeapByteBuffer/get
                  impl.clj:   89  helins.binf.protocol.impl/eval17986/fn
             protocol.cljc:   24  helins.binf.protocol$eval11976$fn__12035$G__11961__12042/invoke
                 binf.cljc:   69  helins.binf$ra_u8/invokeStatic
                 binf.cljc:   63  helins.binf$ra_u8/invoke
                      REPL:   66  helins.binf.test/eval18113

Fix:

(extend-type ByteBuffer
  binf.protocol/-IByteBuffer
  (-array-index [this position]
    position))

This is the same the existing implementation for DirectByteBuffer.

sh54 avatar May 31 '21 13:05 sh54

Latest commit on main has fixed this (not yet released) if you fancy giving it a go.

I rewrote the tests using test.check so hopefully there won't be anymore edge cases. But I'll wait a bit before cutting a release.

helins avatar Jun 03 '21 17:06 helins