lua-users home
lua-l archive

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

[Hum, the question is already answered... so for what it's worth...]


> 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.