diff --git a/source/ps/CConsole.cpp b/source/ps/CConsole.cpp index 58aca29e1c..b3860040e1 100755 --- a/source/ps/CConsole.cpp +++ b/source/ps/CConsole.cpp @@ -457,19 +457,23 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){ std::map::iterator Iter; - if (szLine[0] == '\\'){ + if (szLine[0] == '\\') + { swscanf(szLine, L"\\%ls", szCommand); Trim(szCommand); ToLower(szCommand); - if (!wcscmp(szCommand, L"info")){ + if (!wcscmp(szCommand, L"info")) + { InsertMessage(L""); InsertMessage(L"[Information]"); InsertMessage(L" -View commands \"\\commands\""); InsertMessage(L" -Call command \"\\\""); InsertMessage(L" -Say \"\""); InsertMessage(L""); - }else if (!wcscmp(szCommand, L"commands")){ + } + else if (!wcscmp(szCommand, L"commands")) + { InsertMessage(L""); InsertMessage(L"[Commands]"); @@ -479,7 +483,9 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){ InsertMessage(L" \\%ls", Iter->first.data()); InsertMessage(L""); - }else{ + } + else + { Iter = m_mapFuncList.find(szCommand); if (Iter == m_mapFuncList.end()) InsertMessage(L"unknown command <%ls>", szCommand); @@ -487,22 +493,26 @@ void CConsole::ProcessBuffer(const wchar_t* szLine){ Iter->second(); } } - else if( szLine[0] == ':' ) + else if (szLine[0] == ':') { // Process it as JavaScript - // Convert Unicode to 8-bit sort-of-ASCII - g_ScriptingHost.ExecuteScript( CStr16( szLine + 1 ) ); } - else if( szLine[0] == '?' ) + else if (szLine[0] == '?') { // Process it as JavaScript and display the result jsval rval = g_ScriptingHost.ExecuteScript( CStr16( szLine + 1 ) ); - if( rval ) - InsertMessage( L"%hs", g_ScriptingHost.ValueToString( rval ).c_str() ); + if (rval) + { + try { + InsertMessage( L"%ls", g_ScriptingHost.ValueToUCString( rval ).c_str() ); + } catch (PSERROR_Scripting_ConversionFailed) { + InsertMessage( L"%hs", "" ); + } + } } else SendChatMessage(szLine); diff --git a/source/ps/scripting/JSInterface_Console.cpp b/source/ps/scripting/JSInterface_Console.cpp index 6a27390f8c..38632f2b09 100755 --- a/source/ps/scripting/JSInterface_Console.cpp +++ b/source/ps/scripting/JSInterface_Console.cpp @@ -85,18 +85,18 @@ JSBool JSI_Console::getConsole( JSContext* cx, JSObject* obj, jsval id, jsval* v JSBool JSI_Console::writeConsole( JSContext* UNUSEDPARAM(context), JSObject* UNUSEDPARAM(globalObject), unsigned int argc, jsval* argv, jsval* UNUSEDPARAM(rval) ) { assert( argc >= 1 ); - CStr output; + CStrW output; for( unsigned int i = 0; i < argc; i++ ) { try { - CStr arg = g_ScriptingHost.ValueToString( argv[0] ); + CStrW arg = g_ScriptingHost.ValueToUCString( argv[0] ); output += arg; } catch( PSERROR_Scripting_ConversionFailed ) { } } - g_Console->InsertMessage( L"%hs", (const char*)output ); + g_Console->InsertMessage( L"%ls", output.c_str() ); return( JS_TRUE ); } diff --git a/source/scripting/ScriptingHost.cpp b/source/scripting/ScriptingHost.cpp index b0843d2033..702c5a253e 100755 --- a/source/scripting/ScriptingHost.cpp +++ b/source/scripting/ScriptingHost.cpp @@ -279,9 +279,11 @@ bool ScriptingHost::ValueToBool(const jsval value) std::string ScriptingHost::ValueToString(const jsval value) { - JSString * string = JS_ValueToString(m_Context, value); + JSString* string = JS_ValueToString(m_Context, value); + if (string == NULL) + throw PSERROR_Scripting_ConversionFailed(); - char * bytes = JS_GetStringBytes(string); + char* bytes = JS_GetStringBytes(string); if (bytes == NULL) throw PSERROR_Scripting_ConversionFailed(); @@ -290,8 +292,15 @@ std::string ScriptingHost::ValueToString(const jsval value) CStrW ScriptingHost::ValueToUCString( const jsval value ) { - JSString* string = JS_ValueToString( m_Context, value ); - return( CStrW( JS_GetStringChars( string ) ) ); + JSString* string = JS_ValueToString(m_Context, value); + if (string == NULL) + throw PSERROR_Scripting_ConversionFailed(); + + jschar* chars = JS_GetStringChars(string); + if (chars == NULL) + throw PSERROR_Scripting_ConversionFailed(); + + return CStrW(chars); } jsval ScriptingHost::UCStringToValue(const utf16string &str)