Introduction

From a running C program, you can call a Lua script. The C program can pass arguments to the Lua script, and the Lua script can pass back a return. The Lua script can serve to initialize the C program, or the Lua script can install a callback routine in the C program.

Calling a Lua script from a C program can be daunting without the right information. Different computers have different names and locations for libraries and include files, and it's difficult to figure out what's causing compile/link problems.

Further complicating things is that with every major version of Lua, the method of calling Lua from C changes. Much documentation on the Internet is specific to Lua 5.0, and won't work on 5.1. Likewise, this documentation is specific to 5.1, and when 5.2 comes out (it's alpha right now), the documentation on this page won't work on the new version.

The purpose of this document is to make it easy for you to do simple Lua calls from C programs.

Anatomy of a Lua Call

A typical interaction from a C program to a Lua 5.1 script looks something like this:

  • Create a Lua state variable
  • Load Lua libraries
  • Load but don't run the Lua script file
  • Priming script run to create script's global vars
  • Pass all arguments to the Lua script on the stack
  • Run the loaded Lua script
  • Retrieve the return from the Lua script
  • Close the Lua state variable
Create a Lua state variable     lua_State *L;
L = luaL_newstate();
Load Lua libraries   luaL_openlibs(L);
Load but don't run the Lua script file   luaL_loadfile(L, "script.lua")
Priming run of the loaded Lua script
to create the script's global variables
 
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Pass all arguments to the Lua script on the stack   Various routines, too many to name.
Run the loaded Lua script  
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Retrieve the return from the Lua script   Various pops and stack calls
Close the Lua state variable   lua_close(L);

Header Files

Always put these three header files at the top of any C program calling Lua:

  1. #include <lua.h>
    #include <lauxlib.h>
    #include <lualib.h>

Compiling and Linking

Nothing is tougher than compiling and linking your first C to Lua program. There are way too many variables, it seems like everything's a black box, and you run around changing things trial and error, crossing your fingers.

But there's a method to the madness, and this subsection explains it. If you know all the content of this section, try not to be offended or insulted -- the extra info is for those who don't know it yet.

Here's a typical command to compile it:

  1. cc -Wall -o hello hello.c -I/path/to/Lua/includes -L/path/to/Lua/libraries -llibLuaName -lm

Let's break that down:

  1. cc
   Your systems C compilation command. Could be cc, or gcc, or something else.
  1. -Wall
  Show all warnings. You might choose not to insert this until all other errors and warnings have been fixed.
  1. -o hello
  The name of the finished executable.
  1. hello.c
  The name of the C file to compile.
  1. -I/path/to/Lua/includes
  Every C compiler checks for include files in certain places. But often  lua.h, liblua.h and lauxlua.h aren't in those places, so you need to tell the compiler where to find them.
  1. -L/path/to/Lua/libraries
  Every C compiler checks for libraries to link in certain places. But often the Lua libraries are elsewhere, so you need to tell the compiler where to find them.
  1. -llibLuaName
  Link in the Lua library. The way you name this library in the compiler's -l option requires some thought and research, which will be explained later in this subsection.
  1. -lm
  Link in the math library (often not necessary)

The Process of Finding the Right Compile Command

Finding the right compile command is a process that looks something like this:

  1. Try to compile, no link, no -Wall, no -I. You might get lucky.
  2. Find the location of the Lua include files.
  3. Using that location, compile, no link, no -Wall.
  4. Compile, no link, use -Wall.
  5. Find the location and filenames of the Lua library files.
  6. Using the location and filenames, compile and link.
  7. Test the Executable.

Step 1: Try to compile, no link, no -Wall, no -I

Do this:

  1. cc -o hello hello.c -c

Notice that the -c means "compile only, do not link". Sometimes it's not all that clear whether an error or warning comes from compile or link, but it's very clear if you only compile. If the preceding command doesn't error out, skip to step 4. However, in my case it errored out:

  1. slitt@mydesk:~$ cc -o hello hello.c -c
    hello.c:8:91: error: lua.h: No such file or directory
    hello.c:9:91: error: lauxlib.h: No such file or directory
    hello.c:10:85: error: lualib.h: No such file or directory
    hello.c:16: error: expected ‘)’ before ‘*’ token
    hello.c: In function main’:
    hello.c:24: error: lua_State undeclared (first use in this function)
    hello.c:24: error: (Each undeclared identifier is reported only once
    hello.c:24: error: for each function it appears in.)
    hello.c:24: error: L undeclared (first use in this function)
    slitt@mydesk:~$

On to step 2...

Step 2: Find the location of the Lua include files

The three Ubiquitous Lua include files are lua.h, lauxlib.h, and lualib.h. Use your Linux box's locate command to find lua.h after running updatedb to make sure that locate finds everything. Make sure that the directory with lua.h also contains lauxlib.h, and lualib.h.

  1. slitt@mydesk:~$ sudo updatedb
    [sudo] password for slitt:
    slitt@mydesk:~$ locate /lua.h
    /usr/include/lua5.1/lua.h
    /usr/include/lua5.1/lua.hpp
    /usr/share/doc/lua5.1-doc/doc/lua.html
    /usr/share/doc/lua5.1-doc/etc/lua.hpp
    slitt@mydesk:~$

Obviously the file you want is in /usr/include/lua5.1. Now see if that directory contains the other two:

  1. slitt@mydesk:~$ ls /usr/include/lua5.1/lualib.h
    /usr/include/lua5.1/lualib.h
    slitt@mydesk:~$ ls /usr/include/lua5.1/lauxlib.h
    /usr/include/lua5.1/lauxlib.h
    slitt@mydesk:~$

OK, that's it. You know you need to add -I /usr/include/lua5.1 to the command.

Step 3: Using that location, compile, no link, no -Wall

  1. cc -o hello hello.c -I/usr/include/lua5.1 -c

In my case the preceding worked:

  1. slitt@mydesk:~$ cc -o hello hello.c -I/usr/include/lua5.1 -c
    slitt@mydesk:~$

If it hadn't worked, and you'd included all three header files in hello.c, and you're sure you got the additional include directory right, suspect wrong code in hello.c itself. If it doesn't produce errors or warnings, go on to the next step...

Step 4: Compile, no link, use -Wall

  1. cc -o hello hello.c -Wall -I/usr/include/lua5.1 -c

In my case, the preceding produced no errors or warnings:

  1. slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -c
    slitt@mydesk:~$

If it produces warnings, try your best to fix them. Warnings are often a sign of problems that rear their ugly head a week after deployment.

Once you have no errors or warnings, go on to the next step...

Step 5: Find the location and filenames of the Lua library files

First a word about how the -l compiler option works. The -l option assumes every library file starts with "lib". Therefore, instead of making you type -lliblua, it makes you type -llua and then out of the goodness of its heart prepends lib before the lua. tacks that on to the front of what you put as the -l option. For instance, if you put -llua, that means that it searches for a file called liblua.a or liblua.o or liblua.so, etc. So you need to find the liblua.* files. Do it as follows:

  1. sudo updatedb
    locate liblua | less

Here's the partial result:

  1. /usr/lib/liblua5.1-sql-postgres.a
    /usr/lib/liblua5.1-sql-postgres.la
    /usr/lib/liblua5.1-sql-postgres.so
    /usr/lib/liblua5.1-sql-postgres.so.2
    /usr/lib/liblua5.1-sql-postgres.so.2.0.0
    /usr/lib/liblua5.1-sql-sqlite3.so.2
    /usr/lib/liblua5.1-sql-sqlite3.so.2.0.0
    /usr/lib/liblua5.1-unix.so.2
    /usr/lib/liblua5.1-unix.so.2.0.0
    /usr/lib/liblua5.1.a
    /usr/lib/liblua5.1.la
    /usr/lib/liblua5.1.so
    /usr/lib/liblua5.1.so.0
    /usr/lib/liblua5.1.so.0.0.0
    /usr/lib/liblua50.so.5
    /usr/lib/liblua50.so.5.0
    /usr/lib/liblualib50.so.5
    /usr/lib/liblualib50.so.5.0
    /usr/lib/debug/usr/lib/liblua5.1.so.0.0.0

See the ones called liblua5.1.*? Those are the Lua libraries on my computer.  So we have a pretty good idea that the -l argument should be lua5.1. We also know that they're located in /usr/lib, which probably is already on the C compiler's library path, so we probably don't need a -L compiler option. If these files had been in /usr/lib/lua, we'd have needed an -L option.

Step 6: Using the location and filenames, compile and link

This step differs from step 4 in two ways:

  1. We remove the -c so that it will attempt to link
  2. We add -llua5.1

Here we go...

  1. slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -llua5.1
    slitt@mydesk:~$

It worked. If it hadn't, we'd have been forced to troubleshoot.

Step 7: Test the Executable

So let's run it:

  1. slitt@mydesk:~$ ./hello
    In C, calling Lua
    This is coming from lua.
    Back in C again
    slitt@mydesk:~$

You haven't seen the code for hello.c and helloscript.lua yet, but take it from me that the preceding output was exactly what it was supposed to do.

Summary

Here's how you build a C program that runs a Lua script:

Create a Lua state variable     lua_State *L;
L = luaL_newstate();
Load Lua libraries   luaL_openlibs(L);
Load but don't run the Lua script file   luaL_loadfile(L, "script.lua")
Priming run of the loaded Lua script
to create the script's global variables
 
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Pass all arguments to the Lua script on the stack   Various routines, too many to name.
Run the loaded Lua script  
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Retrieve the return from the Lua script   Various pops and stack calls
Close the Lua state variable   lua_close(L);

You always include  lua.h, lauxlib.h and lualib.h. You use the Linux locate command to find where those files are, and use that location in a -I compiler option. You always link with liblua, and you use locate commands to find where that library is, and use it in the -l compiler option and if its location isn't in a normal library directory, put its directory in a -L compiler option.

Getting Lua-calling C programs to compile and link can be difficult and frustrating. To limit the difficulty and frustration, use this process:

  1. Try to compile, no link, no -Wall, no -I. You might get lucky.
  2. Find the location of the Lua include files.
  3. Using that location, compile, no link, no -Wall.
  4. Compile, no link, use -Wall.
  5. Find the location and filenames of the Lua library files.
  6. Using the location and filenames, compile and link.
  7. Test the Executable.

Now it's time for your Hello World program.

Hello World

Let's start with the one line Lua script itself. Call it helloscript.lua:

  1. io.write("This is coming from lua.\n")

That's it, one line. Notice it doesn't have the #!/usr/bin/lua line on top. It doesn't have to because the C Lua interface has a built-in Lua interpreter which the lua_pcall() function invokes automatically.

So if you run the C program and see the text "This is coming from lua." you know you've succeded.

Next let's review the steps covered in the Anatomy of a Lua Call article:

Create a Lua state variable     lua_State *L;
L = luaL_newstate();
Load Lua libraries   luaL_openlibs(L);
Load but don't run the Lua script file   luaL_loadfile(L, "script.lua")
Priming run of the loaded Lua script
to create the script's global variables
 
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Pass all arguments to the Lua script on the stack   Various routines, too many to name.
Run the loaded Lua script  
  1. lua_pcall(
    L,
    number_of_args,
    number_of_returns,
    errfunc_idx
    );
Retrieve the return from the Lua script   Various pops and stack calls
Close the Lua state variable   lua_close(L);

Your Hello World program will implement all the above, in that same order, EXCEPT:

  • Priming run of the loaded Lua script
  • Passing arguments to the Lua script
  • Retrieving the return from the Lua script

Also, we'll put in an error handler function called bail() just to make the code look more concise. We'll call this program hello.c. Here's the code:

  1. #include <lua.h> /* Always include this when calling Lua */
    #include <lauxlib.h> /* Always include this when calling Lua */
    #include <lualib.h> /* Always include this when calling Lua */
  2.  
  3. #include <stdlib.h> /* For function exit() */
    #include <stdio.h> /* For input/output */
  4.  
  5. void bail(lua_State *L, char *msg){
    fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
    msg, lua_tostring(L, -1));
    exit(1);
    }
  6.  
  7. int main(void)
    {
    lua_State *L;
  8.  
  9. L = luaL_newstate(); /* Create Lua state variable */
    luaL_openlibs(L); /* Load Lua libraries */
  10.  
  11. if (luaL_loadfile(L, "helloscript.lua")) /* Load but don't run the Lua script */
    bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
  12.  
  13. printf("In C, calling Lua\n");
  14.  
  15. if (lua_pcall(L, 0, 0, 0)) /* Run the loaded Lua script */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  16.  
  17. printf("Back in C again\n");
  18.  
  19. lua_close(L); /* Clean up, free the Lua state var */
  20.  
  21. return 0;
    }

