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. function tweaktable(tab_in)
local tab_out = {numfields=}
for k,v in pairs(tab_in) do
tab_out.numfields = tab_out.numfields +
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 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...
#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 */ #include <stdlib.h> /* For function exit() */
#include <stdio.h> /* For input/output */ void bail(lua_State *L, char *msg){
fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
msg, lua_tostring(L, -));
exit();
} int main(void)
{
lua_State *L; L = luaL_newstate(); /* Create Lua state variable */
luaL_openlibs(L); /* Load Lua libraries */ 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 */ if (lua_pcall(L, , , )) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */ printf("In C, calling Lua->tweaktable()\n");
lua_getglobal(L, "tweaktable"); /* Tell it to run callfuncscript.lua->tweaktable() */
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, -); /* Take key and value, put into table at -3, */
/* then pop key and value so table again at -1 */ 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, -); /* Take key and value, put into table at -3, */
/* then pop key and value so table again at -1 */ if (lua_pcall(L, , , )) /* Run function, !!! NRETURN=1 !!! */
bail(L, "lua_pcall() failed"); printf("============ Back in C again, Iterating thru returned table ============\n"); /* 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, -)) { /* TABLE LOCATED AT -2 IN STACK */
v = lua_tostring(L, -); /* Value at stacktop */
lua_pop(L,); /* Remove value */
k = lua_tostring(L, -); /* Read key at stacktop, */
/* leave in place to guide next lua_next() */
printf("Fromc k=>%s<, v=>%s<\n", k, v);
} lua_close(L); /* Clean up, free the Lua state var */
return ;
}
The preceding code yields the following output:
slitt@mydesk:~$ ./callfunc
Priming run
In C, calling Lua->tweaktable()
At bottom of callfuncscript.lua tweaktable(), numfields=
============ Back in C again, Iterating thru returned table ============
Fromc k=>fname<, v=>MARGIE<
Fromc k=>lname<, v=>MARTINEZ<
Fromc k=>numfields<, v=><
slitt@mydesk:~$ 转自:http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm

C调用Lua中的函数解析table的更多相关文章

  1. ulua c#调用lua中模拟的类成员函数

    项目使用ulua,我神烦这个东西.lua单纯在lua环境使用还好,一旦要跟外界交互,各种月经不调就来了.要记住贼多的细节,你才能稍微处理好.一个破栈,pop来push去,位置一会在-1,一会在-3,2 ...

  2. Lua中的函数

    [前言] Lua中的函数和C++中的函数的含义是一致的,Lua中的函数格式如下: function MyFunc(param) -- Do something end 在调用函数时,也需要将对应的参数 ...

  3. 在C++中调用DLL中的函数 (3)

    1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...

  4. 在C++中调用DLL中的函数(3)

    1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...

  5. 在 lua 中实现函数的重载

    在 lua 中实现函数的重载.注:好吧,lua中原来可以实现重载...local function create() local arg_table = {} local function dispa ...

  6. 在VS2012中采用C++中调用DLL中的函数 (4)

    这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...

  7. 在C++中调用DLL中的函数 (2)

    应用程序使用DLL可以采用两种方式: 一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息. Visual C++6.0在VC\bin目录下提供了一个名为Dumpbin. ...

  8. 在C++中调用DLL中的函数

    如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...

  9. 【原创】在VS2012中采用C++中调用DLL中的函数(4)

    这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...

随机推荐

  1. js中数组的合并和对象的合并

    1 数组合并 1.1 concat 方法 var a = [1,2,3], b = [4,5,6]; var c = a.concat(b); console.log(c);// 1,2,3,4,5, ...

  2. 3、Linux内核模块学习

    一.内核模块的学习   内核的整体框架是非常的大,包含的组件也是非常多,如何将需要的组件包含在内核中呢?选择一,就是将所有的组件全部编译进内核,虽然需要的组件都可以使用,但是内核过分庞大,势必带来效率 ...

  3. 【SpringMVC学习04】Spring、MyBatis和SpringMVC的整合

    前两篇springmvc的文章中都没有和mybatis整合,都是使用静态数据来模拟的,但是springmvc开发不可能不整合mybatis,另外mybatis和spring的整合我之前学习mybati ...

  4. ios程序,顶部和底部产生空白——程序不能全屏运行

    在开发过程中,遇到过这样的问题,整个程序不能以全屏状态运行,顶部和底部出现空白,如下图所示: 这样的原因是:设置的启动页不合适,设置大小合适的启动页就好了

  5. MvcPager 分页示例 — 应用CSS样式

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @model PagedList<string>   <h5>Digg style:</h5> ...

  6. Linux下画原理图和PCB

    Linux下画原理图和PCB Windows下大名鼎鼎的Allegro和经典的Protel 99SE都是不支持Linux操作系统的.做Linux驱动开发免不了要看一下原理图和PCB. 一般的做法有三种 ...

  7. 40: Redraiment的走法(不连续最长子字符串)

    题目描述 :   Redraiment是走梅花桩的高手.Redraiment总是起点不限,从前到后,往高的桩子走,但走的步数最多,不知道为什么?你能替Redraiment研究他最多走的步数吗? 样例输 ...

  8. docker 让容器执行命令 与 进入容器交互

    直接执行命令docker exec mynginx cat /etc/nginx/nginx.conf 进入容器交互docker exec -it 80nginx /bin/bash

  9. 解决Linux上解压jdk报错gzip: stdin: not in gzip format

    最近在阿里上买了个服务器玩,需要安装jdk,在解压过程中遇到了一些问题,又是一番Google度娘,终于解决了.问题原因让我有点无奈…… 输入 #tar -xvf jdk-8u131-linux-x64 ...

  10. Spring整合JMS(消息中间件)

    这一节来说说,异步机制及spring对JMS封装 一.消息异步处理 类似于RMI.Hessian.Burlap等远程方法调用,它们都是同步的,所谓同步调用就是客户端必须等待操作完成,如果远程服务没有返 ...