lua-users home
lua-l archive

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

In Lua, a call like class:cmd(args) is actually class.cmd(class, args) - there are infact two arguments there, not just the one. Hence, before the call to pushstring, you should do a pushvalue to place a copy of the class object on the stack, and then adjust pcall to 2 arguments.

On Tue, Dec 30, 2008 at 11:09 PM, Matthew Metnetsky <> wrote:
Hi all,

I have what I'm hoping is a ridiculously easy question. The following code
works, but no arguments are being passed into the method. Clearly I'm missing something

       lua_State *L;
       int class; // table with functions
       char *cmd; // string representing a method on a class
       char *args; // string to be passed to cmd


       lua_rawgeti(L, LUA_REGISTRYINDEX, class);
       g_assert(lua_type(L, -1) == LUA_TTABLE);
       lua_getfield(L, -1, cmd);
       g_assert(lua_type(L, -1) == LUA_TFUNCTION);
       lua_pushstring(L, args);
       g_assert(lua_type(L, -1) == LUA_TSTRING);

       if (0 != lua_pcall(L, 1, 1, 0)) { // pass 1 argument, get 1 value...

The pseudo lua code looks like....

       class = MyClass()

The method call of 'cmd' on 'class' works, but I cannot seem to get args to be
passed in. The following code looks similar to the lua method:

       function class:cmd(args)
               print("type " .. type(args)) -- prints 'type nil'

Does anyone know what I'm missing in order to get a string passed into


~ Matthew

Version: GnuPG v1.4.9 (GNU/Linux)