As you can see, it's pretty much the same as discussed in the list of steps, except no args are passed into Lua and no return variables are passed out.

Remembering the discussion from the Anatomy of a Lua Call article, compile and link it, troubleshooting as necessary. Here's how it compiles and links on my computer:

  1. slitt@mydesk:~$ cc -o hello hello.c -Wall -I/usr/include/lua5.1 -llua5.1
    slitt@mydesk:~$

Do what's needed til it compiles no errors, no warnings, with the -Wall compiler option. Then run it. It should look like this:

  1. slitt@mydesk:~$ ./hello
    In C, calling Lua
    This is coming from lua.
    Back in C again
    slitt@mydesk:~$

Study the program. Everything should be pretty obvious except lua_pcall(), and that will be discussed later. Once you've gotten output like the preceding and really understand what's going on (with the possible exception oflua_pcall() details), it's time to continue...

Calling Subroutines, Sending Arguments and Receiving Returns

Remember the program from the Hello World article did everything except these:

  • Priming run of the loaded Lua script
  • Passing arguments to the Lua script
  • Retrieving the return from the Lua script

In this article we'll do all these things. By the time you're done constructing the code in this article, you'll have passed arguments,  from Lua to a C subroutine, and have received back return values.

Tourist Trap Alert

All over the Internet, including on some of the Lua project's own documentation, you'll see hints basically telling you that to call a C subroutine you do:

lua_getglobals(L, "subroutine_name")
push_args_to_subroutine
if(lua_pcall(L, num_of_args, num_of_returns, 0)
   error_routine();

These Internet instructions say nothing about doing a priming lua_pcall(), and if you do not do a priming lua_pcall(), you'll get an error message something like this:

attempt to call a nil value

If you get the preceding error message and don't know it's caused by lack of a priming lua_pcall() (or priming lua_call() or priming dofile()), you're about to have several hours or maybe even days of frustration. Mailing list threads old and new about this error basically tell you RTFM or RTFW, but of course we locals know it was reading the web that got you into this problem in the first place.

I saw one guy who took the opportunity to tell the asker "I'm trying to help you help yourself but you won't take the help, so now I'm putting you on my list of people I won't help." All for what we locals know was probably a simple lack of a priming run THAT SEEMS NOT TO BE DOCUMENTED ANYWHERE!

Before calling a function in a Lua script, do that priming run first!!!!!!

OK, let's start by calling a specific function in a Lua script. Create this callfuncscript.lua file:

  1. function tellme()
    io.write("This is coming from lua.tellme.\n")
    end

Now, noting that most of it is from the Hello World article you've already read, create the following callfunc.c:

  1. #include <lua.h> /* Always include this when calling Lua */
    #include <lauxlib.h> /* Always include this when calling Lua */
    #include <lualib.h> /* Prototype for luaL_openlibs(), */
    /* always include this when calling Lua */
  2.  
  3. #include <stdlib.h> /* For function exit() */
    #include <stdio.h> /* For input/output */
  4.  
  5. void bail(lua_State *L, char *msg){
    fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
    msg, lua_tostring(L, -1));
    exit(1);
    }
  6.  
  7. int main(void)
    {
    lua_State *L;
  8.  
  9. L = luaL_newstate(); /* Create Lua state variable */
    luaL_openlibs(L); /* Load Lua libraries */
  10.  
  11. if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
    bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
  12.  
  13. /* ABOVE HERE IS HELLO WORLD CODE */
  14.  
  15. if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  16.  
  17. lua_getglobal(L, "tellme"); /* Tell what function to run */
  18.  
  19. /* BELOW HERE IS THE HELLO WORLD CODE */
    printf("In C, calling Lua\n");
    if (lua_pcall(L, 0, 0, 0)) /* Run the function */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
    printf("Back in C again\n");
  20.  
  21. lua_close(L); /* Clean up, free the Lua state var */
  22.  
  23. return 0;
    }

