gwtproject / gwt

GWT Open Source Project
http://www.gwtproject.org
1.51k stars 372 forks source link

DataViewImpl with offset is incorrectly implemented #9738

Open akbertram opened 2 years ago

akbertram commented 2 years ago

I'm using GWT 2.9.0 and the DataViewImpl class does not respect offset argument to TypedArrays.createDataView(). You can see from the current implementation that the ArrayBufferViewImpl.byteOffset field is completely ignored and instead the byteOffset argument to the accessor methods is passed directly to the underlying ArrayBufferImpl.

https://github.com/gwtproject/gwt/blob/3dc21707a4b6aa1af7139986fa7e3b9c213fee05/user/src/com/google/gwt/typedarrays/server/DataViewImpl.java#L71-L73

Javascript

const buffer = new Int8Array(8);
buffer.set([0,1,2,3,4,5,6,7,8], 0);

const view = new DataView(buffer, 3, 4);
console.log(view0.getInt8(0)); // 3
console.log(view0.getInt8(1); // 4

Equivalent Java

@Test
public void testSanity() {
    Int8Array array = TypedArrays.createInt8Array(8);
    array.set(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7}, 0);

    DataView view = TypedArrays.createDataView(array.buffer(), 3, 4);
    assertThat(view.getInt8(0), equalTo((byte)3));  // returns 0 !!
    assertThat(view.getInt8(1), equalTo((byte)4)); // returns 1
}

Results in:

java.lang.AssertionError: 
Expected: <3>
     but: was <0>
niloc132 commented 2 years ago

Thanks for this. To confirm, this is only broken in the JVM implementation, but works in JS mode?

akbertram commented 2 years ago

Yes, the test I added passed without modification when run as a GWT/J2CL test.