lua-users home
lua-l archive

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


> I heard you this first time, but just didn't have anything good to
say. I'll try anyway.

Thanks for being patient. :-)

> Ah, if your tag method is in C, then yes you'll have to send strings
to Lua 
> and they'll have to be interned.

It's not the sending of strings to Lua I have a problem with. I do use
references for exactly that. It's the *receiving* of strings from Lua I
have a problem with, where I want to be able to quickly match it against
a known string, which is incredibly common with tag methods. Let me make
the example concrete (using 4.0 syntax):

I want Lua syntax like:
    local a = table.KnownKeyName1
    local b = table.KnownKeyName37
    local c = table.KnownKeyName1234
    -- do something with a, b, c, etc.

etc., where those keynames don't exist and the values are supplied by an
index tag method, established like so:

    lua_pushcfunction(m_l, fnIndex);
    lua_settagmethod(m_l, mytag, "index");

    lua_newtable(m_l);
    lua_settag(m_l, mytag);
...

int fnIndex(lua_State * l)
{
    if (LUA_TSTRING == lua_type(l, -1))
    {
        char * keyname = lua_tostring(l, -1);
        if (!strcmp(keyname, "KnownKeyName1"))
            lua_pushnumber(l, somevariable); 
        else if (!strcmp(keyname, "KnownKeyName2"))
            lua_pushnumber(l, someothervariable); 
        ...
        ...
        ...
        else if (!strcmp(keyname, "KnownKeyName999"))
            lua_pushnumber(l, yetanothervariable);
        return 1;
    }

    return 0;
}

How can I avoid all those strcmps? My original mail contained a
suggestion for how Lua could eliminate the need for that.

Thanks again,

Curt