diff --git a/Library/MeasureScript.cpp b/Library/MeasureScript.cpp index 7fbf15ab..78fa9454 100644 --- a/Library/MeasureScript.cpp +++ b/Library/MeasureScript.cpp @@ -117,7 +117,7 @@ void MeasureScript::ReadOptions(ConfigParser& parser, const WCHAR* section) bool hasInitializeFunction = m_LuaScript.IsFunction(g_InitializeFunctionName); m_HasUpdateFunction = m_LuaScript.IsFunction(g_UpdateFunctionName); - lua_State* L = m_LuaScript.GetState(); + auto L = m_LuaScript.GetState(); lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript.GetRef()); *(MeterWindow**)lua_newuserdata(L, sizeof(MeterWindow*)) = m_MeterWindow; diff --git a/Library/lua/LuaManager.cpp b/Library/lua/LuaManager.cpp index 6afde4c8..e82ff37a 100644 --- a/Library/lua/LuaManager.cpp +++ b/Library/lua/LuaManager.cpp @@ -24,7 +24,7 @@ int LuaManager::c_RefCount = 0; lua_State* LuaManager::c_State = 0; -bool LuaManager::c_UnicodeState = false; +std::vector LuaManager::c_UnicodeStateStack; void LuaManager::Initialize() { @@ -73,14 +73,14 @@ void LuaManager::ReportErrors(const std::wstring& file) } std::wstring str(file, file.find_last_of(L'\\') + 1); - str += c_UnicodeState ? StringUtil::WidenUTF8(error) : StringUtil::Widen(error); + str += IsUnicodeState() ? StringUtil::WidenUTF8(error) : StringUtil::Widen(error); LogErrorF(L"Script: %s", str.c_str()); } void LuaManager::PushWide(const WCHAR* str) { lua_State* L = c_State; - const std::string narrowStr = c_UnicodeState ? + const std::string narrowStr = IsUnicodeState() ? StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str); lua_pushlstring(L, narrowStr.c_str(), narrowStr.length()); } @@ -88,7 +88,7 @@ void LuaManager::PushWide(const WCHAR* str) void LuaManager::PushWide(const std::wstring& str) { lua_State* L = c_State; - const std::string narrowStr = c_UnicodeState ? + const std::string narrowStr = IsUnicodeState() ? StringUtil::NarrowUTF8(str) : StringUtil::Narrow(str); lua_pushlstring(L, narrowStr.c_str(), narrowStr.length()); } @@ -98,6 +98,6 @@ std::wstring LuaManager::ToWide(int narg) lua_State* L = c_State; size_t strLen = 0; const char* str = lua_tolstring(L, narg, &strLen); - return c_UnicodeState ? + return IsUnicodeState() ? StringUtil::WidenUTF8(str, (int)strLen) : StringUtil::Widen(str, (int)strLen); } diff --git a/Library/lua/LuaManager.h b/Library/lua/LuaManager.h index b2eb3f13..593cd583 100644 --- a/Library/lua/LuaManager.h +++ b/Library/lua/LuaManager.h @@ -26,15 +26,25 @@ extern "C" #include "lauxlib.h" } +#include + class LuaManager { public: + class ScopedLuaState + { + public: + ScopedLuaState(bool unicode) { LuaManager::c_UnicodeStateStack.push_back(unicode); } + ~ScopedLuaState() { LuaManager::c_UnicodeStateStack.pop_back(); } + operator lua_State*() { return LuaManager::c_State; } + }; + static void Initialize(); static void Finalize(); - static lua_State* GetState(bool unicode) { c_UnicodeState = unicode; return c_State; } + static ScopedLuaState GetState(bool unicode) { return ScopedLuaState(unicode); } - static bool IsUnicodeState() { return c_UnicodeState; } + static bool IsUnicodeState() { return c_UnicodeStateStack.back(); } static void ReportErrors(const std::wstring& file); @@ -53,9 +63,10 @@ private: static void RegisterMeterWindow(lua_State* L); static void RegisterMeterString(lua_State* L); - // If set true |true|, Lua strings converted to/from as if they were encoded in UTF-8. Otherwise - // Lua strings are treated as if they are encoded in the default system encoding. - static bool c_UnicodeState; + // If the back of the vector is |true|, Lua strings converted to/from as if they were encoded + // in UTF-8. Otherwise Lua strings are treated as if they are encoded in the default system + // encoding. + static std::vector c_UnicodeStateStack; }; #endif diff --git a/Library/lua/LuaScript.cpp b/Library/lua/LuaScript.cpp index e98f043e..fe3b06d2 100644 --- a/Library/lua/LuaScript.cpp +++ b/Library/lua/LuaScript.cpp @@ -55,7 +55,7 @@ bool LuaScript::Initialize(const std::wstring& scriptFile) fclose(file); file = nullptr; - lua_State* L = GetState(); + auto L = GetState(); bool scriptLoaded = false; // Treat the script as Unicode if it has the UTF-16 LE BOM. @@ -122,7 +122,7 @@ bool LuaScript::Initialize(const std::wstring& scriptFile) void LuaScript::Uninitialize() { - lua_State* L = GetState(); + auto L = GetState(); if (m_Ref != LUA_NOREF) { @@ -138,7 +138,7 @@ void LuaScript::Uninitialize() */ bool LuaScript::IsFunction(const char* funcName) { - lua_State* L = GetState(); + auto L = GetState(); bool bExists = false; if (IsInitialized()) @@ -164,7 +164,7 @@ bool LuaScript::IsFunction(const char* funcName) */ void LuaScript::RunFunction(const char* funcName) { - lua_State* L = GetState(); + auto L = GetState(); if (IsInitialized()) { @@ -189,7 +189,7 @@ void LuaScript::RunFunction(const char* funcName) */ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std::wstring& strValue) { - lua_State* L = GetState(); + auto L = GetState(); int type = LUA_TNIL; if (IsInitialized()) @@ -234,7 +234,7 @@ int LuaScript::RunFunctionWithReturn(const char* funcName, double& numValue, std */ void LuaScript::RunString(const std::wstring& str) { - lua_State* L = GetState(); + auto L = GetState(); if (IsInitialized()) { diff --git a/Library/lua/LuaScript.h b/Library/lua/LuaScript.h index a97279cc..af47a87f 100644 --- a/Library/lua/LuaScript.h +++ b/Library/lua/LuaScript.h @@ -35,7 +35,7 @@ public: int GetRef() { return m_Ref; } bool IsUnicode() const { return m_Unicode; } - lua_State* GetState() { return LuaManager::GetState(m_Unicode); } + LuaManager::ScopedLuaState GetState() { return LuaManager::GetState(m_Unicode); } bool IsFunction(const char* funcName); void RunFunction(const char* funcName);