I am using the answer to this question to redirect lua's print
to a stringstream. My function code is below.
My issue is that the code doesn't always match what lua would have printed on it's own. Most notably, when I attempt to print a function, I just get the function, rather than the function and address. Example:
> --in lua
> print(os.exit)
function: 0xabcdef01
> --in my interpreter
> print(os.exit)
function
The obvious solution is to force my custom print function to call lua's tostring
prior to writing to luaout
(like the default print does). However, I can't really figure out how to make this work. If anyone can help me out, I'd appreciate it a lot.
Here's my custom print:
static int l_my_print(lua_State* L) {
int nargs = lua_gettop(L);
for (int i=1; i <= nargs; i++) {
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: { /* strings */
luaout << lua_tostring(L, i);
break;
}
case LUA_TBOOLEAN: { /* booleans */
luaout << (lua_toboolean(L, i) ? "true" : "false");
break;
}
case LUA_TNUMBER: { /* numbers */
luaout << lua_tonumber(L, i);
break;
}
default: { /* other values */
luaout << lua_typename(L, t);
break;
}
}
if (i!=nargs){
luaout << "\t";
}
}
luaout << endl;
return 0;
}