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=1} for k,v in pairs(tab_in) do tab_out.numfields = tab_out.numfields + 1 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, -1)); exit(1); } 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, 0, 0, 0)) /* 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, -3); /* 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, -3); /* Take key and value, put into table at -3, */ /* then pop key and value so table again at -1 */ if (lua_pcall(L, 1, 1, 0)) /* 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, -2)) { /* TABLE LOCATED AT -2 IN STACK */ v = lua_tostring(L, -1); /* Value at stacktop */ lua_pop(L,1); /* Remove value */ k = lua_tostring(L, -1); /* 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 0; } The preceding code yields the following output: slitt@mydesk:~$ ./callfunc Priming run In C, calling Lua->tweaktable() At bottom of callfuncscript.lua tweaktable(), numfields=3 ============ Back in C again, Iterating thru returned table ============ Fromc k=>fname<, v=>MARGIE< Fromc k=>lname<, v=>MARTINEZ< Fromc k=>numfields<, v=>3< slitt@mydesk:~$ 转自:http://www.troubleshooters.com/codecorn/lua/lua_c_calls_lua.htm
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
2017-04-01 Ubuntu16.04安装xampp及部署Testlink