Back to index

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

#include <nsXBLProtoImplMethod.h>

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

List of all members.

Public Member Functions

 nsXBLProtoImplMethod (const PRUnichar *aName)
virtual ~nsXBLProtoImplMethod ()
virtual void Destroy (PRBool aIsCompiled)
void AppendBodyText (const nsAString &aBody)
void AddParameter (const nsAString &aName)
void SetLineNumber (PRUint32 aLineNumber)
virtual nsresult InstallMember (nsIScriptContext *aContext, nsIContent *aBoundElement, void *aScriptObject, void *aTargetClassObject, const nsCString &aClassStr)
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 115 of file nsXBLProtoImplMethod.h.


Constructor & Destructor Documentation

Definition at line 55 of file nsXBLProtoImplMethod.cpp.

                                                                 :
  nsXBLProtoImplMember(aName), 
  mUncompiledMethod(nsnull)
#ifdef DEBUG
  , mIsCompiled(PR_FALSE)
#endif
{
  MOZ_COUNT_CTOR(nsXBLProtoImplMethod);
}

Member Function Documentation

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:

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]

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]

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 106 of file nsXBLProtoImplMember.h.

{ return mNext; };

Here is the caller graph for this function:

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

Implements nsXBLProtoImplMember.

Reimplemented in nsXBLProtoImplAnonymousMethod.

Definition at line 129 of file nsXBLProtoImplMethod.cpp.

{
  NS_PRECONDITION(mIsCompiled,
                  "Should not be installing an uncompiled method");
  JSContext* cx = (JSContext*) aContext->GetNativeContext();

  nsIDocument *ownerDoc = aBoundElement->GetOwnerDoc();
  nsIScriptGlobalObject *sgo;

  if (!ownerDoc || !(sgo = ownerDoc->GetScriptGlobalObject())) {
    NS_ERROR("Can't find global object for bound content!");
 
    return NS_ERROR_UNEXPECTED;
  }

  JSObject * scriptObject = (JSObject *) aScriptObject;
  NS_ASSERTION(scriptObject, "uh-oh, script Object should NOT be null or bad things will happen");
  if (!scriptObject)
    return NS_ERROR_FAILURE;

  JSObject * targetClassObject = (JSObject *) aTargetClassObject;
  JSObject * globalObject = sgo->GetGlobalJSObject();

  // now we want to reevaluate our property using aContext and the script object for this window...
  if (mJSMethodObject && targetClassObject) {
    nsDependentString name(mName);
    JSObject * method = ::JS_CloneFunctionObject(cx, mJSMethodObject, globalObject);
    if (!method) {
      return NS_ERROR_OUT_OF_MEMORY;
    }

    nsresult rv;
    nsAutoGCRoot root(&method, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    
    if (!::JS_DefineUCProperty(cx, targetClassObject,
                               NS_REINTERPRET_CAST(const jschar*, mName), 
                               name.Length(), OBJECT_TO_JSVAL(method),
                               NULL, NULL, JSPROP_ENUMERATE)) {
      return NS_ERROR_OUT_OF_MEMORY;
    }
  }
  return NS_OK;
}

Here is the call graph for this function:

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]
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: