* Convert the map to a Lua table */
| 297 | * Convert the map to a Lua table |
| 298 | */ |
| 299 | static 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 | |
| 344 | static const luaL_Reg TMapLib[] = |
| 345 | { |
nothing calls this directly
no test coverage detected