【发布时间】:2012-09-28 18:06:06
【问题描述】:
我正在使用this question 的答案将lua 的print 重定向到字符串流。我的功能代码如下。
我的问题是代码并不总是与 lua 自己打印的内容相匹配。最值得注意的是,当我尝试打印一个函数时,我只得到了函数,而不是函数和地址。示例:
> --in lua
> print(os.exit)
function: 0xabcdef01
> --in my interpreter
> print(os.exit)
function
显而易见的解决方案是强制我的自定义打印函数在写入luaout 之前调用lua 的tostring(就像默认打印一样)。但是,我真的不知道如何使这项工作。如果有人能帮助我,我将不胜感激。
这是我的自定义打印:
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;
}
【问题讨论】:
-
在这里查看我的答案:stackoverflow.com/a/40025297/774082