lua-users home
lua-l archive

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

On Tue, Jan 13, 2009 at 8:36 AM, Juris Kalnins <> wrote:
>> Sorry, but I don't understand how setting a metatable for the lightuserdata type is equivalent to my patch.
> Sorry, I wasn't very clear on that. I meant only the part of your patch that
> implements light tables and functions.
> set "__call" to something like this (sorry, this is not an actual code, i'm
> just making it up here from memory):
> int lightCall(lua_State *L) {
>   CFunction *p = lua_topointer(L, 1);
>   if (!ptr_in_code_segment(p)) return luaL_error(L, "not a function");
>   lua_call(L, lua_gettop(L) - 1, LUA_MULTRET);
>   return lua_gettop(L);
> }

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))
    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,
which defeats the whole purpose. This is why I created the patch in
the first place.