Stability fix for overrides
@@ -102,6 +102,9 @@ bool FunctionOverride::HookRunPureScriptFunction(RED4ext::CClassFunction* apFunc
if (!chain.IsEmpty)
{
+ auto lockedState = chain.pScripting->GetState();
+ auto& luaState = lockedState.Get();
+
TiltedPhoques::StackAllocator<1 << 13> s_allocator;
auto pAllocator = TiltedPhoques::Allocator::Get();
@@ -110,14 +113,12 @@ bool FunctionOverride::HookRunPureScriptFunction(RED4ext::CClassFunction* apFunc
TiltedPhoques::Vector<RED4ext::CStackType> outArgs;
TiltedPhoques::Allocator::Set(pAllocator);
- auto state = chain.pScripting->GetState();
-
auto pContext = apStack->GetContext();
if (!apFunction->flags.isStatic && pContext)
{
const auto weak = RED4ext::WeakHandle<RED4ext::IScriptable>(
*(RED4ext::WeakHandle<RED4ext::IScriptable>*)&pContext->ref);
- auto obj = sol::make_object(state.Get(), WeakReference(state, weak));
+ auto obj = sol::make_object(luaState, WeakReference(lockedState, weak));
args.push_back(obj);
}
@@ -130,7 +131,7 @@ bool FunctionOverride::HookRunPureScriptFunction(RED4ext::CClassFunction* apFunc
arg.type = p->type;
arg.value = pOffset;
- args.push_back(Scripting::ToLua(state, arg));
+ args.push_back(Scripting::ToLua(lockedState, arg));
if (p->flags.isOut)
outArgs.push_back(arg);
@@ -171,7 +172,6 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
}
const auto& chain = itor->second;
- bool isOverridden = false;
// Save state so we can rollback to it after we popped for ourself
auto* pCode = apFrame->code;
@@ -179,6 +179,9 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
if (!chain.IsEmpty)
{
+ auto lockedState = chain.pScripting->GetState();
+ auto& luaState = lockedState.Get();
+
// Cheap allocation
TiltedPhoques::StackAllocator<1 << 13> s_allocator;
@@ -188,8 +191,6 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
TiltedPhoques::Vector<RED4ext::CStackType> outArgs;
TiltedPhoques::Allocator::Set(pAllocator);
- auto state = chain.pScripting->GetState();
-
RED4ext::CStackType self;
if (!apFunction->flags.isStatic)
@@ -207,7 +208,7 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
const auto ref = (RED4ext::WeakHandle<RED4ext::IScriptable>*)&((RED4ext::IScriptable*)self.value)->ref;
const auto weak = RED4ext::WeakHandle<RED4ext::IScriptable>(*ref);
- auto obj = sol::make_object(state.Get(), WeakReference(state, weak));
+ auto obj = sol::make_object(luaState, WeakReference(lockedState, weak));
args.push_back(obj);
}
@@ -245,7 +246,7 @@ void FunctionOverride::HandleOverridenFunction(RED4ext::IScriptable* apContext,
const auto opcode = *(apFrame->code++);
RED4ext::OpcodeHandlers::Run(opcode, (RED4ext::IScriptable*)apFrame->context, apFrame, pInstance, isScriptRef ? pInstance : nullptr);
- args.push_back(Scripting::ToLua(state, arg));
+ args.push_back(Scripting::ToLua(lockedState, arg));
if (pArg->flags.isOut)
{
