Embedding Lua in C
Extending your tools and apps with Lua scripts
- Introduction
- Installation
- Some Lua Concepts
- Starting Lua VM
- Initial Examples
- Note about the updates in the article
- More examples
- Exposing a Simple Variable
- Exposing a Single Function to Lua
- Returning Values
- lua_register Macro
- Exposing Functions to Lua with Namespace
- Running a Lua Script
- Getting a Global Variable from Lua
- Calling a Lua Function in C
- Calling a Lua Function in C with Arguments and Return Value
- Example of Error Handling
- The End
- Resources
Introduction
Allowing users to extend their apps with scripts is an amazing feature. There are many languages for this purpose, but Lua is the choice today, it is powerful and easy to embed.
Lua is an important tool for many industries, being used inside Game Engines, Databases like Redis, and HTTP servers like Nginx, for powering users to extend their features.
An introduction on how to embed it into a C application will be shown, with simple uses, but complete examples of how to work with Lua. I may not cover all concepts around Lua, installation, and other topics like LuaJIT, but it may serve as a starting point for you.
Another thing that I will not cover is how to compile and link your app with the Lua library, if you know a little about C you can do it with almost no effort using the official manual.
The goal is to take you through examples of common cases of using Lua. You may use it as a configuration script, allowing users to configure the properties of your application as you may wish. It is possible to invoke custom functions written by users to react to certain events of your application, which can be used for customizing logic or returning data from them, or maybe calling another application when yours finishes a task. All those possibilities will be covered here.
Installation
I do not cover the installation of Lua development libraries, because it varies and depends on your platform. You can look at the installation guide on the official website here.
Some Lua Concepts
There are three important data structures in Lua: the State, the Virtual Stack, and the Registry Table.
Lua State
When a new state(lua_State
) is created, a new interpreter thread is started and associated with it. It also has a new registry table, where the global definitions(variables and functions) will be for being used by Lua and C. When calling a Lua API C function, it also stores the virtual stack. It glues everything together and is the main parameter for the API calls.
Registry Table
It is the place where all global data were registered to be available to Lua code in a certain state, it is where Lua keeps the global variables and functions. In order to expose a variable from C to Lua, you need to register a new record inside this table. If you need the get the value from a variable or a reference of a Lua function to be called from C, this is the place where you will find it too.
Virtual Stack
The virtual stack is where the data is temporarily stored between calls, you put data in it when you want to pass data to Lua and get from it when Lua returns data to your code in C.
Lua C API
In order to use almost all C API functions in Lua, you need to put the necessary data into the virtual stack and call the function that you need. It is important you check the manual to know which parameters are required for each function. Some values need to be on the stack before calling the function.
As an example of how the API works take a look at this function:
1 |
|
It is used to define a new global variable in the registry table. It only needs two parameters, the lua_State* L
and the name that will identify the value in Lua. There is no information about the value it will store because the function is independent of the type. We need to push a value of the type we want before calling it. After calling lua_setglobal
, it will pop the value on top of the stack and create a new record in the registry with the name that we want.
This is how almost all functions in the Lua C API work, they are simple calls and usually based on the stack values and less on parameters in the function. It makes the API simple and decoupled from an eventual change in the type system.
Another example: If you want to expose to Lua script a variable called WEBSITE_URL
with the value of http://localhost:8000
you should do this:
1 2 |
|
What happens here is, again, lua_pushstring
pushes a string value into the stack and lua_setglobal
pops the value on top of the stack to register the name WEBSITE_URL
inside the Registry Table.
You are going to see other examples of Lua returning values from the stack. This usually happens as a result of calling a Lua function. The operation is similar, but you have to pop the values from the stack and check if the type of the returned data is expected.
One important point is, whenever Lua calls C, it gets a new stack, which is independent of previous stacks and the stacks of C functions that are still active. Manual 4.2
You are also responsible for the correct use of the stack, ensuring its consistency and using its size correctly.
Starting Lua VM
Creating a new interpreter requires you to create a new state as mentioned before.
The basic code that we need to call when we use Lua C API is as follows. We start declaring a lua_State
pointer and initialize it with the luaL_newstate
function.
Note that versions before 5.3 lua_open() is used instead.
It is our pointer to our Lua Virtual Machine and this lua_State
store all data that we share between Lua and C. We can have many Lua states to run scripts with different contexts.
1 |
|
After opening a state, we are ready to call functions from Lua API.
One important function to be called is luaL_openlibs
, it makes available the Lua Standard Library for the code that we will run afterward. It is not a requirement, but without this, you will not be able to call use libraries like math, io, string, and utf8, inside Lua code.
1 |
|
You can also open only the libraries that you know it will be useful or safe to allow scripts to call, see the example bellow, more information here.
1 2 3 |
|
After using the lua state, when you do not need to execute anything else, you have to close the state with:
1 |
|
Initial Examples
I wrote some examples of basic operations like running Lua code from a string, loading a script file, exposing variables and C functions to Lua code, recovering the values from global variables inside the Lua code and calling Lua function in C.
Initializing Lua VM
Here is our first example, it is a starting point for using Lua with C.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Let's dissect the code structure. We start adding the headers:
1 2 3 |
|
Start a new Lua state:
1 |
|
With L
state you're now able to call Lua C API and interact with Lua VM. Next, open the Lua standard libraries:
1 |
|
And when you application finishes, do not forget to close the state:
1 |
|
Running Lua code
This example shows how to load a Lua code from string and run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
The new things here are the API functions: luaL_loadstring
, which is in charge of loading the code chunk on the top of the stack, lua_pcall
is in charge of running the code in the stack, and if the execution is successful, we remove the code from the top(lua_gettop
) of the stack with lua_pop
.
Note about the updates in the article
Before continuing with the examples I have to explain some updates I made in the article.
After the feedback that I received, I decided to make the following examples more clear and simple using some macros available from Lua API.
The following changes were made:
- Instead of using
lua_pcall
,luaL_loadstring
, andluaL_loadfile
, I modified the code to use:luaL_dostring
, which is equivalent to callinglua_pcall
andluaL_loadstring
luaL_dofile
, which is equivalent to callinglua_pcall
andluaL_loadfile
- use
lua_pcall
only when I do not run the function just after loading a file or a string.
All those macros use LUA_MULRET
, which is an argument that tells lua_pcall
that it must expect a variable number of returned values.
Another point is that when we call lua_pcall
or its variants, a result is returned indicating if the call was successfully executed or not. We can use the constant LUA_OK
to check if there is a success or not.
When an error happens during lua_pcall
, it returns a different value of LUA_OK
and puts the error on the top of the stack. We are able to get this error using lua_tostring(L, lua_gettop(L))
.
I do not use lua_call
because it is a non-protected way to call a function, its use implies that Lua will call a panic function and then call abort. It is possible to handle those errors by setting a new panic function with lua_atpanic
, but it is not covered in this article.
Using the protected call, any error will make Lua call long`jmp, recover from the most recent active recovery point, and set the error on top of the stack.
More examples
Exposing a Simple Variable
It is a really common need to expose some variables for Lua code, and it is simple to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
We use here lua_pushinteger
to put an integer on the top of the stack and after it, we use lua_setglobal
to get this value and set as a global variable named as answer.
After exposing the variable we can use it inside the Lua code.
Note: For more complex types and structures, check Lua manual.
Exposing a Single Function to Lua
This example is a little bit more complex, I added some comments inside the code to explain the more important lines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
|
This example is similar to the exposing variables, but here we push a function pointer instead of an integer value.
Returning Values
An important note here is related to the value returned by the function multiplication, which represents the number of values pushed onto the stack as return values. The function exposed to Lua must follow the definition of the lua_CFunction type. A better explanation is found in the documentation:
In order to communicate properly with Lua, a C function must use the following protocol, which defines the way parameters and results are passed: a C function receives its arguments from Lua in its stack in direct order (the first argument is pushed first). So, when the function starts, lua_gettop(L) returns the number of arguments received by the function. The first argument (if any) is at index 1 and its last argument is at index lua_gettop(L). To return values to Lua, a C function just pushes them onto the stack, in direct order (the first result is pushed first), and returns the number of results. Any other value in the stack below the results will be properly discarded by Lua. Like a Lua function, a C function called Lua can also return many results.
lua_register Macro
It is also possible to use the macro called lua_register
which does the same work as lua_pushcfunction
and lua_setglobal
.
1 |
|
Instead of writing this:
1 2 |
|
Using the macro you just need to write this:
1 |
|
Exposing Functions to Lua with Namespace
We use a table to create a namespace, we put all functions inside this table.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
|
Running a Lua Script
The only difference between running code from a string or file is that we use luaL_loadfile
instead of luaL_loadstring
.
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Getting a Global Variable from Lua
Retrieving values from a Lua script is a good way of configuring your app.
Note that we can only get the variable after running the Lua code.
1 2 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Calling a Lua Function in C
Calling a function defined in a Lua code it is pretty similar of getting a variable, but to execute the function you must check if it is a function on the top of the stack with lua_isfunction
and call it with lua_pcall
. If it is successfully executed you have to remove from the stack with lua_pop(L, lua_gettop(L))
.
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Calling a Lua Function in C with Arguments and Return Value
When you call a function defined in Lua, you can pass argument values and get the return value. To do that you need to put onto the stack the list of arguments after putting the function on the stack. After running the function you can check on the top of the stack a get the return value.
1 2 3 4 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
|
Example of Error Handling
A simple way to handle errors when calling a function:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
The End
There is much more about this topic, you can learn more about Lua in the official manual, LuaJIT is another important thing, as it is a way to use Lua with better performance. If you are interested in game development, check out Love2D, which is an amazing tool. Finally, check my repository with all examples. I hope it will be useful for you.
Thank you!
PS: Thanks for all fixes and improvements sent to me over time. They're welcome!