| 1595 | } |
| 1596 | |
| 1597 | static void RenderType(lua_State *state, const compound_identity *node) |
| 1598 | { |
| 1599 | assert(node->getName()); |
| 1600 | std::string name = node->getFullName(); |
| 1601 | |
| 1602 | // Frame: |
| 1603 | // base+1 - outer table |
| 1604 | // base+2 - metatable of outer table |
| 1605 | // base+3 - inner table |
| 1606 | // base+4 - pairs table |
| 1607 | Lua::StackUnwinder base(state); |
| 1608 | |
| 1609 | lua_newtable(state); |
| 1610 | if (!lua_checkstack(state, 20)) |
| 1611 | return; |
| 1612 | |
| 1613 | SaveInTable(state, const_cast<compound_identity*>(node), &DFHACK_TYPEID_TABLE_TOKEN); |
| 1614 | |
| 1615 | // metatable |
| 1616 | lua_newtable(state); |
| 1617 | int ix_meta = base+2; |
| 1618 | |
| 1619 | lua_dup(state); |
| 1620 | lua_setmetatable(state, base+1); |
| 1621 | |
| 1622 | lua_pushstring(state, name.c_str()); |
| 1623 | lua_setfield(state, ix_meta, "__metatable"); |
| 1624 | |
| 1625 | lua_getfield(state, LUA_REGISTRYINDEX, DFHACK_TYPE_TOSTRING_NAME); |
| 1626 | lua_setfield(state, ix_meta, "__tostring"); |
| 1627 | |
| 1628 | lua_pushlightuserdata(state, const_cast<compound_identity*>(node)); |
| 1629 | lua_rawsetp(state, ix_meta, &DFHACK_IDENTITY_FIELD_TOKEN); |
| 1630 | |
| 1631 | // inner table |
| 1632 | lua_newtable(state); |
| 1633 | int ftable = base+3; |
| 1634 | |
| 1635 | lua_dup(state); |
| 1636 | lua_setfield(state, ix_meta, "__index"); |
| 1637 | |
| 1638 | // pairs table - reuse index table |
| 1639 | lua_dup(state); |
| 1640 | int ptable = base+4; |
| 1641 | |
| 1642 | lua_pushvalue(state, ptable); |
| 1643 | lua_pushcclosure(state, wtype_pairs, 1); |
| 1644 | lua_setfield(state, ix_meta, "__pairs"); |
| 1645 | |
| 1646 | switch (node->type()) |
| 1647 | { |
| 1648 | case IDTYPE_UNION: // TODO: change this to union-type? what relies on this? |
| 1649 | lua_pushboolean(state, true); |
| 1650 | lua_setfield(state, ftable, "_union"); |
| 1651 | [[fallthrough]]; |
| 1652 | case IDTYPE_STRUCT: |
| 1653 | lua_pushstring(state, "struct-type"); |
| 1654 | lua_setfield(state, ftable, "_kind"); |
no test coverage detected