bug 580128 - Move test that tests for existence of XrayWrapper's into chrome, and fix lookupMethod bug that was triggered by the test move. r=mrbkap
authorJohnny Stenback <jst@mozilla.com>
Sun, 10 Oct 2010 15:42:45 -0700
changeset 55670 1b4b203671e3e1313096887cd5b820d2ea5bf5e4
parent 55669 335272ab0550a0766b658e6451c671bae60afccb
child 55671 e83716cb71307b90ce6afad96c9378de77433802
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs580128
milestone2.0b8pre
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 580128 - Move test that tests for existence of XrayWrapper's into chrome, and fix lookupMethod bug that was triggered by the test move. r=mrbkap
js/src/xpconnect/src/xpccomponents.cpp
js/src/xpconnect/tests/chrome/Makefile.in
js/src/xpconnect/tests/chrome/test_bug517163.xul
js/src/xpconnect/tests/mochitest/Makefile.in
js/src/xpconnect/tests/mochitest/test_bug517163.html
--- a/js/src/xpconnect/src/xpccomponents.cpp
+++ b/js/src/xpconnect/src/xpccomponents.cpp
@@ -2710,29 +2710,16 @@ nsXPCComponents_Utils::GetSandbox(nsIXPC
     if (!mSandbox && !(mSandbox = new nsXPCComponents_utils_Sandbox())) {
         *aSandbox = nsnull;
         return NS_ERROR_OUT_OF_MEMORY;
     }
     NS_ADDREF(*aSandbox = mSandbox);
     return NS_OK;
 }
 
-static JSBool
-MethodWrapper(JSContext *cx, uintN argc, jsval *vp)
-{
-    JSObject *thisobj = JS_THIS_OBJECT(cx, vp);
-    if (!thisobj)
-        return JS_FALSE;
-
-    jsval *argv = JS_ARGV(cx, vp);
-    jsval v;
-    return JS_GetReservedSlot(cx, JSVAL_TO_OBJECT(JS_CALLEE(cx, vp)), 0, &v) &&
-           JS_CallFunctionValue(cx, thisobj, v, argc, argv, vp);
-}
-
 /* void lookupMethod (); */
 NS_IMETHODIMP
 nsXPCComponents_Utils::LookupMethod()
 {
     nsresult rv;
 
     nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
     if(NS_FAILED(rv))
@@ -2828,48 +2815,44 @@ nsXPCComponents_Utils::LookupMethod()
     if(!member || member->IsConstant())
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
     // it would a be a big surprise if there is a member without an interface :)
     XPCNativeInterface* iface = inner_cc.GetInterface();
     if(!iface)
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
-    // get (and perhaps lazily create) the member's cloned function
     jsval funval;
-    if(!member->NewFunctionObject(inner_cc, iface, wrapper->GetFlatJSObject(),
-                                  &funval))
-        return NS_ERROR_XPC_BAD_CONVERT_JS;
+    JSFunction *oldfunction;
+
+    {
+        JSAutoEnterCompartment ac;
+
+        if (!ac.enter(inner_cc, wrapper->GetFlatJSObject())) {
+            return NS_ERROR_UNEXPECTED;
+        }
+
+        // get (and perhaps lazily create) the member's cloned function
+        if(!member->NewFunctionObject(inner_cc, iface, wrapper->GetFlatJSObject(),
+                                      &funval))
+            return NS_ERROR_XPC_BAD_CONVERT_JS;
+
+        oldfunction = JS_ValueToFunction(inner_cc, funval);
+        NS_ASSERTION(oldfunction, "Function is not a function");
+    }
 
     // Stick the function in the return value. This roots it.
     *retval = funval;
 
     // Callers of this method are implicitly buying into
-    // XPCNativeWrapper-like protection. The easiest way
-    // to enforce this is to use our own wrapper.
-    // Note: We use the outer call context to ensure that we wrap
-    // the function in the right scope.
-    NS_ASSERTION(JSVAL_IS_OBJECT(funval), "Function is not an object");
-    JSContext *outercx;
-    cc->GetJSContext(&outercx);
-    JSFunction *oldfunction = JS_ValueToFunction(outercx, funval);
-    NS_ASSERTION(oldfunction, "Function is not a function");
-
-    JSFunction *f = JS_NewFunction(outercx, MethodWrapper,
-                                   JS_GetFunctionArity(oldfunction), 0,
-                                   JS_GetScopeChain(outercx),
-                                   JS_GetFunctionName(oldfunction));
-    if(!f)
-        return NS_ERROR_FAILURE;
-
-    JSObject *funobj = JS_GetFunctionObject(f);
-    if(!JS_SetReservedSlot(outercx, funobj, 0, funval))
-        return NS_ERROR_FAILURE;
-
-    *retval = OBJECT_TO_JSVAL(funobj);
+    // XPCNativeWrapper-like protection. The easiest way to enforce
+    // this is to let the JS engine wrap the function.
+    if (!JS_WrapValue(inner_cc, retval)) {
+        return NS_ERROR_UNEXPECTED;
+    }
 
     // Tell XPConnect that we returned the function through the call context.
     cc->SetReturnValueWasSet(PR_TRUE);
     return NS_OK;
 }
 
 /* void reportError (); */
 NS_IMETHODIMP
