duansha7453 2017-10-17 12:56
浏览 53

如何在不修改堆栈的情况下将表的内容转储到堆栈上?

First off I'm very much a n00b at Lua. Literally started yesterday evening. Btw, I'm using golua which as I understand it mirrors the usual C API quite closely. So any suggestions in C will likely apply to my case as well.

Anyway, to the issue. I'm (naturally) quite early in my experiments in integrating Lua in a Go application and as such is testing various methods and way to do things.

One of the first things I'm looking to do is call a Lua function with a table (based on a JSON object), do some operations on the table there and then return it. The table may contain nested tables. The scripting part isn't an issue here, I think I got that bit covered. However, I want to peek inside the stack to verify what I'm passing to the function is correct. Likewise also peek at the value returned from Lua.

But how?

This bit is taken from the reference manual:

static void stackDump (lua_State *L) {
  int i;
  int top = lua_gettop(L);
  for (i = 1; i <= top; i++) {  /* repeat for each level */
    int t = lua_type(L, i);
    switch (t) {

      case LUA_TSTRING:  /* strings */
        printf("`%s'", lua_tostring(L, i));
        break;

      case LUA_TBOOLEAN:  /* booleans */
        printf(lua_toboolean(L, i) ? "true" : "false");
        break;

      case LUA_TNUMBER:  /* numbers */
        printf("%g", lua_tonumber(L, i));
        break;

      default:  /* other values */
        printf("%s", lua_typename(L, t));
        break;

    }
    printf("  ");  /* put a separator */
  }
  printf("
");  /* end the listing */
}

However, when the stack contains a table this will only print "table", which of course isn't terribly useful if the content of the table is the thing we're actually interested in.

I know how to "pop" a table from the stack (using lua_next() etc), but that of course modifies the stack, so I can't use that just before I call the lua function...

I've searched high and low, but everything I've found is akin to the above snippet.

edit: this is to find out why this lua code doesn't print anything:

for k, v in ipairs(arg) do
    print(k .. ' = ' .. v)
end

Where arg is the argument to the function (the table).

edit 2: ahem, turns out the above should be using pairs(arg). However, the usefulness of the question still stands, in general.

edit 3: found ref.manual source

  • 写回答

1条回答 默认 最新

  • douhuang2282 2018-05-20 00:55
    关注

    The usual method for getting the value at a table key is to use the Lua stack operation lua_getfield(). The table should be already on the stack. lua_getfield() will obtain the value of a particular key and push that on the stack. So that value you could pop off since you don't want to change the stack any. It doesn't change the table that was already on the stack.

    If you really are going to modify the tables you might just have to use the lua stack to do it, that's kind of how it works.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)