Skip to content

Commit

Permalink
Merge pull request Tencent#145 from skys712/master
Browse files Browse the repository at this point in the history
fix DynamicBinding failed in Loop Call NewObject
  • Loading branch information
rowechien authored Apr 4, 2020
2 parents bfabb2b + c031661 commit 268fc57
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
32 changes: 21 additions & 11 deletions Plugins/UnLua/Source/UnLua/Private/LuaDynamicBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,34 @@ bool FLuaDynamicBinding::IsValid(UClass *InClass) const
return Class && Class == InClass && ModuleName.Len() > 0;
}

bool FLuaDynamicBinding::Setup(UClass *InClass, const TCHAR *InModuleName, int32 InInitializerTableRef)
bool FLuaDynamicBinding::Push(UClass *InClass, const TCHAR *InModuleName, int32 InInitializerTableRef)
{
if (!InClass || (Class && Class != InClass) || (ModuleName.Len() > 0 && ModuleName != InModuleName) || (!InModuleName && InInitializerTableRef == INDEX_NONE))
{
return false;
}
FLuaDynamicBindingStackNode StackNode;

StackNode.Class = Class;
StackNode.ModuleName = ModuleName;
StackNode.InitializerTableRef = InitializerTableRef;

Stack.Push(StackNode);

Class = InClass;
ModuleName = InModuleName;
InitializerTableRef = InInitializerTableRef;

return true;
}

int32 FLuaDynamicBinding::Cleanup()
int32 FLuaDynamicBinding::Pop()
{
Class = nullptr;
ModuleName.Empty();
check(Stack.Num() > 0);

FLuaDynamicBindingStackNode StackNode = Stack.Pop();
int32 TableRef = InitializerTableRef;
InitializerTableRef = INDEX_NONE;

Class = StackNode.Class;
ModuleName = StackNode.ModuleName;
InitializerTableRef = StackNode.InitializerTableRef;

return TableRef;
}

Expand All @@ -49,15 +59,15 @@ FScopedLuaDynamicBinding::FScopedLuaDynamicBinding(lua_State *InL, UClass *Class
{
if (L)
{
bValid = GLuaDynamicBinding.Setup(Class, ModuleName, InitializerTableRef);
bValid = GLuaDynamicBinding.Push(Class, ModuleName, InitializerTableRef);
}
}

FScopedLuaDynamicBinding::~FScopedLuaDynamicBinding()
{
if (bValid)
{
int32 InitializerTableRef = GLuaDynamicBinding.Cleanup();
int32 InitializerTableRef = GLuaDynamicBinding.Pop();
if (InitializerTableRef != INDEX_NONE)
{
check(L);
Expand Down
14 changes: 12 additions & 2 deletions Plugins/UnLua/Source/UnLua/Private/LuaDynamicBinding.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ struct FLuaDynamicBinding
{}

bool IsValid(UClass *InClass) const;
bool Setup(UClass *InClass, const TCHAR *InModuleName, int32 InInitializerTableRef);
int32 Cleanup();

UClass *Class;
FString ModuleName;
int32 InitializerTableRef;

struct FLuaDynamicBindingStackNode
{
UClass *Class;
FString ModuleName;
int32 InitializerTableRef;
};

TArray<FLuaDynamicBindingStackNode> Stack;

bool Push(UClass *InClass, const TCHAR *InModuleName, int32 InInitializerTableRef);
int32 Pop();
};

extern FLuaDynamicBinding GLuaDynamicBinding;
Expand Down

0 comments on commit 268fc57

Please sign in to comment.