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

Method MakeMetatable

library/LuaWrapper.cpp:1148–1173  ·  view source on GitHub ↗

* Make a metatable with most common fields, and an empty table for UPVAL_FIELDTABLE. */

Source from the content-addressed store, hash-verified

1146 * Make a metatable with most common fields, and an empty table for UPVAL_FIELDTABLE.
1147 */
1148void LuaWrapper::MakeMetatable(lua_State *state, const type_identity *type, const char *kind)
1149{
1150 int base = lua_gettop(state);
1151 lua_newtable(state); // metatable
1152
1153 lua_pushstring(state, type->getFullName().c_str());
1154 lua_setfield(state, base+1, "__metatable");
1155
1156 lua_pushlightuserdata(state, const_cast<type_identity*>(type));
1157 lua_rawsetp(state, base+1, &DFHACK_IDENTITY_FIELD_TOKEN);
1158
1159 LookupInTable(state, const_cast<type_identity*>(type), &DFHACK_TYPEID_TABLE_TOKEN);
1160 if (lua_isnil(state, -1))
1161 {
1162 // Copy the string from __metatable if no real type
1163 lua_pop(state, 1);
1164 lua_getfield(state, base+1, "__metatable");
1165 }
1166 lua_setfield(state, base+1, "_type");
1167
1168 lua_pushstring(state, kind);
1169 lua_setfield(state, base+1, "_kind");
1170
1171 // Create the field table
1172 lua_newtable(state);
1173}
1174
1175/**
1176 * Enable a metafield by injecting an entry into a UPVAL_FIELDTABLE.

Callers

nothing calls this directly

Calls 9

lua_gettopFunction · 0.85
lua_pushstringFunction · 0.85
lua_setfieldFunction · 0.85
lua_pushlightuserdataFunction · 0.85
lua_rawsetpFunction · 0.85
LookupInTableFunction · 0.85
lua_getfieldFunction · 0.85
c_strMethod · 0.80
getFullNameMethod · 0.45

Tested by

no test coverage detected