C读取写入lua table常用接口
C读取写入lua table常用接口
//lua_gettable
lua_getglobal(L, "mytable") //<== push mytable
lua_pushnumber(L, 1) //<== push key 1
lua_gettable(L, -2) //<== pop key 1, push mytable[1]
//lua_settable
lua_getglobal(L, "mytable") //<== push mytable
lua_pushnumber(L, 1) //<== push key 1
lua_pushstring(L, "abc") //<== push value "abc"
lua_settable(L, -3) //<== mytable[1] = "abc", pop key & value
//lua_rawget:
//用法同lua_gettable,但更快(因为当key不存在时不用访问元方法__index)
//lua_rawset:
//用法同lua_settable,但更快(因为当key不存在时不用访问元方法__newindex)
//lua_rawgeti必须为数值键
lua_getglobal(L, "mytable") //<== push mytable
lua_rawgeti(L, -1, 1) // <== push mytable[1],作用同下面两行调用
lua_pushnumber(L, 1) //<== push key 1
lua_rawget(L,-2) // <== pop key 1, push mytable[1]
//lua_rawseti必须为数值键
lua_getglobal(L, "mytable") //<== push mytable
lua_pushstring(L, "abc") // <== push value "abc"
lua_rawseti(L, -2, 1) //<== mytable[1] = "abc", pop value "abc"
//lua_getfield必须为字符串键
lua_getglobal(L, "mytable") //<== push mytable
lua_getfield(L, -1, "x") //<== push mytable["x"],作用同下面两行调用
lua_pushstring(L, "x") //<== push key "x"
lua_gettable(L,-2) //<== pop key "x", push mytable["x"]
//lua_setfield必须为字符串键
lua_getglobal(L, "mytable") //<== push mytable
lua_pushstring(L, "abc") //<== push value "abc"
lua_setfield(L, -2, "x") //<== mytable["x"] = "abc", pop value "abc"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律