Bug 705344 - Use IDL for Components.(utils.)lookupMethod; r=bholley
authorMs2ger <ms2ger@gmail.com>
Sun, 18 Dec 2011 11:08:04 +0100
changeset 82876 50b70f639c2c2856cd9137a9227d3fdfd0f83d62
parent 82875 d2fd65ac7af1ea1f85152304aa859f5f78fca985
child 82877 93ab106616a2923200c75f9d4bb8227a6316d5ca
push id21708
push userMs2ger@gmail.com
push dateSun, 18 Dec 2011 10:16:31 +0000
treeherdermozilla-central@543af61eee05 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbholley
bugs705344
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 705344 - Use IDL for Components.(utils.)lookupMethod; r=bholley
js/xpconnect/idl/xpccomponents.idl
js/xpconnect/src/XPCComponents.cpp
--- a/js/xpconnect/idl/xpccomponents.idl
+++ b/js/xpconnect/idl/xpccomponents.idl
@@ -1,11 +1,10 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
- *
- * ***** BEGIN LICENSE BLOCK *****
+/* -*- Mode: IDL; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/* ***** BEGIN LICENSE BLOCK *****
  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  *
  * The contents of this file are subject to the Mozilla Public License Version
  * 1.1 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  * http://www.mozilla.org/MPL/
  *
  * Software distributed under the License is distributed on an "AS IS" basis,
@@ -148,17 +147,17 @@ interface nsIXPCComponents_utils_Sandbox
 interface ScheduledGCCallback : nsISupports
 {
     void callback();
 };
 
 /**
 * interface of Components.utils
 */
-[scriptable, uuid(1fbd5d17-707f-4874-9635-59ad2438caf0)]
+[scriptable, uuid(d41d626c-c222-4c38-ba5f-6499baff3b11)]
 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.
@@ -177,17 +176,18 @@ interface nsIXPCComponents_Utils : nsISu
      * It must be called with two params: an object and a method name.
      * It returns a function object or throws an exception on error.
      * This method exists only to solve mozilla browser problems
      * when chrome attempts to lookup and call methods in content
      * and *must* not get confused by method properties that have been
      * replaced in the content JS code. This method is not recommended for
      * any other use.
      */
-    void lookupMethod();
+    [implicit_jscontext]
+    jsval lookupMethod(in jsval obj, in jsval name);
 
     readonly attribute nsIXPCComponents_utils_Sandbox Sandbox;
 
     /*
      * evalInSandbox is designed to be called from JavaScript only.
      *
      * evalInSandbox evaluates the provided source string in the given sandbox.
      * It returns the result of the evaluation to the caller.
@@ -342,17 +342,17 @@ interface nsIXPCComponents_Utils : nsISu
 
     [implicit_jscontext]
     void setGCZeal(in long zeal);
 };
 
 /**
 * interface of JavaScript's 'Components' object
 */
-[scriptable, uuid(155809f1-71f1-47c5-be97-d812ba560405)]
+[scriptable, uuid(9d654574-cb2c-4bd0-a0fd-c10443fe2135)]
 interface nsIXPCComponents : nsISupports
 {
     readonly attribute nsIXPCComponents_Interfaces      interfaces;
     readonly attribute nsIXPCComponents_InterfacesByID  interfacesByID;
     readonly attribute nsIXPCComponents_Classes         classes;
     readonly attribute nsIXPCComponents_ClassesByID     classesByID;
     readonly attribute nsIStackFrame                    stack;
     readonly attribute nsIXPCComponents_Results         results;
@@ -364,16 +364,17 @@ interface nsIXPCComponents : nsISupports
     readonly attribute nsIXPCComponents_Constructor     Constructor;
 
     boolean isSuccessCode(in nsresult result);
 
     /* @deprecated Use Components.utils.lookupMethod instead.
      * (But are you sure you really want this method any more?
      *  See http://developer-test.mozilla.org/en/docs/XPCNativeWrapper )
      */
-    [deprecated] void lookupMethod();
+    [deprecated,implicit_jscontext]
+    jsval lookupMethod(in jsval obj, in jsval name);
 
     /* @deprecated Use Components.utils.reportError instead. */
     [deprecated] void reportError();
 
     /* 'lastResult' is accessible via JavaScript only */
     /* 'returnCode' is accessible via JavaScript only */
 };
