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

Method get_object_identity

library/LuaWrapper.cpp:498–526  ·  view source on GitHub ↗

* Given a DF object reference or type, safely retrieve its identity pointer. */

Source from the content-addressed store, hash-verified

496 * Given a DF object reference or type, safely retrieve its identity pointer.
497 */
498const type_identity *LuaWrapper::get_object_identity(lua_State *state, int objidx,
499 const char *ctx, bool allow_type,
500 bool keep_metatable)
501{
502 if (allow_type && !keep_metatable && lua_isstring(state, objidx))
503 {
504 int idx = luaL_checkoption(state, objidx, NULL, primitive_types);
505 return primitive_identities[idx];
506 }
507
508 if (!lua_getmetatable(state, objidx))
509 luaL_error(state, "Invalid object in %s", ctx);
510
511 if (!allow_type && !lua_isuserdata(state, objidx))
512 luaL_error(state, "Object expected in %s", ctx);
513
514 if (!is_valid_metatable(state, objidx, -1))
515 luaL_error(state, "Invalid object metatable in %s", ctx);
516
517 // Extract identity from metatable
518 lua_rawgetp(state, -1, &DFHACK_IDENTITY_FIELD_TOKEN);
519
520 type_identity *id = (type_identity*)lua_touserdata(state, -1);
521 if (!id)
522 luaL_error(state, "Invalid object identity in %s", ctx);
523
524 lua_pop(state, keep_metatable ? 1 : 2);
525 return id;
526}
527
528static bool check_type_compatible(lua_State *state, int obj1, int obj2,
529 const type_identity **type1, const type_identity **type2,

Callers

nothing calls this directly

Calls 8

lua_isstringFunction · 0.85
luaL_checkoptionFunction · 0.85
lua_getmetatableFunction · 0.85
luaL_errorFunction · 0.85
lua_isuserdataFunction · 0.85
is_valid_metatableFunction · 0.85
lua_rawgetpFunction · 0.85
lua_touserdataFunction · 0.85

Tested by

no test coverage detected