C Api To Build Lua Interpreter

Syntax

  • lua_State *L = lua_open(); // Create a new VM state; Lua 5.0
  • lua_State *L = luaL_newstate(); // Create a new VM state; Lua 5.1+
  • int luaL_dofile(lua_State *L, const char *filename); // Run a lua script with the given filename using the specified lua_State
  • void luaL_openlibs(lua_State *L); // Load all standard libraries into the specified lua_State
  • void lua_close(lua_State *L); // Close VM state and release any resources inside
  • void lua_call(lua_State *L, int nargs, int nresults); // Call the luavalue at index -(nargs + 1)

Lua is implemented in pure ANSI C and compiles unmodified in all platforms that have an ANSI C compiler. Lua also compiles cleanly as C. Lua is very easy to build and install. There are detailed instructions in the package but here is a simple terminal session that downloads the current release of Lua and builds it in Linux. Those instructions are for embedding Lua into your C or C program. In that case, you need to interact with the C API for Lua to run Lua scripts inside your program. If you want to program in pure Lua, you just need the Lua interpreter lua. Lua scripts are not converted to C before execution. Demonstrate how to embed a lua interpreter in C code, expose a C-defined function to Lua script, evaluate a Lua script, call a C-defined function from Lua, and call a Lua-defined function from C (the host). In this example, we want the mood to be set by a Lua script. Here is mood.lua. The first step is to define the function. All C or C functions that will be called from Lua will be called using a pointer of this type: typedef int (.luaCFunction) (luaState.L); In other words, functions must have a Lua interpreter as the only argument and return only an integer. Since a Lua interpreter is used for the argument, the.

Remarks

Lua as well provides a proper C API to it's Virtual Machine. In contrary to VM itself, C API interface is stack based. So, most of the functions intended to be used with data is either adding some stuff on-top of virtual stack, or removing from it. Also, all the API calls must be used carefully within stack and it's limitations.

In general, anything available on Lua language can be done using it's C API. Also, there is some addition functionality like direct access to internal registry, change behavior of standard memory allocator or garbage collector.

You can compile provided Lua C API examples by executing following on Your terminal:

Creating Lua Virtual Machine

C Api To Build Lua Interpreter

5.1
5.1

Calling Lua functions

In the example above we're doing these things:

Standalone lua interpreter
  • creating and setting up Lua VM as shown on the first example
  • getting and pushing a Lua function from global Lua table onto virtual stack
  • pushing string 'Hello C API' as an input argument onto the virtual stack
  • instructing VM to call a function with one argument which is already on the stack
  • closing and cleaning up

NOTE:

Bare in mind, that lua_call() pops the function and it's arguments from the stack leaving only the result.

Also, it would be safer using Lua protected call - lua_pcall() instead.

Embedded Lua Interpreter with Custom API and Lua Customization

Demonstrate how to embed a lua interpreter in C code, expose a C-defined function to Lua script, evaluate a Lua script, call a C-defined function from Lua, and call a Lua-defined function from C (the host).

In this example, we want the mood to be set by a Lua script. Here is mood.lua:

Notice, mood() is not called in the script. It is just defined for the host application to call. Also notice that the script calls a function called hostgetversion() that is not defined in the script.

Next, we define a host application that uses 'mood.lua'. Here is the 'hostlua.c':

And here is the output:

Even after we have compile 'hostlua.c', we are still free to modify 'mood.lua' to change the output of our program!

Table manipulation

In order to access or alter an index on a table, you need to somehow place the table into the stack.
Let's assume, for this examples that your table is a global variable named tbl.

Getting the content at a particular index:

As we have seen, all you have to do is to push the table into the stack, push the index and call lua_gettable. the -2 argument means that the table is the second element from the top of the stack.
lua_gettable triggers metamethods. Samsung j210f frp unlock umt. If you do not want to trigger metamethods, use lua_rawget instead. It uses the same arguments.

Setting the content at a particular index:

The same drill as getting the content. You have to push the stack, push the index and then push the value into the stack. after that, you call lua_settable. the -3 argument is the position of the table in the stack. To avoid triggering metamethods, use lua_rawset instead of lua_settable. It uses the same arguments.

C Api To Build Lua Interpreter Free

Transferring the content from a table to another:

Now we are putting together all we learned here. I put the stack content on the comments so you do not get lost.

We put both tables into the stack, push the index of table 1 into the stack, and get the value at tbl1.index1. Note the -3 argument on gettable. I am looking at the first table (third from the top) and not the second. Then we push the index of the second table, copy the tbl1.index1 to the top of the stack and then call lua_settable, on the 4th item from the top.

For housecleaning sake, I have purged the top element, so only the two tables remains at the stack.