[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: calling table:function from c/c++
- From: "Wim Couwenberg" <w.couwenberg@...>
- Date: Mon, 31 Mar 2003 21:57:06 +0200
[Hum, the question is already answered... so for what it's worth...]
Hi,
> The program complains about calling nil function.
> My rough guess is that the function "test:fn" belongs to the
> table "test", and not globally known.
That, plus you used the "self" notation with a colon instead of a dot. So
the function "fn" now expects three arguments (the first being the table in
which it was located.)
Here is the important part:
/* retrieve global "table" */
lua_getglobal(L, "table");
/* get fn key */
lua_pushstring(L, "fn");
lua_gettable(L, -2); /* table resides at stack index -2 */
lua_insert(L, -2); /* and swap with table... */
/* call fn with table, 1, 2 */
lua_pushnumber(L, 1);
lua_pushnumber(L, 2); /* stack: fn table 1 2 */
lua_call(L, 3, 1); /* 3 args 1 result */
If you define f like "function table.fn(n1, n2) ... end" then you can drop
the first argument.
Bye,
Wim