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

Function FillEnumKeys

library/LuaWrapper.cpp:1468–1560  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1466}
1467
1468static void FillEnumKeys(lua_State *state, int ix_meta, int ftable, enum_identity *eid)
1469{
1470 const char *const *keys = eid->getKeys();
1471
1472 // Create a new table attached to ftable as __index
1473 lua_newtable(state);
1474 int base = lua_gettop(state);
1475 lua_newtable(state);
1476
1477 auto *complex = eid->getComplex();
1478
1479 // For enums, set mapping between keys and values
1480 if (complex)
1481 {
1482 for (size_t i = 0; i < complex->size(); i++)
1483 {
1484 if (keys[i])
1485 AssociateId(state, base+1, complex->index_value_map[i], keys[i]);
1486 }
1487 }
1488 else
1489 {
1490 for (int64_t i = eid->getFirstItem(), j = 0; i <= eid->getLastItem(); i++, j++)
1491 {
1492 if (keys[j])
1493 AssociateId(state, base+1, i, keys[j]);
1494 }
1495 }
1496
1497 if (complex)
1498 {
1499 lua_pushvalue(state, base + 1);
1500 lua_pushlightuserdata(state, (void*)complex);
1501 lua_pushcclosure(state, complex_enum_ipairs, 2);
1502 lua_setfield(state, ix_meta, "__ipairs");
1503
1504 lua_pushinteger(state, 0); // unused; to align ComplexData
1505 lua_pushlightuserdata(state, (void*)complex);
1506 lua_pushcclosure(state, complex_enum_next_item, 2);
1507 lua_setfield(state, ftable, "next_item");
1508
1509 lua_pushinteger(state, eid->getFirstItem());
1510 lua_setfield(state, ftable, "_first_item");
1511
1512 lua_pushinteger(state, eid->getLastItem());
1513 lua_setfield(state, ftable, "_last_item");
1514
1515 lua_pushboolean(state, true);
1516 lua_setfield(state, ftable, "_complex");
1517 }
1518 else
1519 {
1520 if (eid->getFirstItem() <= eid->getLastItem())
1521 {
1522 lua_pushvalue(state, base + 1);
1523 lua_pushinteger(state, eid->getFirstItem() - 1);
1524 lua_pushinteger(state, eid->getLastItem());
1525 lua_pushcclosure(state, wtype_ipairs, 3);

Callers 1

RenderTypeFunction · 0.85

Calls 13

lua_gettopFunction · 0.85
lua_pushvalueFunction · 0.85
lua_pushlightuserdataFunction · 0.85
lua_pushcclosureFunction · 0.85
lua_setfieldFunction · 0.85
lua_pushintegerFunction · 0.85
lua_pushbooleanFunction · 0.85
lua_rawgetpFunction · 0.85
freeze_tableFunction · 0.85
lua_setmetatableFunction · 0.85
c_strMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected