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

Function PushArray

Plugins/UnLua/Source/UnLua/Private/UnLuaBase.cpp:352–395  ·  view source on GitHub ↗

* Push an untyped dynamic array (same memory layout with TArray) */

Source from the content-addressed store, hash-verified

350 * Push an untyped dynamic array (same memory layout with TArray)
351 */
352 int32 PushArray(lua_State *L, const FScriptArray *ScriptArray, TSharedPtr<ITypeInterface> TypeInterface, bool bCreateCopy)
353 {
354 if (!L || !ScriptArray || !TypeInterface)
355 {
356 return 0;
357 }
358
359 const auto Registry = FLuaEnv::FindEnvChecked(L).GetContainerRegistry();
360 if (bCreateCopy)
361 {
362 int32 Num = ScriptArray->Num();
363 int32 ElementSize = TypeInterface->GetSize();
364
365 const auto LuaArray = Registry->NewArray(L, TypeInterface, FLuaArray::OwnedBySelf);
366 FScriptArray *DestScriptArray = LuaArray->GetContainerPtr(); // create a new FScriptArray
367
368#if ENGINE_MAJOR_VERSION >=5
369 DestScriptArray->Empty(Num, ElementSize, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
370 DestScriptArray->Add(Num, ElementSize, __STDCPP_DEFAULT_NEW_ALIGNMENT__);
371#else
372 DestScriptArray->Empty(Num, ElementSize);
373 DestScriptArray->Add(Num, ElementSize);
374#endif
375
376 if (Num)
377 {
378 uint8 *SrcData = (uint8*)ScriptArray->GetData();
379 uint8 *DestData = (uint8*)DestScriptArray->GetData();
380 for (int32 i = 0; i < Num; ++i)
381 {
382 TypeInterface->Initialize(DestData);
383 TypeInterface->Copy(DestData, SrcData); // copy data
384 SrcData += ElementSize;
385 DestData += ElementSize;
386 }
387 }
388 }
389 else
390 {
391 // get the cached array or create a new one if not found
392 Registry->FindOrAdd(L, (FScriptArray*)ScriptArray, TypeInterface);
393 }
394 return 1;
395 }
396
397 /**
398 * Push an untyped set (same memory layout with TSet). see PushArray

Callers 1

PushFunction · 0.85

Calls 10

NewArrayMethod · 0.80
FindOrAddMethod · 0.80
NumMethod · 0.45
GetSizeMethod · 0.45
GetContainerPtrMethod · 0.45
EmptyMethod · 0.45
AddMethod · 0.45
GetDataMethod · 0.45
InitializeMethod · 0.45
CopyMethod · 0.45

Tested by

no test coverage detected