Index: ps/trunk/source/gui/Scripting/JSInterface_GUISize.h =================================================================== --- ps/trunk/source/gui/Scripting/JSInterface_GUISize.h (revision 25442) +++ ps/trunk/source/gui/Scripting/JSInterface_GUISize.h (revision 25443) @@ -1,39 +1,40 @@ /* Copyright (C) 2021 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 . */ #ifndef INCLUDED_JSI_GUISIZE #define INCLUDED_JSI_GUISIZE -class CStr8; -class ScriptInterface; +#include "ps/CStr.h" +#include "scriptinterface/ScriptForward.h" +#include "scriptinterface/ScriptTypes.h" namespace JSI_GUISize { extern JSClass JSI_class; extern JSClassOps JSI_classops; extern JSPropertySpec JSI_props[]; extern JSFunctionSpec JSI_methods[]; void RegisterScriptClass(ScriptInterface& scriptInterface); bool construct(JSContext* cx, uint argc, JS::Value* vp); bool toString(JSContext* cx, uint argc, JS::Value* vp); - CStr8 ToPercentString(double pix, double per); + CStr ToPercentString(double pix, double per); } #endif // INCLUDED_JSI_GUISIZE Index: ps/trunk/source/gui/SettingTypes/CGUISize.cpp =================================================================== --- ps/trunk/source/gui/SettingTypes/CGUISize.cpp (revision 25442) +++ ps/trunk/source/gui/SettingTypes/CGUISize.cpp (revision 25443) @@ -1,230 +1,231 @@ /* Copyright (C) 2021 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 "CGUISize.h" #include "gui/Scripting/JSInterface_GUISize.h" #include "ps/CLogger.h" +#include "ps/CStr.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptInterface.h" CGUISize::CGUISize() : pixel(), percent() { } CGUISize::CGUISize(const CRect& pixel, const CRect& percent) : pixel(pixel), percent(percent) { } CGUISize CGUISize::Full() { return CGUISize(CRect(0, 0, 0, 0), CRect(0, 0, 100, 100)); } CRect CGUISize::GetSize(const CRect& parent) const { // If it's a 0 0 100% 100% we need no calculations if (percent == CRect(0.f, 0.f, 100.f, 100.f) && pixel == CRect()) return parent; CRect client; // This should probably be cached and not calculated all the time for every object. client.left = parent.left + (parent.right-parent.left)*percent.left/100.f + pixel.left; client.top = parent.top + (parent.bottom-parent.top)*percent.top/100.f + pixel.top; client.right = parent.left + (parent.right-parent.left)*percent.right/100.f + pixel.right; client.bottom = parent.top + (parent.bottom-parent.top)*percent.bottom/100.f + pixel.bottom; return client; } bool CGUISize::FromString(const CStr& Value) { /* * GUISizes contain a left, top, right, and bottom * for example: "50%-150 10%+9 50%+150 10%+25" means * the left edge is at 50% minus 150 pixels, the top * edge is at 10% plus 9 pixels, the right edge is at * 50% plus 150 pixels, and the bottom edge is at 10% * plus 25 pixels. * All four coordinates are required and can be * defined only in pixels, only in percents, or some * combination of both. */ // Check the input is only numeric const char* input = Value.c_str(); - CStr buffer = ""; + CStr buffer; unsigned int coord = 0; float pixels[4] = {0, 0, 0, 0}; float percents[4] = {0, 0, 0, 0}; for (unsigned int i = 0; i < Value.length(); ++i) { switch (input[i]) { case '.': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer.push_back(input[i]); break; case '+': pixels[coord] += buffer.ToFloat(); buffer = "+"; break; case '-': pixels[coord] += buffer.ToFloat(); buffer = "-"; break; case '%': percents[coord] += buffer.ToFloat(); buffer = ""; break; case ' ': pixels[coord] += buffer.ToFloat(); buffer = ""; ++coord; break; default: LOGERROR("CGUISize definition may only include numbers. Your input: '%s'", Value.c_str()); return false; } if (coord > 3) { LOGERROR("Too many CGUISize parameters (4 max). Your input: '%s'", Value.c_str()); return false; } } if (coord < 3) { LOGERROR("Too few CGUISize parameters (4 min). Your input: '%s'", Value.c_str()); return false; } // Now that we're at the end of the string, flush the remaining buffer. pixels[coord] += buffer.ToFloat(); // Now store the coords in the right place pixel.left = pixels[0]; pixel.top = pixels[1]; pixel.right = pixels[2]; pixel.bottom = pixels[3]; percent.left = percents[0]; percent.top = percents[1]; percent.right = percents[2]; percent.bottom = percents[3]; return true; } void CGUISize::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret) const { const ScriptInterface& scriptInterface = rq.GetScriptInterface(); ret.setObjectOrNull(scriptInterface.CreateCustomObject("GUISize")); if (!ret.isObject()) { ScriptException::Raise(rq, "CGUISize value is not an Object"); return; } JS::RootedObject obj(rq.cx, &ret.toObject()); if (!JS_InstanceOf(rq.cx, obj, &JSI_GUISize::JSI_class, nullptr)) { ScriptException::Raise(rq, "CGUISize value is not a CGUISize class instance"); return; } #define P(x, y, z)\ if (!Script::SetProperty(rq, ret, #z, x.y)) \ { \ ScriptException::Raise(rq, "Could not SetProperty '%s'", #z); \ return; \ } P(pixel, left, left); P(pixel, top, top); P(pixel, right, right); P(pixel, bottom, bottom); P(percent, left, rleft); P(percent, top, rtop); P(percent, right, rright); P(percent, bottom, rbottom); #undef P } bool CGUISize::FromJSVal(const ScriptRequest& rq, JS::HandleValue v) { if (v.isString()) { CStrW str; if (!Script::FromJSVal(rq, v, str)) { LOGERROR("CGUISize could not read JS string"); return false; } if (!FromString(str.ToUTF8())) { LOGERROR("CGUISize could not parse JS string"); return false; } return true; } if (!v.isObject()) { LOGERROR("CGUISize value is not an String, nor Object"); return false; } JS::RootedObject obj(rq.cx, &v.toObject()); if (!JS_InstanceOf(rq.cx, obj, &JSI_GUISize::JSI_class, nullptr)) { LOGERROR("CGUISize value is not a CGUISize class instance"); return false; } #define P(x, y, z) \ if (!Script::GetProperty(rq, v, #z, x.y))\ {\ LOGERROR("CGUISize could not get object property '%s'", #z);\ return false;\ } P(pixel, left, left); P(pixel, top, top); P(pixel, right, right); P(pixel, bottom, bottom); P(percent, left, rleft); P(percent, top, rtop); P(percent, right, rright); P(percent, bottom, rbottom); #undef P return true; } Index: ps/trunk/source/lobby/scripting/GlooxScriptConversions.cpp =================================================================== --- ps/trunk/source/lobby/scripting/GlooxScriptConversions.cpp (revision 25442) +++ ps/trunk/source/lobby/scripting/GlooxScriptConversions.cpp (revision 25443) @@ -1,61 +1,61 @@ /* Copyright (C) 2021 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 "lib/config2.h" #if CONFIG2_LOBBY - +#include "lib/utf8.h" #include "lobby/XmppClient.h" #include "scriptinterface/ScriptConversions.h" template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const glooxwrapper::string& val) { ToJSVal(rq, ret, wstring_from_utf8(val.to_string())); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::Presence::PresenceType& val) { ToJSVal(rq, ret, XmppClient::GetPresenceString(val)); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::MUCRoomRole& val) { ToJSVal(rq, ret, XmppClient::GetRoleString(val)); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::StanzaError& val) { ToJSVal(rq, ret, wstring_from_utf8(XmppClient::StanzaErrorToString(val))); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::ConnectionError& val) { ToJSVal(rq, ret, wstring_from_utf8(XmppClient::ConnectionErrorToString(val))); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::RegistrationResult& val) { ToJSVal(rq, ret, wstring_from_utf8(XmppClient::RegistrationResultToString(val))); } template<> void Script::ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, const gloox::CertStatus& val) { ToJSVal(rq, ret, wstring_from_utf8(XmppClient::CertificateErrorToString(val))); } #endif // CONFIG2_LOBBY Index: ps/trunk/source/ps/Mod.h =================================================================== --- ps/trunk/source/ps/Mod.h (revision 25442) +++ ps/trunk/source/ps/Mod.h (revision 25443) @@ -1,67 +1,66 @@ /* Copyright (C) 2021 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 . */ #ifndef INCLUDED_MOD #define INCLUDED_MOD #include "ps/CStr.h" #include "ps/GameSetup/CmdLineArgs.h" - -class ScriptContext; +#include "scriptinterface/ScriptForward.h" extern std::vector g_modsLoaded; extern CmdLineArgs g_args; namespace Mod { JS::Value GetAvailableMods(const ScriptInterface& scriptInterface); const std::vector& GetEnabledMods(); const std::vector& GetIncompatibleMods(); const std::vector& GetFailedMods(); /** * This reads the version numbers from the launched mods. * It caches the result, since the reading of zip files is slow and * JS pages can request the version numbers too often easily. * Make sure this is called after each MountMods call. */ void CacheEnabledModVersions(const shared_ptr& scriptContext); const std::vector& GetModsFromArguments(const CmdLineArgs& args, int flags); bool AreModsCompatible(const ScriptInterface& scriptInterface, const std::vector& mods, const JS::RootedValue& availableMods); bool CheckAndEnableMods(const ScriptInterface& scriptInterface, const std::vector& mods); bool CompareVersionStrings(const CStr& required, const CStr& op, const CStr& version); void SetDefaultMods(); void ClearIncompatibleMods(); /** * Get the loaded mods and their version. * "user" mod and "mod" mod are ignored as they are irrelevant for compatibility checks. * * @param scriptInterface the ScriptInterface in which to create the return data. * @return list of loaded mods with the format [[modA, versionA], [modB, versionB], ...] */ JS::Value GetLoadedModsWithVersions(const ScriptInterface& scriptInterface); /** * Gets info (version and mods loaded) on the running engine * * @param scriptInterface the ScriptInterface in which to create the return data. * @return list of objects containing data */ JS::Value GetEngineInfo(const ScriptInterface& scriptInterface); } #endif // INCLUDED_MOD Index: ps/trunk/source/ps/ModIo.h =================================================================== --- ps/trunk/source/ps/ModIo.h (revision 25442) +++ ps/trunk/source/ps/ModIo.h (revision 25443) @@ -1,208 +1,211 @@ /* Copyright (C) 2021 Wildfire Games. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #ifndef INCLUDED_MODIO #define INCLUDED_MODIO #include "lib/external_libraries/curl.h" #include "lib/os_path.h" +#include "scriptinterface/ScriptForward.h" +#include #include #include +#include // TODO: Allocate instance of the below two using sodium_malloc? struct PKStruct { unsigned char sig_alg[2] = {}; // == "Ed" unsigned char keynum[8] = {}; // should match the keynum in the sigstruct, else this is the wrong key unsigned char pk[crypto_sign_PUBLICKEYBYTES] = {}; }; struct SigStruct { unsigned char sig_alg[2] = {}; // "ED" (since we only support the hashed mode) unsigned char keynum[8] = {}; // should match the keynum in the PKStruct unsigned char sig[crypto_sign_BYTES] = {}; }; struct ModIoModData { std::map properties; std::vector dependencies; SigStruct sig; }; enum class DownloadProgressStatus { NONE, // Default state GAMEID, // The game ID is being downloaded READY, // The game ID has been downloaded LISTING, // The mod list is being downloaded LISTED, // The mod list has been downloaded DOWNLOADING, // A mod file is being downloaded SUCCESS, // A mod file has been downloaded FAILED_GAMEID, // Game ID couldn't be retrieved FAILED_LISTING, // Mod list couldn't be retrieved FAILED_DOWNLOADING, // File couldn't be retrieved FAILED_FILECHECK // The file is corrupted }; struct DownloadProgressData { DownloadProgressStatus status; double progress; std::string error; }; struct DownloadCallbackData; /** * mod.io API interfacing code. * * Overview * * This class interfaces with a remote API provider that returns a list of mod files. * These can then be downloaded after some cursory checking of well-formedness of the returned * metadata. * Downloaded files are checked for well formedness by validating that they fit the size and hash * indicated by the API, then we check if the file is actually signed by a trusted key, and only * if all of that is success the file is actually possible to be loaded as a mod. * * Security considerations * * This both distrusts the loaded JS mods, and the API as much as possible. * We do not want a malicious mod to use this to download arbitrary files, nor do we want the API * to make us download something we have not verified. * Therefore we only allow mods to download one of the mods returned by this class (using indices). * * This (mostly) necessitates parsing the API responses here, as opposed to in JS. * One could alternatively parse the responses in a locked down JS context, but that would require * storing that code in here, or making sure nobody can overwrite it. Also this would possibly make * some of the needed accesses for downloading and verifying files a bit more complicated. * * Everything downloaded from the API has its signature verified against our public key. * This is a requirement, as otherwise a compromise of the API would result in users installing * possibly malicious files. * So a compromised API can just serve old files that we signed, so in that case there would need * to be an issue in that old file that was missed. * * To limit the extend to how old those files could be the signing key should be rotated * regularly (e.g. every release). To allow old versions of the engine to still use the API * files can be signed by both the old and the new key for some amount of time, that however * only makes sense in case a mod is compatible with both engine versions. * * Note that this does not prevent all possible attacks a package manager/update system should * defend against. This is intentionally not an update system since proper package managers already * exist. However there is some possible overlap in attack vectors and these should be evalutated * whether they apply and to what extend we can fix that on our side (or how to get the API provider * to help us do so). For a list of some possible issues see: * https://github.com/theupdateframework/specification/blob/master/tuf-spec.md * * The mod.io settings are also locked down such that only mods that have been authorized by us * show up in API queries. This is both done so that all required information (dependencies) * are stored for the files, and that only mods that have been checked for being ok are actually * shown to users. */ class ModIo { NONCOPYABLE(ModIo); public: ModIo(); ~ModIo(); // Async requests void StartGetGameId(); void StartListMods(); void StartDownloadMod(u32 idx); /** * Advance the current async request and perform final steps if the download is complete. * * @param scriptInterface used for parsing the data and possibly install the mod. * @return true if the download is complete (successful or not), false otherwise. */ bool AdvanceRequest(const ScriptInterface& scriptInterface); /** * Cancel the current async request and clean things up */ void CancelRequest(); const std::vector& GetMods() const { return m_ModData; } const DownloadProgressData& GetDownloadProgress() const { return m_DownloadProgressData; } private: static size_t ReceiveCallback(void* buffer, size_t size, size_t nmemb, void* userp); static size_t DownloadCallback(void* buffer, size_t size, size_t nmemb, void* userp); static int DownloadProgressCallback(void* clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow); CURLMcode SetupRequest(const std::string& url, bool fileDownload); void TearDownRequest(); bool ParseGameId(const ScriptInterface& scriptInterface, std::string& err); bool ParseMods(const ScriptInterface& scriptInterface, std::string& err); void DeleteDownloadedFile(); bool VerifyDownloadedFile(std::string& err); // Utility methods for parsing mod.io responses and metadata static bool ParseGameIdResponse(const ScriptInterface& scriptInterface, const std::string& responseData, int& id, std::string& err); static bool ParseModsResponse(const ScriptInterface& scriptInterface, const std::string& responseData, std::vector& modData, const PKStruct& pk, std::string& err); static bool ParseSignature(const std::vector& minisigs, SigStruct& sig, const PKStruct& pk, std::string& err); // Url parts std::string m_BaseUrl; std::string m_GamesRequest; std::string m_GameId; // Query parameters std::string m_ApiKey; std::string m_IdQuery; CURL* m_Curl; CURLM* m_CurlMulti; curl_slist* m_Headers; char m_ErrorBuffer[CURL_ERROR_SIZE]; std::string m_ResponseData; // Current mod download int m_DownloadModID; OsPath m_DownloadFilePath; DownloadCallbackData* m_CallbackData; DownloadProgressData m_DownloadProgressData; PKStruct m_pk; std::vector m_ModData; friend class TestModIo; }; extern ModIo* g_ModIo; #endif // INCLUDED_MODIO Index: ps/trunk/source/scriptinterface/ScriptConversions.h =================================================================== --- ps/trunk/source/scriptinterface/ScriptConversions.h (revision 25442) +++ ps/trunk/source/scriptinterface/ScriptConversions.h (revision 25443) @@ -1,156 +1,157 @@ /* Copyright (C) 2021 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 . */ #ifndef INCLUDED_SCRIPTCONVERSIONS #define INCLUDED_SCRIPTCONVERSIONS #include "ScriptRequest.h" #include "ScriptExceptions.h" #include "ScriptExtraHeaders.h" // for typed arrays #include +#include namespace Script { /** * Convert a JS::Value to a C++ type. (This might trigger GC.) */ template bool FromJSVal(const ScriptRequest& rq, const JS::HandleValue val, T& ret); /** * Convert a C++ type to a JS::Value. (This might trigger GC. The return * value must be rooted if you don't want it to be collected.) * NOTE: We are passing the JS::Value by reference instead of returning it by value. * The reason is a memory corruption problem that appears to be caused by a bug in Visual Studio. * Details here: http://www.wildfiregames.com/forum/index.php?showtopic=17289&p=285921 */ template void ToJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret, T const& val); template<> inline void ToJSVal(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::PersistentRootedValue& a) { handle.set(a); } template<> inline void ToJSVal >(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::Heap& a) { handle.set(a); } template<> inline void ToJSVal(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::RootedValue& a) { handle.set(a); } template <> inline void ToJSVal(const ScriptRequest& UNUSED(rq), JS::MutableHandleValue handle, const JS::HandleValue& a) { handle.set(a); } /** * Convert a named property of an object to a C++ type. */ template inline bool FromJSProperty(const ScriptRequest& rq, const JS::HandleValue val, const char* name, T& ret, bool strict = false) { if (!val.isObject()) return false; JS::RootedObject obj(rq.cx, &val.toObject()); bool hasProperty; if (!JS_HasProperty(rq.cx, obj, name, &hasProperty) || !hasProperty) return false; JS::RootedValue value(rq.cx); if (!JS_GetProperty(rq.cx, obj, name, &value)) return false; if (strict && value.isNull()) return false; return FromJSVal(rq, value, ret); } template inline void ToJSVal_vector(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector& val) { JS::RootedObject obj(rq.cx, JS::NewArrayObject(rq.cx, 0)); if (!obj) { ret.setUndefined(); return; } ENSURE(val.size() <= std::numeric_limits::max()); for (u32 i = 0; i < val.size(); ++i) { JS::RootedValue el(rq.cx); Script::ToJSVal(rq, &el, val[i]); JS_SetElement(rq.cx, obj, i, el); } ret.setObject(*obj); } #define FAIL(msg) STMT(ScriptException::Raise(rq, msg); return false) template inline bool FromJSVal_vector(const ScriptRequest& rq, JS::HandleValue v, std::vector& out) { JS::RootedObject obj(rq.cx); if (!v.isObject()) FAIL("Argument must be an array"); bool isArray; obj = &v.toObject(); if ((!JS::IsArrayObject(rq.cx, obj, &isArray) || !isArray) && !JS_IsTypedArrayObject(obj)) FAIL("Argument must be an array"); u32 length; if (!JS::GetArrayLength(rq.cx, obj, &length)) FAIL("Failed to get array length"); out.clear(); out.reserve(length); for (u32 i = 0; i < length; ++i) { JS::RootedValue el(rq.cx); if (!JS_GetElement(rq.cx, obj, i, &el)) FAIL("Failed to read array element"); T el2; if (!Script::FromJSVal(rq, el, el2)) return false; out.push_back(el2); } return true; } #undef FAIL #define JSVAL_VECTOR(T) \ template<> void Script::ToJSVal >(const ScriptRequest& rq, JS::MutableHandleValue ret, const std::vector& val) \ { \ ToJSVal_vector(rq, ret, val); \ } \ template<> bool Script::FromJSVal >(const ScriptRequest& rq, JS::HandleValue v, std::vector& out) \ { \ return FromJSVal_vector(rq, v, out); \ } } // namespace Script #endif //INCLUDED_SCRIPTCONVERSIONS Index: ps/trunk/source/scriptinterface/tests/test_FunctionWrapper.h =================================================================== --- ps/trunk/source/scriptinterface/tests/test_FunctionWrapper.h (revision 25442) +++ ps/trunk/source/scriptinterface/tests/test_FunctionWrapper.h (revision 25443) @@ -1,111 +1,113 @@ /* Copyright (C) 2021 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/FunctionWrapper.h" +#include "scriptinterface/ScriptContext.h" +#include "scriptinterface/ScriptInterface.h" class TestFunctionWrapper : public CxxTest::TestSuite { public: // TODO C++20: use lambda functions directly, names are 'N params, void/returns'. static void _1p_v(int) {}; static void _3p_v(int, bool, std::string) {}; static int _3p_r(int a, bool, std::string) { return a; }; static void _0p_v() {}; static int _0p_r() { return 1; }; void test_simple_wrappers() { static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); } static void _handle(JS::HandleValue) {}; static void _handle_2(int, JS::HandleValue, bool) {}; static void _script_interface(const ScriptInterface&) {}; static int _script_interface_2(const ScriptInterface&, int a, bool) { return a; }; static void _script_request(const ScriptRequest&) {}; static int _script_request_2(const ScriptRequest&, int a, bool) { return a; }; void test_special_wrappers() { static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); static_assert(std::is_same_v), JSNative>); } class test_method { public: void method_1() {}; int method_2(int, const int&) { return 4; }; void const_method_1() const {}; int const_method_2(int, const int&) const { return 4; }; }; void test_method_wrappers() { static_assert(std::is_same_v>), JSNative>); static_assert(std::is_same_v>), JSNative>); static_assert(std::is_same_v>), JSNative>); static_assert(std::is_same_v>), JSNative>); } void test_calling() { ScriptInterface script("Test", "Test", g_ScriptContext); ScriptRequest rq(script); ScriptFunction::Register<&TestFunctionWrapper::_1p_v>(script, "_1p_v"); { std::string input = "Test._1p_v(0);"; JS::RootedValue val(rq.cx); TS_ASSERT(script.Eval(input.c_str(), &val)); } ScriptFunction::Register<&TestFunctionWrapper::_3p_r>(script, "_3p_r"); { std::string input = "Test._3p_r(4, false, 'test');"; int ret = 0; TS_ASSERT(script.Eval(input.c_str(), ret)); TS_ASSERT_EQUALS(ret, 4); } ScriptFunction::Register<&TestFunctionWrapper::_script_interface_2>(script, "_cmpt_private_2"); { std::string input = "Test._cmpt_private_2(4);"; int ret = 0; TS_ASSERT(script.Eval(input.c_str(), ret)); TS_ASSERT_EQUALS(ret, 4); } } }; Index: ps/trunk/source/simulation2/serialization/BinarySerializer.cpp =================================================================== --- ps/trunk/source/simulation2/serialization/BinarySerializer.cpp (revision 25442) +++ ps/trunk/source/simulation2/serialization/BinarySerializer.cpp (revision 25443) @@ -1,493 +1,493 @@ /* Copyright (C) 2021 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 "BinarySerializer.h" #include "lib/alignment.h" +#include "lib/utf8.h" #include "ps/CLogger.h" - #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/ScriptExtraHeaders.h" #include "scriptinterface/ScriptRequest.h" #include "scriptinterface/JSON.h" #include "SerializedScriptTypes.h" static u8 GetArrayType(js::Scalar::Type arrayType) { switch(arrayType) { case js::Scalar::Int8: return SCRIPT_TYPED_ARRAY_INT8; case js::Scalar::Uint8: return SCRIPT_TYPED_ARRAY_UINT8; case js::Scalar::Int16: return SCRIPT_TYPED_ARRAY_INT16; case js::Scalar::Uint16: return SCRIPT_TYPED_ARRAY_UINT16; case js::Scalar::Int32: return SCRIPT_TYPED_ARRAY_INT32; case js::Scalar::Uint32: return SCRIPT_TYPED_ARRAY_UINT32; case js::Scalar::Float32: return SCRIPT_TYPED_ARRAY_FLOAT32; case js::Scalar::Float64: return SCRIPT_TYPED_ARRAY_FLOAT64; case js::Scalar::Uint8Clamped: return SCRIPT_TYPED_ARRAY_UINT8_CLAMPED; default: LOGERROR("Cannot serialize unrecognized typed array view: %d", arrayType); throw PSERROR_Serialize_InvalidScriptValue(); } } CBinarySerializerScriptImpl::CBinarySerializerScriptImpl(const ScriptInterface& scriptInterface, ISerializer& serializer) : m_ScriptInterface(scriptInterface), m_Serializer(serializer), m_ScriptBackrefsNext(0) { ScriptRequest rq(m_ScriptInterface); JS_AddExtraGCRootsTracer(rq.cx, Trace, this); } CBinarySerializerScriptImpl::~CBinarySerializerScriptImpl() { ScriptRequest rq(m_ScriptInterface); JS_RemoveExtraGCRootsTracer(rq.cx, Trace, this); } void CBinarySerializerScriptImpl::HandleScriptVal(JS::HandleValue val) { ScriptRequest rq(m_ScriptInterface); switch (JS_TypeOfValue(rq.cx, val)) { case JSTYPE_UNDEFINED: { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_VOID); break; } case JSTYPE_NULL: // This type is never actually returned (it's a JS2 feature) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL); break; } case JSTYPE_OBJECT: { if (val.isNull()) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_NULL); break; } JS::RootedObject obj(rq.cx, &val.toObject()); // If we've already serialized this object, just output a reference to it u32 tag = GetScriptBackrefTag(obj); if (tag != 0) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BACKREF); m_Serializer.NumberU32("tag", tag, 0, JSVAL_INT_MAX); break; } // Arrays, Maps and Sets are special cases of Objects bool isArray; bool isMap; bool isSet; if (JS::IsArrayObject(rq.cx, obj, &isArray) && isArray) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY); // TODO: probably should have a more efficient storage format // Arrays like [1, 2, ] have an 'undefined' at the end which is part of the // length but seemingly isn't enumerated, so store the length explicitly uint length = 0; if (!JS::GetArrayLength(rq.cx, obj, &length)) throw PSERROR_Serialize_ScriptError("JS::GetArrayLength failed"); m_Serializer.NumberU32_Unbounded("array length", length); } else if (JS_IsTypedArrayObject(obj)) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_TYPED_ARRAY); m_Serializer.NumberU8_Unbounded("array type", GetArrayType(JS_GetArrayBufferViewType(obj))); m_Serializer.NumberU32_Unbounded("byte offset", JS_GetTypedArrayByteOffset(obj)); m_Serializer.NumberU32_Unbounded("length", JS_GetTypedArrayLength(obj)); bool sharedMemory; // Now handle its array buffer // this may be a backref, since ArrayBuffers can be shared by multiple views JS::RootedValue bufferVal(rq.cx, JS::ObjectValue(*JS_GetArrayBufferViewBuffer(rq.cx, obj, &sharedMemory))); HandleScriptVal(bufferVal); break; } else if (JS::IsArrayBufferObject(obj)) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_ARRAY_BUFFER); #if BYTE_ORDER != LITTLE_ENDIAN #error TODO: need to convert JS ArrayBuffer data to little-endian #endif u32 length = JS::GetArrayBufferByteLength(obj); m_Serializer.NumberU32_Unbounded("buffer length", length); JS::AutoCheckCannotGC nogc; bool sharedMemory; m_Serializer.RawBytes("buffer data", (const u8*)JS::GetArrayBufferData(obj, &sharedMemory, nogc), length); break; } else if (JS::IsMapObject(rq.cx, obj, &isMap) && isMap) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_MAP); m_Serializer.NumberU32_Unbounded("map size", JS::MapSize(rq.cx, obj)); JS::RootedValue keyValueIterator(rq.cx); if (!JS::MapEntries(rq.cx, obj, &keyValueIterator)) throw PSERROR_Serialize_ScriptError("JS::MapEntries failed"); JS::ForOfIterator it(rq.cx); if (!it.init(keyValueIterator)) throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed"); JS::RootedValue keyValuePair(rq.cx); bool done; while (true) { if (!it.next(&keyValuePair, &done)) throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed"); if (done) break; JS::RootedObject keyValuePairObj(rq.cx, &keyValuePair.toObject()); JS::RootedValue key(rq.cx); JS::RootedValue value(rq.cx); ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 0, &key)); ENSURE(JS_GetElement(rq.cx, keyValuePairObj, 1, &value)); HandleScriptVal(key); HandleScriptVal(value); } break; } else if (JS::IsSetObject(rq.cx, obj, &isSet) && isSet) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_SET); m_Serializer.NumberU32_Unbounded("set size", JS::SetSize(rq.cx, obj)); JS::RootedValue valueIterator(rq.cx); if (!JS::SetValues(rq.cx, obj, &valueIterator)) throw PSERROR_Serialize_ScriptError("JS::SetValues failed"); JS::ForOfIterator it(rq.cx); if (!it.init(valueIterator)) throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::init failed"); JS::RootedValue value(rq.cx); bool done; while (true) { if (!it.next(&value, &done)) throw PSERROR_Serialize_ScriptError("JS::ForOfIterator::next failed"); if (done) break; HandleScriptVal(value); } break; } else { // Find type of object const JSClass* jsclass = JS_GetClass(obj); if (!jsclass) throw PSERROR_Serialize_ScriptError("JS_GetClass failed"); JSProtoKey protokey = JSCLASS_CACHED_PROTO_KEY(jsclass); if (protokey == JSProto_Object) { // Object class - check for user-defined prototype JS::RootedObject proto(rq.cx); if (!JS_GetPrototype(rq.cx, obj, &proto)) throw PSERROR_Serialize_ScriptError("JS_GetPrototype failed"); SPrototypeSerialization protoInfo = GetPrototypeInfo(rq, proto); if (protoInfo.name == "Object") m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT); else { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_PROTOTYPE); m_Serializer.String("proto", wstring_from_utf8(protoInfo.name), 0, 256); // Does it have custom Serialize function? // if so, we serialize the data it returns, rather than the object's properties directly if (protoInfo.hasCustomSerialize) { // If serialize is null, don't serialize anything more if (!protoInfo.hasNullSerialize) { JS::RootedValue data(rq.cx); if (!ScriptFunction::Call(rq, val, "Serialize", &data)) throw PSERROR_Serialize_ScriptError("Prototype Serialize function failed"); m_Serializer.ScriptVal("data", &data); } // Break here to skip the custom object property serialization logic below. break; } } } else if (protokey == JSProto_Number) { // Get primitive value double d; if (!JS::ToNumber(rq.cx, val, &d)) throw PSERROR_Serialize_ScriptError("JS::ToNumber failed"); // Refuse to serialize NaN values: their representation can differ, leading to OOS // and in general this is indicative of an underlying bug rather than desirable behaviour. if (std::isnan(d)) { LOGERROR("Cannot serialize NaN values."); throw PSERROR_Serialize_InvalidScriptValue(); } // Standard Number object m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_NUMBER); m_Serializer.NumberDouble_Unbounded("value", d); break; } else if (protokey == JSProto_String) { // Standard String object m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_STRING); // Get primitive value JS::RootedString str(rq.cx, JS::ToString(rq.cx, val)); if (!str) throw PSERROR_Serialize_ScriptError("JS_ValueToString failed"); ScriptString("value", str); break; } else if (protokey == JSProto_Boolean) { // Standard Boolean object m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_OBJECT_BOOLEAN); // Get primitive value bool b = JS::ToBoolean(val); m_Serializer.Bool("value", b); break; } else { // Unrecognized class LOGERROR("Cannot serialise JS objects with unrecognized class '%s'", jsclass->name); throw PSERROR_Serialize_InvalidScriptValue(); } } // Find all properties (ordered by insertion time) JS::Rooted ida(rq.cx, JS::IdVector(rq.cx)); if (!JS_Enumerate(rq.cx, obj, &ida)) throw PSERROR_Serialize_ScriptError("JS_Enumerate failed"); m_Serializer.NumberU32_Unbounded("num props", (u32)ida.length()); for (size_t i = 0; i < ida.length(); ++i) { JS::RootedId id(rq.cx, ida[i]); JS::RootedValue idval(rq.cx); JS::RootedValue propval(rq.cx); // Forbid getters, which might delete values and mess things up. JS::Rooted desc(rq.cx); if (!JS_GetPropertyDescriptorById(rq.cx, obj, id, &desc)) throw PSERROR_Serialize_ScriptError("JS_GetPropertyDescriptorById failed"); if (desc.hasGetterObject()) throw PSERROR_Serialize_ScriptError("Cannot serialize property getters"); // Get the property name as a string if (!JS_IdToValue(rq.cx, id, &idval)) throw PSERROR_Serialize_ScriptError("JS_IdToValue failed"); JS::RootedString idstr(rq.cx, JS::ToString(rq.cx, idval)); if (!idstr) throw PSERROR_Serialize_ScriptError("JS_ValueToString failed"); ScriptString("prop name", idstr); if (!JS_GetPropertyById(rq.cx, obj, id, &propval)) throw PSERROR_Serialize_ScriptError("JS_GetPropertyById failed"); HandleScriptVal(propval); } break; } case JSTYPE_FUNCTION: { // We can't serialise functions, but we can at least name the offender (hopefully) std::wstring funcname(L"(unnamed)"); JS::RootedFunction func(rq.cx, JS_ValueToFunction(rq.cx, val)); if (func) { JS::RootedString string(rq.cx, JS_GetFunctionId(func)); if (string) { if (JS_StringHasLatin1Chars(string)) { size_t length; JS::AutoCheckCannotGC nogc; const JS::Latin1Char* ch = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length); if (ch && length > 0) funcname.assign(ch, ch + length); } else { size_t length; JS::AutoCheckCannotGC nogc; const char16_t* ch = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length); if (ch && length > 0) funcname.assign(ch, ch + length); } } } LOGERROR("Cannot serialise JS objects of type 'function': %s", utf8_from_wstring(funcname)); throw PSERROR_Serialize_InvalidScriptValue(); } case JSTYPE_STRING: { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_STRING); JS::RootedString stringVal(rq.cx, val.toString()); ScriptString("string", stringVal); break; } case JSTYPE_NUMBER: { // Refuse to serialize NaN values: their representation can differ, leading to OOS // and in general this is indicative of an underlying bug rather than desirable behaviour. if (val == JS::NaNValue()) { LOGERROR("Cannot serialize NaN values."); throw PSERROR_Serialize_InvalidScriptValue(); } // To reduce the size of the serialized data, we handle integers and doubles separately. // We can't check for val.isInt32 and val.isDouble directly, because integer numbers are not guaranteed // to be represented as integers. A number like 33 could be stored as integer on the computer of one player // and as double on the other player's computer. That would cause out of sync errors in multiplayer games because // their binary representation and thus the hash would be different. double d; d = val.toNumber(); i32 integer; if (JS_DoubleIsInt32(d, &integer)) { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_INT); m_Serializer.NumberI32_Unbounded("value", integer); } else { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_DOUBLE); m_Serializer.NumberDouble_Unbounded("value", d); } break; } case JSTYPE_BOOLEAN: { m_Serializer.NumberU8_Unbounded("type", SCRIPT_TYPE_BOOLEAN); bool b = val.toBoolean(); m_Serializer.NumberU8_Unbounded("value", b ? 1 : 0); break; } default: { debug_warn(L"Invalid TypeOfValue"); throw PSERROR_Serialize_InvalidScriptValue(); } } } void CBinarySerializerScriptImpl::ScriptString(const char* name, JS::HandleString string) { ScriptRequest rq(m_ScriptInterface); #if BYTE_ORDER != LITTLE_ENDIAN #error TODO: probably need to convert JS strings to little-endian #endif size_t length; JS::AutoCheckCannotGC nogc; // Serialize strings directly as UTF-16 or Latin1, to avoid expensive encoding conversions bool isLatin1 = JS_StringHasLatin1Chars(string); m_Serializer.Bool("isLatin1", isLatin1); if (isLatin1) { const JS::Latin1Char* chars = JS_GetLatin1StringCharsAndLength(rq.cx, nogc, string, &length); if (!chars) throw PSERROR_Serialize_ScriptError("JS_GetLatin1StringCharsAndLength failed"); m_Serializer.NumberU32_Unbounded("string length", (u32)length); m_Serializer.RawBytes(name, (const u8*)chars, length); } else { const char16_t* chars = JS_GetTwoByteStringCharsAndLength(rq.cx, nogc, string, &length); if (!chars) throw PSERROR_Serialize_ScriptError("JS_GetTwoByteStringCharsAndLength failed"); m_Serializer.NumberU32_Unbounded("string length", (u32)length); m_Serializer.RawBytes(name, (const u8*)chars, length*2); } } void CBinarySerializerScriptImpl::Trace(JSTracer *trc, void *data) { CBinarySerializerScriptImpl* serializer = static_cast(data); serializer->m_ScriptBackrefTags.trace(trc); } u32 CBinarySerializerScriptImpl::GetScriptBackrefTag(JS::HandleObject obj) { // To support non-tree structures (e.g. "var x = []; var y = [x, x];"), we need a way // to indicate multiple references to one object(/array). So every time we serialize a // new object, we give it a new tag; when we serialize it a second time we just refer // to that tag. ScriptRequest rq(m_ScriptInterface); ObjectTagMap::Ptr ptr = m_ScriptBackrefTags.lookup(JS::Heap(obj.get())); if (!ptr.found()) { if (!m_ScriptBackrefTags.put(JS::Heap(obj.get()), ++m_ScriptBackrefsNext)) { JS::RootedValue objval(rq.cx, JS::ObjectValue(*obj.get())); LOGERROR("BinarySerializer: error at insertion. Object was %s", Script::ToString(rq, &objval)); return 0; } // Return 0 to mean "you have to serialize this object"; return 0; } else return ptr->value(); } Index: ps/trunk/source/simulation2/serialization/StdDeserializer.cpp =================================================================== --- ps/trunk/source/simulation2/serialization/StdDeserializer.cpp (revision 25442) +++ ps/trunk/source/simulation2/serialization/StdDeserializer.cpp (revision 25443) @@ -1,496 +1,497 @@ /* Copyright (C) 2021 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 "StdDeserializer.h" #include "lib/byte_order.h" +#include "lib/utf8.h" #include "ps/CLogger.h" #include "ps/CStr.h" #include "scriptinterface/FunctionWrapper.h" #include "scriptinterface/Object.h" #include "scriptinterface/ScriptConversions.h" #include "scriptinterface/ScriptExtraHeaders.h" // For typed arrays and ArrayBuffer #include "scriptinterface/ScriptInterface.h" #include "simulation2/serialization/ISerializer.h" #include "simulation2/serialization/SerializedScriptTypes.h" #include "simulation2/serialization/StdSerializer.h" // for DEBUG_SERIALIZER_ANNOTATE CStdDeserializer::CStdDeserializer(const ScriptInterface& scriptInterface, std::istream& stream) : m_ScriptInterface(scriptInterface), m_Stream(stream) { JS_AddExtraGCRootsTracer(ScriptRequest(scriptInterface).cx, CStdDeserializer::Trace, this); // Insert a dummy object in front, as valid tags start at 1. m_ScriptBackrefs.emplace_back(nullptr); } CStdDeserializer::~CStdDeserializer() { JS_RemoveExtraGCRootsTracer(ScriptRequest(m_ScriptInterface).cx, CStdDeserializer::Trace, this); } void CStdDeserializer::Trace(JSTracer *trc, void *data) { reinterpret_cast(data)->TraceMember(trc); } void CStdDeserializer::TraceMember(JSTracer *trc) { for (JS::Heap& backref : m_ScriptBackrefs) JS::TraceEdge(trc, &backref, "StdDeserializer::m_ScriptBackrefs"); } void CStdDeserializer::Get(const char* name, u8* data, size_t len) { #if DEBUG_SERIALIZER_ANNOTATE std::string strName; char c = m_Stream.get(); ENSURE(c == '<'); while (1) { c = m_Stream.get(); if (c == '>') break; else strName += c; } ENSURE(strName == name); #else UNUSED2(name); #endif m_Stream.read((char*)data, (std::streamsize)len); if (!m_Stream.good()) { // hit eof before len, or other errors // NOTE: older libc++ versions incorrectly set eofbit on the last char; test gcount as a workaround // see https://llvm.org/bugs/show_bug.cgi?id=9335 if (m_Stream.bad() || m_Stream.fail() || (m_Stream.eof() && m_Stream.gcount() != (std::streamsize)len)) throw PSERROR_Deserialize_ReadFailed(); } } std::istream& CStdDeserializer::GetStream() { return m_Stream; } void CStdDeserializer::RequireBytesInStream(size_t numBytes) { // It would be nice to do: // if (numBytes > (size_t)m_Stream.rdbuf()->in_avail()) // throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream"); // but that doesn't work (at least on MSVC) since in_avail isn't // guaranteed to return the actual number of bytes available; see e.g. // http://social.msdn.microsoft.com/Forums/en/vclanguage/thread/13009a88-933f-4be7-bf3d-150e425e66a6#70ea562d-8605-4742-8851-1bae431ce6ce // Instead we'll just verify that it's not an extremely large number: if (numBytes > 64*MiB) throw PSERROR_Deserialize_OutOfBounds("RequireBytesInStream"); } void CStdDeserializer::AddScriptBackref(JS::HandleObject obj) { m_ScriptBackrefs.push_back(JS::Heap(obj)); } void CStdDeserializer::GetScriptBackref(size_t tag, JS::MutableHandleObject ret) { ENSURE(m_ScriptBackrefs.size() > tag); ret.set(m_ScriptBackrefs[tag]); } //////////////////////////////////////////////////////////////// JS::Value CStdDeserializer::ReadScriptVal(const char* UNUSED(name), JS::HandleObject preexistingObject) { ScriptRequest rq(m_ScriptInterface); uint8_t type; NumberU8_Unbounded("type", type); switch (type) { case SCRIPT_TYPE_VOID: return JS::UndefinedValue(); case SCRIPT_TYPE_NULL: return JS::NullValue(); case SCRIPT_TYPE_ARRAY: case SCRIPT_TYPE_OBJECT: case SCRIPT_TYPE_OBJECT_PROTOTYPE: { JS::RootedObject obj(rq.cx); if (type == SCRIPT_TYPE_ARRAY) { u32 length; NumberU32_Unbounded("array length", length); obj.set(JS::NewArrayObject(rq.cx, length)); } else if (type == SCRIPT_TYPE_OBJECT) { obj.set(JS_NewPlainObject(rq.cx)); } else // SCRIPT_TYPE_OBJECT_PROTOTYPE { CStrW prototypeName; String("proto", prototypeName, 0, 256); // If an object was passed, no need to construct a new one. if (preexistingObject != nullptr) obj.set(preexistingObject); else { JS::RootedValue constructor(rq.cx); if (!ScriptInterface::GetGlobalProperty(rq, prototypeName.ToUTF8(), &constructor)) throw PSERROR_Deserialize_ScriptError("Deserializer failed to get constructor object"); JS::RootedObject newObj(rq.cx); if (!JS::Construct(rq.cx, constructor, JS::HandleValueArray::empty(), &newObj)) throw PSERROR_Deserialize_ScriptError("Deserializer failed to construct object"); obj.set(newObj); } JS::RootedObject prototype(rq.cx); JS_GetPrototype(rq.cx, obj, &prototype); SPrototypeSerialization info = GetPrototypeInfo(rq, prototype); if (preexistingObject != nullptr && prototypeName != wstring_from_utf8(info.name)) throw PSERROR_Deserialize_ScriptError("Deserializer failed: incorrect pre-existing object"); if (info.hasCustomDeserialize) { AddScriptBackref(obj); // If Serialize is null, we'll still call Deserialize but with undefined argument JS::RootedValue data(rq.cx); if (!info.hasNullSerialize) ScriptVal("data", &data); JS::RootedValue objVal(rq.cx, JS::ObjectValue(*obj)); ScriptFunction::CallVoid(rq, objVal, "Deserialize", data); return JS::ObjectValue(*obj); } else if (info.hasNullSerialize) { // If we serialized null, this means we're pretty much a default-constructed object. // Nothing to do. AddScriptBackref(obj); return JS::ObjectValue(*obj); } } if (!obj) throw PSERROR_Deserialize_ScriptError("Deserializer failed to create new object"); AddScriptBackref(obj); uint32_t numProps; NumberU32_Unbounded("num props", numProps); bool isLatin1; for (uint32_t i = 0; i < numProps; ++i) { Bool("isLatin1", isLatin1); if (isLatin1) { std::vector propname; ReadStringLatin1("prop name", propname); JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr)); utf16string prp(propname.begin(), propname.end());; // TODO: Should ask upstream about getting a variant of JS_SetProperty with a length param. if (!JS_SetUCProperty(rq.cx, obj, (const char16_t*)prp.data(), prp.length(), propval)) throw PSERROR_Deserialize_ScriptError(); } else { utf16string propname; ReadStringUTF16("prop name", propname); JS::RootedValue propval(rq.cx, ReadScriptVal("prop value", nullptr)); if (!JS_SetUCProperty(rq.cx, obj, (const char16_t*)propname.data(), propname.length(), propval)) throw PSERROR_Deserialize_ScriptError(); } } return JS::ObjectValue(*obj); } case SCRIPT_TYPE_STRING: { JS::RootedString str(rq.cx); ScriptString("string", &str); return JS::StringValue(str); } case SCRIPT_TYPE_INT: { int32_t value; NumberI32("value", value, JSVAL_INT_MIN, JSVAL_INT_MAX); return JS::NumberValue(value); } case SCRIPT_TYPE_DOUBLE: { double value; NumberDouble_Unbounded("value", value); JS::RootedValue rval(rq.cx, JS::NumberValue(value)); if (rval.isNull()) throw PSERROR_Deserialize_ScriptError("JS_NewNumberValue failed"); return rval; } case SCRIPT_TYPE_BOOLEAN: { uint8_t value; NumberU8("value", value, 0, 1); return JS::BooleanValue(value ? true : false); } case SCRIPT_TYPE_BACKREF: { i32 tag; NumberI32("tag", tag, 0, JSVAL_INT_MAX); JS::RootedObject obj(rq.cx); GetScriptBackref(tag, &obj); if (!obj) throw PSERROR_Deserialize_ScriptError("Invalid backref tag"); return JS::ObjectValue(*obj); } case SCRIPT_TYPE_OBJECT_NUMBER: { double value; NumberDouble_Unbounded("value", value); JS::RootedValue val(rq.cx, JS::NumberValue(value)); JS::RootedObject ctorobj(rq.cx); if (!JS_GetClassObject(rq.cx, JSProto_Number, &ctorobj)) throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed"); JS::RootedObject obj(rq.cx, JS_New(rq.cx, ctorobj, JS::HandleValueArray(val))); if (!obj) throw PSERROR_Deserialize_ScriptError("JS_New failed"); AddScriptBackref(obj); return JS::ObjectValue(*obj); } case SCRIPT_TYPE_OBJECT_STRING: { JS::RootedString str(rq.cx); ScriptString("value", &str); if (!str) throw PSERROR_Deserialize_ScriptError(); JS::RootedValue val(rq.cx, JS::StringValue(str)); JS::RootedObject ctorobj(rq.cx); if (!JS_GetClassObject(rq.cx, JSProto_String, &ctorobj)) throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed"); JS::RootedObject obj(rq.cx, JS_New(rq.cx, ctorobj, JS::HandleValueArray(val))); if (!obj) throw PSERROR_Deserialize_ScriptError("JS_New failed"); AddScriptBackref(obj); return JS::ObjectValue(*obj); } case SCRIPT_TYPE_OBJECT_BOOLEAN: { bool value; Bool("value", value); JS::RootedValue val(rq.cx, JS::BooleanValue(value)); JS::RootedObject ctorobj(rq.cx); if (!JS_GetClassObject(rq.cx, JSProto_Boolean, &ctorobj)) throw PSERROR_Deserialize_ScriptError("JS_GetClassObject failed"); JS::RootedObject obj(rq.cx, JS_New(rq.cx, ctorobj, JS::HandleValueArray(val))); if (!obj) throw PSERROR_Deserialize_ScriptError("JS_New failed"); AddScriptBackref(obj); return JS::ObjectValue(*obj); } case SCRIPT_TYPE_TYPED_ARRAY: { u8 arrayType; u32 byteOffset, length; NumberU8_Unbounded("array type", arrayType); NumberU32_Unbounded("byte offset", byteOffset); NumberU32_Unbounded("length", length); // To match the serializer order, we reserve the typed array's backref tag here JS::RootedObject arrayObj(rq.cx); AddScriptBackref(arrayObj); // Get buffer object JS::RootedValue bufferVal(rq.cx, ReadScriptVal("buffer", nullptr)); if (!bufferVal.isObject()) throw PSERROR_Deserialize_ScriptError(); JS::RootedObject bufferObj(rq.cx, &bufferVal.toObject()); if (!JS::IsArrayBufferObject(bufferObj)) throw PSERROR_Deserialize_ScriptError("js_IsArrayBuffer failed"); switch(arrayType) { case SCRIPT_TYPED_ARRAY_INT8: arrayObj = JS_NewInt8ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_UINT8: arrayObj = JS_NewUint8ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_INT16: arrayObj = JS_NewInt16ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_UINT16: arrayObj = JS_NewUint16ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_INT32: arrayObj = JS_NewInt32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_UINT32: arrayObj = JS_NewUint32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_FLOAT32: arrayObj = JS_NewFloat32ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_FLOAT64: arrayObj = JS_NewFloat64ArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; case SCRIPT_TYPED_ARRAY_UINT8_CLAMPED: arrayObj = JS_NewUint8ClampedArrayWithBuffer(rq.cx, bufferObj, byteOffset, length); break; default: throw PSERROR_Deserialize_ScriptError("Failed to deserialize unrecognized typed array view"); } if (!arrayObj) throw PSERROR_Deserialize_ScriptError("js_CreateTypedArrayWithBuffer failed"); return JS::ObjectValue(*arrayObj); } case SCRIPT_TYPE_ARRAY_BUFFER: { u32 length; NumberU32_Unbounded("buffer length", length); #if BYTE_ORDER != LITTLE_ENDIAN #error TODO: need to convert JS ArrayBuffer data from little-endian #endif void* contents = malloc(length); ENSURE(contents); RawBytes("buffer data", (u8*)contents, length); JS::RootedObject bufferObj(rq.cx, JS::NewArrayBufferWithContents(rq.cx, length, contents)); AddScriptBackref(bufferObj); return JS::ObjectValue(*bufferObj); } case SCRIPT_TYPE_OBJECT_MAP: { JS::RootedObject obj(rq.cx, JS::NewMapObject(rq.cx)); AddScriptBackref(obj); u32 mapSize; NumberU32_Unbounded("map size", mapSize); for (u32 i=0; i& str) { uint32_t len; NumberU32_Unbounded("string length", len); RequireBytesInStream(len); str.resize(len); Get(name, (u8*)str.data(), len); } void CStdDeserializer::ReadStringUTF16(const char* name, utf16string& str) { uint32_t len; NumberU32_Unbounded("string length", len); RequireBytesInStream(len*2); str.resize(len); Get(name, (u8*)str.data(), len*2); } void CStdDeserializer::ScriptString(const char* name, JS::MutableHandleString out) { #if BYTE_ORDER != LITTLE_ENDIAN #error TODO: probably need to convert JS strings from little-endian #endif ScriptRequest rq(m_ScriptInterface); bool isLatin1; Bool("isLatin1", isLatin1); if (isLatin1) { std::vector str; ReadStringLatin1(name, str); out.set(JS_NewStringCopyN(rq.cx, (const char*)str.data(), str.size())); if (!out) throw PSERROR_Deserialize_ScriptError("JS_NewStringCopyN failed"); } else { utf16string str; ReadStringUTF16(name, str); out.set(JS_NewUCStringCopyN(rq.cx, (const char16_t*)str.data(), str.length())); if (!out) throw PSERROR_Deserialize_ScriptError("JS_NewUCStringCopyN failed"); } } void CStdDeserializer::ScriptVal(const char* name, JS::MutableHandleValue out) { out.set(ReadScriptVal(name, nullptr)); } void CStdDeserializer::ScriptObjectAssign(const char* name, JS::HandleValue objVal) { ScriptRequest rq(m_ScriptInterface); if (!objVal.isObject()) throw PSERROR_Deserialize_ScriptError(); JS::RootedObject obj(rq.cx, &objVal.toObject()); ReadScriptVal(name, obj); }