mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-02 20:15:34 +00:00
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
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
8ea293b988
commit
ba5eb3b5cf
123
lua/flatbuffers/binaryarray.lua
Normal file
123
lua/flatbuffers/binaryarray.lua
Normal file
@@ -0,0 +1,123 @@
|
||||
local m = {} -- the module table
|
||||
|
||||
local mt = {} -- the module metatable
|
||||
|
||||
-- given a binary array, set a metamethod to return its length
|
||||
-- (e.g., #binaryArray, calls this)
|
||||
function mt:__len()
|
||||
return self.size
|
||||
end
|
||||
|
||||
-- Create a new binary array of an initial size
|
||||
function m.New(sizeOrString)
|
||||
-- the array storage itself
|
||||
local o = {}
|
||||
|
||||
if type(sizeOrString) == "string" then
|
||||
o.str = sizeOrString
|
||||
o.size = #sizeOrString
|
||||
elseif type(sizeOrString) == "number" then
|
||||
o.data = {}
|
||||
o.size = sizeOrString
|
||||
else
|
||||
error("Expect a integer size value or string to construct a binary array")
|
||||
end
|
||||
-- set the inheritance
|
||||
setmetatable(o, {__index = mt, __len = mt.__len})
|
||||
return o
|
||||
end
|
||||
|
||||
-- Get a slice of the binary array from start to end position
|
||||
function mt:Slice(startPos, endPos)
|
||||
startPos = startPos or 0
|
||||
endPos = endPos or self.size
|
||||
local d = self.data
|
||||
if d then
|
||||
-- if the self.data is defined, we are building the buffer
|
||||
-- in a Lua table
|
||||
|
||||
-- new table to store the slice components
|
||||
local b = {}
|
||||
|
||||
-- starting with the startPos, put all
|
||||
-- values into the new table to be concat later
|
||||
-- updated the startPos based on the size of the
|
||||
-- value
|
||||
while startPos < endPos do
|
||||
local v = d[startPos] or '/0'
|
||||
table.insert(b, v)
|
||||
startPos = startPos + #v
|
||||
end
|
||||
|
||||
-- combine the table of strings into one string
|
||||
-- this is faster than doing a bunch of concats by themselves
|
||||
return table.concat(b)
|
||||
else
|
||||
-- n.b start/endPos are 0-based incoming, so need to convert
|
||||
-- correctly. in python a slice includes start -> end - 1
|
||||
return self.str:sub(startPos+1, endPos)
|
||||
end
|
||||
end
|
||||
|
||||
-- Grow the binary array to a new size, placing the exisiting data
|
||||
-- at then end of the new array
|
||||
function mt:Grow(newsize)
|
||||
-- the new table to store the data
|
||||
local newT = {}
|
||||
|
||||
-- the offset to be applied to existing entries
|
||||
local offset = newsize - self.size
|
||||
|
||||
-- loop over all the current entries and
|
||||
-- add them to the new table at the correct
|
||||
-- offset location
|
||||
local d = self.data
|
||||
for i,data in pairs(d) do
|
||||
newT[i + offset] = data
|
||||
end
|
||||
|
||||
-- update this storage with the new table and size
|
||||
self.data = newT
|
||||
self.size = newsize
|
||||
end
|
||||
|
||||
-- memorization for padding strings
|
||||
local pads = {}
|
||||
|
||||
-- pad the binary with n \0 bytes at the starting position
|
||||
function mt:Pad(n, startPos)
|
||||
-- use memorization to avoid creating a bunch of strings
|
||||
-- all the time
|
||||
local s = pads[n]
|
||||
if not s then
|
||||
s = string.rep('\0', n)
|
||||
pads[n] = s
|
||||
end
|
||||
|
||||
-- store the padding string at the start position in the
|
||||
-- Lua table
|
||||
self.data[startPos] = s
|
||||
end
|
||||
|
||||
-- Sets the binary array value at the specified position
|
||||
function mt:Set(value, position)
|
||||
self.data[position] = value
|
||||
end
|
||||
|
||||
-- locals for slightly faster access
|
||||
local sunpack = string.unpack
|
||||
local spack = string.pack
|
||||
|
||||
-- Pack the data into a binary representation
|
||||
function m.Pack(fmt, ...)
|
||||
return spack(fmt, ...)
|
||||
end
|
||||
|
||||
-- Unpack the data from a binary representation in
|
||||
-- a Lua value
|
||||
function m.Unpack(fmt, s, pos)
|
||||
return sunpack(fmt, s.str, pos + 1)
|
||||
end
|
||||
|
||||
-- Return the binary array module
|
||||
return m
|
||||
Reference in New Issue
Block a user