summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Bailey <derekbailey@google.com>2021-04-27 13:02:13 -0700
committerGitHub <noreply@github.com>2021-04-27 13:02:13 -0700
commit14725d6c3b901f210ee08715fb5c359369bcf3ef (patch)
treedfc10b2dd184864bfce19f0e8973ee05c466e1b9
parent16836ff95a7f112f66a8c5994a508e6bfec010c9 (diff)
downloadflatbuffers-14725d6c3b901f210ee08715fb5c359369bcf3ef.tar.gz
flatbuffers-14725d6c3b901f210ee08715fb5c359369bcf3ef.tar.bz2
flatbuffers-14725d6c3b901f210ee08715fb5c359369bcf3ef.zip
[Lua] GetRootAs can accept strings. Made Luatest Benchmarks optional (#6593)
-rw-r--r--src/idl_gen_lua.cpp4
-rw-r--r--tests/MyGame/Example/Monster.lua3
-rw-r--r--tests/MyGame/Example/Referrable.lua3
-rw-r--r--tests/MyGame/Example/Stat.lua3
-rw-r--r--tests/MyGame/Example/TestSimpleTableWithEnum.lua3
-rw-r--r--tests/MyGame/Example/TypeAliases.lua3
-rw-r--r--tests/MyGame/Example2/Monster.lua3
-rw-r--r--tests/MyGame/InParentNamespace.lua3
-rw-r--r--tests/luatest.lua34
-rw-r--r--tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.lua3
-rw-r--r--tests/namespace_test/NamespaceA/SecondTableInA.lua3
-rw-r--r--tests/namespace_test/NamespaceA/TableInFirstNS.lua3
-rw-r--r--tests/namespace_test/NamespaceC/TableInC.lua3
13 files changed, 71 insertions, 0 deletions
diff --git a/src/idl_gen_lua.cpp b/src/idl_gen_lua.cpp
index 9788485e..e7e78343 100644
--- a/src/idl_gen_lua.cpp
+++ b/src/idl_gen_lua.cpp
@@ -124,6 +124,10 @@ class LuaGenerator : public BaseGenerator {
code += "function " + NormalizedName(struct_def) + ".GetRootAs" +
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) +
"local n = flatbuffers.N.UOffsetT:Unpack(buf, offset)\n";
code += std::string(Indent) + "local o = " + NormalizedName(struct_def) +
diff --git a/tests/MyGame/Example/Monster.lua b/tests/MyGame/Example/Monster.lua
index 6e520025..fbd2c74e 100644
--- a/tests/MyGame/Example/Monster.lua
+++ b/tests/MyGame/Example/Monster.lua
@@ -14,6 +14,9 @@ function Monster.New()
return o
end
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 o = Monster.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/Example/Referrable.lua b/tests/MyGame/Example/Referrable.lua
index 9b0f5a19..bb78f439 100644
--- a/tests/MyGame/Example/Referrable.lua
+++ b/tests/MyGame/Example/Referrable.lua
@@ -13,6 +13,9 @@ function Referrable.New()
return o
end
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 o = Referrable.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/Example/Stat.lua b/tests/MyGame/Example/Stat.lua
index 69991843..d7fd0580 100644
--- a/tests/MyGame/Example/Stat.lua
+++ b/tests/MyGame/Example/Stat.lua
@@ -13,6 +13,9 @@ function Stat.New()
return o
end
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 o = Stat.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/Example/TestSimpleTableWithEnum.lua b/tests/MyGame/Example/TestSimpleTableWithEnum.lua
index 32c8251d..5c95bf17 100644
--- a/tests/MyGame/Example/TestSimpleTableWithEnum.lua
+++ b/tests/MyGame/Example/TestSimpleTableWithEnum.lua
@@ -13,6 +13,9 @@ function TestSimpleTableWithEnum.New()
return o
end
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 o = TestSimpleTableWithEnum.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/Example/TypeAliases.lua b/tests/MyGame/Example/TypeAliases.lua
index 90f569cd..91c62c44 100644
--- a/tests/MyGame/Example/TypeAliases.lua
+++ b/tests/MyGame/Example/TypeAliases.lua
@@ -13,6 +13,9 @@ function TypeAliases.New()
return o
end
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 o = TypeAliases.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/Example2/Monster.lua b/tests/MyGame/Example2/Monster.lua
index 347b5dba..670ca005 100644
--- a/tests/MyGame/Example2/Monster.lua
+++ b/tests/MyGame/Example2/Monster.lua
@@ -13,6 +13,9 @@ function Monster.New()
return o
end
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 o = Monster.New()
o:Init(buf, n + offset)
diff --git a/tests/MyGame/InParentNamespace.lua b/tests/MyGame/InParentNamespace.lua
index b3fa0c85..8a754b97 100644
--- a/tests/MyGame/InParentNamespace.lua
+++ b/tests/MyGame/InParentNamespace.lua
@@ -13,6 +13,9 @@ function InParentNamespace.New()
return o
end
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 o = InParentNamespace.New()
o:Init(buf, n + offset)
diff --git a/tests/luatest.lua b/tests/luatest.lua
index a2a9d639..4e70e07c 100644
--- a/tests/luatest.lua
+++ b/tests/luatest.lua
@@ -1,5 +1,16 @@
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)
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',
count, #buf, dur, rate, dataRate))
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 =
{
@@ -265,6 +285,14 @@ local tests =
d = "Tests Canonical flatbuffer file included in repo"
},
{
+ f = getRootAs_canAcceptString,
+ d = "Tests that GetRootAs<type>() generated methods accept strings"
+ },
+}
+
+local benchmarks =
+{
+ {
f = benchmarkMakeMonster,
d = "Benchmark making monsters",
args = {
@@ -302,6 +330,12 @@ local result, err = xpcall(function()
return s:sub(1,-2)
end
+ if performBenchmarkTests then
+ for _,benchmark in ipairs(benchmarks) do
+ table.insert(tests, benchmark)
+ end
+ end
+
local testsPassed, testsFailed = 0,0
for _,test in ipairs(tests) do
local allargs = test.args or {{}}
diff --git a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.lua b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.lua
index dd45e583..af86203e 100644
--- a/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.lua
+++ b/tests/namespace_test/NamespaceA/NamespaceB/TableInNestedNS.lua
@@ -13,6 +13,9 @@ function TableInNestedNS.New()
return o
end
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 o = TableInNestedNS.New()
o:Init(buf, n + offset)
diff --git a/tests/namespace_test/NamespaceA/SecondTableInA.lua b/tests/namespace_test/NamespaceA/SecondTableInA.lua
index 9a60e4b1..b953c124 100644
--- a/tests/namespace_test/NamespaceA/SecondTableInA.lua
+++ b/tests/namespace_test/NamespaceA/SecondTableInA.lua
@@ -13,6 +13,9 @@ function SecondTableInA.New()
return o
end
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 o = SecondTableInA.New()
o:Init(buf, n + offset)
diff --git a/tests/namespace_test/NamespaceA/TableInFirstNS.lua b/tests/namespace_test/NamespaceA/TableInFirstNS.lua
index 172e16fd..1b62cc5f 100644
--- a/tests/namespace_test/NamespaceA/TableInFirstNS.lua
+++ b/tests/namespace_test/NamespaceA/TableInFirstNS.lua
@@ -13,6 +13,9 @@ function TableInFirstNS.New()
return o
end
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 o = TableInFirstNS.New()
o:Init(buf, n + offset)
diff --git a/tests/namespace_test/NamespaceC/TableInC.lua b/tests/namespace_test/NamespaceC/TableInC.lua
index bb4fef04..71e48427 100644
--- a/tests/namespace_test/NamespaceC/TableInC.lua
+++ b/tests/namespace_test/NamespaceC/TableInC.lua
@@ -13,6 +13,9 @@ function TableInC.New()
return o
end
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 o = TableInC.New()
o:Init(buf, n + offset)