Bug 864363 - Fix standalone unit test to use the JSRuntime from XPConnect. r=bsmedberg
authorBobby Holley <bobbyholley@gmail.com>
Mon, 22 Apr 2013 16:49:52 -0400
changeset 140464 86983e77d4a5bef61fcb433286c8084bf8fdab85
parent 140463 cfa76f87cb150f476f5f4f63e48ded85fe4b4c21
child 140465 756245b309a286767ee55a460ea6218bdb86c300
push id2579
push userakeybl@mozilla.com
push dateMon, 24 Jun 2013 18:52:47 +0000
treeherdermozilla-beta@b69b7de8a05a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs864363
milestone23.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 864363 - Fix standalone unit test to use the JSRuntime from XPConnect. r=bsmedberg It turns out that the JS engine asserts that we only ever have one JSRuntime per thread. So when we spin up XPConnect in XPCOM initialization, we run into assertions when this standalone test does JS_NewRuntime. FWIW, it probably shouldn't be creating a random runtime and then passing that cx into Gecko APIs anyway. It sure would be nice if C++ unit tests could run with internal linkage... :-(
startupcache/test/TestStartupCache.cpp
--- a/startupcache/test/TestStartupCache.cpp
+++ b/startupcache/test/TestStartupCache.cpp
@@ -16,16 +16,17 @@
 #include "nsIStorageStream.h"
 #include "nsIObjectInputStream.h"
 #include "nsIObjectOutputStream.h"
 #include "nsIURI.h"
 #include "nsStringAPI.h"
 #include "nsIPrefBranch.h"
 #include "nsIPrefService.h"
 #include "nsITelemetry.h"
+#include "nsIJSContextStack.h"
 #include "jsapi.h"
 #include "prio.h"
 
 namespace mozilla {
 namespace scache {
 
 NS_IMPORT nsresult
 NewObjectInputStreamFromBuffer(char* buffer, uint32_t len, 
@@ -392,29 +393,16 @@ TestEarlyShutdown() {
     fail("PutBuffer gave an unexpected failure, expected NOT_AVAILABLE");
     return rv;
   }
  
   return NS_OK;
 }
 
 bool
-SetupJS(JSContext **cxp)
-{
-  JSRuntime *rt = JS_NewRuntime(32 * 1024 * 1024, JS_NO_HELPER_THREADS);
-  if (!rt)
-    return false;
-  JSContext *cx = JS_NewContext(rt, 8192);
-  if (!cx)
-    return false;
-  *cxp = cx;
-  return true;
-}
-
-bool
 GetHistogramCounts(const char *testmsg, const nsACString &histogram_id,
                    JSContext *cx, JS::Value *counts)
 {
   nsCOMPtr<nsITelemetry> telemetry = do_GetService("@mozilla.org/base/telemetry;1");
   JS::AutoValueRooter h(cx);
   nsresult trv = telemetry->GetHistogramById(histogram_id, cx, h.addr());
   if (NS_FAILED(trv)) {
     fail("%s: couldn't get histogram %s", testmsg, ToNewCString(histogram_id));
@@ -501,21 +489,33 @@ int main(int argc, char** argv)
   if (xpcom.failed())
     return 1;
 
   nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
   prefs->SetIntPref("hangmonitor.timeout", 0);
   
   int rv = 0;
   // nsITelemetry doesn't have a nice C++ interface.
-  JSContext *cx;
-  bool use_js = true;
-  if (!SetupJS(&cx))
-    use_js = false;
+  JSContext *cx = nullptr;
 
+  // XPCOM initialization spins up XPConnect, which spins up a JSRuntime, which
+  // we can only have one of per thread. So we need to get a JSContext out of
+  // XPConnect here, rather than creating our own runtime. XPConnect rules
+  // dictate that we push the context as well, but we're trying to make the
+  // pushing/popping APIs accessible only through nsCxPusher, which isn't
+  // accessible via the external linkage used by this test. We can get away with
+  // using the cx here without triggering a cx stack assert, so just do that
+  // for now. Eventually, the whole notion of pushing and popping will just go
+  // away.
+  nsCOMPtr<nsIThreadJSContextStack> stack =
+    do_GetService("@mozilla.org/js/xpc/ContextStack;1");
+  if (stack)
+    cx = stack->GetSafeJSContext();
+
+  bool use_js = !!cx;
   JSAutoRequest req(cx);
   static JSClass global_class = {
     "global", JSCLASS_NEW_RESOLVE | JSCLASS_GLOBAL_FLAGS | JSCLASS_HAS_PRIVATE,
     JS_PropertyStub,  JS_DeletePropertyStub,
     JS_PropertyStub,  JS_StrictPropertyStub,
     JS_EnumerateStub, JS_ResolveStub,
     JS_ConvertStub
   };