summaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorDerek Bailey <dbaileychess@gmail.com>2019-11-04 14:27:59 -0800
committerWouter van Oortmerssen <aardappel@gmail.com>2019-11-04 14:27:59 -0800
commit661bedd837f3c9c3ddff092e812159d3a3ebecdc (patch)
treecd5ed3104903889e7138ff8decc44632a0808991 /lua
parent8526e12d73d4b793024c60dd348f8b92a24c6ce2 (diff)
downloadflatbuffers-661bedd837f3c9c3ddff092e812159d3a3ebecdc.tar.gz
flatbuffers-661bedd837f3c9c3ddff092e812159d3a3ebecdc.tar.bz2
flatbuffers-661bedd837f3c9c3ddff092e812159d3a3ebecdc.zip
Add Lua FlatbufferBuilder Clean() method to enable reuseable builders (#5606)
Diffstat (limited to 'lua')
-rw-r--r--lua/flatbuffers/builder.lua17
1 files changed, 17 insertions, 0 deletions
diff --git a/lua/flatbuffers/builder.lua b/lua/flatbuffers/builder.lua
index 2fb22204..19f214de 100644
--- a/lua/flatbuffers/builder.lua
+++ b/lua/flatbuffers/builder.lua
@@ -60,6 +60,23 @@ function m.New(initialSize)
return o
end
+-- Clears the builder and resets the state. It does not actually clear the backing binary array, it just reuses it as
+-- needed. This is a performant way to use the builder for multiple constructions without the overhead of multiple
+-- builder allocations.
+function mt:Clear()
+ self.finished = false
+ self.nested = false
+ self.minalign = 1
+ self.currentVTable = nil
+ self.objectEnd = nil
+ self.head = #self.bytes -- place the head at the end of the binary array
+
+ -- clear vtables instead of making a new table
+ local vtable = self.vtables
+ local vtableCount = #vtable
+ for i=1,vtableCount do vtable[i] = nil end
+end
+
function mt:Output(full)
assert(self.finished, "Builder Not Finished")
if full then