The only change over the Hello World code is a priming run and pushing the tellme() Lua function via the lua_getglobal() call. Note that the second lua_pcall() still has its second arg, number_of_args, of 0, because you're not sending an argument to the Lua function tellme().

Arguments and Return Value

The previous part of this article was written primarily to make sure you knew that to call a specific Lua function, you need to push that function on the stack with lua_getglobal(), and that you make sure to do a priming run, because without it you're in for hours of frustrating debugging and detour ridden RTFW.

Litt Opinion

There are several pieces of example code on the net showing the passing of arguments to entire Lua scripts rather than functions, and those whole Lua scripts returning arguments. They do this by, within C, setting a global variable to be used in Lua. I've chosen not to reproduce these techniques here because:

  1. In my opinion the internals of such code is a little hard to understand
  2. I never liked global variables in the first place.
  3. Multiple args to the main Lua script require multiple global variables such code becomes quite muddled
  4. Functions are built from the ground up to accept a series of arguments.

Now take your callfuncscript.lua file and add a function called square, so the whole script looks like this:

  1. function tellme()
    io.write("This is coming from lua.tellme.\n")
    end
  2.  
  3. function square(n)
    io.write("Within callfuncscript.lua fcn square, arg=")
    io.write(tostring(n))
    n = n * n
    io.write(", square=")
    io.write(tostring(n))
    print(".")
    return(n)
    end
  4.  
  5. print("Priming run")

And change callfunc.c to look like the following:

  1. #include <lua.h> /* Always include this when calling Lua */
    #include <lauxlib.h> /* Always include this when calling Lua */
    #include <lualib.h> /* Prototype for luaL_openlibs(), */
    /* always include this when calling Lua */
  2.  
  3. #include <stdlib.h> /* For function exit() */
    #include <stdio.h> /* For input/output */
  4.  
  5. void bail(lua_State *L, char *msg){
    fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
    msg, lua_tostring(L, -1));
    exit(1);
    }
  6.  
  7. int main(void)
    {
    lua_State *L;
  8.  
  9. L = luaL_newstate(); /* Create Lua state variable */
    luaL_openlibs(L); /* Load Lua libraries */
  10.  
  11. if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
    bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
  12.  
  13. if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  14.  
  15. printf("In C, calling Lua->tellme()\n");
  16.  
  17. lua_getglobal(L, "tellme"); /* Tell it to run callfuncscript.lua->tellme() */
    if (lua_pcall(L, 0, 0, 0)) /* Run the function */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  18.  
  19. printf("Back in C again\n");
    printf("In C, calling Lua->square(6)\n");
  20.  
  21. lua_getglobal(L, "square"); /* Tell it to run callfuncscript.lua->square() */
    lua_pushnumber(L, 6); /* Submit 6 as the argument to square() */
    if (lua_pcall(L, 1, 1, 0)) /* Run function, !!! NRETURN=1 !!! */
    bail(L, "lua_pcall() failed");
  22.  
  23. printf("Back in C again\n");
    int mynumber = lua_tonumber(L, -1);
    printf("Returned number=%d\n", mynumber);
  24.  
  25. lua_close(L); /* Clean up, free the Lua state var */
    return 0;
    }

Be sure to notice the following things in the preceding C code:

  • Even though two functions are called, only one priming run is needed. Each Lua script requires only one priming run in order to create its globals so its functions are visible to the C program.
  • In order to fully pass an integer into callfuncscript.lua's square() function and receive the function's return, the lua_pcall()'s nargs argument must be 1 (one argument passed in) and its nreturns argument must be 1 (1 argument passed back). If nargs is 0 you'll get the dreaded " attempt to call a number value" error, while if nreturns is 0 it will always return 0. So remember, this lua_pcall() is lua_pcall(L, 1, 1, 0).

Passing Tables to Lua Functions

A use case that happens often is the passing of tables to and from Lua functions. How is that handled? There are a few idioms you see over and over again to make it happen. Before discussing the idioms, here's the code:

The Lua Program

Here's the Lua program. As you can see, function tweaktable receives a table argument, converts all values to upper case, counts the values, and puts all that info in a new table that's returned.

  1. function tellme()
    io.write("This is coming from lua.tellme.\n")
    end
  2.  
  3. function square(n)
    io.write("Within callfuncscript.lua fcn square, arg=")
    io.write(tostring(n))
    n = n * n
    io.write(", square=")
    io.write(tostring(n))
    print(".")
    return(n)
    end
  4.  
  5. function tweaktable(tab_in)
    local tab_out = {numfields=1}
    for k,v in pairs(tab_in) do
    tab_out.numfields = tab_out.numfields + 1
    tab_out[tostring(k)] = string.upper(tostring(v))
    end
    tab_out.numfields = tostring(tab_out.numfields)
    io.write("At bottom of callfuncscript.lua tweaktable(), numfields=")
    io.write(tab_out.numfields)
    print()
    return tab_out
    end
  6.  
  7. print("Priming run")

The C Program

The C program is the same as all the others except stacking arguments to Lua is a little different, and recovering the table passed back from Lua is a little different. The Lua tweaktable() function takes a table as its one and only argument and passes back one table. It passes back a completely different table so there's absolutely no question of the changes being made by reference to the args rather than passback.

Start by looking and running the code, and then we'll discuss some of the idioms that make it work...

  1. #include <lua.h> /* Always include this when calling Lua */
    #include <lauxlib.h> /* Always include this when calling Lua */
    #include <lualib.h> /* Prototype for luaL_openlibs(), */
    /* always include this when calling Lua */
  2.  
  3. #include <stdlib.h> /* For function exit() */
    #include <stdio.h> /* For input/output */
  4.  
  5. void bail(lua_State *L, char *msg){
    fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
    msg, lua_tostring(L, -1));
    exit(1);
    }
  6.  
  7. int main(void)
    {
    lua_State *L;
  8.  
  9. L = luaL_newstate(); /* Create Lua state variable */
    luaL_openlibs(L); /* Load Lua libraries */
  10.  
  11. if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
    bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
  12.  
  13. if (lua_pcall(L, 0, 0, 0)) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
    bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  14.  
  15. printf("In C, calling Lua->tweaktable()\n");
    lua_getglobal(L, "tweaktable"); /* Tell it to run callfuncscript.lua->square() */
    lua_newtable(L); /* Push empty table onto stack table now at -1 */
    lua_pushliteral(L, "fname"); /* Push a key onto the stack, table now at -2 */
    lua_pushliteral(L, "Margie"); /* Push a value onto the stack, table now at -3 */
    lua_settable(L, -3); /* Take key and value, put into table at -3, */
    /* then pop key and value so table again at -1 */
  16.  
  17. lua_pushliteral(L, "lname"); /* Push a key onto the stack, table now at -2 */
    lua_pushliteral(L, "Martinez"); /* Push a value onto the stack, table now at -3 */
    lua_settable(L, -3); /* Take key and value, put into table at -3, */
    /* then pop key and value so table again at -1 */
  18.  
  19. if (lua_pcall(L, 1, 1, 0)) /* Run function, !!! NRETURN=1 !!! */
    bail(L, "lua_pcall() failed");
  20.  
  21. printf("============ Back in C again, Iterating thru returned table ============\n");
  22.  
  23. /* table is in the stack at index 't' */
    lua_pushnil(L); /* Make sure lua_next starts at beginning */
    const char *k, *v;
    while (lua_next(L, -2)) { /* TABLE LOCATED AT -2 IN STACK */
    v = lua_tostring(L, -1); /* Value at stacktop */
    lua_pop(L,1); /* Remove value */
    k = lua_tostring(L, -1); /* Read key at stacktop, */
    /* leave in place to guide next lua_next() */
    printf("Fromc k=>%s<, v=>%s<\n", k, v);
    }
  24.  
  25. lua_close(L); /* Clean up, free the Lua state var */
    return 0;
    }

The preceding code yields the following output:

  1. slitt@mydesk:~$ ./callfunc
    Priming run
    In C, calling Lua->tweaktable()
    At bottom of callfuncscript.lua tweaktable(), numfields=3
    ============ Back in C again, Iterating thru returned table ============
    Fromc k=>fname<, v=>MARGIE<
    Fromc k=>lname<, v=>MARTINEZ<
    Fromc k=>numfields<, v=>3<
    slitt@mydesk:~$

Table Argument Passing Idioms

You start by placing an empty table on the stack, and build it it one key/value pair at a time. The way you place an empty table on the stack is with the following command:

  1. lua_newtable(L);

The way you give the now stacked table a key/value pair is like this:

  1. /* Table at stack top, index -1 right now. */
    lua_pushliteral(L, "fname"); /* Push a key onto the stack, table ends up at -2 */
    lua_pushliteral(L, "Margie"); /* Push a value onto the stack, table ends up at -3 */
    lua_settable(L, -3); /* Incorporate key and value, into the table */
    /* then pop twice so table again at -1 */
    /* repeat as necessary */

You can do that over and over again with different key/value pairs. Each time the numeric argument to lua_settable() is -3 because lua_settable pops the two stack items above it after incorporating them in its table, so at its end it's at the same level that it was before you stacked the key and value.

Note that you might be stacking things other than literals. Here are the many pushers available:

  • lua_pushboolean
  • lua_pushcclosure
  • lua_pushcfunction
  • lua_pushfstring
  • lua_pushinteger
  • lua_pushlightuserdata
  • lua_pushliteral
  • lua_pushlstring
  • lua_pushnil
  • lua_pushnumber
  • lua_pushstring
  • lua_pushthread
  • lua_pushvalue
  • lua_pushvfstring

Other functions push or at least put things on the stack, including:

  1. lua_getglobal(L, fcnname)
In spite of what it sounds like, this function actually puts  the function corresponding to the fcnname argument on the top of the stack.
  1. lua_newtable(L)
This function puts an empty table at the top of the stack.
  1. lua_next(L, -level)
This function replaces the previous key with the new one, and then pushes the new value on top of that. You need to do a pop to stay even.

And here are some poppers:

  1. lua_pop(L,number)
This pops off the stack, number levels.
  1. lua_settable(L, -level)
This pops twice after assigning the key/value pair in the top two stack positions to the table at level -level.

Table Return Passing Idioms

When a Lua function called from C returns ends it puts its return values on the stack. When a return value is a table, retrieving the table's elements isn't obvious. Assuming the Lua function returned only one thing and that thing is a table, then that table is at the stack's top when it comes back to C. Here's how you access it:

  1. lua_pushnil(L); /* Make sure lua_next starts at beginning */
    const char *k, *v;
    while (lua_next(L, -2)) { /* TABLE LOCATED AT -2 IN STACK */
    v = lua_tostring(L, -1); /* Value at stacktop */
    lua_pop(L,1); /* Remove value */
    k = lua_tostring(L, -1); /* Read key at stacktop, */
    /* leave in place to guide next lua_next() */
    /* Do what you need to with k and v */
    }

In the preceding, you push a nil in order to make sure lua_next() starts with the first item. The reason you use -2 as an arg to lua_next() is because after you push the nil, the returned table is the second item down on the stack. On each iteration, lua_next() replaces the existing key with the next one, and then pushes the next value on top of it. Therefore to keep things in place you need one pop per cycle.

On every iteration immediately after lua_next(), the top of the stack is the new value, one down from that is the new key, and one down from that is the table.