--- a/js/src/xpconnect/tests/chrome/Makefile.in
+++ b/js/src/xpconnect/tests/chrome/Makefile.in
@@ -51,12 +51,13 @@ include $(topsrcdir)/config/rules.mk
 		test_bug533596.xul \
 		test_doublewrappedcompartments.xul \
 		test_evalInSandbox.xul \
 		test_sandboxImport.xul \
 		test_wrappers.xul \
 		test_wrappers-2.xul \
 		test_bug484459.xul \
 		test_cows.xul \
+		test_bug517163.xul \
 		$(NULL)
 
 libs:: $(_CHROME_FILES)
 	$(INSTALL) $(foreach f,$^,"$f") $(DEPTH)/_tests/testing/mochitest/chrome/$(relativesrcdir)
rename from js/src/xpconnect/tests/mochitest/test_bug517163.html
rename to js/src/xpconnect/tests/chrome/test_bug517163.xul
--- a/js/src/xpconnect/tests/mochitest/test_bug517163.html
+++ b/js/src/xpconnect/tests/chrome/test_bug517163.xul
@@ -1,29 +1,53 @@
-<!DOCTYPE HTML>
-<html>
+<?xml version="1.0"?>
+<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
+                 type="text/css"?>
 <!--
 https://bugzilla.mozilla.org/show_bug.cgi?id=517163
 -->
-<head>
-  <title>Test for Bug 517163</title>
-  <script type="application/javascript" src="/MochiKit/packed.js"></script>
-  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
-</head>
-<body>
-<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=517163">Mozilla Bug 517163</a>
-<p id="display"></p>
-<div id="content" style="display: none">
-  
-</div>
-<pre id="test">
-<script type="application/javascript">
+<window title="Mozilla Bug 517163"
+  xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
+  <script type="application/javascript"
+          src="chrome://mochikit/content/MochiKit/packed.js"></script>
+  <script type="application/javascript"
+          src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+
+  <!-- test results are displayed in the html:body -->
+  <body xmlns="http://www.w3.org/1999/xhtml">
+  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=517163"
+     target="_blank">Mozilla Bug 517163</a>
+  </body>
+
+  <!-- test code goes here -->
+  <script type="application/javascript"><![CDATA[
+    function dont_call() {
+      ok(false, "This method should not be called.");
+    }
 
-/** Test for Bug 517163 **/
-netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
-var rval = Components.utils.lookupMethod(document, "getElementById")("content");
-ok(/XPCNativeWrapper/(rval.toString()), "got an XPCNativeWrapper back");
+    function go() {
+      /** Test for Bug 517163 **/
+      var rval = Components.utils.lookupMethod($("ifr").contentDocument,
+                                               "getElementsByTagName")("body");
+      ok(/XrayWrapper/(rval.toString()), "got an XPCNativeWrapper back");
+
+      // Overwrite one of our own functions and make sure we can still
+      // reach the underlying function through lookupMethod.
+
+      var iframe = $('ifr');
+      document.getElementById = dont_call;
+      rval = Components.utils.lookupMethod(document, "getElementById")("ifr");
+      is(rval, iframe, "got the wrong element back");
 
-</script>
-</pre>
-</body>
-</html>
+      SimpleTest.finish();
+    }
+
+    SimpleTest.waitForExplicitFinish();
+    ]]>
+  </script>
+
+  <iframe type="content"
+          src="http://mochi.test:8888"
+          onload="go()"
+          id="ifr">
+  </iframe>
+</window>
--- a/js/src/xpconnect/tests/mochitest/Makefile.in
+++ b/js/src/xpconnect/tests/mochitest/Makefile.in
@@ -63,17 +63,16 @@ include $(topsrcdir)/config/rules.mk
 		test_bug462428.html \
 		test_bug478438.html \
 		test_bug500691.html \
 		test_bug502959.html \
 		test_bug503926.html \
 		test_bug504877.html \
 		test_bug505915.html \
 		file_bug505915.html \
-		test_bug517163.html \
 		test_bug553407.html \
 		test_bug560351.html \
 		test_bug564330.html \
 		test_frameWrapping.html \
 		test_bug589028.html \
 		bug589028_helper.html \
 		$(NULL)