Script: Improve 6560518

The Unicode state is not tracked using a stack instead of a raw boolean. This means that a Unicode script that e.g. updates a non-Unicode script measure will now work properly.
This commit is contained in:
Birunthan Mohanathas 2013-08-07 17:48:13 +03:00
parent 20393df751
commit a3efbbac3f
5 changed files with 29 additions and 18 deletions

View File

@ -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;

View File

@ -24,7 +24,7 @@
int LuaManager::c_RefCount = 0;
lua_State* LuaManager::c_State = 0;
bool LuaManager::c_UnicodeState = false;
std::vector<bool> 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);
}

View File

@ -26,15 +26,25 @@ extern "C"
#include "lauxlib.h"
}
#include <vector>
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<bool> c_UnicodeStateStack;
};
#endif

View File

@ -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())
{

View File

@ -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);