Calling Lua From a C Program的更多相关文章

  1. C调用Lua中的函数解析table

    Passing Tables to Lua Functions A use case that happens often is the passing of tables to and from L ...

  2. 解释型语言和编译型语言如何交互?以lua和c为例

    转自http://my.oschina.net/mayqlzu/blog/113528 问题: 最近lua很火,因为<愤怒的小鸟>使用了lua,ios上有lua解释器?它是怎么嵌入大ios ...

  3. Lua的require和module小结

    Lua的require和module小结  module特性是lua5.1中新增的,用于设置Lua文件自己的模块,最常用的方式是module(name,package.seeall),有时候lua文件 ...

  4. (原)lua提示cannot load incompatible bytecode

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5754872.html 前段时间用终端和zerobrane运行torch的lua程序.zerobrane ...

  5. 正确lua简单的扩展,可以加速相关C++数据。

    很早的时候,我有一件事纠结.如果,我在这里C++打开界面脚本.使用C++其中一个目标,和.我的程序有很多不同的lua虚拟机.每个虚拟机与一个关联C++对象,它是多线程,那么如何快速应利用这个好时机lu ...

  6. Sublime Text3配置Lua运行环境

    Sublime Text3配置Lua运行环境 前言 要问现在哪个编译器最能扛得住潮流,要数Sublime Text3了,由于它的轻量,插件丰富,美观,造就了一大批粉丝(本菜鸡也是哦) 在以前的工作中使 ...

  7. C/C++和Lua是如何进行通信的?

    为了实现Lua和其他语言之间的通信,Lua虚拟机为C/C++提供了两个特性: Lua_State状态机 lua_State主要是管理一个lua虚拟机的执行环境, 一个lua虚拟机可以有多个执行环境.L ...

  8. 控制台游戏引擎CGE——贪吃蛇

    今天我也来发一个控制台游戏.先看图: 缘起 LZ是一个有严重拖延症的人,表现的形式就是隔一段时间就要刷一刷博客园. 这不前几天,看到了魏大师<使用Lua脚本语言开发出高扩展性的系统...> ...

  9. 《C#图解教程》读书笔记之六:接口和转换

    本篇已收录至<C#图解教程>读书笔记目录贴,点击访问该目录可获取更多内容. 一.接口那点事儿 (1)什么是接口? 一组函数成员而未实现的引用类型.只有类和结构能实现接口. (2)从ICom ...

随机推荐

  1. js 函数的调用模式

    1.函数调用 调用一个函数将暂停当前函数的执行,传递控制权和参数给新函数.除了函数声明时定义的形参,每个函数还接受两个附加的参数:this和arguments(arguments并不是一个真正的数组, ...

  2. HashMap 读后感

    HashMap是一个哈希表,内部通过链地址法解决哈希冲突.内部用Entry数组保存数据,每个Entry是一个单向链表. HashMap不保证插入的顺序,线程不同步,允许null 下面是几个重要点: 保 ...

  3. struts2中访问servlet API

    Struts2中的Action没有与任何Servlet API耦合,,但对于WEB应用的控制器而言,不访问Servlet API几乎是不可能的,例如需要跟踪HTTP Session状态等.Struts ...

  4. 从源码看java中Integer的缓存问题

    在开始详细的说明问题之前,我们先看一段代码 public static void compare1(){ Integer i1 = 127, i2 = 127, i3 = 128, i4 = 128; ...

  5. 简单版解决IE兼容性问题

    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" /> 在访问页面时, 如 ...

  6. C#主要支持 5 种动态创建对象的方式

    C#主要支持 5 种动态创建对象的方式: 1. Type.InvokeMember 2. ContructorInfo.Invoke 3. Activator.CreateInstance(Type) ...

  7. 【Qt】Qt之自定义界面(窗体缩放-跨平台终极版)【转】

    简述 通过上一节内容,我们实现了窗体的缩放,功能很不错,但是很遗憾-不支持跨平台!如果对于多平台来说,这是一个硬伤,所以,我们急需要一个能够支持跨平台的实现方案. 在网上看到过很多不同的实现方式,多多 ...

  8. 封装底层Ajax

    创建Ajax简易步骤:创建Ajax对象:var xhr=new XMLHttpRequest();链接服务器:xhr.open('get','a.php',true);发送请求或数据:xhr.send ...

  9. 1103. Integer Factorization (30)

    The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  10. 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表中的标识列指定显式值

    今天在处理数据时遇到这样一个错误 消息 8101,级别 16,状态 1,第 1 行 仅当使用了列列表并且 IDENTITY_INSERT 为 ON 时,才能为表'dbo.StockDetailValu ...