Back to index

lightning-sunbird  0.9+nobinonly
Public Member Functions | Protected Attributes | Friends
nsXBLProtoImplAnonymousMethod Class Reference

#include <nsXBLProtoImplMethod.h>

Inheritance diagram for nsXBLProtoImplAnonymousMethod:
Inheritance graph
[legend]
Collaboration diagram for nsXBLProtoImplAnonymousMethod:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 nsXBLProtoImplAnonymousMethod ()
nsresult Execute (nsIContent *aBoundElement)
virtual nsresult InstallMember (nsIScriptContext *aContext, nsIContent *aBoundElement, void *aScriptObject, void *aTargetClassObject, const nsCString &aClassStr)
virtual void Destroy (PRBool aIsCompiled)
void AppendBodyText (const nsAString &aBody)
void AddParameter (const nsAString &aName)
void SetLineNumber (PRUint32 aLineNumber)
virtual nsresult CompileMember (nsIScriptContext *aContext, const nsCString &aClassStr, void *aClassObject)
nsXBLProtoImplMemberGetNext ()
void SetNext (nsXBLProtoImplMember *aNext)

Protected Attributes

union {
nsXBLUncompiledMethodmUncompiledMethod
JSObjectmJSMethodObject
}; 
nsXBLProtoImplMembermNext
PRUnicharmName

Friends

struct nsAutoGCRoot

Detailed Description

Definition at line 147 of file nsXBLProtoImplMethod.h.


Constructor & Destructor Documentation

Definition at line 149 of file nsXBLProtoImplMethod.h.


Member Function Documentation

void nsXBLProtoImplMethod::AddParameter ( const nsAString &  aName) [inherited]

Definition at line 101 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing uncompiled method");
  if (!mUncompiledMethod) {
    mUncompiledMethod = new nsXBLUncompiledMethod();
    if (!mUncompiledMethod)
      return;
  }

  mUncompiledMethod->AddParameter(aText);
}

Here is the caller graph for this function:

void nsXBLProtoImplMethod::AppendBodyText ( const nsAString &  aBody) [inherited]

Definition at line 87 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing uncompiled method");
  if (!mUncompiledMethod) {
    mUncompiledMethod = new nsXBLUncompiledMethod();
    if (!mUncompiledMethod)
      return;
  }

  mUncompiledMethod->AppendBodyText(aText);
}

Here is the caller graph for this function:

nsresult nsXBLProtoImplMethod::CompileMember ( nsIScriptContext aContext,
const nsCString aClassStr,
void aClassObject 
) [virtual, inherited]

Implements nsXBLProtoImplMember.

Definition at line 179 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Trying to compile an already-compiled method");
  NS_PRECONDITION(aClassObject,
                  "Must have class object to compile");

#ifdef DEBUG
  // We have some "ok" early returns after which we consider ourselves compiled
  mIsCompiled = PR_TRUE;
#endif
  
  // No parameters or body was supplied, so don't install method.
  if (!mUncompiledMethod)
    return NS_OK;

  // Don't install method if no name or body was supplied.
  if (!(mName && mUncompiledMethod->mBodyText.GetText())) {
    delete mUncompiledMethod;
    mUncompiledMethod = nsnull;
    return NS_OK;
  }

  nsDependentString body(mUncompiledMethod->mBodyText.GetText());
  if (body.IsEmpty()) {
    delete mUncompiledMethod;
    mUncompiledMethod = nsnull;
    return NS_OK;
  }

#ifdef DEBUG
  // OK, now we have some error early returns that mean we're not
  // really compiled...
  mIsCompiled = PR_FALSE;
#endif

  // We have a method.
  // Allocate an array for our arguments.
  PRInt32 paramCount = mUncompiledMethod->GetParameterCount();
  char** args = nsnull;
  if (paramCount > 0) {
    args = new char*[paramCount];
    if (!args)
      return NS_ERROR_OUT_OF_MEMORY;
  }

  // Add our parameters to our args array.
  PRInt32 argPos = 0; 
  for (nsXBLParameter* curr = mUncompiledMethod->mParameters; 
       curr; 
       curr = curr->mNext) {
    args[argPos] = curr->mName;
    argPos++;
  }

  // Now that we have a body and args, compile the function
  // and then define it.
  NS_ConvertUCS2toUTF8 cname(mName);
  nsCAutoString functionUri(aClassStr);
  PRInt32 hash = functionUri.RFindChar('#');
  if (hash != kNotFound) {
    functionUri.Truncate(hash);
  }

  JSObject* methodObject = nsnull;
  nsresult rv = aContext->CompileFunction(aClassObject,
                                          cname,
                                          paramCount,
                                          (const char**)args,
                                          body, 
                                          functionUri.get(),
                                          mUncompiledMethod->mBodyText.GetLineNumber(),
                                          PR_TRUE,
                                          (void **) &methodObject);

  // Destroy our uncompiled method and delete our arg list.
  delete mUncompiledMethod;
  delete [] args;
  if (NS_FAILED(rv)) {
    mUncompiledMethod = nsnull;
    return rv;
  }

  mJSMethodObject = methodObject;

  if (methodObject) {
    // Root the compiled prototype script object.
    rv = nsContentUtils::AddJSGCRoot(&mJSMethodObject,
                                     "nsXBLProtoImplMethod::mJSMethodObject");
    if (NS_FAILED(rv)) {
      mJSMethodObject = nsnull;
    }
  }
  
#ifdef DEBUG
  mIsCompiled = NS_SUCCEEDED(rv);
#endif
  return rv;
}

Here is the call graph for this function:

void nsXBLProtoImplMethod::Destroy ( PRBool  aIsCompiled) [virtual, inherited]

Implements nsXBLProtoImplMember.

Definition at line 71 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(aIsCompiled == mIsCompiled,
                  "Incorrect aIsCompiled in nsXBLProtoImplMethod::Destroy");
  if (aIsCompiled) {
    if (mJSMethodObject)
      nsContentUtils::RemoveJSGCRoot(&mJSMethodObject);
    mJSMethodObject = nsnull;
  }
  else {
    delete mUncompiledMethod;
    mUncompiledMethod = nsnull;
  }
}

Here is the call graph for this function:

Definition at line 281 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(mIsCompiled, "Can't execute uncompiled method");
  
  if (!mJSMethodObject) {
    // Nothing to do here
    return NS_OK;
  }

  // Get the script context the same way
  // nsXBLProtoImpl::InstallImplementation does.
  nsIDocument* document = aBoundElement->GetOwnerDoc();
  if (!document) {
    return NS_OK;
  }

  nsIScriptGlobalObject* global = document->GetScriptGlobalObject();
  if (!global) {
    return NS_OK;
  }

  nsCOMPtr<nsIScriptContext> context = global->GetContext();
  if (!context) {
    return NS_OK;
  }
  
  JSContext* cx = (JSContext*) context->GetNativeContext();

  JSObject* globalObject = global->GetGlobalJSObject();

  nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
  nsresult rv =
    nsContentUtils::XPConnect()->WrapNative(cx, globalObject,
                                            aBoundElement,
                                            NS_GET_IID(nsISupports),
                                            getter_AddRefs(wrapper));
  NS_ENSURE_SUCCESS(rv, rv);

  JSObject* thisObject;
  rv = wrapper->GetJSObject(&thisObject);
  NS_ENSURE_SUCCESS(rv, rv);

  // Clone the function object, using thisObject as the parent so "this" is in
  // the scope chain of the resulting function (for backwards compat to the
  // days when this was an event handler).
  JSObject* method = ::JS_CloneFunctionObject(cx, mJSMethodObject,
                                              thisObject);
  if (!method) {
    return NS_ERROR_OUT_OF_MEMORY;
  }

  // Now call the method

  // Use nsCxPusher to make sure we call ScriptEvaluated when we're done.
  nsCxPusher pusher;
  NS_ENSURE_STATE(pusher.Push(aBoundElement));


  // Check whether it's OK to call the method.
  rv = nsContentUtils::GetSecurityManager()->CheckFunctionAccess(cx, method, thisObject);

  JSBool ok = JS_TRUE;
  if (NS_SUCCEEDED(rv)) {
    jsval retval;
    ok = ::JS_CallFunctionValue(cx, thisObject, OBJECT_TO_JSVAL(method),
                                0 /* argc */, nsnull /* argv */, &retval);
  }

  if (!ok) {
    // If a constructor or destructor threw an exception, it doesn't
    // stop anything else.  We just report it.
    ::JS_ReportPendingException(cx);
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 106 of file nsXBLProtoImplMember.h.

{ return mNext; };

Here is the caller graph for this function:

virtual nsresult nsXBLProtoImplAnonymousMethod::InstallMember ( nsIScriptContext aContext,
nsIContent aBoundElement,
void aScriptObject,
void aTargetClassObject,
const nsCString aClassStr 
) [inline, virtual]

Reimplemented from nsXBLProtoImplMethod.

Definition at line 158 of file nsXBLProtoImplMethod.h.

                                                             {
    return NS_OK;
  }
void nsXBLProtoImplMethod::SetLineNumber ( PRUint32  aLineNumber) [inherited]

Definition at line 115 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing uncompiled method");
  if (!mUncompiledMethod) {
    mUncompiledMethod = new nsXBLUncompiledMethod();
    if (!mUncompiledMethod)
      return;
  }

  mUncompiledMethod->SetLineNumber(aLineNumber);
}

Here is the caller graph for this function:

void nsXBLProtoImplMember::SetNext ( nsXBLProtoImplMember aNext) [inline, inherited]

Definition at line 107 of file nsXBLProtoImplMember.h.

{ mNext = aNext; };

Here is the caller graph for this function:


Friends And Related Function Documentation

friend struct nsAutoGCRoot [friend, inherited]

Definition at line 119 of file nsXBLProtoImplMember.h.


Member Data Documentation

union { ... } [protected, inherited]
PRUnichar* nsXBLProtoImplMember::mName [protected, inherited]

Definition at line 122 of file nsXBLProtoImplMember.h.

Definition at line 121 of file nsXBLProtoImplMember.h.


The documentation for this class was generated from the following files: