Back to index

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

#include <nsXBLProtoImplProperty.h>

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

List of all members.

Public Member Functions

 nsXBLProtoImplProperty (const PRUnichar *aName, const PRUnichar *aGetter, const PRUnichar *aSetter, const PRUnichar *aReadOnly)
virtual ~nsXBLProtoImplProperty ()
virtual void Destroy (PRBool aIsCompiled)
void AppendGetterText (const nsAString &aGetter)
void AppendSetterText (const nsAString &aSetter)
void SetGetterLineNumber (PRUint32 aLineNumber)
void SetSetterLineNumber (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 {
nsXBLTextWithLineNumbermGetterText
JSObjectmJSGetterObject
}; 
union {
nsXBLTextWithLineNumbermSetterText
JSObjectmJSSetterObject
}; 
uintN mJSAttributes
nsXBLProtoImplMembermNext
PRUnicharmName

Friends

struct nsAutoGCRoot

Detailed Description

Definition at line 49 of file nsXBLProtoImplProperty.h.


Constructor & Destructor Documentation

Definition at line 54 of file nsXBLProtoImplProperty.cpp.

                                                                           :
  nsXBLProtoImplMember(aName), 
  mGetterText(nsnull),
  mSetterText(nsnull),
  mJSAttributes(JSPROP_ENUMERATE)
#ifdef DEBUG
  , mIsCompiled(PR_FALSE)
#endif
{
  MOZ_COUNT_CTOR(nsXBLProtoImplProperty);

  if (aReadOnly) {
    nsAutoString readOnly; readOnly.Assign(*aReadOnly);
    if (readOnly.LowerCaseEqualsLiteral("true"))
      mJSAttributes |= JSPROP_READONLY;
  }

  if (aGetter)
    AppendGetterText(nsDependentString(aGetter));
  if (aSetter)
    AppendSetterText(nsDependentString(aSetter));
}

Member Function Documentation

Definition at line 109 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing getter text");
  if (!mGetterText) {
    mGetterText = new nsXBLTextWithLineNumber();
    if (!mGetterText)
      return;
  }

  mGetterText->AppendText(aText);
}

Here is the caller graph for this function:

Definition at line 123 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing setter text");
  if (!mSetterText) {
    mSetterText = new nsXBLTextWithLineNumber();
    if (!mSetterText)
      return;
  }

  mSetterText->AppendText(aText);
}

Here is the caller graph for this function:

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

Implements nsXBLProtoImplMember.

Definition at line 224 of file nsXBLProtoImplProperty.cpp.

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

  if (!mName)
    return NS_ERROR_FAILURE; // Without a valid name, we can't install the member.

  // We have a property.
  nsresult rv = NS_OK;

  nsCAutoString functionUri;
  if (mGetterText || mSetterText) {
    functionUri = aClassStr;
    PRInt32 hash = functionUri.RFindChar('#');
    if (hash != kNotFound) {
      functionUri.Truncate(hash);
    }
  }

  PRBool deletedGetter = PR_FALSE;
  if (mGetterText && mGetterText->GetText()) {
    nsDependentString getter(mGetterText->GetText());
    if (!getter.IsEmpty()) {
      // Compile into a temp object so we don't wipe out mGetterText
      JSObject* getterObject = nsnull;
      rv = aContext->CompileFunction(aClassObject,
                                     NS_LITERAL_CSTRING("get_") +
                                     NS_ConvertUCS2toUTF8(mName),
                                     0,
                                     nsnull,
                                     getter, 
                                     functionUri.get(),
                                     mGetterText->GetLineNumber(),
                                     PR_TRUE,
                                     (void **) &getterObject);

      // Make sure we free mGetterText here before setting mJSGetterObject, since
      // that'll overwrite mGetterText
      delete mGetterText;
      deletedGetter = PR_TRUE;
      mJSGetterObject = getterObject;
    
      if (mJSGetterObject && NS_SUCCEEDED(rv)) {
        mJSAttributes |= JSPROP_GETTER | JSPROP_SHARED;
        // Root the compiled prototype script object.
        rv = nsContentUtils::AddJSGCRoot(&mJSGetterObject,
                                         "nsXBLProtoImplProperty::mJSGetterObject");
      }
      if (NS_FAILED(rv)) {
        mJSGetterObject = nsnull;
        mJSAttributes &= ~JSPROP_GETTER;
        /*chaining to return failure*/
      }
    }
  } // if getter is not empty

  if (!deletedGetter) {  // Empty getter
    delete mGetterText;
    mJSGetterObject = nsnull;
  }
  
  if (NS_FAILED(rv)) {
    // We failed to compile our getter.  So either we've set it to null, or
    // it's still set to the text object.  In either case, it's safe to return
    // the error here, since then we'll be cleaned up as uncompiled and that
    // will be ok.  Going on and compiling the setter and _then_ returning an
    // error, on the other hand, will try to clean up a compiled setter as
    // uncompiled and crash.
    return rv;
  }

  PRBool deletedSetter = PR_FALSE;
  if (mSetterText && mSetterText->GetText()) {
    nsDependentString setter(mSetterText->GetText());
    if (!setter.IsEmpty()) {
      // Compile into a temp object so we don't wipe out mSetterText
      JSObject* setterObject = nsnull;
      rv = aContext->CompileFunction(aClassObject,
                                     NS_LITERAL_CSTRING("set_") +
                                     NS_ConvertUCS2toUTF8(mName),
                                     1,
                                     gPropertyArgs,
                                     setter, 
                                     functionUri.get(),
                                     mSetterText->GetLineNumber(),
                                     PR_TRUE,
                                     (void **) &setterObject);

      // Make sure we free mSetterText here before setting mJSGetterObject, since
      // that'll overwrite mSetterText
      delete mSetterText;
      deletedSetter = PR_TRUE;
      mJSSetterObject = setterObject;

      if (mJSSetterObject && NS_SUCCEEDED(rv)) {
        mJSAttributes |= JSPROP_SETTER | JSPROP_SHARED;
        // Root the compiled prototype script object.
        rv = nsContentUtils::AddJSGCRoot(&mJSSetterObject,
                                         "nsXBLProtoImplProperty::mJSSetterObject");
      }
      if (NS_FAILED(rv)) {
        mJSSetterObject = nsnull;
        mJSAttributes &= ~JSPROP_SETTER;
        /*chaining to return failure*/
      }
    }
  } // if setter wasn't empty....

  if (!deletedSetter) {  // Empty setter
    delete mSetterText;
    mJSSetterObject = nsnull;
  }

#ifdef DEBUG
  mIsCompiled = NS_SUCCEEDED(rv);
#endif
  
  return rv;
}

Here is the call graph for this function:

void nsXBLProtoImplProperty::Destroy ( PRBool  aIsCompiled) [virtual]

Implements nsXBLProtoImplMember.

Definition at line 86 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(aIsCompiled == mIsCompiled,
                  "Incorrect aIsCompiled in nsXBLProtoImplProperty::Destroy");

  if ((mJSAttributes & JSPROP_GETTER) && mJSGetterObject) {
    nsContentUtils::RemoveJSGCRoot(&mJSGetterObject);
  }
  else {
    delete mGetterText;
  }

  if ((mJSAttributes & JSPROP_SETTER) && mJSSetterObject) {
    nsContentUtils::RemoveJSGCRoot(&mJSSetterObject);
  }
  else {
    delete mSetterText;
  }

  mGetterText = mSetterText = 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 nsXBLProtoImplProperty::InstallMember ( nsIScriptContext aContext,
nsIContent aBoundElement,
void aScriptObject,
void aTargetClassObject,
const nsCString aClassStr 
) [virtual]

Implements nsXBLProtoImplMember.

Definition at line 167 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(mIsCompiled,
                  "Should not be installing an uncompiled property");
  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 ((mJSGetterObject || mJSSetterObject) && targetClassObject) {
    JSObject * getter = nsnull;
    if (mJSGetterObject)
      if (!(getter = ::JS_CloneFunctionObject(cx, mJSGetterObject, globalObject)))
        return NS_ERROR_OUT_OF_MEMORY;

    nsresult rv;
    nsAutoGCRoot getterroot(&getter, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    
    JSObject * setter = nsnull;
    if (mJSSetterObject)
      if (!(setter = ::JS_CloneFunctionObject(cx, mJSSetterObject, globalObject)))
        return NS_ERROR_OUT_OF_MEMORY;

    nsAutoGCRoot setterroot(&setter, &rv);
    NS_ENSURE_SUCCESS(rv, rv);
    
    nsDependentString name(mName);
    if (!::JS_DefineUCProperty(cx, targetClassObject,
                               NS_REINTERPRET_CAST(const jschar*, mName), 
                               name.Length(), JSVAL_VOID,  (JSPropertyOp) getter, 
                               (JSPropertyOp) setter, mJSAttributes))
      return NS_ERROR_OUT_OF_MEMORY;
  }
  return NS_OK;
}

Here is the call graph for this function:

Definition at line 137 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing getter text");
  if (!mGetterText) {
    mGetterText = new nsXBLTextWithLineNumber();
    if (!mGetterText)
      return;
  }

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

Definition at line 151 of file nsXBLProtoImplProperty.cpp.

{
  NS_PRECONDITION(!mIsCompiled,
                  "Must not be compiled when accessing setter text");
  if (!mSetterText) {
    mSetterText = new nsXBLTextWithLineNumber();
    if (!mSetterText)
      return;
  }

  mSetterText->SetLineNumber(aLineNumber);
}

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]
union { ... } [protected]

Definition at line 90 of file nsXBLProtoImplProperty.h.

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: