Bug 761695 - Stop stashing a raw WN pointer in XPCWN Xray holders. r=peterv
authorBobby Holley <bobbyholley@gmail.com>
Fri, 05 Oct 2012 18:59:22 +0200
changeset 109389 cf00f8f3f80dc41c497ce3d5715365784aa6c132
parent 109388 26ffd02bc89ce5b1df54129ca7e8d872f5414f4f
child 109390 8d62ef879869f54b2c7c5ee6155a64924a41619f
push id16008
push userbobbyholley@gmail.com
push dateFri, 05 Oct 2012 16:59:43 +0000
treeherdermozilla-inbound@3b4562fd4f20 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspeterv
bugs761695
milestone18.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 761695 - Stop stashing a raw WN pointer in XPCWN Xray holders. r=peterv We might as well do this dynamically, which simplifies the code. Note that we could avoid the reserved slot by parenting the holder to the wrapper. But the JS parent API is deprecated, and we need to move away from it to reserved slots anyhow. We might as well start here, with the added advantage that parenting to the global makes us consistent with the other Xray types.
js/xpconnect/wrappers/XrayWrapper.cpp
--- a/js/xpconnect/wrappers/XrayWrapper.cpp
+++ b/js/xpconnect/wrappers/XrayWrapper.cpp
@@ -23,17 +23,17 @@
 #include "mozilla/dom/BindingUtils.h"
 
 using namespace mozilla::dom;
 
 namespace xpc {
 
 using namespace js;
 
-static const uint32_t JSSLOT_WN = 0;
+static const uint32_t JSSLOT_WRAPPER = 0;
 static const uint32_t JSSLOT_RESOLVING = 1;
 
 static XPCWrappedNative *GetWrappedNative(JSObject *obj);
 
 namespace XrayUtils {
 
 JSClass HolderClass = {
     "NativePropertyHolder",
@@ -255,29 +255,17 @@ JSObject *
 createHolder(JSContext *cx, JSObject *wrapper)
 {
     JSObject *global = JS_GetGlobalForObject(cx, wrapper);
     JSObject *holder = JS_NewObjectWithGivenProto(cx, &HolderClass, nullptr,
                                                   global);
     if (!holder)
         return nullptr;
 
-    JSObject *inner = js::UnwrapObject(wrapper, /* stopAtOuter = */ false);
-    XPCWrappedNative *wn = GetWrappedNative(inner);
-
-    // A note about ownership: the holder has a direct pointer to the wrapped
-    // native that we're wrapping. Normally, we'd have to AddRef the pointer
-    // so that it doesn't have to be collected, but then we'd have to tell the
-    // cycle collector. Fortunately for us, we know that the Xray wrapper
-    // itself has a reference to the flat JS object which will hold the
-    // wrapped native alive. Furthermore, the reachability of that object and
-    // the associated holder are exactly the same, so we can use that for our
-    // strong reference.
-    MOZ_ASSERT(IS_WN_WRAPPER(inner));
-    js::SetReservedSlot(holder, JSSLOT_WN, PrivateValue(wn));
+    js::SetReservedSlot(holder, JSSLOT_WRAPPER, ObjectValue(*wrapper));
     js::SetReservedSlot(holder, JSSLOT_RESOLVING, PrivateValue(NULL));
     return holder;
 }
 
 }
 
 using namespace XrayUtils;
 
@@ -476,17 +464,18 @@ GetWrappedNative(JSObject *obj)
     MOZ_ASSERT(IS_WN_WRAPPER_OBJECT(obj));
     return static_cast<XPCWrappedNative *>(js::GetObjectPrivate(obj));
 }
 
 static XPCWrappedNative *
 GetWrappedNativeFromHolder(JSObject *holder)
 {
     MOZ_ASSERT(js::GetObjectJSClass(holder) == &HolderClass);
-    return static_cast<XPCWrappedNative *>(js::GetReservedSlot(holder, JSSLOT_WN).toPrivate());
+    JSObject *wrapper = &js::GetReservedSlot(holder, JSSLOT_WRAPPER).toObject();
+    return GetWrappedNative(js::UnwrapObject(wrapper, /* stopAtOuter = */ false));
 }
 
 static JSObject *
 GetWrappedNativeObjectFromHolder(JSObject *holder)
 {
     return GetWrappedNativeFromHolder(holder)->GetFlatJSObject();
 }