lua-users home
lua-l archive

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


Hi Steve,

   Just one question, Why lua_settop(m_L, 0) after each function?
Does the SetThreadLuaPointer function create a thread? I'm not sure if
doing a lua_settop to 0 will not destroy data that you expect to be
there after that... When I need to ensure that a function leaves the
stack as it was before it execution I always do that:

// this function is for internal use, as you can see it can not be called from
// lua, is a helper function called by other C functions.
void myFunction(lua_State *L, int param1, int param2, ...) {
     int top = lua_gettop(L); // get the current top
    /*
       here do thins with Lua as you want...
       top var helps to access data too :)
     */
    lua_settop(L,top); //restore the stack
}


May be you can check what kind of data do you have in the stack before
doing a settop to 0. Remember that Threads for example are pushed onto
the stack as other values, so if you remove them without store them in
registry or a table, or whatever... they will be collected.

Hope it helps, :)

Cheers,
    Jose L.


On 9/11/06, Steve Heller <steve@steveheller.com> wrote:
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);
}



--
 Jose L. Hidalgo Valiño (PpluX)
 ---- http://www.pplux.com ----