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