MCPcopy Create free account
hub / github.com/Tencent/UnLua / GetUserdata

Function GetUserdata

Plugins/UnLua/Source/UnLua/Private/LuaCore.cpp:312–365  ·  view source on GitHub ↗

* Get the address of userdata * * @param Index - Lua stack index * @param[out] OutTwoLvlPtr - whether the address is a two level pointer * @param[out] OutClassMetatable - whether the userdata comes from a metatable * @return - the untyped dynamic array */

Source from the content-addressed store, hash-verified

310 * @return - the untyped dynamic array
311 */
312void* GetUserdata(lua_State *L, int32 Index, bool *OutTwoLvlPtr, bool *OutClassMetatable)
313{
314 // Index < LUA_REGISTRYINDEX => upvalues
315 if (Index < 0 && Index > LUA_REGISTRYINDEX)
316 {
317 int32 Top = lua_gettop(L);
318 Index = Top + Index + 1;
319 }
320
321 void *Userdata = nullptr;
322 bool bTwoLvlPtr = false, bClassMetatable = false;
323
324 int32 Type = lua_type(L, Index);
325 switch (Type)
326 {
327 case LUA_TTABLE:
328 {
329 lua_pushstring(L, "Object");
330 Type = lua_rawget(L, Index);
331 if (Type == LUA_TUSERDATA)
332 {
333 Userdata = lua_touserdata(L, -1); // get the raw UObject
334 }
335 else
336 {
337 lua_pop(L, 1);
338 lua_pushstring(L, "ClassDesc");
339 Type = lua_rawget(L, Index);
340 if (Type == LUA_TLIGHTUSERDATA)
341 {
342 Userdata = lua_touserdata(L, -1); // get the 'FClassDesc' pointer
343 bClassMetatable = true;
344 }
345 }
346 bTwoLvlPtr = true; // set two level pointer flag
347 lua_pop(L, 1);
348 }
349 break;
350 case LUA_TUSERDATA:
351 Userdata = GetUserdataFast(L, Index, &bTwoLvlPtr); // get the userdata pointer
352 break;
353 }
354
355 if (OutTwoLvlPtr)
356 {
357 *OutTwoLvlPtr = bTwoLvlPtr;
358 }
359 if (OutClassMetatable)
360 {
361 *OutClassMetatable = bClassMetatable;
362 }
363
364 return Userdata;
365}
366
367/**
368 * Get the address of userdata, fast path

Callers 3

GetCppInstanceFunction · 0.85
UObject_DeleteFunction · 0.85
CopyBackMethod · 0.85

Calls 6

lua_gettopFunction · 0.85
lua_typeFunction · 0.85
lua_pushstringFunction · 0.85
lua_rawgetFunction · 0.85
lua_touserdataFunction · 0.85
GetUserdataFastFunction · 0.85

Tested by

no test coverage detected