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.
(written 2008-10-18 14:49:17)
Associated topics are indexed under
C212 - C and C based languages - Memory ManagementU117 - Other Lua Subjects
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 CCalling functions in C from your Lua script - a first HowToHow many cups of coffee?Lua Course, and the Wiltshire Countryside tooFormatting with a leading + / Lua and PerlValidating Credit Card Numbers