Skip to content

Commit

Permalink
more fixes
Browse files Browse the repository at this point in the history
BinaryArrayNew: back then I thought lua parameters were passed on the stack.. nope
EndVector: need to clear nested bit
PrependSlot: handle U64/I64 == number check
On that note, implement __eq/__le/__lt for int64/uint64
Also implement general i64/u64/float64 methods for int64/uint64
& get rid of NI64/NU64 types, just use unwrapped integers
Use ls.RaisePanic over panic
  • Loading branch information
serprex committed Mar 15, 2024
1 parent a3087a2 commit 80ef2de
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 47 deletions.
2 changes: 1 addition & 1 deletion flow/pua/flatbuffers_binaryarray.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func FlatBuffers_BinaryArray_Loader(ls *lua.LState) int {
}

func BinaryArrayNew(ls *lua.LState) int {
lval := ls.Get(-1)
lval := ls.Get(1)
var ba BinaryArray
switch val := lval.(type) {
case lua.LString:
Expand Down
53 changes: 35 additions & 18 deletions flow/pua/flatbuffers_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ type Builder struct {
minalign uint8
}

func (b *Builder) EndVector(vectorSize int) int {
func (b *Builder) EndVector(ls *lua.LState, vectorSize int) int {
if !b.nested {
panic("EndVector called outside nested context")
ls.RaiseError("EndVector called outside nested context")
return 0
}
b.nested = false
b.PlaceU64(uint64(vectorSize), uint32n)
return b.Offset()
}
Expand Down Expand Up @@ -78,10 +80,26 @@ func (b *Builder) PrependU64(n N, x uint64) {
}

func (b *Builder) PrependSlot(ls *lua.LState, n N, slotnum int, x lua.LValue, d lua.LValue) {
// TODO implement __eq for U64/I64
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(slotnum)
b.Slot(ls, slotnum)
}
}

Expand All @@ -108,9 +126,10 @@ func (b *Builder) PrependVOffsetT(off uint16) {
b.PlaceU64(uint64(off), uint16n)
}

func (b *Builder) Slot(slotnum int) {
func (b *Builder) Slot(ls *lua.LState, slotnum int) {
if !b.nested {
panic("Slot called outside nested context")
ls.RaiseError("Slot called outside nested context")
return
}
for slotnum < len(b.currentVT) {
b.currentVT = append(b.currentVT, 0)
Expand Down Expand Up @@ -300,11 +319,11 @@ func BuilderStartObject(ls *lua.LState) int {
ls.RaiseError("StartObject called inside nested context")
return 0
}
b.nested = true

numFields := int(ls.CheckNumber(2))
b.currentVT = make([]int, numFields)
b.objectEnd = b.Offset()
b.nested = true
return 0
}

Expand All @@ -317,8 +336,10 @@ func BuilderWriteVtable(ls *lua.LState) int {
func BuilderEndObject(ls *lua.LState) int {
b := LuaBuilder.StartMeta(ls)
if !b.nested {
panic("EndObject called outside nested context")
ls.RaiseError("EndObject called outside nested context")
return 0
}
b.nested = false
ls.Push(lua.LNumber(b.WriteVtable(ls)))
return 1
}
Expand Down Expand Up @@ -380,12 +401,8 @@ func BuilderStartVector(ls *lua.LState) int {

func BuilderEndVector(ls *lua.LState) int {
b := LuaBuilder.StartMeta(ls)
if !b.nested {
ls.RaiseError("EndVector called outside nested context")
}
b.nested = false
b.PlaceU64(uint64(ls.CheckNumber(2)), uint32n)
ls.Push(lua.LNumber(b.Offset()))
size := int(ls.CheckNumber(2))
ls.Push(lua.LNumber(b.EndVector(ls, size)))
return 1
}

Expand All @@ -404,13 +421,13 @@ func BuilderCreateString(ls *lua.LState) int {
b.head -= lens
copy(b.ba.data[b.head:], s)

return b.EndVector(lens)
return b.EndVector(ls, lens)
}

func BuilderSlot(ls *lua.LState) int {
b := LuaBuilder.StartMeta(ls)
slotnum := int(ls.CheckNumber(2))
b.Slot(slotnum)
b.Slot(ls, slotnum)
return 0
}

Expand Down Expand Up @@ -516,7 +533,7 @@ func BuilderPrependStructSlot(ls *lua.LState) int {
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(int(ls.CheckNumber(2)))
b.Slot(ls, int(ls.CheckNumber(2)))
}
}
return 0
Expand All @@ -528,7 +545,7 @@ func BuilderPrependUOffsetTRelativeSlot(ls *lua.LState) int {
d := int(ls.CheckNumber(4))
if x != d {
b.PrependOffsetTRelative(ls, x, uint32n)
b.Slot(int(ls.CheckNumber(2)))
b.Slot(ls, int(ls.CheckNumber(2)))
}
return 0
}
Expand Down
31 changes: 11 additions & 20 deletions flow/pua/flatbuffers_numtypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ func (n *N) Pack(ls *lua.LState, buf []byte, val lua.LValue) {
switch lv := val.(type) {
case *lua.LUserData:
switch v := lv.Value.(type) {
case NI64:
n.PackU64(buf, uint64(v.val))
case NU64:
n.PackU64(buf, v.val)
case int64:
n.PackU64(buf, uint64(v))
case uint64:
n.PackU64(buf, v)
default:
n.PackU64(buf, 0)
}
Expand All @@ -88,10 +88,10 @@ func (n *N) Pack(ls *lua.LState, buf []byte, val lua.LValue) {
switch lv := val.(type) {
case *lua.LUserData:
switch v := lv.Value.(type) {
case NI64:
n.PackU64(buf, math.Float64bits(float64(v.val)))
case NU64:
n.PackU64(buf, math.Float64bits(float64(v.val)))
case int64:
n.PackU64(buf, math.Float64bits(float64(v)))
case uint64:
n.PackU64(buf, math.Float64bits(float64(v)))
default:
n.PackU64(buf, 0)
}
Expand Down Expand Up @@ -146,9 +146,9 @@ func (n *N) Unpack(ls *lua.LState, buf []byte) lua.LValue {
case 8:
u64 := binary.LittleEndian.Uint64(buf)
if n.signed {
return LuaNI64.New(ls, NI64{int64(u64)})
return LuaI64.New(ls, int64(u64))
} else {
return LuaNU64.New(ls, NU64{u64})
return LuaU64.New(ls, u64)
}
}
case tyfloat:
Expand All @@ -165,16 +165,7 @@ func (n *N) Unpack(ls *lua.LState, buf []byte) lua.LValue {
panic("invalid numeric metatype")
}

type (
NI64 struct{ val int64 }
NU64 struct{ val uint64 }
)

var (
LuaN = LuaUserDataType[N]{Name: "flatbuffers_n"}
LuaNI64 = LuaUserDataType[NI64]{Name: "flatbuffers_i64"}
LuaNU64 = LuaUserDataType[NU64]{Name: "flatbuffers_u64"}
)
var LuaN = LuaUserDataType[N]{Name: "flatbuffers_n"}

func FlatBuffers_N_Loader(ls *lua.LState) int {
m := ls.NewTable()
Expand Down
Loading

0 comments on commit 80ef2de

Please sign in to comment.