* Given a DF object reference or type, safely retrieve its identity pointer. */
| 496 | * Given a DF object reference or type, safely retrieve its identity pointer. |
| 497 | */ |
| 498 | const 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 | |
| 528 | static bool check_type_compatible(lua_State *state, int obj1, int obj2, |
| 529 | const type_identity **type1, const type_identity **type2, |
nothing calls this directly
no test coverage detected