Bug 541742 - Make XPCNativeWrapper.unwrap work. r=bzbarsky
--- 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>