diff --git a/lua/flatbuffers/builder.lua b/lua/flatbuffers/builder.lua index 19f214de5..4a0c4f6a2 100644 --- a/lua/flatbuffers/builder.lua +++ b/lua/flatbuffers/builder.lua @@ -29,12 +29,12 @@ local getAlignSize = compat.GetAlignSize local function vtableEqual(a, objectStart, b) UOffsetT:EnforceNumber(objectStart) - if (#a * VOffsetT.bytewidth) ~= #b then + if (#a * 2) ~= #b then return false end for i, elem in ipairs(a) do - local x = string.unpack(VOffsetT.packFmt, b, 1 + (i - 1) * VOffsetT.bytewidth) + local x = string.unpack(VOffsetT.packFmt, b, 1 + (i - 1) * 2) if x ~= 0 or elem ~= 0 then local y = objectStart - elem if x ~= y then @@ -121,7 +121,7 @@ function mt:WriteVtable() local vt2lenstr = self.bytes:Slice(vt2Start, vt2Start+1) local vt2Len = string.unpack(VOffsetT.packFmt, vt2lenstr, 1) - local metadata = VtableMetadataFields * VOffsetT.bytewidth + local metadata = VtableMetadataFields * 2 local vt2End = vt2Start + vt2Len local vt2 = self.bytes:Slice(vt2Start+metadata,vt2End) @@ -150,7 +150,7 @@ function mt:WriteVtable() self:PrependVOffsetT(objectSize) local vBytes = #self.currentVTable + VtableMetadataFields - vBytes = vBytes * VOffsetT.bytewidth + vBytes = vBytes * 2 self:PrependVOffsetT(vBytes) local objectStart = #self.bytes - objectOffset @@ -225,17 +225,17 @@ function mt:Prep(size, additionalBytes) end function mt:PrependSOffsetTRelative(off) - self:Prep(SOffsetT.bytewidth, 0) + self:Prep(4, 0) assert(off <= self:Offset(), "Offset arithmetic error") - local off2 = self:Offset() - off + SOffsetT.bytewidth + local off2 = self:Offset() - off + 4 self:Place(off2, SOffsetT) end function mt:PrependUOffsetTRelative(off) - self:Prep(UOffsetT.bytewidth, 0) + self:Prep(4, 0) local soffset = self:Offset() if off <= soffset then - local off2 = soffset - off + UOffsetT.bytewidth + local off2 = soffset - off + 4 self:Place(off2, UOffsetT) else error("Offset arithmetic error") @@ -245,8 +245,9 @@ end function mt:StartVector(elemSize, numElements, alignment) assert(not self.nested) self.nested = true - self:Prep(Uint32.bytewidth, elemSize * numElements) - self:Prep(alignment, elemSize * numElements) + local elementSize = elemSize * numElements + self:Prep(4, elementSize) -- Uint32 length + self:Prep(alignment, elementSize) return self:Offset() end @@ -263,7 +264,7 @@ function mt:CreateString(s) assert(type(s) == "string") - self:Prep(UOffsetT.bytewidth, (#s + 1)*Uint8.bytewidth) + self:Prep(4, #s + 1) self:Place(0, Uint8) local l = #s @@ -271,20 +272,21 @@ function mt:CreateString(s) self.bytes:Set(s, self.head, self.head + l) - return self:EndVector(#s) + return self:EndVector(l) end function mt:CreateByteVector(x) assert(not self.nested) self.nested = true - self:Prep(UOffsetT.bytewidth, #x*Uint8.bytewidth) local l = #x + self:Prep(4, l) + self.head = self.head - l self.bytes:Set(x, self.head, self.head + l) - return self:EndVector(#x) + return self:EndVector(l) end function mt:Slot(slotnum) @@ -295,12 +297,7 @@ end local function finish(self, rootTable, sizePrefix) UOffsetT:EnforceNumber(rootTable) - local prepSize = UOffsetT.bytewidth - if sizePrefix then - prepSize = prepSize + Int32.bytewidth - end - - self:Prep(self.minalign, prepSize) + self:Prep(self.minalign, sizePrefix and 8 or 4) self:PrependUOffsetTRelative(rootTable) if sizePrefix then local size = #self.bytes - self.head @@ -325,8 +322,9 @@ function mt:Prepend(flags, off) end function mt:PrependSlot(flags, o, x, d) - flags:EnforceNumber(x) - flags:EnforceNumber(d) + flags:EnforceNumbers(x,d) +-- flags:EnforceNumber(x) +-- flags:EnforceNumber(d) if x ~= d then self:Prepend(flags, x) self:Slot(o) diff --git a/lua/flatbuffers/numTypes.lua b/lua/flatbuffers/numTypes.lua index 8fec21c4d..01f419930 100644 --- a/lua/flatbuffers/numTypes.lua +++ b/lua/flatbuffers/numTypes.lua @@ -34,6 +34,20 @@ function type_mt:EnforceNumber(n) error("Number is not in the valid range") end +function type_mt:EnforceNumbers(a,b) + -- duplicate code since the overhead of function calls + -- for such a popular method is time consuming + if not self.min_value and not self.max_value then + return + end + + if self.min_value <= a and a <= self.max_value and self.min_value <= b and b <= self.max_value then + return + end + + error("Number is not in the valid range") +end + function type_mt:EnforceNumberAndPack(n) return bpack(self.packFmt, n) end @@ -58,6 +72,7 @@ local bool_mt = Unpack = function(self, buf, pos) return buf[pos] == "1" end, ValidNumber = function(self, n) return true end, -- anything is a valid boolean in Lua EnforceNumber = function(self, n) end, -- anything is a valid boolean in Lua + EnforceNumbers = function(self, a, b) end, -- anything is a valid boolean in Lua EnforceNumberAndPack = function(self, n) return self:Pack(value) end, } diff --git a/lua/flatbuffers/view.lua b/lua/flatbuffers/view.lua index da0f8bf7c..fde15c3d8 100644 --- a/lua/flatbuffers/view.lua +++ b/lua/flatbuffers/view.lua @@ -6,69 +6,83 @@ local mt_name = "flatbuffers.view.mt" local N = require("flatbuffers.numTypes") local binaryarray = require("flatbuffers.binaryarray") +local function enforceOffset(off) + if off < 0 or off > 42949672951 then + error("Offset is not valid") + end +end + +local unpack = string.unpack +local function unPackUoffset(bytes, off) + return unpack("