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 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错