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