summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDerek Bailey <derekbailey@google.com>2021-05-05 16:36:49 -0700
committerGitHub <noreply@github.com>2021-05-05 16:36:49 -0700
commit82aed82b8462ec1f083d9471edef2868692ded6a (patch)
treeb463052dc299c50d9d202e838c7f85242666a2ff
parent60ff76630d590478f12246ab6c1db50232272437 (diff)
downloadflatbuffers-82aed82b8462ec1f083d9471edef2868692ded6a.tar.gz
flatbuffers-82aed82b8462ec1f083d9471edef2868692ded6a.tar.bz2
flatbuffers-82aed82b8462ec1f083d9471edef2868692ded6a.zip
Added support for Lua 5.1, 5.2 and 5.4 (#6606)
This adds basic support for different Lua versions. For Lua 5.2 and Lua 5.3, both the Bit32 and Compat53 Lua modules must be installed for it to work. You can typically get these on Linux using apt install lua-compat53 lua-bit32 For Lua 5.4, it should work as is, as it is a clean superset of Lua 5.3, which is what the original Lua Flatbuffers supported.
-rw-r--r--lua/flatbuffers/compat.lua27
-rw-r--r--lua/flatbuffers/compat_5_1.lua21
-rwxr-xr-xtests/LuaTest.sh2
-rw-r--r--tests/luatest.lua2
4 files changed, 38 insertions, 14 deletions
diff --git a/lua/flatbuffers/compat.lua b/lua/flatbuffers/compat.lua
index fbce4578..1296c33f 100644
--- a/lua/flatbuffers/compat.lua
+++ b/lua/flatbuffers/compat.lua
@@ -1,12 +1,15 @@
-local m
-if _VERSION == "Lua 5.3" then
- m = require("flatbuffers.compat_5_3")
-else
- local ok = pcall(require, "jit")
- if not ok then
- error("Only Lua 5.3 or LuaJIT is supported")
- else
- m = require("flatbuffers.compat_luajit")
- end
-end
-return m \ No newline at end of file
+local compats = {
+ ["Lua 5.1"] = function()
+ -- Check if Lua JIT is installed first
+ local ok = pcall(require, "jit")
+ if not ok then
+ return require("flatbuffers.compat_5_1")
+ else
+ return require("flatbuffers.compat_luajit")
+ end
+ end,
+ ["Lua 5.2"] = function() return require("flatbuffers.compat_5_1") end,
+ ["Lua 5.3"] = function() return require("flatbuffers.compat_5_3") end,
+ ["Lua 5.4"] = function() return require("flatbuffers.compat_5_3") end,
+}
+return assert(compats[_VERSION], "Unsupported Lua Version: ".._VERSION)() \ No newline at end of file
diff --git a/lua/flatbuffers/compat_5_1.lua b/lua/flatbuffers/compat_5_1.lua
new file mode 100644
index 00000000..8c5e4321
--- /dev/null
+++ b/lua/flatbuffers/compat_5_1.lua
@@ -0,0 +1,21 @@
+local m = {}
+local ok, bit = pcall(require, "bit32")
+assert(ok, "The Bit32 library must be installed")
+assert(pcall(require, "compat53"), "The Compat 5.3 library must be installed")
+
+m.GetAlignSize = function(k, size)
+ return bit.band(bit.bnot(k) + 1,(size - 1))
+end
+
+if not table.unpack then
+ table.unpack = unpack
+end
+
+if not table.pack then
+ table.pack = pack
+end
+
+m.string_pack = string.pack
+m.string_unpack = string.unpack
+
+return m
diff --git a/tests/LuaTest.sh b/tests/LuaTest.sh
index b6f17b54..d736a6b1 100755
--- a/tests/LuaTest.sh
+++ b/tests/LuaTest.sh
@@ -19,7 +19,7 @@ test_dir="$(pwd)"
${test_dir}/../flatc --lua -I include_test monster_test.fbs
-declare -a versions=(luajit lua5.3)
+declare -a versions=(luajit lua5.1 lua5.2 lua5.3 lua5.4)
for i in "${versions[@]}"
do
diff --git a/tests/luatest.lua b/tests/luatest.lua
index 952c4fae..7380d7d2 100644
--- a/tests/luatest.lua
+++ b/tests/luatest.lua
@@ -370,4 +370,4 @@ if not result then
print("Unable to run tests due to test framework error: ",err)
end
-os.exit(result or -1)
+os.exit(result and 0 or -1)