Lua C API: setting error source information
我正在实现一个简单的LUA在线解释器,该解释器将从纯文本(即C字符串)中获取多个LUA脚本并运行它们。一切正常,但现在当这些脚本发生语法或运行时错误时,我正在测试程序的响应。
到目前为止,当发生错误时,在调用
1 | [string"..."]:7: attempt to call field 'push' (a nil value) |
现在,我想要的是LUA的运行时,用虚拟文件名替换令牌
1 | my.lua:7: attempt to call field 'push' (a nil value) |
我试图分析LUA的源代码,以找出LUA解释器如何成功实现此目的。到目前为止,我发现的是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | LUALIB_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { LoadF lf; int status, readstatus; int c; int fnameindex = lua_gettop(L) + 1; /* index of filename on the stack */ if (filename == NULL) { lua_pushliteral(L,"=stdin"); lf.f = stdin; } else { lua_pushfstring(L,"@%s", filename); lf.f = fopen(filename,"r"); if (lf.f == NULL) return errfile(L,"open", fnameindex); } //... status = lua_load(L, getF, &lf, lua_tostring(L, -1), mode); //... } LUALIB_API int luaL_loadstring (lua_State *L, const char *s) { return luaL_loadbuffer(L, s, strlen(s), s); //eventually calls lua_load } |
我的代码仅调用
我想念点吗?谢谢。
1 2 3 4 | int luaL_loadbuffer (lua_State *L, const char *buff, size_t sz, const char *name); |
例:
1 | luaL_loadbuffer(L, code, code_length,"@my.lua"); |
编辑
必须使用名称前的'@'告诉LUA块的名称实际上是脚本的名称,而不是脚本代码本身。
它应该将缓冲区作为lua-chunk加载,并使用第四个参数中的名称作为调试信息和错误消息。