* Find a cached script container or create a new one * * @return - null if container is already cached, or the new created userdata otherwise */
| 494 | * @return - null if container is already cached, or the new created userdata otherwise |
| 495 | */ |
| 496 | void* CacheScriptContainer(lua_State *L, void *Key, const FScriptContainerDesc &Desc) |
| 497 | { |
| 498 | if (!Key) |
| 499 | { |
| 500 | UNLUA_LOGERROR(L, LogUnLua, Warning, TEXT("%s, Invalid key!"), ANSI_TO_TCHAR(__FUNCTION__)); |
| 501 | return nullptr; |
| 502 | } |
| 503 | |
| 504 | // return null if container is already cached, or create/cache/return a new ud |
| 505 | void *Userdata = nullptr; |
| 506 | lua_getfield(L, LUA_REGISTRYINDEX, "ScriptContainerMap"); |
| 507 | lua_pushlightuserdata(L, Key); |
| 508 | int32 Type = lua_rawget(L, -2); |
| 509 | if (Type == LUA_TNIL) |
| 510 | { |
| 511 | lua_pop(L, 1); |
| 512 | |
| 513 | Userdata = NewUserdataWithContainerTag(L, Desc.GetSize()); // create new userdata |
| 514 | luaL_setmetatable(L, Desc.GetName()); // set metatable |
| 515 | lua_pushlightuserdata(L, Key); |
| 516 | lua_pushvalue(L, -2); |
| 517 | lua_rawset(L, -4); // cache it in 'ScriptContainerMap' |
| 518 | UnLua::FLuaEnv::FindEnv(L)->GetDanglingCheck()->CaptureContainer(L, Key); |
| 519 | } |
| 520 | #if UE_BUILD_DEBUG |
| 521 | else |
| 522 | { |
| 523 | check(Type == LUA_TUSERDATA); |
| 524 | } |
| 525 | #endif |
| 526 | lua_remove(L, -2); |
| 527 | return Userdata; // return null if container is already cached, or the new created userdata otherwise |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Get a script container at the given stack index |
no test coverage detected