michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsXBLProtoImplMethod_h__ michael@0: #define nsXBLProtoImplMethod_h__ michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsString.h" michael@0: #include "nsString.h" michael@0: #include "nsXBLMaybeCompiled.h" michael@0: #include "nsXBLProtoImplMember.h" michael@0: #include "nsXBLSerialize.h" michael@0: michael@0: class nsIContent; michael@0: michael@0: struct nsXBLParameter { michael@0: nsXBLParameter* mNext; michael@0: char* mName; michael@0: michael@0: nsXBLParameter(const nsAString& aName) { michael@0: MOZ_COUNT_CTOR(nsXBLParameter); michael@0: mName = ToNewCString(aName); michael@0: mNext = nullptr; michael@0: } michael@0: michael@0: ~nsXBLParameter() { michael@0: MOZ_COUNT_DTOR(nsXBLParameter); michael@0: nsMemory::Free(mName); michael@0: NS_CONTENT_DELETE_LIST_MEMBER(nsXBLParameter, this, mNext); michael@0: } michael@0: }; michael@0: michael@0: struct nsXBLUncompiledMethod { michael@0: nsXBLParameter* mParameters; michael@0: nsXBLParameter* mLastParameter; michael@0: nsXBLTextWithLineNumber mBodyText; michael@0: michael@0: nsXBLUncompiledMethod() : michael@0: mParameters(nullptr), michael@0: mLastParameter(nullptr), michael@0: mBodyText() michael@0: { michael@0: MOZ_COUNT_CTOR(nsXBLUncompiledMethod); michael@0: } michael@0: michael@0: ~nsXBLUncompiledMethod() { michael@0: MOZ_COUNT_DTOR(nsXBLUncompiledMethod); michael@0: delete mParameters; michael@0: } michael@0: michael@0: int32_t GetParameterCount() { michael@0: int32_t result = 0; michael@0: for (nsXBLParameter* curr = mParameters; curr; curr=curr->mNext) michael@0: result++; michael@0: return result; michael@0: } michael@0: michael@0: void AppendBodyText(const nsAString& aText) { michael@0: mBodyText.AppendText(aText); michael@0: } michael@0: michael@0: void AddParameter(const nsAString& aText) { michael@0: nsXBLParameter* param = new nsXBLParameter(aText); michael@0: if (!param) michael@0: return; michael@0: if (!mParameters) michael@0: mParameters = param; michael@0: else michael@0: mLastParameter->mNext = param; michael@0: mLastParameter = param; michael@0: } michael@0: michael@0: void SetLineNumber(uint32_t aLineNumber) { michael@0: mBodyText.SetLineNumber(aLineNumber); michael@0: } michael@0: }; michael@0: michael@0: class nsXBLProtoImplMethod: public nsXBLProtoImplMember michael@0: { michael@0: public: michael@0: nsXBLProtoImplMethod(const char16_t* aName); michael@0: virtual ~nsXBLProtoImplMethod(); michael@0: michael@0: void AppendBodyText(const nsAString& aBody); michael@0: void AddParameter(const nsAString& aName); michael@0: michael@0: void SetLineNumber(uint32_t aLineNumber); michael@0: michael@0: virtual nsresult InstallMember(JSContext* aCx, michael@0: JS::Handle aTargetClassObject) MOZ_OVERRIDE; michael@0: virtual nsresult CompileMember(const nsCString& aClassStr, michael@0: JS::Handle aClassObject) MOZ_OVERRIDE; michael@0: michael@0: virtual void Trace(const TraceCallbacks& aCallbacks, void *aClosure) MOZ_OVERRIDE; michael@0: michael@0: nsresult Read(nsIObjectInputStream* aStream); michael@0: virtual nsresult Write(nsIObjectOutputStream* aStream) MOZ_OVERRIDE; michael@0: michael@0: bool IsCompiled() const michael@0: { michael@0: return mMethod.IsCompiled(); michael@0: } michael@0: michael@0: void SetUncompiledMethod(nsXBLUncompiledMethod* aUncompiledMethod) michael@0: { michael@0: mMethod.SetUncompiled(aUncompiledMethod); michael@0: } michael@0: michael@0: nsXBLUncompiledMethod* GetUncompiledMethod() const michael@0: { michael@0: return mMethod.GetUncompiled(); michael@0: } michael@0: michael@0: protected: michael@0: void SetCompiledMethod(JSObject* aCompiledMethod) michael@0: { michael@0: mMethod.SetJSFunction(aCompiledMethod); michael@0: } michael@0: michael@0: JSObject* GetCompiledMethod() const michael@0: { michael@0: return mMethod.GetJSFunction(); michael@0: } michael@0: michael@0: JSObject* GetCompiledMethodPreserveColor() const michael@0: { michael@0: return mMethod.GetJSFunctionPreserveColor(); michael@0: } michael@0: michael@0: JS::Heap > mMethod; michael@0: }; michael@0: michael@0: class nsXBLProtoImplAnonymousMethod : public nsXBLProtoImplMethod { michael@0: public: michael@0: nsXBLProtoImplAnonymousMethod(const char16_t* aName) : michael@0: nsXBLProtoImplMethod(aName) michael@0: {} michael@0: michael@0: nsresult Execute(nsIContent* aBoundElement); michael@0: michael@0: // Override InstallMember; these methods never get installed as members on michael@0: // binding instantiations (though they may hang out in mMembers on the michael@0: // prototype implementation). michael@0: virtual nsresult InstallMember(JSContext* aCx, michael@0: JS::Handle aTargetClassObject) MOZ_OVERRIDE { michael@0: return NS_OK; michael@0: } michael@0: michael@0: using nsXBLProtoImplMethod::Write; michael@0: nsresult Write(nsIObjectOutputStream* aStream, michael@0: XBLBindingSerializeDetails aType); michael@0: }; michael@0: michael@0: #endif // nsXBLProtoImplMethod_h__