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

Method get_object_internal

library/LuaWrapper.cpp:350–397  ·  view source on GitHub ↗

* Verify that the value matches the identity, and return ptr if so. */

Source from the content-addressed store, hash-verified

348 * Verify that the value matches the identity, and return ptr if so.
349 */
350void *LuaWrapper::get_object_internal(lua_State *state, const type_identity *type, int val_index, bool exact_type, bool in_method)
351{
352 /*
353 * Non-userdata results in NULL; nil for NULL gets handled here too.
354 */
355 if (!lua_isuserdata(state, val_index))
356 return NULL;
357
358 /*
359 * Light user data is allowed with null type; otherwise bail out.
360 */
361 if (!lua_getmetatable(state, val_index)) // () -> metatable?
362 {
363 if (!type && lua_islightuserdata(state, val_index))
364 return lua_touserdata(state, val_index);
365
366 return NULL;
367 }
368
369 /*
370 * Verify that the metatable is known, and refers to the correct type.
371 * Here doing reverse lookup of identity by metatable.
372 */
373 if (!LookupTypeInfo(state, in_method)) // metatable -> type?
374 return NULL;
375
376 if (type && lua_touserdata(state, -1) != type)
377 {
378 /*
379 * If valid but different type, do an intelligent comparison.
380 */
381 lua_pop(state, 1); // type -> ()
382 lua_getmetatable(state, val_index);
383
384 if (!is_type_compatible(state, type, 0, lua_gettop(state), exact_type))
385 {
386 lua_pop(state, 1); // metatable -> ()
387 return NULL;
388 }
389 }
390
391 lua_pop(state, 1); // type -> ()
392
393 /*
394 * Finally decode the reference.
395 */
396 return get_object_ref(state, val_index);
397}
398
399/**
400 * Check if the object and metatable are a valid DF reference or type.

Callers

nothing calls this directly

Calls 5

lua_isuserdataFunction · 0.85
lua_getmetatableFunction · 0.85
lua_touserdataFunction · 0.85
is_type_compatibleFunction · 0.85
lua_gettopFunction · 0.85

Tested by

no test coverage detected