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