Files
flatbuffers-bigfoot/lua/flatbuffers/view.lua
Derek Bailey ba5eb3b5cf Lua (5.3) Language addition (#4804)
* starting Lua port of python implmention. Syncing commit

* Bulk of Lua module port from Python done. Not tested, only static analysis. Need to work on binary strings. Started work on flatc lua code generation

* Fixed all the basic errors to produced a binary output from the builder, don't know if it is generated correctly, but it contains data, so that must be good

* fixed binary set command that was extending the array improperly

* continued improvement

* Moved lua submodules down a directory so their names don't clash with potential other modules. Added compat module to provide Lua versioning logic

* Successful sample port from Python

* working on testing Lua code with formal tests

* continued to work on tests and fixes to code to make tests pass

* Added reading buffer test

* Changed binaryarray implmentation to use a temporary table for storing data, and then serialize it to a string when requested. This double the rate of building flatbuffers compared to the string approach.

* Didn't need encode module as it just added another layer of indirection that isn't need

* profiled reading buffers, optimizations to increase read performance of monster data to ~7 monster / millisecond

* Writing profiler improvments. Get about
~2 monsters/millisecond building rate

* removed Numpy generation from Lua (came from the Python port)

* math.pow is deprecated in Lua 5.3, so changed to ^ notation. Also added .bat script for starting Lua tests

* adding results of generate_code.bat

* simple edits for code review in PR.

* There was a buffer overflow in inserting the keywords into the unorder set for both the Lua and Python code gens. Changed insertion to use iterators.

* fixed spacing issue

* basic documenation/tutorial updates. Updated sample_binary.lua to reflect the tutorial better

* removed windows-specific build step in Lua tests
2018-07-05 15:55:57 -07:00

97 lines
2.3 KiB
Lua

local m = {}
local mt = {}
local mt_name = "flatbuffers.view.mt"
local N = require("flatbuffers.numTypes")
local binaryarray = require("flatbuffers.binaryarray")
function m.New(buf, pos)
N.UOffsetT:EnforceNumber(pos)
-- need to convert from a string buffer into
-- a binary array
local o = {
bytes = type(buf) == "string" and binaryarray.New(buf) or buf,
pos = pos
}
setmetatable(o, {__index = mt, __metatable = mt_name})
return o
end
function mt:Offset(vtableOffset)
local vtable = self.pos - self:Get(N.SOffsetT, self.pos)
local vtableEnd = self:Get(N.VOffsetT, vtable)
if vtableOffset < vtableEnd then
return self:Get(N.VOffsetT, vtable + vtableOffset)
end
return 0
end
function mt:Indirect(off)
N.UOffsetT:EnforceNumber(off)
return off + N.UOffsetT:Unpack(self.bytes, off)
end
function mt:String(off)
N.UOffsetT:EnforceNumber(off)
off = off + N.UOffsetT:Unpack(self.bytes, off)
local start = off + N.UOffsetT.bytewidth
local length = N.UOffsetT:Unpack(self.bytes, off)
return self.bytes:Slice(start, start+length)
end
function mt:VectorLen(off)
N.UOffsetT:EnforceNumber(off)
off = off + self.pos
off = off + N.UOffsetT:Unpack(self.bytes, off)
return N.UOffsetT:Unpack(self.bytes, off)
end
function mt:Vector(off)
N.UOffsetT:EnforceNumber(off)
off = off + self.pos
local x = off + self:Get(N.UOffsetT, off)
x = x + N.UOffsetT.bytewidth
return x
end
function mt:Union(t2, off)
assert(getmetatable(t2) == mt_name)
N.UOffsetT:EnforceNumber(off)
off = off + self.pos
t2.pos = off + self:Get(N.UOffsetT, off)
t2.bytes = self.bytes
end
function mt:Get(flags, off)
N.UOffsetT:EnforceNumber(off)
return flags:Unpack(self.bytes, off)
end
function mt:GetSlot(slot, d, validatorFlags)
N.VOffsetT:EnforceNumber(slot)
if validatorFlags then
validatorFlags:EnforceNumber(d)
end
local off = self:Offset(slot)
if off == 0 then
return d
end
return self:Get(validatorFlags, self.pos + off)
end
function mt:GetVOffsetTSlot(slot, d)
N.VOffsetT:EnforceNumber(slot)
N.VOffsetT:EnforceNumber(d)
local off = self:Offset(slot)
if off == 0 then
return d
end
return off
end
return m