mirror of
https://github.com/google/flatbuffers.git
synced 2026-07-01 11:21:38 +00:00
[Lua] GetRootAs can accept strings. Made Luatest Benchmarks optional (#6593)
This commit is contained in:
@@ -124,6 +124,10 @@ class LuaGenerator : public BaseGenerator {
|
|||||||
|
|
||||||
code += "function " + NormalizedName(struct_def) + ".GetRootAs" +
|
code += "function " + NormalizedName(struct_def) + ".GetRootAs" +
|
||||||
NormalizedName(struct_def) + "(buf, offset)\n";
|
NormalizedName(struct_def) + "(buf, offset)\n";
|
||||||
|
code += std::string(Indent) + "if type(buf) == \"string\" then\n";
|
||||||
|
code += std::string(Indent) + Indent +
|
||||||
|
"buf = flatbuffers.binaryArray.New(buf)\n";
|
||||||
|
code += std::string(Indent) + "end\n";
|
||||||
code += std::string(Indent) +
|
code += std::string(Indent) +
|
||||||
"local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)\n";
|
"local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)\n";
|
||||||
code += std::string(Indent) + "local o = " + NormalizedName(struct_def) +
|
code += std::string(Indent) + "local o = " + NormalizedName(struct_def) +
|
||||||
|
|||||||
@@ -14,6 +14,9 @@ function Monster.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function Monster.GetRootAsMonster(buf, offset)
|
function Monster.GetRootAsMonster(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = Monster.New()
|
local o = Monster.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function Referrable.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function Referrable.GetRootAsReferrable(buf, offset)
|
function Referrable.GetRootAsReferrable(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = Referrable.New()
|
local o = Referrable.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function Stat.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function Stat.GetRootAsStat(buf, offset)
|
function Stat.GetRootAsStat(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = Stat.New()
|
local o = Stat.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function TestSimpleTableWithEnum.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function TestSimpleTableWithEnum.GetRootAsTestSimpleTableWithEnum(buf, offset)
|
function TestSimpleTableWithEnum.GetRootAsTestSimpleTableWithEnum(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = TestSimpleTableWithEnum.New()
|
local o = TestSimpleTableWithEnum.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function TypeAliases.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function TypeAliases.GetRootAsTypeAliases(buf, offset)
|
function TypeAliases.GetRootAsTypeAliases(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = TypeAliases.New()
|
local o = TypeAliases.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function Monster.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function Monster.GetRootAsMonster(buf, offset)
|
function Monster.GetRootAsMonster(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = Monster.New()
|
local o = Monster.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function InParentNamespace.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function InParentNamespace.GetRootAsInParentNamespace(buf, offset)
|
function InParentNamespace.GetRootAsInParentNamespace(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = InParentNamespace.New()
|
local o = InParentNamespace.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -1,5 +1,16 @@
|
|||||||
package.path = string.format("../lua/?.lua;./?.lua;%s",package.path)
|
package.path = string.format("../lua/?.lua;./?.lua;%s",package.path)
|
||||||
|
|
||||||
|
local performBenchmarkTests = false
|
||||||
|
|
||||||
|
if #arg > 1 then
|
||||||
|
print("usage: lua luatests [benchmark]");
|
||||||
|
return
|
||||||
|
elseif #arg > 0 then
|
||||||
|
if(arg[1] == "benchmark") then
|
||||||
|
performBenchmarkTests = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function checkReadBuffer(buf, offset, sizePrefix)
|
local function checkReadBuffer(buf, offset, sizePrefix)
|
||||||
offset = offset or 0
|
offset = offset or 0
|
||||||
|
|
||||||
@@ -248,6 +259,15 @@ local function benchmarkReadBuffer(count)
|
|||||||
print(string.format('traversed %d %d-byte flatbuffers in %.2fsec: %.2f/msec, %.2fMB/sec',
|
print(string.format('traversed %d %d-byte flatbuffers in %.2fsec: %.2f/msec, %.2fMB/sec',
|
||||||
count, #buf, dur, rate, dataRate))
|
count, #buf, dur, rate, dataRate))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function getRootAs_canAcceptString()
|
||||||
|
local f = assert(io.open('monsterdata_test.mon', 'rb'))
|
||||||
|
local wireData = f:read("*a")
|
||||||
|
f:close()
|
||||||
|
assert(type(wireData) == "string", "Data is not a string");
|
||||||
|
local mon = monster.GetRootAsMonster(wireData, 0)
|
||||||
|
assert(mon:Hp() == 80, "Monster Hp is not 80")
|
||||||
|
end
|
||||||
|
|
||||||
local tests =
|
local tests =
|
||||||
{
|
{
|
||||||
@@ -264,6 +284,14 @@ local tests =
|
|||||||
f = testCanonicalData,
|
f = testCanonicalData,
|
||||||
d = "Tests Canonical flatbuffer file included in repo"
|
d = "Tests Canonical flatbuffer file included in repo"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
f = getRootAs_canAcceptString,
|
||||||
|
d = "Tests that GetRootAs<type>() generated methods accept strings"
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
local benchmarks =
|
||||||
|
{
|
||||||
{
|
{
|
||||||
f = benchmarkMakeMonster,
|
f = benchmarkMakeMonster,
|
||||||
d = "Benchmark making monsters",
|
d = "Benchmark making monsters",
|
||||||
@@ -302,6 +330,12 @@ local result, err = xpcall(function()
|
|||||||
return s:sub(1,-2)
|
return s:sub(1,-2)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if performBenchmarkTests then
|
||||||
|
for _,benchmark in ipairs(benchmarks) do
|
||||||
|
table.insert(tests, benchmark)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local testsPassed, testsFailed = 0,0
|
local testsPassed, testsFailed = 0,0
|
||||||
for _,test in ipairs(tests) do
|
for _,test in ipairs(tests) do
|
||||||
local allargs = test.args or {{}}
|
local allargs = test.args or {{}}
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function TableInNestedNS.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function TableInNestedNS.GetRootAsTableInNestedNS(buf, offset)
|
function TableInNestedNS.GetRootAsTableInNestedNS(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = TableInNestedNS.New()
|
local o = TableInNestedNS.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function SecondTableInA.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function SecondTableInA.GetRootAsSecondTableInA(buf, offset)
|
function SecondTableInA.GetRootAsSecondTableInA(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = SecondTableInA.New()
|
local o = SecondTableInA.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function TableInFirstNS.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function TableInFirstNS.GetRootAsTableInFirstNS(buf, offset)
|
function TableInFirstNS.GetRootAsTableInFirstNS(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = TableInFirstNS.New()
|
local o = TableInFirstNS.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ function TableInC.New()
|
|||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
function TableInC.GetRootAsTableInC(buf, offset)
|
function TableInC.GetRootAsTableInC(buf, offset)
|
||||||
|
if type(buf) == "string" then
|
||||||
|
buf = flatbuffers.binaryArray.New(buf)
|
||||||
|
end
|
||||||
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)
|
||||||
local o = TableInC.New()
|
local o = TableInC.New()
|
||||||
o:Init(buf, n + offset)
|
o:Init(buf, n + offset)
|
||||||
|
|||||||
Reference in New Issue
Block a user