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