1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=4 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is mozilla.org code, released
18 : * June 24, 2010.
19 : *
20 : * The Initial Developer of the Original Code is
21 : * The Mozilla Foundation
22 : *
23 : * Contributor(s):
24 : * Andreas Gal <gal@mozilla.com>
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either of the GNU General Public License Version 2 or later (the "GPL"),
28 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #ifndef dombindings_h
41 : #define dombindings_h
42 :
43 : #include "jsapi.h"
44 : #include "jsproxy.h"
45 : #include "xpcpublic.h"
46 : #include "nsString.h"
47 :
48 : namespace mozilla {
49 : namespace dom {
50 : namespace binding {
51 :
52 : inline nsWrapperCache*
53 16280 : GetWrapperCache(nsWrapperCache *cache)
54 : {
55 16280 : return cache;
56 : }
57 :
58 : // nsGlobalWindow implements nsWrapperCache, but doesn't always use it. Don't
59 : // try to use it without fixing that first.
60 : class nsGlobalWindow;
61 : inline nsWrapperCache*
62 : GetWrapperCache(nsGlobalWindow *not_allowed);
63 :
64 : inline nsWrapperCache*
65 : GetWrapperCache(void *p)
66 : {
67 : return nsnull;
68 : }
69 :
70 :
71 : class ProxyHandler : public js::ProxyHandler {
72 : protected:
73 : ProxyHandler() : js::ProxyHandler(ProxyFamily())
74 : {
75 : }
76 :
77 : public:
78 : virtual bool isInstanceOf(JSObject *prototype) = 0;
79 : };
80 :
81 : class NoType;
82 : class NoOp {
83 : public:
84 : typedef NoType* T;
85 : enum {
86 : hasOp = 0
87 : };
88 : };
89 :
90 : template<typename Type>
91 : class Op {
92 : public:
93 : typedef Type T;
94 : enum {
95 : hasOp = 1
96 : };
97 : };
98 :
99 : template<typename Type>
100 : class Getter : public Op<Type>
101 : {
102 : };
103 :
104 : template<typename Type>
105 : class Setter : public Op<Type>
106 : {
107 : };
108 :
109 : template<class Getter, class Setter=NoOp>
110 : class Ops
111 : {
112 : public:
113 : typedef Getter G;
114 : typedef Setter S;
115 : };
116 :
117 : typedef Ops<NoOp, NoOp> NoOps;
118 :
119 : template<class ListType, class Base, class IndexOps, class NameOps=NoOps>
120 : class DerivedListClass {
121 : public:
122 : typedef ListType LT;
123 : typedef Base B;
124 : typedef IndexOps IO;
125 : typedef NameOps NO;
126 : };
127 :
128 : class NoBase {
129 : public:
130 : static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope);
131 : static bool shouldCacheProtoShape(JSContext *cx, JSObject *proto, bool *shouldCache)
132 : {
133 : *shouldCache = true;
134 : return true;
135 : }
136 : static bool resolveNativeName(JSContext *cx, JSObject *proxy, jsid id, JSPropertyDescriptor *desc)
137 : {
138 : return true;
139 : }
140 : static bool nativeGet(JSContext *cx, JSObject *proxy, JSObject *proto, jsid id, bool *found,
141 : JS::Value *vp)
142 : {
143 : *found = false;
144 : return true;
145 : }
146 : static nsISupports* nativeToSupports(nsISupports* aNative)
147 : {
148 : return aNative;
149 : }
150 : };
151 :
152 : template<class ListType, class IndexOps, class NameOps=NoOps>
153 : class ListClass : public DerivedListClass<ListType, NoBase, IndexOps, NameOps> {
154 : };
155 :
156 : template<class LC>
157 : class ListBase : public ProxyHandler {
158 : protected:
159 : typedef typename LC::LT ListType;
160 : typedef typename LC::B Base;
161 : typedef typename LC::IO::G::T IndexGetterType;
162 : typedef typename LC::IO::S::T IndexSetterType;
163 : typedef typename LC::NO::G::T NameGetterType;
164 : typedef typename LC::NO::S::T NameSetterType;
165 : enum {
166 : hasIndexGetter = LC::IO::G::hasOp,
167 : hasIndexSetter = LC::IO::S::hasOp,
168 : hasNameGetter = LC::NO::G::hasOp,
169 : hasNameSetter = LC::NO::S::hasOp
170 : };
171 :
172 : private:
173 : friend void Register(nsDOMClassInfoData *aData);
174 :
175 : static ListBase<LC> instance;
176 :
177 : static js::Class sInterfaceClass;
178 :
179 : struct Properties {
180 : jsid &id;
181 : JSPropertyOp getter;
182 : JSStrictPropertyOp setter;
183 : };
184 : struct Methods {
185 : jsid &id;
186 : JSNative native;
187 : unsigned nargs;
188 : };
189 :
190 : static Properties sProtoProperties[];
191 : static size_t sProtoPropertiesCount;
192 : static Methods sProtoMethods[];
193 : static size_t sProtoMethodsCount;
194 :
195 : static JSObject *ensureExpandoObject(JSContext *cx, JSObject *obj);
196 :
197 : static js::Shape *getProtoShape(JSObject *obj);
198 : static void setProtoShape(JSObject *obj, js::Shape *shape);
199 :
200 : static JSBool length_getter(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
201 :
202 : static inline bool getItemAt(ListType *list, uint32_t i, IndexGetterType &item);
203 : static inline bool setItemAt(JSContext *cx, ListType *list, uint32_t i, IndexSetterType item);
204 :
205 : static inline bool namedItem(JSContext *cx, JSObject *obj, jsval *name, NameGetterType &result,
206 : bool *hasResult);
207 :
208 : static inline bool getNamedItem(ListType *list, const nsAString& aName, NameGetterType &item);
209 : static inline bool setNamedItem(JSContext *cx, ListType *list, const nsAString& aName,
210 : NameSetterType item);
211 :
212 : static bool getPropertyOnPrototype(JSContext *cx, JSObject *proxy, jsid id, bool *found,
213 : JS::Value *vp);
214 : static bool hasPropertyOnPrototype(JSContext *cx, JSObject *proxy, jsid id);
215 :
216 : public:
217 : static JSObject *create(JSContext *cx, XPCWrappedNativeScope *scope, ListType *list,
218 : nsWrapperCache* cache, bool *triedToWrap);
219 :
220 : static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope, bool *enabled)
221 : {
222 : *enabled = true;
223 : return getPrototype(cx, scope);
224 : }
225 :
226 : bool getPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
227 : JSPropertyDescriptor *desc);
228 : bool getOwnPropertyDescriptor(JSContext *cx, JSObject *proxy, jsid id, bool set,
229 : JSPropertyDescriptor *desc);
230 : bool defineProperty(JSContext *cx, JSObject *proxy, jsid id,
231 : JSPropertyDescriptor *desc);
232 : bool getOwnPropertyNames(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
233 : bool delete_(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
234 : bool enumerate(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
235 : bool fix(JSContext *cx, JSObject *proxy, JS::Value *vp);
236 :
237 : bool has(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
238 : bool hasOwn(JSContext *cx, JSObject *proxy, jsid id, bool *bp);
239 : bool get(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, JS::Value *vp);
240 : bool getElementIfPresent(JSContext *cx, JSObject *proxy, JSObject *receiver,
241 : uint32_t index, JS::Value *vp, bool *present);
242 : bool set(JSContext *cx, JSObject *proxy, JSObject *receiver, jsid id, bool strict,
243 : JS::Value *vp);
244 : bool keys(JSContext *cx, JSObject *proxy, JS::AutoIdVector &props);
245 : bool iterate(JSContext *cx, JSObject *proxy, unsigned flags, JS::Value *vp);
246 :
247 : /* Spidermonkey extensions. */
248 : bool hasInstance(JSContext *cx, JSObject *proxy, const JS::Value *vp, bool *bp);
249 : JSString *obj_toString(JSContext *cx, JSObject *proxy);
250 : void finalize(JSContext *cx, JSObject *proxy);
251 :
252 : static bool proxyHandlerIsList(js::ProxyHandler *handler) {
253 : return handler == &instance;
254 : }
255 : static bool objIsList(JSObject *obj) {
256 : return js::IsProxy(obj) && proxyHandlerIsList(js::GetProxyHandler(obj));
257 : }
258 : static inline bool instanceIsListObject(JSContext *cx, JSObject *obj, JSObject *callee);
259 : virtual bool isInstanceOf(JSObject *prototype)
260 : {
261 : return js::GetObjectClass(prototype) == &sInterfaceClass;
262 : }
263 : static inline ListType *getListObject(JSObject *obj);
264 :
265 : static JSObject *getPrototype(JSContext *cx, XPCWrappedNativeScope *scope);
266 : static inline bool protoIsClean(JSContext *cx, JSObject *proto, bool *isClean);
267 : static bool shouldCacheProtoShape(JSContext *cx, JSObject *proto, bool *shouldCache);
268 : static bool resolveNativeName(JSContext *cx, JSObject *proxy, jsid id,
269 : JSPropertyDescriptor *desc);
270 : static bool nativeGet(JSContext *cx, JSObject *proxy, JSObject *proto, jsid id, bool *found,
271 : JS::Value *vp);
272 : static ListType *getNative(JSObject *proxy);
273 : static nsISupports* nativeToSupports(ListType* aNative)
274 : {
275 : return Base::nativeToSupports(aNative);
276 : }
277 : };
278 :
279 : struct nsISupportsResult
280 : {
281 : nsISupportsResult()
282 : {
283 : }
284 : nsISupports *mResult;
285 : nsWrapperCache *mCache;
286 : };
287 :
288 : }
289 : }
290 : }
291 :
292 : #include "dombindings_gen.h"
293 :
294 : #endif /* dombindings_h */
|