Bug 541742 - Make XPCNativeWrapper.unwrap work. r=bzbarsky
authorBlake Kaplan <mrbkap@gmail.com>
Mon, 25 Jan 2010 11:58:26 -0800
changeset 37489 71f78cfd324d5d20f7e02497fc7ad3434ee8640d
parent 37488 83eac7a5262a398d7336b6d8fed7dc3bf1608e90
child 37490 bedba3a902e58f3bceb533f497b1cd0f34861ebd
push idunknown
push userunknown
push dateunknown
reviewersbzbarsky
bugs541742
milestone1.9.3a1pre
Bug 541742 - Make XPCNativeWrapper.unwrap work. r=bzbarsky
js/src/xpconnect/src/XPCNativeWrapper.cpp
js/src/xpconnect/tests/chrome/test_bug533596.xul
js/src/xpconnect/tests/mochitest/bug500931_helper.html
--- a/js/src/xpconnect/src/XPCNativeWrapper.cpp
+++ b/js/src/xpconnect/src/XPCNativeWrapper.cpp
@@ -1175,17 +1175,27 @@ UnwrapNW(JSContext *cx, uintN argc, jsva
     return ThrowException(NS_ERROR_INVALID_ARG, cx);
   }
 
   if (!IsNativeWrapper(JSVAL_TO_OBJECT(v))) {
     JS_SET_RVAL(cx, vp, v);
     return JS_TRUE;
   }
 
-  return GetwrappedJSObject(cx, JSVAL_TO_OBJECT(v), vp);
+  XPCWrappedNative *wn;
+  if (!XPCNativeWrapper::GetWrappedNative(cx, JSVAL_TO_OBJECT(v), &wn)) {
+    return JS_FALSE;
+  }
+
+  if (!wn) {
+    JS_SET_RVAL(cx, vp, JSVAL_NULL);
+    return JS_TRUE;
+  }
+
+  return GetwrappedJSObject(cx, wn->GetFlatJSObject(), vp);
 }
 
 static JSFunctionSpec static_functions[] = {
   JS_FN("unwrap", UnwrapNW, 1, 0),
   JS_FS_END
 };
 
 // static
--- a/js/src/xpconnect/tests/chrome/test_bug533596.xul
+++ b/js/src/xpconnect/tests/chrome/test_bug533596.xul
@@ -1,16 +1,16 @@
 <?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=500931
+https://bugzilla.mozilla.org/show_bug.cgi?id=533596
 -->
-<window title="Mozilla Bug 500931"
+<window title="Mozilla Bug 533596"
   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">
@@ -29,16 +29,17 @@ https://bugzilla.mozilla.org/show_bug.cg
     try { XPCNativeWrapper.unwrap(null); } catch (e) {}
 
     var o = {};
     is(o, XPCNativeWrapper.unwrap(o), "unwrap on a random object returns it");
 
     var win = $('ifr').contentWindow;
     var utils = window.getInterface(Components.interfaces.nsIDOMWindowUtils);
     is(utils.getClassName(win), "XPCNativeWrapper", "win is an XPCNW");
+    ok("x" in XPCNativeWrapper.unwrap(win), "actually unwrapped");
     is(utils.getClassName(XPCNativeWrapper.unwrap(win)), "XPCSafeJSObjectWrapper",
        "unwrap on an NW returns the same object");
     is(utils.getClassName(XPCNativeWrapper.unwrap(new XPCNativeWrapper(win))), "XPCSafeJSObjectWrapper",
        "unwrap on an explicit NW works too");
 
     ok(utils.getClassName(window) !== "XPCNativeWrapper", "window is not a native wrapper");
     ok(utils.getClassName(XPCNativeWrapper.unwrap(new XPCNativeWrapper(window))) !== "XPCSafeJSObjectWrapper",
        "unwrapping a chrome object returns the object itself");
--- a/js/src/xpconnect/tests/mochitest/bug500931_helper.html
+++ b/js/src/xpconnect/tests/mochitest/bug500931_helper.html
@@ -1,7 +1,8 @@
 <html>
     <head>
         <title>Inner frame for bug 500931 mochitest</title>
+        <script>x = 42;</script>
     </head>
     <body>
     </body>
 </html>