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

Function meta_enum_attr_index

library/LuaWrapper.cpp:1049–1080  ·  view source on GitHub ↗

* Metamethod: __index for enum.attrs */

Source from the content-addressed store, hash-verified

1047 * Metamethod: __index for enum.attrs
1048 */
1049static int meta_enum_attr_index(lua_State *state)
1050{
1051 if (!lua_isnumber(state, 2))
1052 lua_rawget(state, UPVAL_FIELDTABLE);
1053 if (!lua_isnumber(state, 2))
1054 luaL_error(state, "Invalid index in enum.attrs[]");
1055
1056 auto id = (enum_identity*)lua_touserdata(state, lua_upvalueindex(2));
1057 auto *complex = id->getComplex();
1058
1059 int64_t idx = lua_tonumber(state, 2);
1060 if (complex)
1061 {
1062 auto it = complex->value_index_map.find(idx);
1063 if (it != complex->value_index_map.end())
1064 idx = int64_t(it->second);
1065 else
1066 idx = id->getLastItem() + 1;
1067 }
1068 else
1069 {
1070 if (idx < id->getFirstItem() || idx > id->getLastItem())
1071 idx = id->getLastItem()+1;
1072 idx -= id->getFirstItem();
1073 }
1074
1075 uint8_t *ptr = (uint8_t*)id->getAttrs();
1076 auto atype = id->getAttrType();
1077
1078 push_object_internal(state, atype, ptr + unsigned(atype->byte_size()*idx));
1079 return 1;
1080}
1081
1082/**
1083 * Metamethod: df.isvalid(obj[,allow_null])

Callers

nothing calls this directly

Calls 7

lua_isnumberFunction · 0.85
lua_rawgetFunction · 0.85
luaL_errorFunction · 0.85
lua_touserdataFunction · 0.85
byte_sizeMethod · 0.80
findMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected