Bug 1329111 - Use JSONWriter when generating the GetSharedLibraryInfo() JSON. r?njn draft
authorMarkus Stange <mstange@themasta.com>
Mon, 13 Mar 2017 17:08:38 -0400
changeset 498494 27eaac1536b24adc9bf1b24e9f78b04d308b4130
parent 498493 b6054b54356373652ed01b95bdad48d52590e637
child 498495 fa11f74812a02cd01ea87a95b158856584a8bbc6
push id49211
push userbmo:mstange@themasta.com
push dateTue, 14 Mar 2017 21:26:46 +0000
reviewersnjn
bugs1329111
milestone55.0a1
Bug 1329111 - Use JSONWriter when generating the GetSharedLibraryInfo() JSON. r?njn MozReview-Commit-ID: LZalkkFreym
tools/profiler/core/platform.cpp
--- a/tools/profiler/core/platform.cpp
+++ b/tools/profiler/core/platform.cpp
@@ -1029,47 +1029,46 @@ private:
   ~ProfileSaveEvent() {}
 
   AddSubProfileFunc mFunc;
   void* mClosure;
 };
 
 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.GetNativeDebugName() << "\"";
-  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.GetNativeDebugName().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;
+  JSONWriter w(MakeUnique<OStreamJSONWriteFunc>(os));
+  w.StartArrayElement();
+  for (size_t i = 0; i < info.GetSize(); i++) {
+    AddSharedLibraryInfoToStream(w, info.GetEntry(i));
   }
-
-  std::ostringstream os;
-  os << "[";
-  AddSharedLibraryInfoToStream(os, info.GetEntry(0));
-
-  for (size_t i = 1; i < info.GetSize(); i++) {
-    os << ",";
-    AddSharedLibraryInfoToStream(os, info.GetEntry(i));
-  }
-
-  os << "]";
+  w.EndArray();
   return os.str();
 }
 
 static void
 StreamTaskTracer(PS::LockRef aLock, SpliceableJSONWriter& aWriter)
 {
 #ifdef MOZ_TASK_TRACER
   aWriter.StartArrayProperty("data");