lua-users home
lua-l archive

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


Hi!
I'm a little new at Lua, so I probably missed something obvious, but I can't
seem to register a C function into a Lua table. What I want to do is
register the static function State::DoubleNumber into the Lua table State so
that I can call it from Lua code as State.DoubleNumber. I'm using Lua
5.0-beta. This is the C++ code that I'm using:

...
    Command("State = {}");
    Register(State::DoubleNumber, "State", "DoubleNumber");
...
void State::Command(const char* str)
{
    luaL_loadbuffer(L, str, strlen(str), "Command");
    lua_call(L, 0, 0);
}
...
void State::Register(Method method, const char* module, const char* name)
{
    lua_pushstring(L, module);
    lua_pushstring(L, name);
    lua_pushcfunction(L, method);
    lua_settable(L, 1);        // << crashes here
    lua_pop(L, 1);
}
...
Method is a typedef:
...
    typedef int (*Method)(lua_State*);
...

The code crashes on the commented line in State::Register. Unfortunately, I
have to go to school now, so I can't be any more verbose, but does anyone
know what I'm doing wrong? I have also checked that the stack item at
position 1 is indeed a string with the value "State" just before the line
that crashes.

Thanks,
Nick