1. Passing Tables to Lua Functions
  2. 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:
  3. The Lua Program
  4. 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.
  5.  
  6. function tweaktable(tab_in)
  7. local tab_out = {numfields=}
  8. for k,v in pairs(tab_in) do
  9. tab_out.numfields = tab_out.numfields +
  10. tab_out[tostring(k)] = string.upper(tostring(v))
  11. end
  12. tab_out.numfields = tostring(tab_out.numfields)
  13. io.write("At bottom of callfuncscript.lua tweaktable(), numfields=")
  14. io.write(tab_out.numfields)
  15. print()
  16. return tab_out
  17. end
  18.  
  19. print("Priming run")
  20. The C Program
  21. 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.
  22.  
  23. Start by looking and running the code, and then we'll discuss some of the idioms that make it work...
  24. #include <lua.h> /* Always include this when calling Lua */
  25. #include <lauxlib.h> /* Always include this when calling Lua */
  26. #include <lualib.h> /* Prototype for luaL_openlibs(), */
  27. /* always include this when calling Lua */
  28.  
  29. #include <stdlib.h> /* For function exit() */
  30. #include <stdio.h> /* For input/output */
  31.  
  32. void bail(lua_State *L, char *msg){
  33. fprintf(stderr, "\nFATAL ERROR:\n %s: %s\n\n",
  34. msg, lua_tostring(L, -));
  35. exit();
  36. }
  37.  
  38. int main(void)
  39. {
  40. lua_State *L;
  41.  
  42. L = luaL_newstate(); /* Create Lua state variable */
  43. luaL_openlibs(L); /* Load Lua libraries */
  44.  
  45. if (luaL_loadfile(L, "callfuncscript.lua")) /* Load but don't run the Lua script */
  46. bail(L, "luaL_loadfile() failed"); /* Error out if file can't be read */
  47.  
  48. if (lua_pcall(L, , , )) /* PRIMING RUN. FORGET THIS AND YOU'RE TOAST */
  49. bail(L, "lua_pcall() failed"); /* Error out if Lua file has an error */
  50.  
  51. printf("In C, calling Lua->tweaktable()\n");
  52. lua_getglobal(L, "tweaktable"); /* Tell it to run callfuncscript.lua->tweaktable() */
  53. lua_newtable(L); /* Push empty table onto stack table now at -1 */
  54. lua_pushliteral(L, "fname"); /* Push a key onto the stack, table now at -2 */
  55. lua_pushliteral(L, "Margie"); /* Push a value onto the stack, table now at -3 */
  56. lua_settable(L, -); /* Take key and value, put into table at -3, */
  57. /* then pop key and value so table again at -1 */
  58.  
  59. lua_pushliteral(L, "lname"); /* Push a key onto the stack, table now at -2 */
  60. lua_pushliteral(L, "Martinez"); /* Push a value onto the stack, table now at -3 */
  61. lua_settable(L, -); /* Take key and value, put into table at -3, */
  62. /* then pop key and value so table again at -1 */
  63.  
  64. if (lua_pcall(L, , , )) /* Run function, !!! NRETURN=1 !!! */
  65. bail(L, "lua_pcall() failed");
  66.  
  67. printf("============ Back in C again, Iterating thru returned table ============\n");
  68.  
  69. /* table is in the stack at index 't' */
  70. lua_pushnil(L); /* Make sure lua_next starts at beginning */
  71. const char *k, *v;
  72. while (lua_next(L, -)) { /* TABLE LOCATED AT -2 IN STACK */
  73. v = lua_tostring(L, -); /* Value at stacktop */
  74. lua_pop(L,); /* Remove value */
  75. k = lua_tostring(L, -); /* Read key at stacktop, */
  76. /* leave in place to guide next lua_next() */
  77. printf("Fromc k=>%s<, v=>%s<\n", k, v);
  78. }
  79.  
  80. lua_close(L); /* Clean up, free the Lua state var */
  81. return ;
  82. }
  83. The preceding code yields the following output:
  84. slitt@mydesk:~$ ./callfunc
  85. Priming run
  86. In C, calling Lua->tweaktable()
  87. At bottom of callfuncscript.lua tweaktable(), numfields=
  88. ============ Back in C again, Iterating thru returned table ============
  89. Fromc k=>fname<, v=>MARGIE<
  90. Fromc k=>lname<, v=>MARTINEZ<
  91. Fromc k=>numfields<, v=><
  92. slitt@mydesk:~$
  93.  
  94. 转自: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. Java Swing界面编程(21)---事件处理:窗口事件

    WindowLIstener是专门处理窗口的事件监听窗口.一个窗口的全部变化.如窗口的打开.关闭等都能够使用这个接口进行监听. 实现WIndowListener: package com.beyole ...

  2. 【Java】Java_19递归算法

    1.递归算法 A方法调用B方法,我们很容易理解!递归就是:A方法调用A方法!就是自己调用自己,因此我们在设计递归算法时,一定要指明什么时候自己不调用自己.否则,就是个死循环! 1.1递归算法要点 递归 ...

  3. ajax个人学习笔记

    1. function createXHR(){ if(typeof XMLHttpRequest != 'undefined'){ return new XMLHttpRequest(); }els ...

  4. 【Lucene】Apache Lucene全文检索引擎架构之入门实战1

    Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟的 ...

  5. 给mysql root用户设置密码

    使用其他用户进入数据库, 用select PASSWORD('你要设置的密码'), 然后直接update mysql.user set  mysql.user.Password='你PASSWORD( ...

  6. Linux基础之常用基本命令备忘

    Linux基础之常用基本命令备忘 PWD   查询当前所在Linux上的位置 /         根目录 CD(change directory)切换目录  语法 CD /(注意添加空格)   LS ...

  7. lua学习笔记(三)

    类型与值   lua是一种动态类型的语言,在语言中没有类型定义的语法,每个值都携带了它自身的类型信息   lua中有8种基础类型     nil       只与自身相等assert(nil==nil ...

  8. Android使用LinearViewLayout展示数据

    如果要滚动,使用ScrollView来包裹这个LinearViewLayout. ListView控件,自己带有滚动效果的. BaseAdapter LayoutInflater 其他两种绑定方式 A ...

  9. IDEA中配置svn

    反正这个命令行我是勾选了的,软件占用内存不大,我就都选了. 然后是配置IDEA 试一下效果,果然阔以,哈哈.

  10. Vim使用技巧(1) -- 普通模式技巧 【持续更新】

    直观的来一张键盘图先 符号快捷键 . //重复上次修改 ^ //光标快速跳转到当前行开头 $ //光标快速跳转到当前行结尾 f{char} //光标快速跳转到当前行下一个{char}字符,{char} ...