MCPcopy Create free account
hub / github.com/DFHack/dfhack / MakeContainerMetatable

Function MakeContainerMetatable

library/LuaTypes.cpp:1655–1697  ·  view source on GitHub ↗

* Make a container-style object metatable. */

Source from the content-addressed store, hash-verified

1653 * Make a container-style object metatable.
1654 */
1655static void MakeContainerMetatable(lua_State *state, const container_identity *type,
1656 const type_identity *item, int count, const type_identity *ienum)
1657{
1658 int base = lua_gettop(state);
1659
1660 MakeMetatable(state, type, "container");
1661 SetPtrMethods(state, base+1, base+2);
1662
1663 // Update the type name using full info
1664 lua_pushstring(state, type->getFullName(item).c_str());
1665 lua_dup(state);
1666 lua_setfield(state, base+1, "__metatable");
1667 lua_setfield(state, base+1, "_type");
1668
1669 lua_pushlightuserdata(state, const_cast<type_identity*>(item));
1670 lua_setfield(state, base+1, "_field_identity");
1671
1672 if (count >= 0)
1673 {
1674 lua_pushinteger(state, count);
1675 lua_setfield(state, base+1, "_count");
1676 }
1677
1678 SetContainerMethod(state, base+1, base+2, meta_container_len, "__len", type, item, count);
1679 SetContainerMethod(state, base+1, base+2, meta_container_index, "__index", type, item, count);
1680 SetContainerMethod(state, base+1, base+2, meta_container_newindex, "__newindex", type, item, count);
1681
1682 SetContainerMethod(state, base+1, base+2, meta_container_field_reference, "_field", type, item, count);
1683
1684 AddContainerMethodFun(state, base+1, base+2, method_container_resize, "resize", type, item, count);
1685 AddContainerMethodFun(state, base+1, base+2, method_container_erase, "erase", type, item, count);
1686 AddContainerMethodFun(state, base+1, base+2, method_container_insert, "insert", type, item, count);
1687
1688 // push the index table
1689 AttachEnumKeys(state, base+1, base+2, ienum);
1690
1691 PushContainerMethod(state, base+1, base+3, meta_container_next, type, item, count);
1692 SetPairsMethod(state, base+1, "__pairs");
1693 PushContainerMethod(state, base+1, base+3, meta_container_nexti, type, item, count);
1694 SetPairsMethod(state, base+1, "__ipairs");
1695
1696 lua_pop(state, 1);
1697}
1698
1699/*
1700 * Metatable construction identity methods.

Callers 2

build_metatableMethod · 0.85
GetAdHocMetatableFunction · 0.85

Calls 9

lua_gettopFunction · 0.85
lua_pushstringFunction · 0.85
lua_dupFunction · 0.85
lua_setfieldFunction · 0.85
lua_pushlightuserdataFunction · 0.85
lua_pushintegerFunction · 0.85
AddContainerMethodFunFunction · 0.85
c_strMethod · 0.80
getFullNameMethod · 0.45

Tested by

no test coverage detected