--- a/js/xpconnect/src/XPCComponents.cpp
+++ b/js/xpconnect/src/XPCComponents.cpp
@@ -2603,110 +2603,62 @@ nsXPCComponents_Utils::GetSandbox(nsIXPC
         return NS_ERROR_OUT_OF_MEMORY;
     }
     NS_ADDREF(*aSandbox = mSandbox);
     return NS_OK;
 }
 
 /* void lookupMethod (); */
 NS_IMETHODIMP
-nsXPCComponents_Utils::LookupMethod()
+nsXPCComponents_Utils::LookupMethod(const JS::Value& object,
+                                    const JS::Value& name,
+                                    JSContext *cx,
+                                    JS::Value *retval)
 {
-    nsresult rv;
-
-    nsCOMPtr<nsIXPConnect> xpc(do_GetService(nsIXPConnect::GetCID(), &rv));
-    if (NS_FAILED(rv))
-        return NS_ERROR_FAILURE;
-
-    // get the xpconnect native call context
-    nsAXPCNativeCallContext *cc = nsnull;
-    xpc->GetCurrentNativeCallContext(&cc);
-    if (!cc)
-        return NS_ERROR_FAILURE;
-
-// Check disabled until deprecated Components.lookupMethod removed.
-#undef CHECK_FOR_INDIRECT_CALL
-#ifdef CHECK_FOR_INDIRECT_CALL
-    // verify that we are being called from JS (i.e. the current call is
-    // to this object - though we don't verify that it is to this exact method)
-    nsCOMPtr<nsISupports> callee;
-    cc->GetCallee(getter_AddRefs(callee));
-    if (!callee || callee.get() !=
-        static_cast<const nsISupports*>
-                   (static_cast<const nsIXPCComponents_Utils*>(this)))
-        return NS_ERROR_FAILURE;
-#endif
-
-    // Get JSContext of current call
-    JSContext* cx;
-    rv = cc->GetJSContext(&cx);
-    if (NS_FAILED(rv) || !cx)
-        return NS_ERROR_FAILURE;
-
     JSAutoRequest ar(cx);
 
-    // get place for return value
-    jsval *retval = nsnull;
-    rv = cc->GetRetValPtr(&retval);
-    if (NS_FAILED(rv) || !retval)
-        return NS_ERROR_FAILURE;
-
-    // get argc and argv and verify arg count
-    PRUint32 argc;
-    rv = cc->GetArgc(&argc);
-    if (NS_FAILED(rv))
-        return NS_ERROR_FAILURE;
-
-    if (argc < 2)
-        return NS_ERROR_XPC_NOT_ENOUGH_ARGS;
-
-    jsval* argv;
-    rv = cc->GetArgvPtr(&argv);
-    if (NS_FAILED(rv) || !argv)
-        return NS_ERROR_FAILURE;
-
     // first param must be a JSObject
-    if (JSVAL_IS_PRIMITIVE(argv[0]))
+    if (JSVAL_IS_PRIMITIVE(object))
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
-    JSObject* obj = JSVAL_TO_OBJECT(argv[0]);
+    JSObject* obj = JSVAL_TO_OBJECT(object);
     while (obj && !js::IsWrapper(obj) && !IS_WRAPPER_CLASS(js::GetObjectClass(obj)))
         obj = JS_GetPrototype(cx, obj);
 
     if (!obj)
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
-    argv[0] = OBJECT_TO_JSVAL(obj);
-    rv = nsXPConnect::GetXPConnect()->GetJSObjectOfWrapper(cx, obj, &obj);
+    JSObject* unwrappedObject;
+    nsresult rv = nsXPConnect::GetXPConnect()->GetJSObjectOfWrapper(cx, obj, &unwrappedObject);
     if (NS_FAILED(rv))
         return rv;
 
-    obj = JS_ObjectToInnerObject(cx, obj);
-    if (!obj)
+    unwrappedObject = JS_ObjectToInnerObject(cx, unwrappedObject);
+    if (!unwrappedObject)
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
     // second param must be a string
-    if (!JSVAL_IS_STRING(argv[1]))
+    if (!JSVAL_IS_STRING(name))
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
-    // Make sure the name (argv[1]) that we use for looking up the
-    // method/property is atomized.
-
+    // Make sure the name that we use for looking up the method/property is
+    // atomized.
     jsid name_id;
-    if (!JS_ValueToId(cx, argv[1], &name_id) ||
-        !JS_IdToValue(cx, name_id, &argv[1]))
+    JS::Value dummy;
+    if (!JS_ValueToId(cx, name, &name_id) ||
+        !JS_IdToValue(cx, name_id, &dummy))
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
     // this will do verification and the method lookup for us
     // Note that if |obj| is an XPCNativeWrapper this will all still work.
     // We'll hand back the same method that we'd hand back for the underlying
     // XPCWrappedNative.  This means no deep wrapping, unfortunately, but we
     // can't keep track of both the underlying function and the
     // XPCNativeWrapper at once in a single parent slot...
-    XPCCallContext inner_cc(JS_CALLER, cx, obj, nsnull, name_id);
+    XPCCallContext inner_cc(JS_CALLER, cx, unwrappedObject, nsnull, name_id);
 
     // was our jsobject really a wrapped native at all?
     XPCWrappedNative* wrapper = inner_cc.GetWrapper();
     if (!wrapper || !wrapper->IsValid())
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
     // did we find a method/attribute by that name?
     XPCNativeMember* member = inner_cc.GetMember();
@@ -2716,29 +2668,24 @@ nsXPCComponents_Utils::LookupMethod()
     // 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;
 
     jsval funval;
 
     // get (and perhaps lazily create) the member's cloned function
-    if (!member->NewFunctionObject(inner_cc, iface,
-                                   JSVAL_TO_OBJECT(argv[0]),
-                                   &funval))
+    if (!member->NewFunctionObject(inner_cc, iface, obj, &funval))
         return NS_ERROR_XPC_BAD_CONVERT_JS;
 
     NS_ASSERTION(JS_ValueToFunction(inner_cc, funval),
                  "Function is not a function");
 
     // Stick the function in the return value. This roots it.
     *retval = funval;
-
-    // Tell XPConnect that we returned the function through the call context.
-    cc->SetReturnValueWasSet(true);
     return NS_OK;
 }
 
 /* void reportError (); */
 NS_IMETHODIMP
 nsXPCComponents_Utils::ReportError()
 {
     // This function shall never fail! Silently eat any failure conditions.
@@ -4342,27 +4289,30 @@ nsXPCComponents::AttachNewComponentsObje
 
     return NS_SUCCEEDED(wrapper->GetJSObject(&obj)) &&
            obj && JS_DefinePropertyById(ccx, aGlobal, id, OBJECT_TO_JSVAL(obj),
                                         nsnull, nsnull,
                                         JSPROP_PERMANENT | JSPROP_READONLY);
 }
 
 /* void lookupMethod (); */
-NS_IMETHODIMP nsXPCComponents::LookupMethod()
+NS_IMETHODIMP
+nsXPCComponents::LookupMethod(const JS::Value& object,
+                              const JS::Value& name,
+                              JSContext *cx,
+                              JS::Value *retval)
 {
-    nsresult rv;
+    NS_WARNING("Components.lookupMethod deprecated, use Components.utils.lookupMethod");
+
     nsCOMPtr<nsIXPCComponents_Utils> utils;
-
-    NS_WARNING("Components.lookupMethod deprecated, use Components.utils.lookupMethod");
-    rv = GetUtils(getter_AddRefs(utils));
+    nsresult rv = GetUtils(getter_AddRefs(utils));
     if (NS_FAILED(rv))
         return rv;
 
-    return utils->LookupMethod();
+    return utils->LookupMethod(object, name, cx, retval);
 }
 
 /* void reportError (); */
 NS_IMETHODIMP nsXPCComponents::ReportError()
 {
     nsresult rv;
     nsCOMPtr<nsIXPCComponents_Utils> utils;