Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions lua/flatbuffers/builder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -296,25 +296,62 @@ function mt:Slot(slotnum)
self.currentVTable[slotnum + 1] = self:Offset()
end

local function finish(self, rootTable, sizePrefix)
local function finish(self, rootTable, sizePrefix, fileIdentifier)
UOffsetT:EnforceNumber(rootTable)
self:Prep(self.minalign, sizePrefix and 8 or 4)
self:PrependUOffsetTRelative(rootTable)
local hasFid = fileIdentifier ~= nil
if hasFid then
assert(#fileIdentifier == 4, "File identifier must be exactly 4 bytes")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checked by flatc, so doesn't need to be checked here again if this function is typically called by generated code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is technically accessible by the low level public API -- I have added a convenience function to call it from the generated code but that function is brand new. Most lua users will be interating with the Finish calls below and could pass a bad value in here. The below code will only add the first 4 bytes, so it wouldn't break, but I figure its better to tell the user of the problem than let it silently succeed.

end

local fid_byte_count = (hasFid and 4 or 0)

-- alignment:
-- root offset (4)
-- optional file id (4)
-- optional size prefix (4)
self:Prep(
self.minalign,
(sizePrefix and 4 or 0) +
4 +
fid_byte_count
)

-- root offset (points past file identifier if present)
self:PrependUOffsetTRelative(rootTable + fid_byte_count)

-- file identifier
if hasFid then
for i = 4, 1, -1 do
self:PrependByte(string.byte(fileIdentifier, i))
end
end

-- size prefix
if sizePrefix then
local size = self.bytes.size - self.head
Int32:EnforceNumber(size)
self:PrependInt32(size)
end

self.finished = true
return self.head
end


function mt:Finish(rootTable)
return finish(self, rootTable, false)
return finish(self, rootTable, false, nil)
end

function mt:FinishSizePrefixed(rootTable)
return finish(self, rootTable, true)
return finish(self, rootTable, true, nil)
end

function mt:FinishWithIdentifier(rootTable, fileIdentifier)
return finish(self, rootTable, false, fileIdentifier)
end

function mt:FinishSizePrefixedWithIdentifier(rootTable, fileIdentifier)
return finish(self, rootTable, true, fileIdentifier)
end

function mt:Prepend(flags, off)
Expand Down
35 changes: 35 additions & 0 deletions src/bfbs_gen_lua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,13 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
code += "\n";

if (object == root_object) {
// emit file identifier if present
const auto ident = schema_->file_ident();
if (ident && ident->size() == 4) {
code += "local FileIdentifier = \"" + ident->str() + "\"\n";
code += "\n";
}

code += "function " + object_name + ".GetRootAs" + object_name +
"(buf, offset)\n";
code += " if type(buf) == \"string\" then\n";
Expand Down Expand Up @@ -454,6 +461,34 @@ class LuaBfbsGenerator : public BaseBfbsGenerator {
code += " return builder:EndObject()\n";
code += "end\n";
code += "\n";

if (object == root_object) {
code += "function " + object_name + ".Finish" + object_name +
"Buffer(builder, offset)\n";
// emit file identifier if present
const auto ident = schema_->file_ident();
if (ident && ident->size() == 4) {
code += " builder:FinishWithIdentifier(offset, FileIdentifier)\n";
} else {
code += " builder:Finish(offset)\n";
}
code += "end\n";
code += "\n";

// size prefixed option
code += "function " + object_name + ".FinishSizePrefixed" +
object_name + "Buffer(builder, offset)\n";
// emit file identifier if present
if (ident && ident->size() == 4) {
code +=
" builder:FinishSizePrefixedWithIdentifier(offset, "
"FileIdentifier)\n";
} else {
code += " builder:FinishSizePrefixed(offset)\n";
}
code += "end\n";
code += "\n";
}
}

EmitCodeBlock(code, object_name, ns, object->declaration_file()->str());
Expand Down
10 changes: 10 additions & 0 deletions tests/MyGame/Example/Monster.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ function Monster.New()
return o
end

local FileIdentifier = "MONS"

function Monster.GetRootAsMonster(buf, offset)
if type(buf) == "string" then
buf = flatbuffers.binaryArray.New(buf)
Expand Down Expand Up @@ -1099,4 +1101,12 @@ function Monster.End(builder)
return builder:EndObject()
end

function Monster.FinishMonsterBuffer(builder, offset)
builder:FinishWithIdentifier(offset, FileIdentifier)
end

function Monster.FinishSizePrefixedMonsterBuffer(builder, offset)
builder:FinishSizePrefixedWithIdentifier(offset, FileIdentifier)
end

return Monster
Loading