From e552737d0aeb29a3a924c2eaf578560bb8b6fc26 Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Mon, 13 Mar 2017 17:08:38 -0400 Subject: [PATCH] Bug 1329111 - Use JSONWriter when generating the GetSharedLibraryInfo() JSON. r=njn MozReview-Commit-ID: LZalkkFreym --HG-- extra : rebase_source : 71d95c3ff6454d72515c33ea7df742d95e94872b --- tools/profiler/core/platform.cpp | 41 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/tools/profiler/core/platform.cpp b/tools/profiler/core/platform.cpp index 77f08c91b874..558de688940f 100644 --- a/tools/profiler/core/platform.cpp +++ b/tools/profiler/core/platform.cpp @@ -1034,37 +1034,36 @@ private: NS_IMPL_ISUPPORTS(ProfileSaveEvent, nsIProfileSaveEvent) +const static uint64_t kJS_MAX_SAFE_UINTEGER = +9007199254740991ULL; + +static int64_t +SafeJSInteger(uint64_t aValue) { + return aValue <= kJS_MAX_SAFE_UINTEGER ? int64_t(aValue) : -1; +} + static void -AddSharedLibraryInfoToStream(std::ostream& aStream, const SharedLibrary& aLib) +AddSharedLibraryInfoToStream(JSONWriter& aWriter, const SharedLibrary& aLib) { - aStream << "{"; - aStream << "\"start\":" << aLib.GetStart(); - aStream << ",\"end\":" << aLib.GetEnd(); - aStream << ",\"offset\":" << aLib.GetOffset(); - aStream << ",\"name\":\"" << aLib.GetNativeDebugPath() << "\""; - const std::string& breakpadId = aLib.GetBreakpadId(); - aStream << ",\"breakpadId\":\"" << breakpadId << "\""; - aStream << "}"; + aWriter.StartObjectElement(); + aWriter.IntProperty("start", SafeJSInteger(aLib.GetStart())); + aWriter.IntProperty("end", SafeJSInteger(aLib.GetEnd())); + aWriter.IntProperty("offset", SafeJSInteger(aLib.GetOffset())); + aWriter.StringProperty("name", aLib.GetNativeDebugPath().c_str()); + aWriter.StringProperty("breakpadId", aLib.GetBreakpadId().c_str()); + aWriter.EndObject(); } static std::string GetSharedLibraryInfoStringInternal() { SharedLibraryInfo info = SharedLibraryInfo::GetInfoForSelf(); - if (info.GetSize() == 0) { - return "[]"; - } - std::ostringstream os; - os << "["; - AddSharedLibraryInfoToStream(os, info.GetEntry(0)); - - for (size_t i = 1; i < info.GetSize(); i++) { - os << ","; - AddSharedLibraryInfoToStream(os, info.GetEntry(i)); + JSONWriter w(MakeUnique(os)); + w.StartArrayElement(); + for (size_t i = 0; i < info.GetSize(); i++) { + AddSharedLibraryInfoToStream(w, info.GetEntry(i)); } - - os << "]"; + w.EndArray(); return os.str(); }