if (NS_FAILED(rv = CreateScriptObject(context, document)))
nsAutoString className; nsCAutoString classStr; interfaceElement->GetAttribute(kNameSpaceID_None, kNameAtom, className); if (!className.IsEmpty()) { classStr.AssignWithConversion(className); } else { GetBindingURI(classStr); }  JSObject* scriptObject; JSObject* classObject; if (NS_FAILED(rv = InitClass(classStr, context, document, (void**)&scriptObject, (void**)&classObject)))
nsXBLBinding::InstallProperties(nsIContent* aBoundElement){  // Always install the base class properties first, so that  // derived classes can reference the base class properties.  if (mNextBinding)    mNextBinding->InstallProperties(aBoundElement);   // Fetch the handlers element for this binding.  nsCOMPtr<nsIContent> interfaceElement;  GetImmediateChild(kInterfaceAtom, getter_AddRefs(interfaceElement));  if (interfaceElement) {    // Get our bound element's script context.    nsresult rv;    nsCOMPtr<nsIDocument> document;    mBoundElement->GetDocument(*getter_AddRefs(document));    if (!document)      return NS_OK;    nsCOMPtr<nsIScriptGlobalObject> global;    document->GetScriptGlobalObject(getter_AddRefs(global));    if (!global)      return NS_OK;    nsCOMPtr<nsIScriptContext> context;    rv = global->GetContext(getter_AddRefs(context));    if (NS_FAILED(rv)) return rv;    // Create our script object.    if (NS_FAILED(rv = CreateScriptObject(context, document)))      return rv;    JSContext* cx = (JSContext*)context->GetNativeContext();    // Do a walk.    PRInt32 childCount;    interfaceElement->ChildCount(childCount);    for (PRInt32 i = 0; i < childCount; i++) {      nsCOMPtr<nsIContent> child;      interfaceElement->ChildAt(i, *getter_AddRefs(child));      // See if we're a property or a method.      nsCOMPtr<nsIAtom> tagName;      child->GetTag(*getter_AddRefs(tagName));      if (tagName.get() == kMethodAtom) {        // Obtain our name attribute.        nsAutoString name, body;        child->GetAttribute(kNameSpaceID_None, kNameAtom, name);        // Now walk all of our args.        // XXX I'm lame. 32 max args allowed.        char* args[32];        PRUint32 argCount = 0;        PRInt32 kidCount;        child->ChildCount(kidCount);        for (PRInt32 j = 0; j < kidCount; j++)        {          nsCOMPtr<nsIContent> arg;          child->ChildAt(j, *getter_AddRefs(arg));          nsCOMPtr<nsIAtom> kidTagName;          arg->GetTag(*getter_AddRefs(kidTagName));          if (kidTagName.get() == kArgumentAtom) {            // Get the argname and add it to the array.            nsAutoString argName;            arg->GetAttribute(kNameSpaceID_None, kNameAtom, argName);            char* argStr = argName.ToNewCString();            args[argCount] = argStr;            argCount++;          }          else if (kidTagName.get() == kBodyAtom) {            PRInt32 textCount;            arg->ChildCount(textCount);                        for (PRInt32 k = 0; k < textCount; k++) {              // Get the child.              nsCOMPtr<nsIContent> textChild;              arg->ChildAt(k, *getter_AddRefs(textChild));              nsCOMPtr<nsIDOMText> text(do_QueryInterface(textChild));              if (text) {                nsAutoString data;                text->GetData(data);                body += data;              }            }          }        }        // Now that we have a body and args, compile the function        // and then define it as a property.        if (!body.IsEmpty()) {          void* myFunc;          nsCAutoString cname; cname.AssignWithConversion(name.GetUnicode());          rv = context->CompileFunction(mScriptObject,                                        cname,                                        argCount,                                        (const char**)args,                                        body,                                         nsnull,                                        0,                                        PR_FALSE,                                        &myFunc);        }      }      else if (tagName.get() == kPropertyAtom) {        // Obtain our name attribute.        nsAutoString name;        child->GetAttribute(kNameSpaceID_None, kNameAtom, name);        if (!name.IsEmpty()) {          // We have a property.          nsAutoString getter, setter, readOnly;          child->GetAttribute(kNameSpaceID_None, kOnGetAtom, getter);          child->GetAttribute(kNameSpaceID_None, kOnSetAtom, setter);          child->GetAttribute(kNameSpaceID_None, kReadOnlyAtom, readOnly);          void* getFunc = nsnull;          void* setFunc = nsnull;          uintN attrs = 0;          if (readOnly.EqualsWithConversion("true"))            attrs |= JSPROP_READONLY;          // try for first <getter> tag          if (getter.IsEmpty()) {            PRInt32 childCount;            child->ChildCount(childCount);            nsCOMPtr<nsIContent> getterElement;            for (PRInt32 j=0; j<childCount; j++) {              child->ChildAt(j, *getter_AddRefs(getterElement));                            if (!getterElement) continue;                            nsCOMPtr<nsIAtom> getterTag;              getterElement->GetTag(*getter_AddRefs(getterTag));                            if (getterTag.get() == kGetterAtom) {                GetTextData(getterElement, getter);                break;          // stop at first tag              }            }          }                    if (!getter.IsEmpty()) {            rv = context->CompileFunction(mScriptObject,                                          "onget",                                          0,                                          nsnull,                                          getter,                                           nsnull,                                          0,                                          PR_FALSE,                                          &getFunc);            if (NS_FAILED(rv)) return NS_ERROR_FAILURE;            attrs |= JSPROP_GETTER;          }          // try for first <setter> tag          if (setter.IsEmpty()) {            PRInt32 childCount;            child->ChildCount(childCount);            nsCOMPtr<nsIContent> setterElement;            for (PRInt32 j=0; j<childCount; j++) {              child->ChildAt(j, *getter_AddRefs(setterElement));                            if (!setterElement) continue;                            nsCOMPtr<nsIAtom> setterTag;              setterElement->GetTag(*getter_AddRefs(setterTag));              if (setterTag.get() == kSetterAtom) {                GetTextData(setterElement, setter);                break;          // stop at first tag              }            }          }                    if (!setter.IsEmpty()) {            rv = context->CompileFunction(mScriptObject,                                          "onset",                                          1,                                          gPropertyArg,                                          setter,                                           nsnull,                                          0,                                          PR_FALSE,                                          &setFunc);            if (NS_FAILED(rv)) return NS_ERROR_FAILURE;            attrs |= JSPROP_SETTER;          }          if (getFunc || setFunc) {            // Having either a getter or setter results in the            // destruction of any initial value that might be set.            // This means we only have to worry about defining the getter            // or setter.            ::JS_DefineUCProperty(cx, (JSObject*)mScriptObject, name.GetUnicode(),                                        name.Length(), JSVAL_VOID,                                       (JSPropertyOp) getFunc,                                        (JSPropertyOp) setFunc,                                        attrs);           } else {            // Look for a normal value and just define that.            nsCOMPtr<nsIContent> textChild;            PRInt32 textCount;            child->ChildCount(textCount);            nsAutoString answer;            for (PRInt32 j = 0; j < textCount; j++) {              // Get the child.              child->ChildAt(j, *getter_AddRefs(textChild));              nsCOMPtr<nsIDOMText> text(do_QueryInterface(textChild));              if (text) {                nsAutoString data;                text->GetData(data);                answer += data;              }            }            if (!answer.IsEmpty()) {              // Evaluate our script and obtain a value.              jsval result = nsnull;              PRBool undefined;              rv = context->EvaluateStringWithValue(answer,                                            mScriptObject,                                           nsnull, nsnull, 0, nsnull,                                           (void*) &result, &undefined);                            if (!undefined) {                // Define that value as a property                ::JS_DefineUCProperty(cx, (JSObject*)mScriptObject, name.GetUnicode(),                                            name.Length(), result,                                           nsnull, nsnull,                                           attrs);               }            }          }        }      }    }  }  return NS_OK;}