lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>From Lua 5.1 manualvoid lua_settop (lua_State *L, int index);Accepts any acceptable index, or 0, and sets the stack top to this index. If the new top is larger than the old one, then the new elements are filled with nil. If index is 0, then all stack elements are removed. 

Why call lua_settop(L,0) ?







----- Original Message ----- 
From: "Steve Heller" <steve@steveheller.com>
To: <lua@bazar2.conectiva.com.br>
Sent: Tuesday, September 12, 2006 5:18 AM
Subject: Problem with table created from C


> Hi all,
> 
> I'm having a problem when creating a Lua table from
> C++, then asking for its value later. For some reason,
> the table doesn't seem to exist.
> 
> Code that shows what I'm doing is at the bottom of
> this message. The
> "GetThreadLuaPointer"/"SetThreadLuaPointer" functions
> use thread local storage to save the Lua environment
> pointer. There are two phases of this program.
> 
> 1. Store the rules into the RuleManager table, via
> code like this:
> 
> LuaRuleManager RulesObject;
> LuaRuleManager.AddRule(ruleNumber,ruleString);
> 
> 2. Retrieve the rules from the RuleManager table, via
> code like this:
> 
> LuaRuleManager RulesObject;
> string result = LuaRuleManager.GetRule(ruleNumber);
> 
> The storage part works fine; I've confirmed that the
> rules are actually being saved in the RuleManager
> table. Unfortunately, when I try to retrieve the
> rules, the RuleManager object comes up as nil.
> 
> Any suggestions as to what the problem may be, or how
> to fix it?
> 
> Thanks.
> 
> --------------------------------
> LuaRuleManager::LuaRuleManager()
> {
>    m_L = GetThreadLuaPointer();
>    
>    if (m_L == 0)
>        {
>        m_L = lua_open();              /* opens Lua */
>        SetThreadLuaPointer(m_L);
>        luaL_openlibs(m_L);            /* open
> standard libraries */
>        
>        lua_newtable(m_L);             // create new
> table
>        lua_setglobal(m_L,"RuleManager");    // assign
> it to global variable named RuleManager
>        }
> }
> 
> LuaRuleManager::~LuaRuleManager()
> {
> }
> 
> void LuaRuleManager::AddRule(int id, const string&
> RuleString)
> {
>    lua_getglobal(m_L,"RuleManager");
>    if (!lua_istable(m_L,-1))
>        __asm int 3; //RuleManager table not found
>    
>    lua_pushnumber(m_L,id);
>    lua_pushstring(m_L,RuleString.c_str());
>    lua_settable(m_L,-3);
>    lua_settop (m_L, 0);
> }
> 
> string LuaRuleManager::GetRule(int id)
> {
>    lua_getglobal(m_L,"RuleManager");
>    if (!lua_istable(m_L,-1))
>        {
>        PassThrough::DumpStack(m_L);
>        __asm int 3; //RuleManager table not found
>        }
>        
>    lua_pushnumber(m_L,id);
>    lua_gettable(m_L,-2);
>    size_t size;
>    const char* rule = lua_tolstring(m_L,-1,&size);
>    lua_settop (m_L, 0);
>    
>    return string(rule,size);
> }
>