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

Function TMap_ToTable

Plugins/UnLua/Source/UnLua/Private/BaseLib/LuaLib_Map.cpp:299–342  ·  view source on GitHub ↗

* Convert the map to a Lua table */

Source from the content-addressed store, hash-verified

297 * Convert the map to a Lua table
298 */
299static int32 TMap_ToTable(lua_State* L)
300{
301 int32 NumParams = lua_gettop(L);
302 if (NumParams != 1)
303 return luaL_error(L, "invalid parameters");
304
305 FLuaMap* Map = (FLuaMap*)(GetCppInstanceFast(L, 1));
306 TMap_Guard(L, Map);
307
308 if (!Map->KeyInterface->IsValid())
309 return luaL_error(L, TCHAR_TO_UTF8(*FString::Printf(TEXT("invalid TMap key type:%s"), *Map->ValueInterface->GetName())));
310
311 if (!Map->ValueInterface->IsValid())
312 return luaL_error(L, TCHAR_TO_UTF8(*FString::Printf(TEXT("invalid TMap value type:%s"), *Map->ValueInterface->GetName())));
313
314 void* MemData = FMemory::Malloc(sizeof(FLuaArray), alignof(FLuaArray));
315 FLuaArray* Keys = Map->Keys(MemData);
316 Keys->Inner->Initialize(Keys->ElementCache);
317 lua_newtable(L);
318 for (int32 i = 0; i < Keys->Num(); ++i)
319 {
320 Keys->Get(i, Keys->ElementCache);
321 Keys->Inner->Read(L, Keys->ElementCache, true);
322
323 void* ValueCache = (uint8*)Map->ElementCache + Map->MapLayout.ValueOffset;
324 Map->ValueInterface->Initialize(ValueCache);
325 bool bSuccess = Map->Find(Keys->ElementCache, ValueCache);
326 if (bSuccess)
327 {
328 Map->ValueInterface->Read(L, Map->ValueInterface->GetOffset() > 0 ? Map->ElementCache : ValueCache, true);
329 }
330 else
331 {
332 lua_pushnil(L);
333 }
334
335 lua_rawset(L, -3);
336
337 Map->ValueInterface->Destruct(ValueCache);
338 }
339 Keys->Inner->Destruct(Keys->ElementCache);
340 FMemory::Free(MemData);
341 return 1;
342}
343
344static const luaL_Reg TMapLib[] =
345{

Callers

nothing calls this directly

Calls 15

lua_gettopFunction · 0.85
luaL_errorFunction · 0.85
GetCppInstanceFastFunction · 0.85
TMap_GuardFunction · 0.85
lua_pushnilFunction · 0.85
lua_rawsetFunction · 0.85
KeysMethod · 0.80
IsValidMethod · 0.45
GetNameMethod · 0.45
InitializeMethod · 0.45
NumMethod · 0.45
GetMethod · 0.45

Tested by

no test coverage detected