Merge branch 'lua_callback' into 'master'

Support multiple arguments in Lua callbacks

See merge request OpenMW/openmw!1416
fix-static-urls
uramer 3 years ago
commit ec52a9bea0

@ -440,4 +440,24 @@ return {
EXPECT_EQ(counter4, 25);
}
TEST_F(LuaScriptsContainerTest, CallbackWrapper)
{
LuaUtil::Callback callback{mLua.sol()["print"], mLua.newTable()};
callback.mHiddenData[LuaUtil::ScriptsContainer::sScriptDebugNameKey] = "some_script.lua";
callback.mHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = LuaUtil::ScriptsContainer::ScriptId{nullptr, 0};
testing::internal::CaptureStdout();
callback(1.5);
EXPECT_EQ(internal::GetCapturedStdout(), "1.5\n");
testing::internal::CaptureStdout();
callback(1.5, 2.5);
EXPECT_EQ(internal::GetCapturedStdout(), "1.5\t2.5\n");
testing::internal::CaptureStdout();
callback.mHiddenData[LuaUtil::ScriptsContainer::sScriptIdKey] = sol::nil;
callback(1.5, 2.5);
EXPECT_EQ(internal::GetCapturedStdout(), "Ignored callback to the removed script some_script.lua\n");
}
}

@ -530,13 +530,4 @@ namespace LuaUtil
updateTimerQueue(mHoursTimersQueue, gameHours);
}
void Callback::operator()(sol::object arg) const
{
if (mHiddenData[ScriptsContainer::sScriptIdKey] != sol::nil)
LuaUtil::call(mFunc, std::move(arg));
else
Log(Debug::Debug) << "Ignored callback to the removed script "
<< mHiddenData.get<std::string>(ScriptsContainer::sScriptDebugNameKey);
}
}

@ -242,7 +242,7 @@ namespace LuaUtil
int64_t mTemporaryCallbackCounter = 0;
};
// Wrapper for a single-argument Lua function.
// Wrapper for a Lua function.
// Holds information about the script the function belongs to.
// Needed to prevent callback calls if the script was removed.
struct Callback
@ -250,7 +250,15 @@ namespace LuaUtil
sol::function mFunc;
sol::table mHiddenData; // same object as Script::mHiddenData in ScriptsContainer
void operator()(sol::object arg) const;
template <typename... Args>
void operator()(Args&&... args) const
{
if (mHiddenData[ScriptsContainer::sScriptIdKey] != sol::nil)
LuaUtil::call(mFunc, std::forward<Args>(args)...);
else
Log(Debug::Debug) << "Ignored callback to the removed script "
<< mHiddenData.get<std::string>(ScriptsContainer::sScriptDebugNameKey);
}
};
}

Loading…
Cancel
Save