Index: ps/trunk/source/scriptinterface/ScriptConversions.cpp =================================================================== --- ps/trunk/source/scriptinterface/ScriptConversions.cpp (revision 23794) +++ ps/trunk/source/scriptinterface/ScriptConversions.cpp (revision 23795) @@ -1,384 +1,373 @@ /* Copyright (C) 2020 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "ScriptConversions.h" #include "graphics/Entity.h" #include "maths/Vector2D.h" #include "ps/utf16string.h" #include "ps/CStr.h" #define FAIL(msg) STMT(JS_ReportError(cx, msg); return false) // Implicit type conversions often hide bugs, so warn about them #define WARN_IF_NOT(c, v) STMT(if (!(c)) { JS_ReportWarning(cx, "Script value conversion check failed: %s (got type %s)", #c, InformalValueTypeName(v)); }) // TODO: SpiderMonkey: Follow upstream progresses about JS_InformalValueTypeName in the API // https://bugzilla.mozilla.org/show_bug.cgi?id=1285917 static const char* InformalValueTypeName(const JS::Value& v) { if (v.isObject()) return "object"; if (v.isString()) return "string"; if (v.isSymbol()) return "symbol"; if (v.isNumber()) return "number"; if (v.isBoolean()) return "boolean"; if (v.isNull()) return "null"; if (v.isUndefined()) return "undefined"; return "value"; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, bool& out) { JSAutoRequest rq(cx); WARN_IF_NOT(v.isBoolean(), v); out = JS::ToBoolean(v); return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, float& out) { JSAutoRequest rq(cx); double tmp; WARN_IF_NOT(v.isNumber(), v); if (!JS::ToNumber(cx, v, &tmp)) return false; out = tmp; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, double& out) { JSAutoRequest rq(cx); WARN_IF_NOT(v.isNumber(), v); if (!JS::ToNumber(cx, v, &out)) return false; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, i32& out) { JSAutoRequest rq(cx); WARN_IF_NOT(v.isNumber(), v); if (!JS::ToInt32(cx, v, &out)) return false; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, u32& out) { JSAutoRequest rq(cx); WARN_IF_NOT(v.isNumber(), v); if (!JS::ToUint32(cx, v, &out)) return false; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, u16& out) { JSAutoRequest rq(cx); WARN_IF_NOT(v.isNumber(), v); if (!JS::ToUint16(cx, v, &out)) return false; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, u8& out) { JSAutoRequest rq(cx); u16 tmp; WARN_IF_NOT(v.isNumber(), v); if (!JS::ToUint16(cx, v, &tmp)) return false; out = (u8)tmp; return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, std::wstring& 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"); if (JS_StringHasLatin1Chars(str)) { size_t length; JS::AutoCheckCannotGC nogc; const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(cx, nogc, str, &length); if (!ch) FAIL("JS_GetLatin1StringCharsAndLength failed"); out.assign(ch, ch + length); } else { size_t length; JS::AutoCheckCannotGC nogc; const char16_t* ch = JS_GetTwoByteStringCharsAndLength(cx, nogc, str, &length); if (!ch) FAIL("JS_GetTwoByteStringsCharsAndLength failed"); // out of memory out.assign(ch, ch + length); } return true; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, Path& out) { std::wstring string; if (!FromJSVal(cx, v, string)) return false; out = string; return true; } template<> bool ScriptInterface::FromJSVal(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; } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CStr8& out) { return ScriptInterface::FromJSVal(cx, v, static_cast(out)); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CStrW& out) { return ScriptInterface::FromJSVal(cx, v, static_cast(out)); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, Entity& out) { JSAutoRequest rq(cx); if (!v.isObject()) FAIL("Argument must be an object"); JS::RootedObject obj(cx, &v.toObject()); JS::RootedValue templateName(cx); JS::RootedValue id(cx); JS::RootedValue player(cx); JS::RootedValue position(cx); JS::RootedValue rotation(cx); // TODO: Report type errors if (!JS_GetProperty(cx, obj, "player", &player) || !FromJSVal(cx, player, out.playerID)) FAIL("Failed to read Entity.player property"); if (!JS_GetProperty(cx, obj, "templateName", &templateName) || !FromJSVal(cx, templateName, out.templateName)) FAIL("Failed to read Entity.templateName property"); if (!JS_GetProperty(cx, obj, "id", &id) || !FromJSVal(cx, id, out.entityID)) FAIL("Failed to read Entity.id property"); if (!JS_GetProperty(cx, obj, "position", &position) || !FromJSVal(cx, position, out.position)) FAIL("Failed to read Entity.position property"); if (!JS_GetProperty(cx, obj, "rotation", &rotation) || !FromJSVal(cx, rotation, out.rotation)) FAIL("Failed to read Entity.rotation property"); return true; } //////////////////////////////////////////////////////////////// // Primitive types: template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const bool& val) { ret.setBoolean(val); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const float& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const double& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const i32& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const u16& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const u8& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const u32& val) { ret.set(JS::NumberValue(val)); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const std::wstring& val) { JSAutoRequest rq(cx); utf16string utf16(val.begin(), val.end()); JS::RootedString str(cx, JS_NewUCStringCopyN(cx, reinterpret_cast (utf16.c_str()), utf16.length())); if (str) ret.setString(str); else ret.setUndefined(); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const Path& val) { ToJSVal(cx, ret, val.string()); } template<> void ScriptInterface::ToJSVal(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(CStr(val).FromUTF8())); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const wchar_t* const& val) { ToJSVal(cx, ret, std::wstring(val)); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const char* const& val) { JSAutoRequest rq(cx); JS::RootedString str(cx, JS_NewStringCopyZ(cx, val)); if (str) ret.setString(str); else ret.setUndefined(); } #define TOJSVAL_CHAR(N) \ template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const wchar_t (&val)[N]) \ { \ ToJSVal(cx, ret, static_cast(val)); \ } \ template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const char (&val)[N]) \ { \ ToJSVal(cx, ret, static_cast(val)); \ } TOJSVAL_CHAR(3) TOJSVAL_CHAR(5) TOJSVAL_CHAR(6) TOJSVAL_CHAR(7) TOJSVAL_CHAR(8) TOJSVAL_CHAR(9) TOJSVAL_CHAR(10) TOJSVAL_CHAR(11) TOJSVAL_CHAR(12) TOJSVAL_CHAR(13) TOJSVAL_CHAR(14) TOJSVAL_CHAR(15) TOJSVAL_CHAR(16) TOJSVAL_CHAR(17) TOJSVAL_CHAR(18) TOJSVAL_CHAR(19) TOJSVAL_CHAR(20) TOJSVAL_CHAR(24) TOJSVAL_CHAR(29) TOJSVAL_CHAR(33) TOJSVAL_CHAR(35) TOJSVAL_CHAR(256) #undef TOJSVAL_CHAR template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CStrW& val) { ToJSVal(cx, ret, static_cast(val)); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CStr8& val) { ToJSVal(cx, ret, static_cast(val)); } //////////////////////////////////////////////////////////////// // Compound types // Instantiate various vector types: JSVAL_VECTOR(int) JSVAL_VECTOR(u32) JSVAL_VECTOR(u16) JSVAL_VECTOR(std::string) JSVAL_VECTOR(std::wstring) JSVAL_VECTOR(std::vector) JSVAL_VECTOR(CStr8) JSVAL_VECTOR(std::vector) JSVAL_VECTOR(std::vector) class IComponent; template<> void ScriptInterface::ToJSVal >(JSContext* cx, JS::MutableHandleValue ret, const std::vector& val) { ToJSVal_vector(cx, ret, val); } template<> bool ScriptInterface::FromJSVal >(JSContext* cx, JS::HandleValue v, std::vector& out) { return FromJSVal_vector(cx, v, out); } template<> void ScriptInterface::ToJSVal(JSContext* cx, JS::MutableHandleValue ret, const CVector2D& val) { std::vector vec = {val.X, val.Y}; ToJSVal_vector(cx, ret, vec); } template<> bool ScriptInterface::FromJSVal(JSContext* cx, JS::HandleValue v, CVector2D& out) { std::vector vec; if (!FromJSVal_vector(cx, v, vec)) return false; if (vec.size() != 2) return false; out.X = vec[0]; out.Y = vec[1]; return true; } #undef FAIL #undef WARN_IF_NOT Index: ps/trunk/source/scriptinterface/tests/test_ScriptConversions.h =================================================================== --- ps/trunk/source/scriptinterface/tests/test_ScriptConversions.h (revision 23794) +++ ps/trunk/source/scriptinterface/tests/test_ScriptConversions.h (revision 23795) @@ -1,254 +1,278 @@ -/* Copyright (C) 2019 Wildfire Games. +/* Copyright (C) 2020 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "lib/self_test.h" #include "scriptinterface/ScriptInterface.h" #include "maths/Fixed.h" #include "maths/FixedVector2D.h" #include "maths/FixedVector3D.h" #include "maths/MathUtil.h" #include "ps/CLogger.h" #include "ps/Filesystem.h" #include "jsapi.h" class TestScriptConversions : public CxxTest::TestSuite { template void convert_to(const T& value, const std::string& expected) { ScriptInterface script("Test", "Test", g_ScriptRuntime); TS_ASSERT(script.LoadGlobalScripts()); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); JS::RootedValue v1(cx); ScriptInterface::ToJSVal(cx, &v1, value); // We want to convert values to strings, but can't just call toSource() on them // since they might not be objects. So just use uneval. std::string source; JS::RootedValue global(cx, script.GetGlobalObject()); TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); TS_ASSERT_STR_EQUALS(source, expected); } template void roundtrip(const T& value, const char* expected) { ScriptInterface script("Test", "Test", g_ScriptRuntime); TS_ASSERT(script.LoadGlobalScripts()); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); JS::RootedValue v1(cx); ScriptInterface::ToJSVal(cx, &v1, value); std::string source; JS::RootedValue global(cx, script.GetGlobalObject()); TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); if (expected) TS_ASSERT_STR_EQUALS(source, expected); T v2 = T(); TS_ASSERT(ScriptInterface::FromJSVal(cx, v1, v2)); TS_ASSERT_EQUALS(value, v2); } template void call_prototype_function(const T& u, const T& v, const std::string& func, const std::string& expected) { ScriptInterface script("Test", "Test", g_ScriptRuntime); TS_ASSERT(script.LoadGlobalScripts()); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); JS::RootedValue v1(cx); ScriptInterface::ToJSVal(cx, &v1, v); JS::RootedValue u1(cx); ScriptInterface::ToJSVal(cx, &u1, u); T r; JS::RootedValue r1(cx); TS_ASSERT(script.CallFunction(u1, func.c_str(), r, v1)); ScriptInterface::ToJSVal(cx, &r1, r); std::string source; JS::RootedValue global(cx, script.GetGlobalObject()); TS_ASSERT(script.CallFunction(global, "uneval", source, r1)); TS_ASSERT_STR_EQUALS(source, expected); } public: void setUp() { g_VFS = CreateVfs(); TS_ASSERT_OK(g_VFS->Mount(L"", DataDir()/"mods"/"_test.sim", VFS_MOUNT_MUST_EXIST)); } void tearDown() { g_VFS.reset(); } void test_roundtrip() { roundtrip(true, "true"); roundtrip(false, "false"); roundtrip(0, "0"); roundtrip(0.5, "0.5"); roundtrip(1e9f, "1000000000"); roundtrip(1e30f, "1.0000000150474662e+30"); roundtrip(0, "0"); roundtrip(123, "123"); roundtrip(-123, "-123"); roundtrip(JSVAL_INT_MAX - 1, "2147483646"); roundtrip(JSVAL_INT_MAX, "2147483647"); roundtrip(JSVAL_INT_MIN + 1, "-2147483647"); roundtrip(JSVAL_INT_MIN, "-2147483648"); roundtrip(0, "0"); roundtrip(123, "123"); roundtrip(JSVAL_INT_MAX - 1, "2147483646"); roundtrip(JSVAL_INT_MAX, "2147483647"); roundtrip(static_cast(JSVAL_INT_MAX) + 1, "2147483648"); std::string s1 = "test"; s1[1] = '\0'; std::string s2 = "тест"; s2[2] = s2[3] = '\0'; std::wstring w1 = L"test"; w1[1] = '\0'; std::wstring w2 = L"тест"; w2[1] = '\0'; roundtrip("", "\"\""); roundtrip("test", "\"test\""); - roundtrip("тест", "\"\\xD1\\x82\\xD0\\xB5\\xD1\\x81\\xD1\\x82\""); + roundtrip("тест", "\"\\u0442\\u0435\\u0441\\u0442\""); roundtrip(s1, "\"t\\x00st\""); - roundtrip(s2, "\"\\xD1\\x82\\x00\\x00\\xD1\\x81\\xD1\\x82\""); + roundtrip(s2, "\"\\u0442\\x00\\x00\\u0441\\u0442\""); roundtrip(L"", "\"\""); roundtrip(L"test", "\"test\""); roundtrip(L"тест", "\"\\u0442\\u0435\\u0441\\u0442\""); roundtrip(w1, "\"t\\x00st\""); roundtrip(w2, "\"\\u0442\\x00\\u0441\\u0442\""); convert_to("", "\"\""); convert_to("test", "\"test\""); convert_to(s1.c_str(), "\"t\""); convert_to(s2.c_str(), "\"\\xD1\\x82\""); roundtrip(fixed::FromInt(0), "0"); roundtrip(fixed::FromInt(123), "123"); roundtrip(fixed::FromInt(-123), "-123"); roundtrip(fixed::FromDouble(123.25), "123.25"); } void test_integers() { ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); // using new uninitialized variables each time to be sure the test doesn't succeeed if ToJSVal doesn't touch the value at all. JS::RootedValue val0(cx), val1(cx), val2(cx), val3(cx), val4(cx), val5(cx), val6(cx), val7(cx), val8(cx); ScriptInterface::ToJSVal(cx, &val0, 0); ScriptInterface::ToJSVal(cx, &val1, JSVAL_INT_MAX - 1); ScriptInterface::ToJSVal(cx, &val2, JSVAL_INT_MAX); ScriptInterface::ToJSVal(cx, &val3, JSVAL_INT_MIN + 1); ScriptInterface::ToJSVal(cx, &val4, -(i64)2147483648u); // JSVAL_INT_MIN TS_ASSERT(val0.isInt32()); TS_ASSERT(val1.isInt32()); TS_ASSERT(val2.isInt32()); TS_ASSERT(val3.isInt32()); TS_ASSERT(val4.isInt32()); ScriptInterface::ToJSVal(cx, &val5, 0); ScriptInterface::ToJSVal(cx, &val6, 2147483646u); // JSVAL_INT_MAX-1 ScriptInterface::ToJSVal(cx, &val7, 2147483647u); // JSVAL_INT_MAX ScriptInterface::ToJSVal(cx, &val8, 2147483648u); // JSVAL_INT_MAX+1 TS_ASSERT(val5.isInt32()); TS_ASSERT(val6.isInt32()); TS_ASSERT(val7.isInt32()); TS_ASSERT(val8.isDouble()); } void test_nonfinite() { roundtrip(std::numeric_limits::infinity(), "Infinity"); roundtrip(-std::numeric_limits::infinity(), "-Infinity"); convert_to(std::numeric_limits::quiet_NaN(), "NaN"); // can't use roundtrip since nan != nan ScriptInterface script("Test", "Test", g_ScriptRuntime); JSContext* cx = script.GetContext(); JSAutoRequest rq(cx); float f = 0; JS::RootedValue testNANVal(cx); ScriptInterface::ToJSVal(cx, &testNANVal, NAN); TS_ASSERT(ScriptInterface::FromJSVal(cx, testNANVal, f)); TS_ASSERT(isnan(f)); } // NOTE: fixed and vector conversions are defined in simulation2/scripting/EngineScriptConversions.cpp void test_fixed() { fixed f; f.SetInternalValue(10590283); roundtrip(f, "161.5948944091797"); f.SetInternalValue(-10590283); roundtrip(f, "-161.5948944091797"); f.SetInternalValue(2000000000); roundtrip(f, "30517.578125"); f.SetInternalValue(2000000001); roundtrip(f, "30517.57814025879"); } void test_vector2d() { CFixedVector2D v(fixed::Zero(), fixed::Pi()); roundtrip(v, "({x:0, y:3.1415863037109375})"); CFixedVector2D u(fixed::FromInt(1), fixed::Zero()); call_prototype_function(u, v, "add", "({x:1, y:3.1415863037109375})"); } void test_vector3d() { CFixedVector3D v(fixed::Zero(), fixed::Pi(), fixed::FromInt(1)); roundtrip(v, "({x:0, y:3.1415863037109375, z:1})"); CFixedVector3D u(fixed::Pi(), fixed::Zero(), fixed::FromInt(2)); call_prototype_function(u, v, "add", "({x:3.1415863037109375, y:3.1415863037109375, z:3})"); } + + void test_utf8utf16_conversion() + { + // Fancier conversion: we store UTF8 and get UTF16 and vice-versa + ScriptInterface script("Test", "Test", g_ScriptRuntime); + TS_ASSERT(script.LoadGlobalScripts()); + JSContext* cx = script.GetContext(); + JSAutoRequest rq(cx); + + std::string in_utf8("éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); + std::wstring in_utf16(L"éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); + + JS::RootedValue v1(cx); + ScriptInterface::ToJSVal(cx, &v1, in_utf8); + std::wstring test_out_utf16; + TS_ASSERT(ScriptInterface::FromJSVal(cx, v1, test_out_utf16)); + TS_ASSERT_EQUALS(test_out_utf16, in_utf16); + + JS::RootedValue v2(cx); + ScriptInterface::ToJSVal(cx, &v2, in_utf16); + std::string test_out_utf8; + TS_ASSERT(ScriptInterface::FromJSVal(cx, v2, test_out_utf8)); + TS_ASSERT_EQUALS(test_out_utf8, in_utf8); + } };