Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef nsXBLProtoImplMethod_h__
8 : #define nsXBLProtoImplMethod_h__
9 :
10 : #include "mozilla/Attributes.h"
11 : #include "nsIAtom.h"
12 : #include "nsString.h"
13 : #include "nsString.h"
14 : #include "nsXBLMaybeCompiled.h"
15 : #include "nsXBLProtoImplMember.h"
16 : #include "nsXBLSerialize.h"
17 :
18 : class nsIContent;
19 :
20 : struct nsXBLParameter {
21 : nsXBLParameter* mNext;
22 : char* mName;
23 :
24 0 : explicit nsXBLParameter(const nsAString& aName) {
25 0 : MOZ_COUNT_CTOR(nsXBLParameter);
26 0 : mName = ToNewCString(aName);
27 0 : mNext = nullptr;
28 0 : }
29 :
30 0 : ~nsXBLParameter() {
31 0 : MOZ_COUNT_DTOR(nsXBLParameter);
32 0 : free(mName);
33 0 : NS_CONTENT_DELETE_LIST_MEMBER(nsXBLParameter, this, mNext);
34 0 : }
35 : };
36 :
37 : struct nsXBLUncompiledMethod {
38 : nsXBLParameter* mParameters;
39 : nsXBLParameter* mLastParameter;
40 : nsXBLTextWithLineNumber mBodyText;
41 :
42 0 : nsXBLUncompiledMethod() :
43 : mParameters(nullptr),
44 : mLastParameter(nullptr),
45 0 : mBodyText()
46 : {
47 0 : MOZ_COUNT_CTOR(nsXBLUncompiledMethod);
48 0 : }
49 :
50 0 : ~nsXBLUncompiledMethod() {
51 0 : MOZ_COUNT_DTOR(nsXBLUncompiledMethod);
52 0 : delete mParameters;
53 0 : }
54 :
55 0 : int32_t GetParameterCount() {
56 0 : int32_t result = 0;
57 0 : for (nsXBLParameter* curr = mParameters; curr; curr=curr->mNext)
58 0 : result++;
59 0 : return result;
60 : }
61 :
62 0 : void AppendBodyText(const nsAString& aText) {
63 0 : mBodyText.AppendText(aText);
64 0 : }
65 :
66 0 : void AddParameter(const nsAString& aText) {
67 0 : nsXBLParameter* param = new nsXBLParameter(aText);
68 0 : if (!mParameters)
69 0 : mParameters = param;
70 : else
71 0 : mLastParameter->mNext = param;
72 0 : mLastParameter = param;
73 0 : }
74 :
75 0 : void SetLineNumber(uint32_t aLineNumber) {
76 0 : mBodyText.SetLineNumber(aLineNumber);
77 0 : }
78 : };
79 :
80 : class nsXBLProtoImplMethod: public nsXBLProtoImplMember
81 : {
82 : public:
83 : explicit nsXBLProtoImplMethod(const char16_t* aName);
84 : virtual ~nsXBLProtoImplMethod();
85 :
86 : void AppendBodyText(const nsAString& aBody);
87 : void AddParameter(const nsAString& aName);
88 :
89 : void SetLineNumber(uint32_t aLineNumber);
90 :
91 : virtual nsresult InstallMember(JSContext* aCx,
92 : JS::Handle<JSObject*> aTargetClassObject) override;
93 : virtual nsresult CompileMember(mozilla::dom::AutoJSAPI& jsapi, const nsString& aClassStr,
94 : JS::Handle<JSObject*> aClassObject) override;
95 :
96 : virtual void Trace(const TraceCallbacks& aCallbacks, void *aClosure) override;
97 :
98 : nsresult Read(nsIObjectInputStream* aStream);
99 : virtual nsresult Write(nsIObjectOutputStream* aStream) override;
100 :
101 1650 : bool IsCompiled() const
102 : {
103 1650 : return mMethod.IsCompiled();
104 : }
105 :
106 0 : void SetUncompiledMethod(nsXBLUncompiledMethod* aUncompiledMethod)
107 : {
108 0 : mMethod.SetUncompiled(aUncompiledMethod);
109 0 : }
110 :
111 579 : nsXBLUncompiledMethod* GetUncompiledMethod() const
112 : {
113 579 : return mMethod.GetUncompiled();
114 : }
115 :
116 : protected:
117 579 : void SetCompiledMethod(JSObject* aCompiledMethod)
118 : {
119 579 : mMethod.SetJSFunction(aCompiledMethod);
120 579 : }
121 :
122 540 : JSObject* GetCompiledMethod() const
123 : {
124 540 : return mMethod.GetJSFunction();
125 : }
126 :
127 579 : JSObject* GetCompiledMethodPreserveColor() const
128 : {
129 579 : return mMethod.GetJSFunctionPreserveColor();
130 : }
131 :
132 : JS::Heap<nsXBLMaybeCompiled<nsXBLUncompiledMethod> > mMethod;
133 : };
134 :
135 0 : class nsXBLProtoImplAnonymousMethod : public nsXBLProtoImplMethod {
136 : public:
137 57 : explicit nsXBLProtoImplAnonymousMethod(const char16_t* aName) :
138 57 : nsXBLProtoImplMethod(aName)
139 57 : {}
140 :
141 : nsresult Execute(nsIContent* aBoundElement, JSAddonId* aAddonId);
142 :
143 : // Override InstallMember; these methods never get installed as members on
144 : // binding instantiations (though they may hang out in mMembers on the
145 : // prototype implementation).
146 42 : virtual nsresult InstallMember(JSContext* aCx,
147 : JS::Handle<JSObject*> aTargetClassObject) override {
148 42 : return NS_OK;
149 : }
150 :
151 : using nsXBLProtoImplMethod::Write;
152 : nsresult Write(nsIObjectOutputStream* aStream,
153 : XBLBindingSerializeDetails aType);
154 : };
155 :
156 : #endif // nsXBLProtoImplMethod_h__
|