-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
617 lines (525 loc) · 14.6 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package gluaflatbuffers
import (
"slices"
"github.com/yuin/gopher-lua"
"github.com/PeerDB-io/glua64"
)
const VtableMetadataFields int = 2
type Builder struct {
ba []byte
vtables []int
currentVT []int
head int
objectEnd int
finished bool
nested bool
minalign uint8
}
func (b *Builder) EndVector(ls *lua.LState, vectorSize int) int {
if !b.nested {
ls.RaiseError("EndVector called outside nested context")
return 0
}
b.nested = false
b.PlaceU64(uint64(vectorSize), uint32n)
return b.Offset()
}
func (b *Builder) Offset() int {
return len(b.ba) - b.head
}
func (b *Builder) Pad(pad int) {
if pad > 0 {
b.head -= pad
Pad(b.ba, pad, b.head)
}
}
func (b *Builder) Place(ls *lua.LState, x lua.LValue, n N) {
b.head -= int(n.width)
n.Pack(ls, b.ba[b.head:], x)
}
func (b *Builder) PlaceU64(u64 uint64, n N) {
b.head -= int(n.width)
n.PackU64(b.ba[b.head:], u64)
}
func (b *Builder) Prep(width uint8, additional int) {
if width > b.minalign {
b.minalign = width
}
k := b.Offset() + additional
alignsize := -k & int(width-1)
space := alignsize + int(width) + additional
if b.head < space {
oldlen := len(b.ba)
b.ba = slices.Grow(b.ba, space)
b.ba = b.ba[:cap(b.ba)]
copy(b.ba[len(b.ba)-oldlen:], b.ba[:oldlen])
b.head += len(b.ba) - oldlen
}
b.Pad(alignsize)
}
func (b *Builder) Prepend(ls *lua.LState, n N, x lua.LValue) {
b.Prep(n.width, 0)
b.Place(ls, x, n)
}
func (b *Builder) PrependU64(n N, x uint64) {
b.Prep(n.width, 0)
b.PlaceU64(x, n)
}
func (b *Builder) PrependSlot(ls *lua.LState, n N, slotnum int, x lua.LValue, d lua.LValue) {
if !ls.Equal(x, d) {
if xud, ok := x.(*lua.LUserData); ok {
// Need to check int64/number because flatbuffers passes default as 0
// but Lua only calls __eq when both operands are same type
if dn, ok := d.(lua.LNumber); ok {
switch xv := xud.Value.(type) {
case int64:
if xv == int64(dn) {
return
}
case uint64:
if xv == uint64(dn) {
return
}
}
}
}
b.Prepend(ls, n, x)
b.Slot(ls, slotnum)
}
}
func (b *Builder) PrependOffsetTRelative(ls *lua.LState, off int, n N) {
b.Prep(4, 0)
boff := b.Offset()
if off > boff {
ls.RaiseError("Offset arithmetic error")
} else {
b.PlaceU64(uint64(boff-off+4), n)
}
}
func (b *Builder) PrependSOffsetTRelative(ls *lua.LState, off int) {
b.PrependOffsetTRelative(ls, off, int32n)
}
func (b *Builder) PrependUOffsetTRelative(ls *lua.LState, off int) {
b.PrependOffsetTRelative(ls, off, uint32n)
}
func (b *Builder) PrependVOffsetT(off uint16) {
b.Prep(2, 0)
b.PlaceU64(uint64(off), uint16n)
}
func (b *Builder) Slot(ls *lua.LState, slotnum int) {
if !b.nested {
ls.RaiseError("Slot called outside nested context")
return
}
for slotnum >= len(b.currentVT) {
b.currentVT = append(b.currentVT, 0)
}
b.currentVT[slotnum] = b.Offset()
}
func vtableEqual(a []int, objectStart int, b []byte) bool {
if len(a)*2 != len(b) {
return false
}
for i, ai := range a {
x := uint16n.UnpackU64(b[i*2:])
if (x != 0 || ai != 0) && int(x) != objectStart-ai {
return false
}
}
return true
}
func (b *Builder) WriteVtable(ls *lua.LState) int {
b.PrependSOffsetTRelative(ls, 0)
objectOffset := b.Offset()
for len(b.currentVT) > 0 && b.currentVT[len(b.currentVT)-1] == 0 {
b.currentVT = b.currentVT[:len(b.currentVT)-1]
}
var existingVtable int
for i := len(b.vtables) - 1; i >= 0; i -= 1 {
vt2Offset := b.vtables[i]
vt2Start := len(b.ba) - vt2Offset
vt2Len := uint16n.UnpackU64(b.ba[vt2Start:])
vt2 := b.ba[vt2Start+VtableMetadataFields*2 : vt2Start+int(vt2Len)]
if vtableEqual(b.currentVT, objectOffset, vt2) {
existingVtable = vt2Offset
break
}
}
if existingVtable == 0 {
for i := len(b.currentVT) - 1; i >= 0; i -= 1 {
var off uint16
if b.currentVT[i] != 0 {
off = uint16(objectOffset - b.currentVT[i])
}
b.PrependVOffsetT(off)
}
// end each vtable with object size & vtable size
b.PrependVOffsetT(uint16(objectOffset - b.objectEnd))
b.PrependVOffsetT(uint16(len(b.currentVT)+VtableMetadataFields) * 2)
newOffset := b.Offset()
int32n.PackU64(b.ba[len(b.ba)-objectOffset:], uint64(newOffset-objectOffset))
b.vtables = append(b.vtables, newOffset)
} else {
b.head = len(b.ba) - objectOffset
int32n.PackU64(b.ba[b.head:], uint64(existingVtable-objectOffset))
}
if len(b.currentVT) != 0 {
b.currentVT = b.currentVT[:0]
}
return objectOffset
}
var LuaBuilder = glua64.UserDataType[*Builder]{Name: "flatbuffers_builder"}
func Builder_Loader(ls *lua.LState) int {
m := ls.NewTable()
m.RawSetString("New", ls.NewFunction(BuilderNew))
mt := LuaBuilder.NewMetatable(ls)
index := ls.SetFuncs(ls.NewTable(), map[string]lua.LGFunction{
"Clear": BuilderClear,
"Output": BuilderOutput,
"StartObject": BuilderStartObject,
"WriteVtable": BuilderWriteVtable,
"EndObject": BuilderEndObject,
"Head": BuilderHead,
"Offset": BuilderOffset,
"Pad": BuilderPad,
"Prep": BuilderPrep,
"StartVector": BuilderStartVector,
"EndVector": BuilderEndVector,
"CreateString": BuilderCreateString,
"CreateByteVector": BuilderCreateByteVector,
"Slot": BuilderSlot,
"Finish": BuilderFinish,
"FinishSizePrefixed": BuilderFinishSizePrefixed,
"Place": BuilderPlace,
"PrependSlot": BuilderPrependSlot,
"PrependBoolSlot": BuilderPrependBoolSlot,
"PrependByteSlot": BuilderPrependUint8Slot,
"PrependUint8Slot": BuilderPrependUint8Slot,
"PrependUint16Slot": BuilderPrependUint16Slot,
"PrependUint32Slot": BuilderPrependUint32Slot,
"PrependUint64Slot": BuilderPrependUint64Slot,
"PrependInt8Slot": BuilderPrependInt8Slot,
"PrependInt16Slot": BuilderPrependInt16Slot,
"PrependInt32Slot": BuilderPrependInt32Slot,
"PrependInt64Slot": BuilderPrependInt64Slot,
"PrependFloat32Slot": BuilderPrependFloat32Slot,
"PrependFloat64Slot": BuilderPrependFloat64Slot,
"PrependStructSlot": BuilderPrependStructSlot,
"PrependUOffsetTRelativeSlot": BuilderPrependUOffsetTRelativeSlot,
"Prepend": BuilderPrepend,
"PrependBool": BuilderPrependBool,
"PrependByte": BuilderPrependUint8,
"PrependUint8": BuilderPrependUint8,
"PrependUint16": BuilderPrependUint16,
"PrependUint32": BuilderPrependUint32,
"PrependUint64": BuilderPrependUint64,
"PrependInt8": BuilderPrependInt8,
"PrependInt16": BuilderPrependInt16,
"PrependInt32": BuilderPrependInt32,
"PrependInt64": BuilderPrependInt64,
"PrependFloat32": BuilderPrependFloat32,
"PrependFloat64": BuilderPrependFloat64,
"PrependVOffsetT": BuilderPrependUint16,
"PrependSOffsetTRelative": BuilderPrependSOffsetTRelative,
"PrependUOffsetTRelative": BuilderPrependUOffsetTRelative,
})
ls.SetField(mt, "__index", index)
ls.Push(m)
return 1
}
func BuilderNew(ls *lua.LState) int {
initialSize := int(ls.CheckNumber(1))
ls.Push(LuaBuilder.New(ls, &Builder{
ba: make([]byte, initialSize),
vtables: make([]int, 0, 4),
currentVT: make([]int, 0, 4),
head: initialSize,
objectEnd: 0,
finished: false,
nested: false,
minalign: 1,
}))
return 1
}
func BuilderClear(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
b.finished = false
b.nested = false
b.minalign = 1
if len(b.vtables) != 0 {
b.vtables = b.vtables[:0]
}
b.currentVT = b.currentVT[:0]
b.objectEnd = 0
b.head = len(b.ba)
return 0
}
func BuilderOutput(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
if lua.LVIsFalse(ls.Get(2)) {
ls.Push(lua.LString(b.ba[b.head:]))
} else {
ls.Push(lua.LString(b.ba))
}
return 1
}
func BuilderStartObject(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
if b.nested {
ls.RaiseError("StartObject called inside nested context")
return 0
}
b.nested = true
numFields := int(ls.CheckNumber(2))
b.currentVT = slices.Grow(b.currentVT[:0], numFields)[:0]
b.objectEnd = b.Offset()
return 0
}
func BuilderWriteVtable(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
b.WriteVtable(ls)
return 0
}
func BuilderEndObject(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
if !b.nested {
ls.RaiseError("EndObject called outside nested context")
return 0
}
b.nested = false
ls.Push(lua.LNumber(b.WriteVtable(ls)))
return 1
}
func BuilderHead(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
ls.Push(lua.LNumber(b.head))
return 1
}
func BuilderOffset(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
ls.Push(lua.LNumber(b.Offset()))
return 1
}
func BuilderPad(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
pad := ls.CheckNumber(2)
b.Pad(int(pad))
return 0
}
func BuilderPrep(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
size := ls.CheckNumber(2)
additional := ls.CheckNumber(3)
b.Prep(uint8(size), int(additional))
return 0
}
func BuilderPrependSOffsetTRelative(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
b.PrependOffsetTRelative(ls, int(ls.CheckNumber(2)), int32n)
return 0
}
func BuilderPrependUOffsetTRelative(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
b.PrependOffsetTRelative(ls, int(ls.CheckNumber(2)), uint32n)
return 0
}
func BuilderStartVector(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
if b.nested {
ls.RaiseError("StartVector called in nested context")
}
b.nested = true
elemSize := int(ls.CheckNumber(2))
numElements := int(ls.CheckNumber(3))
alignment := uint8(ls.CheckNumber(4))
elementSize := elemSize * numElements
b.Prep(4, elementSize)
b.Prep(alignment, elementSize)
ls.Push(lua.LNumber(b.Offset()))
return 1
}
func BuilderEndVector(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
size := int(ls.CheckNumber(2))
ls.Push(lua.LNumber(b.EndVector(ls, size)))
return 1
}
func createBytesHelper(ls *lua.LState, addnul bool) int {
b := LuaBuilder.StartMethod(ls)
s := ls.CheckString(2)
if b.nested {
if addnul {
ls.RaiseError("CreateString called in nested context")
} else {
ls.RaiseError("CreateByteVector called in nested context")
}
return 0
}
b.nested = true
lens := len(s)
if addnul {
b.Prep(4, lens+1)
b.PlaceU64(0, uint8n)
} else {
b.Prep(4, lens)
}
b.head -= lens
copy(b.ba[b.head:], s)
ls.Push(lua.LNumber(b.EndVector(ls, lens)))
return 1
}
func BuilderCreateString(ls *lua.LState) int {
return createBytesHelper(ls, true)
}
func BuilderCreateByteVector(ls *lua.LState) int {
return createBytesHelper(ls, false)
}
func BuilderSlot(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
slotnum := int(ls.CheckNumber(2))
b.Slot(ls, slotnum)
return 0
}
func FinishHelper(ls *lua.LState, sizePrefix bool) int {
b := LuaBuilder.StartMethod(ls)
rootTable := int(ls.CheckNumber(2))
var additional int
if sizePrefix {
additional = 8
} else {
additional = 4
}
b.Prep(b.minalign, additional)
b.PrependUOffsetTRelative(ls, rootTable)
if sizePrefix {
b.PrependU64(int32n, uint64(b.Offset()))
}
b.finished = true
ls.Push(lua.LNumber(b.head))
return 1
}
func BuilderFinish(ls *lua.LState) int {
return FinishHelper(ls, false)
}
func BuilderFinishSizePrefixed(ls *lua.LState) int {
return FinishHelper(ls, true)
}
func BuilderPlace(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
_, n := LuaN.Check(ls, 3)
b.Place(ls, ls.Get(2), n)
return 0
}
func BuilderPrependSlot(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
_, n := LuaN.Check(ls, 2)
slotnum := int(ls.CheckNumber(3))
b.PrependSlot(ls, n, slotnum, ls.Get(4), ls.Get(5))
return 0
}
func PrependSlotHelper(ls *lua.LState, n N) int {
b := LuaBuilder.StartMethod(ls)
slotnum := int(ls.CheckNumber(2))
b.PrependSlot(ls, n, slotnum, ls.Get(3), ls.Get(4))
return 0
}
func BuilderPrependBoolSlot(ls *lua.LState) int {
return PrependSlotHelper(ls, booln)
}
func BuilderPrependUint8Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, uint8n)
}
func BuilderPrependUint16Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, uint16n)
}
func BuilderPrependUint32Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, uint32n)
}
func BuilderPrependUint64Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, uint64n)
}
func BuilderPrependInt8Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, int8n)
}
func BuilderPrependInt16Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, int16n)
}
func BuilderPrependInt32Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, int32n)
}
func BuilderPrependInt64Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, int64n)
}
func BuilderPrependFloat32Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, float32n)
}
func BuilderPrependFloat64Slot(ls *lua.LState) int {
return PrependSlotHelper(ls, float64n)
}
func BuilderPrependStructSlot(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
x := int(ls.CheckNumber(3))
d := int(ls.CheckNumber(4))
if x != d {
if x != b.Offset() {
ls.RaiseError("Tried to write a Struct at an Offset that is different from the current Offset of the Builder.")
} else {
b.Slot(ls, int(ls.CheckNumber(2)))
}
}
return 0
}
func BuilderPrependUOffsetTRelativeSlot(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
x := int(ls.CheckNumber(3))
d := int(ls.CheckNumber(4))
if x != d {
b.PrependOffsetTRelative(ls, x, uint32n)
b.Slot(ls, int(ls.CheckNumber(2)))
}
return 0
}
func BuilderPrepend(ls *lua.LState) int {
b := LuaBuilder.StartMethod(ls)
_, n := LuaN.Check(ls, 2)
b.Prepend(ls, n, ls.Get(3))
return 0
}
func PrependHelper(ls *lua.LState, n N) int {
b := LuaBuilder.StartMethod(ls)
b.Prepend(ls, n, ls.Get(2))
return 0
}
func BuilderPrependBool(ls *lua.LState) int {
return PrependHelper(ls, booln)
}
func BuilderPrependUint8(ls *lua.LState) int {
return PrependHelper(ls, uint8n)
}
func BuilderPrependUint16(ls *lua.LState) int {
return PrependHelper(ls, uint16n)
}
func BuilderPrependUint32(ls *lua.LState) int {
return PrependHelper(ls, uint32n)
}
func BuilderPrependUint64(ls *lua.LState) int {
return PrependHelper(ls, uint64n)
}
func BuilderPrependInt8(ls *lua.LState) int {
return PrependHelper(ls, int8n)
}
func BuilderPrependInt16(ls *lua.LState) int {
return PrependHelper(ls, int16n)
}
func BuilderPrependInt32(ls *lua.LState) int {
return PrependHelper(ls, int32n)
}
func BuilderPrependInt64(ls *lua.LState) int {
return PrependHelper(ls, int64n)
}
func BuilderPrependFloat32(ls *lua.LState) int {
return PrependHelper(ls, float32n)
}
func BuilderPrependFloat64(ls *lua.LState) int {
return PrependHelper(ls, float64n)
}