| 843 | |
| 844 | |
| 845 | LUA_API int lua_setmetatable (lua_State *L, int objindex) { |
| 846 | TValue *obj; |
| 847 | Table *mt; |
| 848 | lua_lock(L); |
| 849 | api_checknelems(L, 1); |
| 850 | obj = index2addr(L, objindex); |
| 851 | if (ttisnil(L->top - 1)) |
| 852 | mt = NULL; |
| 853 | else { |
| 854 | api_check(L, ttistable(L->top - 1), "table expected"); |
| 855 | mt = hvalue(L->top - 1); |
| 856 | } |
| 857 | switch (ttnov(obj)) { |
| 858 | case LUA_TTABLE: { |
| 859 | hvalue(obj)->metatable = mt; |
| 860 | if (mt) { |
| 861 | luaC_objbarrier(L, gcvalue(obj), mt); |
| 862 | luaC_checkfinalizer(L, gcvalue(obj), mt); |
| 863 | } |
| 864 | break; |
| 865 | } |
| 866 | case LUA_TUSERDATA: { |
| 867 | uvalue(obj)->metatable = mt; |
| 868 | if (mt) { |
| 869 | luaC_objbarrier(L, uvalue(obj), mt); |
| 870 | luaC_checkfinalizer(L, gcvalue(obj), mt); |
| 871 | } |
| 872 | break; |
| 873 | } |
| 874 | default: { |
| 875 | G(L)->mt[ttnov(obj)] = mt; |
| 876 | break; |
| 877 | } |
| 878 | } |
| 879 | L->top--; |
| 880 | lua_unlock(L); |
| 881 | return 1; |
| 882 | } |
| 883 | |
| 884 | |
| 885 | LUA_API void lua_setuservalue (lua_State *L, int idx) { |
no test coverage detected