2009-04-16

C Lua snippets

Get currently executed chunk name:
lua_Debug lua_info;
lua_getstack(L, 0, &lua_info);
lua_getinfo(L, "n", &lua_info);
string name = lua_info.name;
Opposite to lua_register:
lua_pushnil(L);
lua_setglobal(L, name.c_str());
stack_dump in C++ flavour ;)
static void stack_dump (lua_State *L) {
    int top = lua_gettop(L);
    for(int i = 1; i <= top; i++) {
        int t = lua_type(L, i);
        switch(t) {
            case LUA_TSTRING:
                cout << "'" << lua_tostring(L, i) << "'";
                break;
            case LUA_TBOOLEAN:
                cout << (lua_toboolean(L, i) ? "true" : "false");
                break;
            case LUA_TNUMBER:
                cout << lua_tonumber(L, i);
                break;
            default:
                cout << lua_typename(L, t);
                break;
        }
        cout << "  ";
    }
    cout << endl;
}

No comments:

Post a Comment