lua-users home
lua-l archive

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

I don't think something like this would work. I'm still not very
familiar with the Lua source code, but what I learnt is that
ultimately (even after trying all metamethods, __call included)
standard Lua would call only call an object of type LUA_TFUNCTION.
Look at this code from ldo.c:

static StkId tryfuncTM (lua_State *L, StkId func) {
  const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
  StkId p;
  ptrdiff_t funcr = savestack(L, func);

  >>>HERE <<< if (!ttisfunction(tm))

This code (see also luaT_gettmbyobj) looks up entry "__call"
in object's metatable (which has to be a real Lua table),
and checks that this entry is a function.
It doesn't check type of object.
It checks type of metamethod.

    luaG_typeerror(L, func, "call");

  /* Open a hole inside the stack at `func' */
  for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
  func = restorestack(L, funcr);  /* previous call may change stack */
setobj2s(L, func, tm); /* tag method is the new function to be called */
  return func;

Objects of type LUA_TFUNCTION are allocated in RAM by luaL_register
(via lua_pushcclosure), so you'd still need RAM to call your function,

Yes, but only for one C function per metamethod + one metatable.