FlatBuffers
An open source project by FPL.
|
Before diving into the FlatBuffers usage in Lua, it should be noted that the Tutorial page has a complete guide to general FlatBuffers usage in all of the supported languages (including Lua). This page is designed to cover the nuances of FlatBuffers usage, specific to Lua.
You should also have read the Building documentation to build flatc
and should be familiar with Using the schema compiler and Writing a schema.
The code for the FlatBuffers Lua library can be found at flatbuffers/lua
. You can browse the library code on the FlatBuffers GitHub page.
The code to test the Lua library can be found at flatbuffers/tests
. The test code itself is located in luatest.lua.
To run the tests, use the LuaTest.sh shell script.
Note: This script requires Lua 5.3 and LuaJIT to be installed.
Note: See Tutorial for a more in-depth example of how to use FlatBuffers in Lua.
There is support for both reading and writing FlatBuffers in Lua.
To use FlatBuffers in your own code, first generate Lua classes from your schema with the --lua
option to flatc
. Then you can include both FlatBuffers and the generated code to read or write a FlatBuffer.
For example, here is how you would read a FlatBuffer binary file in Lua: First, require the module and the generated code. Then read a FlatBuffer binary file into a string
, which you pass to the GetRootAsMonster
function:
Now you can access values like this:
There currently is no support for parsing text (Schema's and JSON) directly from Lua, though you could use the C++ parser through SWIG or ctypes. Please see the C++ documentation for more on text parsing.