flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

[TS] fix incorrect reverse order when writting array of structs

Open sssooonnnggg opened this issue 2 years ago • 4 comments

When writting array of structs to flatbuffer in TS, structs will be written in reverse order.

Example code:

struct Vec3 {
    x: float;
    y: float;
    z: float;
}

table Pos {
    vecs: [Vec3];
}
import { Pos, PosT } from './pos'
import { Vec3T } from './vec3'
import * as flatbuffers from 'flatbuffers'

let origin = new PosT();
origin.vecs = [
    new Vec3T(1, 2, 3),
    new Vec3T(4, 5, 6)
];

console.log('origin:', origin);

let builder = new flatbuffers.Builder();
builder.finish(origin.pack(builder));

let decoded = Pos.getRootAsPos(builder.dataBuffer()).unpack();
console.log('decoded:', decoded);

Will output:

origin: PosT {
  vecs: [ Vec3T { x: 1, y: 2, z: 3 }, Vec3T { x: 4, y: 5, z: 6 } ]
}
decoded: PosT {
  vecs: [ Vec3T { x: 4, y: 5, z: 6 }, Vec3T { x: 1, y: 2, z: 3 } ]
}

Because createObjectOffsetList writes data in reverse order, and many generated codes depend on it. So we need to reverse createStructOffsetList’s input to make the structs order correct.

sssooonnnggg avatar Apr 25 '22 10:04 sssooonnnggg

@dbaileychess @bjornharrtell

sssooonnnggg avatar May 10 '22 11:05 sssooonnnggg

Sorry for the delay. Can we get a test for it?

dbaileychess avatar Jun 14 '22 22:06 dbaileychess

Sorry for the delay. Can we get a test for it?

Ok, I will add a test later.

sssooonnnggg avatar Jun 16 '22 06:06 sssooonnnggg

Sorry for the delay. Can we get a test for it?

Done. 😄

sssooonnnggg avatar Jun 17 '22 07:06 sssooonnnggg

Sorry for the delay! Thanks.

dbaileychess avatar Aug 06 '22 05:08 dbaileychess