C调用Lua中的函数解析table
- 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的更多相关文章
- ulua c#调用lua中模拟的类成员函数
项目使用ulua,我神烦这个东西.lua单纯在lua环境使用还好,一旦要跟外界交互,各种月经不调就来了.要记住贼多的细节,你才能稍微处理好.一个破栈,pop来push去,位置一会在-1,一会在-3,2 ...
- Lua中的函数
[前言] Lua中的函数和C++中的函数的含义是一致的,Lua中的函数格式如下: function MyFunc(param) -- Do something end 在调用函数时,也需要将对应的参数 ...
- 在C++中调用DLL中的函数 (3)
1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...
- 在C++中调用DLL中的函数(3)
1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...
- 在 lua 中实现函数的重载
在 lua 中实现函数的重载.注:好吧,lua中原来可以实现重载...local function create() local arg_table = {} local function dispa ...
- 在VS2012中采用C++中调用DLL中的函数 (4)
这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...
- 在C++中调用DLL中的函数 (2)
应用程序使用DLL可以采用两种方式: 一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息. Visual C++6.0在VC\bin目录下提供了一个名为Dumpbin. ...
- 在C++中调用DLL中的函数
如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...
- 【原创】在VS2012中采用C++中调用DLL中的函数(4)
这两天因为需要用到VS2012来生成一个DLL代码,但是之前并没有用过DLL相关的内容,从昨天开始尝试调试DLL的文件调用,起初笔者在网络上找到了3片采用VSXXX版本进行调试的例子,相关的内容见本人 ...
随机推荐
- Java Swing界面编程(21)---事件处理:窗口事件
WindowLIstener是专门处理窗口的事件监听窗口.一个窗口的全部变化.如窗口的打开.关闭等都能够使用这个接口进行监听. 实现WIndowListener: package com.beyole ...
- 【Java】Java_19递归算法
1.递归算法 A方法调用B方法,我们很容易理解!递归就是:A方法调用A方法!就是自己调用自己,因此我们在设计递归算法时,一定要指明什么时候自己不调用自己.否则,就是个死循环! 1.1递归算法要点 递归 ...
- ajax个人学习笔记
1. function createXHR(){ if(typeof XMLHttpRequest != 'undefined'){ return new XMLHttpRequest(); }els ...
- 【Lucene】Apache Lucene全文检索引擎架构之入门实战1
Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会支持和提供.Lucene提供了一个简单却强大的应用程式接口,能够做全文索引和搜寻.在Java开发环境里Lucene是一个成熟的 ...
- 给mysql root用户设置密码
使用其他用户进入数据库, 用select PASSWORD('你要设置的密码'), 然后直接update mysql.user set mysql.user.Password='你PASSWORD( ...
- Linux基础之常用基本命令备忘
Linux基础之常用基本命令备忘 PWD 查询当前所在Linux上的位置 / 根目录 CD(change directory)切换目录 语法 CD /(注意添加空格) LS ...
- lua学习笔记(三)
类型与值 lua是一种动态类型的语言,在语言中没有类型定义的语法,每个值都携带了它自身的类型信息 lua中有8种基础类型 nil 只与自身相等assert(nil==nil ...
- Android使用LinearViewLayout展示数据
如果要滚动,使用ScrollView来包裹这个LinearViewLayout. ListView控件,自己带有滚动效果的. BaseAdapter LayoutInflater 其他两种绑定方式 A ...
- IDEA中配置svn
反正这个命令行我是勾选了的,软件占用内存不大,我就都选了. 然后是配置IDEA 试一下效果,果然阔以,哈哈.
- Vim使用技巧(1) -- 普通模式技巧 【持续更新】
直观的来一张键盘图先 符号快捷键 . //重复上次修改 ^ //光标快速跳转到当前行开头 $ //光标快速跳转到当前行结尾 f{char} //光标快速跳转到当前行下一个{char}字符,{char} ...