1
0
forked from 0ad/0ad

Adds UTF-8 support for JavaScript files.

This was SVN commit r11750.
This commit is contained in:
historic_bruno 2012-05-04 21:48:46 +00:00
parent a5713c1264
commit 32d2927449
11 changed files with 26 additions and 31 deletions

View File

@ -999,7 +999,7 @@ int CXMLReader::ProgressiveRead()
else if (name == "Script")
{
if (m_MapReader.pSimulation2)
m_MapReader.pSimulation2->SetStartupScript(node.GetText().FromUTF8());
m_MapReader.pSimulation2->SetStartupScript(node.GetText());
}
else
{

View File

@ -1293,7 +1293,7 @@ void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObjec
throw PSERROR_GUI_JSOpenFailed();
}
code = scriptfile.GetAsString();
code = scriptfile.DecodeUTF8(); // assume it's UTF-8
}
// Read the inline code (concatenating to the file code, if both are specified)

View File

@ -156,9 +156,7 @@ void RunHardwareDetection()
return;
}
Status err; // ignore encoding errors
std::wstring code = wstring_from_utf8(file.GetAsString(), &err);
std::string code = file.DecodeUTF8(); // assume it's UTF-8
scriptInterface.LoadScript(scriptName, code);
// Collect all the settings we'll pass to the script:

View File

