
In
a previous article, I showed you a simple example of how you can call a function that's written in C from Lua, and a second example that extended that - passing simple parameters in to the C function, and returning results.
But it's often more complex that that - for example, you may with to pass a table from your Lua script into a C function, so that the C function can make use of a value from the table code like this:
stuff = {hotel = 48, hq = 404, town = 1}
summat = extratasks.dothis(stuff)
This isn't as easy as just passing a value, since the size of a table can vary, and Lua's dynamic memory allocation doesn't sit well, in a simple interface, with C's static model. The "trick" is to tackle it within the C code by using function calls to get (and if necessary) reset values from the table - with the data required by those function calls being processed via the Lua stack.
Here's an example of what I mean in the C code:
/* Looking up based on the key */
/* Add key we're interested in to the stack*/
lua_pushstring(L,"hq");
/* Have Lua functions look up the keyed value */
lua_gettable(L, -2 );
/* Extract the result which is on the stack */
double workon = lua_tonumber(L,-1);
/* Tidy up the stack - get rid of the extra we added*/
lua_pop(L,1);
That code retrieves the "hq" value from the table that's been passed in (called
stuff in my Lua example above) and stores it into a C variable called
workon ... objective achieved!
The full source code of the Lua is available
here and the C source code is
here.
Here's sample output from my testing of the code:
[trainee@easterton three]$ lua wrapper.lua
Lua code running ...
Violets are Blue
Table passed to humbug
Roses are Red
... back into Lua code
Values in Lua now 527 -511
Table contains ...
hq 404
town 1
hotel 48
[trainee@easterton three]$
Looking around, I have found a number of very much more complex examples of how to embed C in Lua, but nothing as easy as the 1 - 2 - 3 steps I have shown you in this article and the previous one. If you're looking to pass strings (strings in Lua are not null terminated, so differ), to iterate through a Lua table within C, or to modify a table, you'll find that covered in Lua's reference documentation. If you would like to learn more about Lua - from the basics through to the topics covered in this article - and you feel I could help you, please have a look at
our public Lua Courses or ask about a private course if you're looking at more specialised needs, or if you have a group of delegates.
Illustration - Delegates on a private Lua course. Although we'll run private courses for larger groups, we limit public courses to just eight delegates so that we can provide good coverage and support for the wide variety of applications to which Lua is put (written 2008-10-18, updated 2010-06-19)
Associated topics are indexed under
C212 - C and C based languages - Memory Management [3416] Storing Tcl source code encoded, and running via your own C program - (2011-09-02)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[2848] C course - final course example puts it all together - (2010-07-02)
[2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
[1670] Dynamic Memory Allocation in C - (2008-06-09)
[1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
[1581] What is an lvalue? (Perl, C) - (2008-03-18)
[1497] Training Season Starts again! - (2008-01-07)
U117 - Other Lua Subjects [2461] Luac - getting lua to start fast by precompiling - (2009-10-20)
[1844] Calling functions in C from your Lua script - a first HowTo - (2008-10-17)
Some other Articles
String matching in Perl with Regular Expressions30th November - Santa Trip from MelkshamLua - IAQ (Infrequently Answered Questions)Old Piles of the South WestPassing a table from Lua into CHow many cups of coffee?Lua Course, and the Wiltshire Countryside tooFormatting with a leading + / Lua and PerlValidating Credit Card Numbers