Bug 708330 - Use IDL for Components.utils.getWeakReference; r=bholley
authorMs2ger <ms2ger@gmail.com>
Sun, 18 Dec 2011 11:11:56 +0100
changeset 82883 515da54158b2
parent 82882 72a269d3e823
child 82884 21fbcd2f27a8
push id21708
push userMs2ger@gmail.com
push date2011-12-18 10:16 +0000
treeherdermozilla-central@543af61eee05 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs708330
milestone11.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 708330 - Use IDL for Components.utils.getWeakReference; r=bholley
js/xpconnect/idl/xpccomponents.idl
js/xpconnect/src/XPCComponents.cpp
js/xpconnect/src/XPCJSWeakReference.cpp
js/xpconnect/src/XPCJSWeakReference.h
--- a/js/xpconnect/idl/xpccomponents.idl
+++ b/js/xpconnect/idl/xpccomponents.idl
@@ -147,17 +147,17 @@ interface nsIXPCComponents_utils_Sandbox
 interface ScheduledGCCallback : nsISupports
 {
     void callback();
 };
 
 /**
 * interface of Components.utils
 */
-[scriptable, uuid(9e0b065d-a755-482d-ab1f-cc0f26a6fdcd)]
+[scriptable, uuid(b032a8f1-9149-4cbe-bee6-4ac5dfe7c80a)]
 interface nsIXPCComponents_Utils : nsISupports
 {
 
     /* reportError is designed to be called from JavaScript only.
      *
      * It will report a JS Error object to the JS console, and return. It
      * is meant for use in exception handler blocks which want to "eat"
      * an exception, but still want to report it to the console.
@@ -248,17 +248,18 @@ interface nsIXPCComponents_Utils : nsISu
      */
     void unload(in AUTF8String registryLocation);
 
     /*
      * To be called from JS only.
      *
      * Return a weak reference for the given JS object.
      */
-    xpcIJSWeakReference getWeakReference(/* in JSObject obj */);
+    [implicit_jscontext]
+    xpcIJSWeakReference getWeakReference(in jsval obj);
 
     /*
      * To be called from JS only.
      *
      * Force an immediate garbage collection cycle.
      */
     [implicit_jscontext]
     void forceGC();
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -3576,24 +3576,23 @@ nsXPCComponents_Utils::Unload(const nsAC
         do_GetService(MOZJSCOMPONENTLOADER_CONTRACTID);
     if (!moduleloader)
         return NS_ERROR_FAILURE;
     return moduleloader->Unload(registryLocation);
 }
 
 /* xpcIJSWeakReference getWeakReference (); */
 NS_IMETHODIMP
-nsXPCComponents_Utils::GetWeakReference(xpcIJSWeakReference **_retval)
+nsXPCComponents_Utils::GetWeakReference(const JS::Value &object, JSContext *cx,
+                                        xpcIJSWeakReference **_retval)
 {
-    nsRefPtr<xpcJSWeakReference> ref(new xpcJSWeakReference());
-    if (!ref)
-        return NS_ERROR_OUT_OF_MEMORY;
-    ref->Init();
-    *_retval = ref;
-    NS_ADDREF(*_retval);
+    nsRefPtr<xpcJSWeakReference> ref = new xpcJSWeakReference();
+    nsresult rv = ref->Init(cx, object);
+    NS_ENSURE_SUCCESS(rv, rv);
+    ref.forget(_retval);
     return NS_OK;
 }
 
 /* void forceGC (); */
 NS_IMETHODIMP
 nsXPCComponents_Utils::ForceGC(JSContext *cx)
 {
     JS_GC(cx);
--- a/js/xpconnect/src/XPCJSWeakReference.cpp
+++ b/js/xpconnect/src/XPCJSWeakReference.cpp
@@ -39,62 +39,39 @@
 #include "XPCJSWeakReference.h"
 
 xpcJSWeakReference::xpcJSWeakReference()
 {
 }
 
 NS_IMPL_ISUPPORTS1(xpcJSWeakReference, xpcIJSWeakReference)
 
-nsresult xpcJSWeakReference::Init()
+nsresult xpcJSWeakReference::Init(JSContext* cx, const JS::Value& object)
 {
-    nsresult rv;
-
-    nsXPConnect* xpc = nsXPConnect::GetXPConnect();
-    if (!xpc) return NS_ERROR_UNEXPECTED;
-
-    nsAXPCNativeCallContext *cc = nsnull;
-    rv = xpc->GetCurrentNativeCallContext(&cc);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    JSContext *cx = nsnull;
-    rv = cc->GetJSContext(&cx);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    PRUint32 argc = 0;
-    rv = cc->GetArgc(&argc);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    if (argc != 1) return NS_ERROR_FAILURE;
-
-    jsval *argv = nsnull;
-    rv = cc->GetArgvPtr(&argv);
-    NS_ENSURE_SUCCESS(rv, rv);
-
     JSAutoRequest ar(cx);
 
-    if (JSVAL_IS_PRIMITIVE(argv[0])) return NS_ERROR_FAILURE;
+    if (!object.isObject())
+        return NS_ERROR_FAILURE;
 
-    JSObject *obj = JSVAL_TO_OBJECT(argv[0]);
+    JSObject& obj = object.toObject();
 
     XPCCallContext ccx(NATIVE_CALLER, cx);
 
     nsRefPtr<nsXPCWrappedJS> wrapped;
-    rv = nsXPCWrappedJS::GetNewOrUsed(ccx,
-                                      obj,
-                                      NS_GET_IID(nsISupports),
-                                      nsnull,
-                                      getter_AddRefs(wrapped));
+    nsresult rv = nsXPCWrappedJS::GetNewOrUsed(ccx,
+                                               &obj,
+                                               NS_GET_IID(nsISupports),
+                                               nsnull,
+                                               getter_AddRefs(wrapped));
     if (!wrapped) {
         NS_ERROR("can't get nsISupportsWeakReference wrapper for obj");
         return rv;
     }
 
-    return static_cast<nsISupportsWeakReference*>(wrapped)->
-        GetWeakReference(getter_AddRefs(mWrappedJSObject));
+    return wrapped->GetWeakReference(getter_AddRefs(mWrappedJSObject));
 }
 
 NS_IMETHODIMP
 xpcJSWeakReference::Get(JSContext* aCx, JS::Value* aRetval)
 {
     *aRetval = JSVAL_NULL;
 
     if (!mWrappedJSObject) {
--- a/js/xpconnect/src/XPCJSWeakReference.h
+++ b/js/xpconnect/src/XPCJSWeakReference.h
@@ -39,17 +39,17 @@
 
 #include "xpcIJSWeakReference.h"
 #include "nsIWeakReference.h"
 
 class xpcJSWeakReference : public xpcIJSWeakReference
 {
 public:
     xpcJSWeakReference();
-    nsresult Init();
+    nsresult Init(JSContext* cx, const JS::Value& object);
 
     NS_DECL_ISUPPORTS
     NS_DECL_XPCIJSWEAKREFERENCE
 
 private:
     nsCOMPtr<nsIWeakReference> mWrappedJSObject;
 };