lua-users home
lua-l archive

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


Sorry, I read the callTM function's source code for several times,
but still don't quite understand the example "entering a Lua funcion
and then calling a metamethod".
Will it generates invalid value under L->top ?
If it does, why only do the clear during atomic phase ?
Would you give me more detail, thank you very much.

 93 static void callTM (lua_State *L, const TValue *f, const TValue *p1,
 94                     const TValue *p2, TValue *p3, int hasres) {
 95   ptrdiff_t result = savestack(L, p3);
 96   setobj2s(L, L->top++, f);  /* push function */
 97   setobj2s(L, L->top++, p1);  /* 1st argument */
 98   setobj2s(L, L->top++, p2);  /* 2nd argument */
 99   if (!hasres)  /* no result? 'p3' is third argument */
100     setobj2s(L, L->top++, p3);  /* 3rd argument */
101   /* metamethod may yield only when called from Lua code */
102   luaD_call(L, L->top - (4 - hasres), hasres, isLua(L->ci));
103   if (hasres) {  /* if has result, move it to its place */
104     p3 = restorestack(L, result);
105     setobjs2s(L, p3, --L->top);
106   }
107 }

2016-05-10 20:57 GMT+08:00 Roberto Ierusalimschy <roberto@inf.puc-rio.br>:
>> Lua 5.2.4
>> In lgc.c propagatemark -> traversestack, line 502-506,
>> clear the slice in the range of [th->top + 1, th->stack + th->stacksize),
>> What if don't clear not-marked stack slice ?
>
> Sometimes Lua increates the stack pointer without filling the
> corresponding slots (e.g., when entering a Lua funcion and then calling
> a metamethod).  This step ensures that the entire stack always has valid
> values, so that later gc cycles can always traverse the stack without
> problems.
>
> -- Roberto
>