1
0
forked from mirrors/0ad

Fix conversion of UTF8 strings between the scripts and the engine.

SpiderMonkey uses UTF16 internally, and only provides APIs for that
encoding, so stop hacking UTF8 strings: properly convert them to and
from UTF16 when passing them through SM.

Patch By: wraitii
Differential Revision: https://code.wildfiregames.com/D2838
This was SVN commit r23795.
This commit is contained in:
Itms
2020-06-30 10:46:06 +00:00
parent dc65912043
commit 56d3aa40fe
2 changed files with 32 additions and 19 deletions
+5 -16
View File
@@ -158,16 +158,10 @@ template<> bool ScriptInterface::FromJSVal<Path>(JSContext* cx, JS::HandleValue
template<> bool ScriptInterface::FromJSVal<std::string>(JSContext* cx, JS::HandleValue v, std::string& out)
{
JSAutoRequest rq(cx);
WARN_IF_NOT(v.isString() || v.isNumber(), v); // allow implicit number conversions
JS::RootedString str(cx, JS::ToString(cx, v));
if (!str)
FAIL("Argument must be convertible to a string");
char* ch = JS_EncodeString(cx, str); // chops off high byte of each char16_t
if (!ch)
FAIL("JS_EncodeString failed"); // out of memory
out.assign(ch, ch + JS_GetStringLength(str));
JS_free(cx, ch);
std::wstring wideout;
if (!FromJSVal(cx, v, wideout))
return false;
out = CStrW(wideout).ToUTF8();
return true;
}
@@ -265,12 +259,7 @@ template<> void ScriptInterface::ToJSVal<Path>(JSContext* cx, JS::MutableHandleV
template<> void ScriptInterface::ToJSVal<std::string>(JSContext* cx, JS::MutableHandleValue ret, const std::string& val)
{
JSAutoRequest rq(cx);
JS::RootedString str(cx, JS_NewStringCopyN(cx, val.c_str(), val.length()));
if (str)
ret.setString(str);
else
ret.setUndefined();
ToJSVal(cx, ret, static_cast<const std::wstring>(CStr(val).FromUTF8()));
}
template<> void ScriptInterface::ToJSVal<const wchar_t*>(JSContext* cx, JS::MutableHandleValue ret, const wchar_t* const& val)