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

Function MakeFieldMetatable

library/LuaTypes.cpp:1579–1604  ·  view source on GitHub ↗

* Make a struct-style object metatable. */

Source from the content-addressed store, hash-verified

1577 * Make a struct-style object metatable.
1578 */
1579static void MakeFieldMetatable(lua_State *state, const struct_identity *pstruct,
1580 lua_CFunction reader, lua_CFunction writer,
1581 lua_CFunction iterator, bool globals = false)
1582{
1583 int base = lua_gettop(state);
1584
1585 MakeMetatable(state, pstruct, "struct"); // meta, fields
1586
1587 // Index the fields
1588 lua_newtable(state);
1589 IndexFields(state, base, pstruct, globals ? IndexFieldsFlags::GLOBALS : 0);
1590
1591 // Add the iteration metamethods
1592 PushStructMethod(state, base+1, base+3, iterator);
1593 SetPairsMethod(state, base+1, "__pairs");
1594 lua_pushnil(state);
1595 SetPairsMethod(state, base+1, "__ipairs");
1596
1597 lua_setfield(state, base+1, "_index_table");
1598
1599 // Add the indexing metamethods
1600 SetStructMethod(state, base+1, base+2, reader, "__index");
1601 SetStructMethod(state, base+1, base+2, writer, "__newindex");
1602
1603 // returns: [metatable readfields writefields];
1604}
1605
1606/**
1607 * Make a primitive-style metatable

Callers 1

build_metatableMethod · 0.85

Calls 4

lua_gettopFunction · 0.85
IndexFieldsFunction · 0.85
lua_pushnilFunction · 0.85
lua_setfieldFunction · 0.85

Tested by

no test coverage detected