@ -877,11 +877,11 @@ bool ScriptInterface::FreezeObject(jsval obj, bool deep)
return JS_FreezeObject(m->m_cx, JSVAL_TO_OBJECT(obj)) ? true : false;
}
bool ScriptInterface::LoadScript(const VfsPath& filename, const std::wstring& code)
bool ScriptInterface::LoadScript(const VfsPath& filename, const std::string& code)
{
// Compile the code in strict mode, to encourage better coding practices and
// to possibly help SpiderMonkey with optimisations
std::wstring codeStrict = L"\"use strict\";\n" + code;
std::wstring codeStrict = L"\"use strict\";\n" + wstring_from_utf8(code);
utf16string codeUtf16(codeStrict.begin(), codeStrict.end());
uintN lineNo = 0; // put the automatic 'use strict' on line 0, so the real code starts at line 1
@ -898,11 +898,11 @@ bool ScriptInterface::LoadScript(const VfsPath& filename, const std::wstring& co
return ok ? true : false;
}
bool ScriptInterface::LoadGlobalScript(const VfsPath& filename, const std::wstring& code)
bool ScriptInterface::LoadGlobalScript(const VfsPath& filename, const std::string& code)
{
// Compile the code in strict mode, to encourage better coding practices and
// to possibly help SpiderMonkey with optimisations
std::wstring codeStrict = L"\"use strict\";\n" + code;
std::wstring codeStrict = L"\"use strict\";\n" + wstring_from_utf8(code);
utf16string codeUtf16(codeStrict.begin(), codeStrict.end());
uintN lineNo = 0; // put the automatic 'use strict' on line 0, so the real code starts at line 1
@ -932,8 +932,7 @@ bool ScriptInterface::LoadGlobalScriptFile(const VfsPath& path)
return false;
}
std::string content(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize());
std::wstring code = wstring_from_utf8(content);
std::wstring code = wstring_from_utf8(file.DecodeUTF8()); // assume it's UTF-8
// Compile the code in strict mode, to encourage better coding practices and
// to possibly help SpiderMonkey with optimisations

View File

@ -249,7 +249,7 @@ public:
* @param code JS code to execute
* @return true on successful compilation and execution; false otherwise
*/
bool LoadScript(const VfsPath& filename, const std::wstring& code);
bool LoadScript(const VfsPath& filename, const std::string& code);
/**
* Load and execute the given script in the global scope.
@ -257,7 +257,7 @@ public:
* @param code JS code to execute
* @return true on successful compilation and execution; false otherwise
*/
bool LoadGlobalScript(const VfsPath& filename, const std::wstring& code);
bool LoadGlobalScript(const VfsPath& filename, const std::string& code);
/**
* Load and execute the given script in the global scope.

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2012 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -30,7 +30,7 @@ public:
{
ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime());
TestLogger logger;
TS_ASSERT(script.LoadScript(L"test.js", L"var x = 1+1;"));
TS_ASSERT(script.LoadScript(L"test.js", "var x = 1+1;"));
TS_ASSERT_WSTR_NOT_CONTAINS(logger.GetOutput(), L"JavaScript error");
TS_ASSERT_WSTR_NOT_CONTAINS(logger.GetOutput(), L"JavaScript warning");
}
@ -39,7 +39,7 @@ public:
{
ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime());
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", L"1+"));
TS_ASSERT(!script.LoadScript(L"test.js", "1+"));
TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript error: test.js line 1\nSyntaxError: syntax error");
}
@ -47,7 +47,7 @@ public:
{
ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime());
TestLogger logger;
TS_ASSERT(script.LoadScript(L"test.js", L"1+1;"));
TS_ASSERT(script.LoadScript(L"test.js", "1+1;"));
TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript warning: test.js line 1\nuseless expression");
}
@ -55,7 +55,7 @@ public:
{
ScriptInterface script("Test", "Test", ScriptInterface::CreateRuntime());
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", L"with(1){}"));
TS_ASSERT(!script.LoadScript(L"test.js", "with(1){}"));
TS_ASSERT_WSTR_CONTAINS(logger.GetOutput(), L"JavaScript error: test.js line 1\nSyntaxError: strict mode code may not contain \'with\' statements");
}

View File

@ -152,7 +152,7 @@ public:
double m_DeltaTime;
float m_LastFrameOffset;
std::wstring m_StartupScript;
std::string m_StartupScript;
CScriptValRooted m_InitAttributes;
CScriptValRooted m_MapSettings;
@ -681,12 +681,12 @@ bool CSimulation2::LoadDefaultScripts()
return m->LoadDefaultScripts(m->m_ComponentManager, &m->m_LoadedScripts);
}
void CSimulation2::SetStartupScript(const std::wstring& code)
void CSimulation2::SetStartupScript(const std::string& code)
{
m->m_StartupScript = code;
}
const std::wstring& CSimulation2::GetStartupScript()
const std::string& CSimulation2::GetStartupScript()
{
return m->m_StartupScript;
}

View File

@ -83,12 +83,12 @@ public:
/**
* Set a startup script, which will get executed before the first turn.
*/
void SetStartupScript(const std::wstring& script);
void SetStartupScript(const std::string& script);
/**
* Get the current startup script.
*/
const std::wstring& GetStartupScript();
const std::string& GetStartupScript();
/**
* Set the attributes identifying the scenario/RMS used to initialise this

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2012 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -39,9 +39,8 @@ public:
{
CVFSFile file;
TS_ASSERT_EQUALS(file.Load(g_VFS, pathname), PSRETURN_OK);
CStr content = file.GetAsString();
std::wstring wcontent(content.begin(), content.end());
TSM_ASSERT(L"Running script "+pathname.string(), scriptInterface.LoadScript(pathname, wcontent));
CStr content = file.DecodeUTF8(); // assume it's UTF-8
TSM_ASSERT(L"Running script "+pathname.string(), scriptInterface.LoadScript(pathname, content));
}
static void Script_LoadComponentScript(void* cbdata, VfsPath pathname)

View File

@ -137,7 +137,7 @@ bool CComponentManager::LoadScript(const VfsPath& filename, bool hotload)
CVFSFile file;
PSRETURN loadOk = file.Load(g_VFS, filename);
ENSURE(loadOk == PSRETURN_OK); // TODO
std::wstring content(file.GetBuffer(), file.GetBuffer() + file.GetBufferSize()); // TODO: encodings etc
std::string content = file.DecodeUTF8(); // assume it's UTF-8
bool ok = m_ScriptInterface.LoadScript(filename, content);
return ok;
}

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2010 Wildfire Games.
/* Copyright (C) 2012 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -132,7 +132,6 @@ void ScriptTestSetup(ScriptInterface& ifc)
std::ifstream ifs(OsString(path).c_str());
ENSURE(ifs.good());
std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
std::wstring wcontent(content.begin(), content.end());
bool ok = ifc.LoadScript(L"test_setup.js", wcontent);
bool ok = ifc.LoadScript(L"test_setup.js", content);
ENSURE(ok);
}