lua-users home
lua-l archive

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


> [LUA CODE]

> myTable = {}
> function myTable:saysomething( a, b )
>     print( "myTable says: " .. a .. " " ..  b )
> end

> [C CODE]
> // luaVM is an open lua_State
> lua_getglobal(luaVM, "myTable");
> lua_pushstring(luaVM, "saysomething");
> lua_gettable(luaVM, -2);

> lua_pushstring(luaVM, "param UNKNOWN!!");
> lua_pushstring(luaVM, "param a");
> lua_pushstring(luaVM, "param b");
> lua_call(luaVM, 3, 0);


I think you are missing the point of "self-calls" (MyTable:saysomething
rather than MyTable.something).

The function object:method syntax creates an implicit first parameter
called "self"

The object:method() syntax supplies an implicit first argument, which is
the object.

This is handled by the Lua compiler in Lua code, but with C code you have
to do it yourself.

Probably, you wanted to say:

  function MyTable.saysomething

instead.

But if you really wanted it to be a self:call, you need to modify your C
code as follows to provide the self argument:

  lua_getglobal(luaVM, "myTable");
  lua_pushstring(luaVM, "saysomething");
  lua_gettable(luaVM, -2);
  /* instead of: lua_pushstring(luaVM, "param UNKNOWN!!"); */
     lua_pushvalue(luaVM, -2); /* push the table as the self arg */

  lua_pushstring(luaVM, "param a");
  lua_pushstring(luaVM, "param b");
  lua_call(luaVM, 3, 0);