Permalink
Browse files

Adds UTF-8 support for JavaScript files.

git-svn-id: http://svn.wildfiregames.com/public/ps/trunk@11750 3db68df2-c116-0410-a063-a993310a9797
  • Loading branch information...
1 parent 88ce4d5 commit 78588fe73fe6ed39680f0c9c1ab1d89431c9b172 ben committed May 4, 2012
@@ -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
@@ -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)
@@ -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:
@@ -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
@@ -249,15 +249,15 @@ class ScriptInterface
* @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.
* @param filename Name for debugging purposes (not used to load the file)
* @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.
@@ -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 @@ class TestScriptInterface : public CxxTest::TestSuite
{
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,23 +39,23 @@ class TestScriptInterface : public CxxTest::TestSuite
{
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");
}
void test_loadscript_strict_warning()
{
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");
}
void test_loadscript_strict_error()
{
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");
}
@@ -152,7 +152,7 @@ class CSimulation2Impl
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;
}
@@ -83,12 +83,12 @@ class CSimulation2
/**
* 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
@@ -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 @@ class TestComponentScripts : public CxxTest::TestSuite
{
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)
@@ -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
@@ -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);
}

0 comments on commit 78588fe

Please sign in to comment.