Advertisement
tinyevil

Untitled

Feb 18th, 2019
449
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.89 KB | None | 0 0
  1.  
  2. LUA_API int lua_setmetatable (lua_State *L, int objindex) {
  3.   TValue *obj;
  4.   Table *mt;
  5.   lua_lock(L);
  6.   api_checknelems(L, 1);
  7.   obj = index2addr(L, objindex);
  8.   api_checkvalidindex(L, obj);
  9.   if (ttisnil(L->top - 1))
  10.     mt = NULL;
  11.   else {
  12.     api_check(L, ttistable(L->top - 1), "table expected");
  13.     mt = hvalue(L->top - 1);
  14.   }
  15.   switch (ttypenv(obj)) {
  16.     case LUA_TTABLE: {
  17.       hvalue(obj)->metatable = mt;
  18.       if (mt)
  19.         luaC_objbarrierback(L, gcvalue(obj), mt);
  20.         luaC_checkfinalizer(L, gcvalue(obj), mt);
  21.       break;
  22.     }
  23.     case LUA_TUSERDATA: {
  24.       uvalue(obj)->metatable = mt;
  25.       if (mt) {
  26.         luaC_objbarrier(L, rawuvalue(obj), mt);
  27.         luaC_checkfinalizer(L, gcvalue(obj), mt);
  28.       }
  29.       break;
  30.     }
  31.     default: {
  32.       G(L)->mt[ttypenv(obj)] = mt;
  33.       break;
  34.     }
  35.   }
  36.   L->top--;
  37.   lua_unlock(L);
  38.   return 1;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement