Bug 1433625 - Implement console.createInstance().time("foo") correctly, r=smaug
authorAndrea Marchesini <amarchesini@mozilla.com>
Mon, 29 Jan 2018 13:25:15 +0100
changeset 453689 78fa743e3f112b632c0fa918a34cf57e82f67c2f
parent 453688 82eaa74fa602973bb830f0df4e67b35a9388f88e
child 453690 c0f08b020685f67a7ea08658731adb410f70b7e6
child 453693 3431bb172f288ccbf22873f3a97a5f4cddd0c3fe
push id8799
push usermtabara@mozilla.com
push dateThu, 01 Mar 2018 16:46:23 +0000
treeherdermozilla-beta@15334014dc67 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1433625
milestone60.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1433625 - Implement console.createInstance().time("foo") correctly, r=smaug
dom/console/Console.cpp
dom/console/Console.h
--- a/dom/console/Console.cpp
+++ b/dom/console/Console.cpp
@@ -1352,16 +1352,21 @@ Console::MethodInternal(JSContext* aCx, 
       return;
     }
   }
 
   // Before processing this CallData differently, it's time to call the dump
   // function.
   if (aMethodName == MethodTrace) {
     MaybeExecuteDumpFunctionForTrace(aCx, stack);
+  } else if ((aMethodName == MethodTime ||
+              aMethodName == MethodTimeEnd) &&
+             !aData.IsEmpty()) {
+    MaybeExecuteDumpFunctionForTime(aCx, aMethodName, aMethodString,
+                                    monotonicTimer, aData[0]);
   } else {
     MaybeExecuteDumpFunction(aCx, aMethodString, aData);
   }
 
   if (NS_IsMainThread()) {
     if (mWindow) {
       callData->SetIDs(mOuterID, mInnerID);
     } else if (!mPassedInnerID.IsEmpty()) {
@@ -2564,17 +2569,17 @@ Console::MonotonicTimer(JSContext* aCx, 
 
   if (NS_IsMainThread()) {
     double duration = (TimeStamp::Now() - mCreationTimeStamp).ToMilliseconds();
 
     // Round down to the nearest 5us, because if the timer is too accurate
     // people can do nasty timing attacks with it.  See similar code in the
     // worker Performance implementation.
     const double maxResolutionMs = 0.005;
-    return nsRFPService::ReduceTimePrecisionAsMSecs(
+    *aTimeStamp = nsRFPService::ReduceTimePrecisionAsMSecs(
       floor(duration / maxResolutionMs) * maxResolutionMs);
     return true;
   }
 
   WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
   MOZ_ASSERT(workerPrivate);
 
   *aTimeStamp = workerPrivate->TimeStampToDOMHighRes(TimeStamp::Now());
@@ -2627,16 +2632,56 @@ Console::MaybeExecuteDumpFunction(JSCont
     message.Append(string);
   }
 
   message.AppendLiteral("\n");
   ExecuteDumpFunction(message);
 }
 
 void
+Console::MaybeExecuteDumpFunctionForTime(JSContext* aCx,
+                                         MethodName aMethodName,
+                                         const nsAString& aMethodString,
+                                         uint64_t aMonotonicTimer,
+                                         const JS::Value& aData)
+{
+  if (!mDumpFunction && !mDumpToStdout) {
+    return;
+  }
+
+  nsAutoString message;
+  message.AssignLiteral("console.");
+  message.Append(aMethodString);
+  message.AppendLiteral(": ");
+
+  if (!mPrefix.IsEmpty()) {
+    message.Append(mPrefix);
+    message.AppendLiteral(": ");
+  }
+
+  JS::Rooted<JS::Value> v(aCx, aData);
+  JS::Rooted<JSString*> jsString(aCx, JS_ValueToSource(aCx, v));
+  if (!jsString) {
+    return;
+  }
+
+  nsAutoJSString string;
+  if (NS_WARN_IF(!string.init(aCx, jsString))) {
+    return;
+  }
+
+  message.Append(string);
+  message.AppendLiteral(" @ ");
+  message.AppendInt(aMonotonicTimer);
+
+  message.AppendLiteral("\n");
+  ExecuteDumpFunction(message);
+}
+
+void
 Console::MaybeExecuteDumpFunctionForTrace(JSContext* aCx, nsIStackFrame* aStack)
 {
   if (!aStack || (!mDumpFunction && !mDumpToStdout)) {
     return;
   }
 
   nsAutoString message;
   message.AssignLiteral("console.trace:\n");
--- a/dom/console/Console.h
+++ b/dom/console/Console.h
@@ -387,16 +387,22 @@ private:
                  const Sequence<JS::Value>& aData,
                  DOMHighResTimeStamp* aTimeStamp);
 
   void
   MaybeExecuteDumpFunction(JSContext* aCx, const nsAString& aMethodName,
                            const Sequence<JS::Value>& aData);
 
   void
+  MaybeExecuteDumpFunctionForTime(JSContext* aCx, MethodName aMethodName,
+                                  const nsAString& aMethodString,
+                                  uint64_t aMonotonicTimer,
+                                  const JS::Value& aData);
+
+  void
   MaybeExecuteDumpFunctionForTrace(JSContext* aCx, nsIStackFrame* aStack);
 
   void
   ExecuteDumpFunction(const nsAString& aMessage);
 
   bool
   IsEnabled(JSContext* aCx) const;