Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
serprex committed Mar 15, 2024
1 parent 5743350 commit 445ae0e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 21 deletions.
10 changes: 3 additions & 7 deletions flow/pua/flatbuffers_binaryarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,13 @@ func BinaryArraySlice(ls *lua.LState) int {
return 1
}

func (ba *BinaryArray) Grow(newsize int) {
newdata := make([]byte, newsize)
copy(newdata[newsize-len(ba.data):], ba.data)
ba.data = newdata
}

func BinaryArrayGrow(ls *lua.LState) int {
baud, ba := LuaBinaryArray.Check(ls, 1)
newsize := int(ls.CheckNumber(2))
if newsize > len(ba.data) {
ba.Grow(newsize)
newdata := make([]byte, newsize)
copy(newdata[newsize-len(ba.data):], ba.data)
ba.data = newdata
baud.Value = ba
}
return 0
Expand Down
23 changes: 9 additions & 14 deletions flow/pua/flatbuffers_builder.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package pua

import (
"slices"

"github.com/yuin/gopher-lua"
)

Expand Down Expand Up @@ -58,18 +60,11 @@ func (b *Builder) Prep(width uint8, additional int) {
space := alignsize + int(width) + additional

if b.head < space {
oldBufSize := len(b.ba.data)
if oldBufSize == 0 {
b.ba.data = make([]byte, space)
b.head = space
} else {
newBufSize := oldBufSize
for newBufSize < oldBufSize+space {
newBufSize *= 2
}
b.ba.Grow(newBufSize)
b.head += newBufSize - oldBufSize
}
oldlen := len(b.ba.data)
newdata := slices.Grow(b.ba.data, space)
newdata = newdata[:cap(newdata)]
copy(newdata[:oldlen], newdata[len(newdata)-oldlen:])
b.head += len(newdata) - oldlen
}

b.Pad(alignsize)
Expand Down Expand Up @@ -137,8 +132,8 @@ func (b *Builder) Slot(ls *lua.LState, slotnum int) {
ls.RaiseError("Slot called outside nested context")
return
}
for slotnum < len(b.currentVT) {
b.currentVT = append(b.currentVT, 0)
if slotnum >= len(b.currentVT) {
b.currentVT = slices.Grow(b.currentVT, slotnum-len(b.currentVT)+1)
}
b.currentVT[slotnum] = b.Offset()
}
Expand Down

0 comments on commit 445ae0e

Please sign in to comment.