* Make a struct-style object metatable. */
| 1577 | * Make a struct-style object metatable. |
| 1578 | */ |
| 1579 | static 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 |
no test coverage detected