1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=4 sw=4 et tw=79 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 SpiderMonkey JavaScript engine.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * Mozilla Corporation.
21 : * Portions created by the Initial Developer are Copyright (C) 2009
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * 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 jsclass_h__
41 : #define jsclass_h__
42 : /*
43 : * A JSClass acts as a vtable for JS objects that allows JSAPI clients to
44 : * control various aspects of the behavior of an object like property lookup.
45 : * js::Class is an engine-private extension that allows more control over
46 : * object behavior and, e.g., allows custom slow layout.
47 : */
48 : #include "jsapi.h"
49 : #include "jsprvtd.h"
50 :
51 : #ifdef __cplusplus
52 :
53 : namespace js {
54 :
55 : class PropertyName;
56 : class SpecialId;
57 :
58 : static JS_ALWAYS_INLINE jsid
59 : SPECIALID_TO_JSID(const SpecialId &sid);
60 :
61 : /*
62 : * We partition the ways to refer to a property into three: by an index
63 : * (uint32_t); by a string whose characters do not represent an index
64 : * (PropertyName, see vm/String.h); and by various special values.
65 : *
66 : * Special values are encoded using SpecialId, which is layout-compatible but
67 : * non-interconvertible with jsid. A SpecialId may be: an object (used by E4X
68 : * and perhaps eventually by Harmony-proposed private names); JSID_VOID, which
69 : * does not occur in JS scripts but may be used to indicate the absence of a
70 : * valid identifier; or JS_DEFAULT_XML_NAMESPACE_ID, if E4X is enabled.
71 : */
72 :
73 : class SpecialId {
74 : uintptr_t bits;
75 :
76 : /* Needs access to raw bits. */
77 : friend JS_ALWAYS_INLINE jsid SPECIALID_TO_JSID(const SpecialId &sid);
78 :
79 : static const uintptr_t TYPE_VOID = JSID_TYPE_VOID;
80 : static const uintptr_t TYPE_OBJECT = JSID_TYPE_OBJECT;
81 : static const uintptr_t TYPE_DEFAULT_XML_NAMESPACE = JSID_TYPE_DEFAULT_XML_NAMESPACE;
82 : static const uintptr_t TYPE_MASK = JSID_TYPE_MASK;
83 :
84 13592 : SpecialId(uintptr_t bits) : bits(bits) { }
85 :
86 : public:
87 3207311 : SpecialId() : bits(TYPE_VOID) { }
88 :
89 : /* Object-valued */
90 :
91 0 : SpecialId(JSObject &obj)
92 0 : : bits(uintptr_t(&obj) | TYPE_OBJECT)
93 : {
94 0 : JS_ASSERT(&obj != NULL);
95 0 : JS_ASSERT((uintptr_t(&obj) & TYPE_MASK) == 0);
96 0 : }
97 :
98 13592 : bool isObject() const {
99 13592 : return (bits & TYPE_MASK) == TYPE_OBJECT && bits != TYPE_OBJECT;
100 : }
101 :
102 0 : JSObject *toObject() const {
103 0 : JS_ASSERT(isObject());
104 0 : return reinterpret_cast<JSObject *>(bits & ~TYPE_MASK);
105 : }
106 :
107 : /* Empty */
108 :
109 0 : static SpecialId empty() {
110 0 : SpecialId sid(TYPE_OBJECT);
111 0 : JS_ASSERT(sid.isEmpty());
112 : return sid;
113 : }
114 :
115 13592 : bool isEmpty() const {
116 13592 : return bits == TYPE_OBJECT;
117 : }
118 :
119 : /* Void */
120 :
121 0 : static SpecialId voidId() {
122 0 : SpecialId sid(TYPE_VOID);
123 0 : JS_ASSERT(sid.isVoid());
124 : return sid;
125 : }
126 :
127 13592 : bool isVoid() const {
128 13592 : return bits == TYPE_VOID;
129 : }
130 :
131 : /* Default XML namespace */
132 :
133 13592 : static SpecialId defaultXMLNamespace() {
134 13592 : SpecialId sid(TYPE_DEFAULT_XML_NAMESPACE);
135 13592 : JS_ASSERT(sid.isDefaultXMLNamespace());
136 : return sid;
137 : }
138 :
139 27184 : bool isDefaultXMLNamespace() const {
140 27184 : return bits == TYPE_DEFAULT_XML_NAMESPACE;
141 : }
142 : };
143 :
144 : static JS_ALWAYS_INLINE jsid
145 13592 : SPECIALID_TO_JSID(const SpecialId &sid)
146 : {
147 : jsid id;
148 13592 : JSID_BITS(id) = sid.bits;
149 13592 : JS_ASSERT_IF(sid.isObject(), JSID_IS_OBJECT(id) && JSID_TO_OBJECT(id) == sid.toObject());
150 13592 : JS_ASSERT_IF(sid.isVoid(), JSID_IS_VOID(id));
151 13592 : JS_ASSERT_IF(sid.isEmpty(), JSID_IS_EMPTY(id));
152 13592 : JS_ASSERT_IF(sid.isDefaultXMLNamespace(), JSID_IS_DEFAULT_XML_NAMESPACE(id));
153 : return id;
154 : }
155 :
156 : static JS_ALWAYS_INLINE bool
157 9 : JSID_IS_SPECIAL(jsid id)
158 : {
159 27 : return JSID_IS_OBJECT(id) || JSID_IS_EMPTY(id) || JSID_IS_VOID(id) ||
160 27 : JSID_IS_DEFAULT_XML_NAMESPACE(id);
161 : }
162 :
163 : static JS_ALWAYS_INLINE SpecialId
164 0 : JSID_TO_SPECIALID(jsid id)
165 : {
166 0 : JS_ASSERT(JSID_IS_SPECIAL(id));
167 0 : if (JSID_IS_OBJECT(id))
168 0 : return SpecialId(*JSID_TO_OBJECT(id));
169 0 : if (JSID_IS_EMPTY(id))
170 0 : return SpecialId::empty();
171 0 : if (JSID_IS_VOID(id))
172 0 : return SpecialId::voidId();
173 0 : JS_ASSERT(JSID_IS_DEFAULT_XML_NAMESPACE(id));
174 0 : return SpecialId::defaultXMLNamespace();
175 : }
176 :
177 : /* js::Class operation signatures. */
178 :
179 : typedef JSBool
180 : (* LookupGenericOp)(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
181 : JSProperty **propp);
182 : typedef JSBool
183 : (* LookupPropOp)(JSContext *cx, JSObject *obj, PropertyName *name, JSObject **objp,
184 : JSProperty **propp);
185 : typedef JSBool
186 : (* LookupElementOp)(JSContext *cx, JSObject *obj, uint32_t index, JSObject **objp,
187 : JSProperty **propp);
188 : typedef JSBool
189 : (* LookupSpecialOp)(JSContext *cx, JSObject *obj, SpecialId sid, JSObject **objp,
190 : JSProperty **propp);
191 : typedef JSBool
192 : (* DefineGenericOp)(JSContext *cx, JSObject *obj, jsid id, const Value *value,
193 : PropertyOp getter, StrictPropertyOp setter, unsigned attrs);
194 : typedef JSBool
195 : (* DefinePropOp)(JSContext *cx, JSObject *obj, PropertyName *name, const Value *value,
196 : PropertyOp getter, StrictPropertyOp setter, unsigned attrs);
197 : typedef JSBool
198 : (* DefineElementOp)(JSContext *cx, JSObject *obj, uint32_t index, const Value *value,
199 : PropertyOp getter, StrictPropertyOp setter, unsigned attrs);
200 : typedef JSBool
201 : (* DefineSpecialOp)(JSContext *cx, JSObject *obj, SpecialId sid, const Value *value,
202 : PropertyOp getter, StrictPropertyOp setter, unsigned attrs);
203 : typedef JSBool
204 : (* GenericIdOp)(JSContext *cx, JSObject *obj, JSObject *receiver, jsid id, Value *vp);
205 : typedef JSBool
206 : (* PropertyIdOp)(JSContext *cx, JSObject *obj, JSObject *receiver, PropertyName *name, Value *vp);
207 : typedef JSBool
208 : (* ElementIdOp)(JSContext *cx, JSObject *obj, JSObject *receiver, uint32_t index, Value *vp);
209 : typedef JSBool
210 : (* ElementIfPresentOp)(JSContext *cx, JSObject *obj, JSObject *receiver, uint32_t index, Value *vp, bool* present);
211 : typedef JSBool
212 : (* SpecialIdOp)(JSContext *cx, JSObject *obj, JSObject *receiver, SpecialId sid, Value *vp);
213 : typedef JSBool
214 : (* StrictGenericIdOp)(JSContext *cx, JSObject *obj, jsid id, Value *vp, JSBool strict);
215 : typedef JSBool
216 : (* StrictPropertyIdOp)(JSContext *cx, JSObject *obj, PropertyName *name, Value *vp, JSBool strict);
217 : typedef JSBool
218 : (* StrictElementIdOp)(JSContext *cx, JSObject *obj, uint32_t index, Value *vp, JSBool strict);
219 : typedef JSBool
220 : (* StrictSpecialIdOp)(JSContext *cx, JSObject *obj, SpecialId sid, Value *vp, JSBool strict);
221 : typedef JSBool
222 : (* GenericAttributesOp)(JSContext *cx, JSObject *obj, jsid id, unsigned *attrsp);
223 : typedef JSBool
224 : (* PropertyAttributesOp)(JSContext *cx, JSObject *obj, PropertyName *name, unsigned *attrsp);
225 : typedef JSBool
226 : (* ElementAttributesOp)(JSContext *cx, JSObject *obj, uint32_t index, unsigned *attrsp);
227 : typedef JSBool
228 : (* SpecialAttributesOp)(JSContext *cx, JSObject *obj, SpecialId sid, unsigned *attrsp);
229 : typedef JSBool
230 : (* DeletePropertyOp)(JSContext *cx, JSObject *obj, PropertyName *name, Value *vp, JSBool strict);
231 : typedef JSBool
232 : (* DeleteElementOp)(JSContext *cx, JSObject *obj, uint32_t index, Value *vp, JSBool strict);
233 : typedef JSBool
234 : (* DeleteSpecialOp)(JSContext *cx, JSObject *obj, SpecialId sid, Value *vp, JSBool strict);
235 : typedef JSType
236 : (* TypeOfOp)(JSContext *cx, JSObject *obj);
237 :
238 : /*
239 : * Prepare to make |obj| non-extensible; in particular, fully resolve its properties.
240 : * On error, return false.
241 : * If |obj| is now ready to become non-extensible, set |*fixed| to true and return true.
242 : * If |obj| refuses to become non-extensible, set |*fixed| to false and return true; the
243 : * caller will throw an appropriate error.
244 : */
245 : typedef JSBool
246 : (* FixOp)(JSContext *cx, JSObject *obj, bool *fixed, AutoIdVector *props);
247 :
248 : typedef JSObject *
249 : (* ObjectOp)(JSContext *cx, JSObject *obj);
250 : typedef void
251 : (* FinalizeOp)(FreeOp *fop, JSObject *obj);
252 : typedef void
253 : (* ClearOp)(JSContext *cx, JSObject *obj);
254 :
255 : #define JS_CLASS_MEMBERS \
256 : const char *name; \
257 : uint32_t flags; \
258 : \
259 : /* Mandatory non-null function pointer members. */ \
260 : JSPropertyOp addProperty; \
261 : JSPropertyOp delProperty; \
262 : JSPropertyOp getProperty; \
263 : JSStrictPropertyOp setProperty; \
264 : JSEnumerateOp enumerate; \
265 : JSResolveOp resolve; \
266 : JSConvertOp convert; \
267 : FinalizeOp finalize; \
268 : \
269 : /* Optionally non-null members start here. */ \
270 : JSCheckAccessOp checkAccess; \
271 : JSNative call; \
272 : JSNative construct; \
273 : JSHasInstanceOp hasInstance; \
274 : JSTraceOp trace
275 :
276 : /*
277 : * The helper struct to measure the size of JS_CLASS_MEMBERS to know how much
278 : * we have to padd js::Class to match the size of JSClass;
279 : */
280 : struct ClassSizeMeasurement
281 : {
282 : JS_CLASS_MEMBERS;
283 : };
284 :
285 : struct ClassExtension
286 : {
287 : JSEqualityOp equality;
288 : JSObjectOp outerObject;
289 : JSObjectOp innerObject;
290 : JSIteratorOp iteratorObject;
291 : void *unused;
292 :
293 : /*
294 : * isWrappedNative is true only if the class is an XPCWrappedNative.
295 : * WeakMaps use this to override the wrapper disposal optimization.
296 : */
297 : bool isWrappedNative;
298 : };
299 :
300 : #define JS_NULL_CLASS_EXT {NULL,NULL,NULL,NULL,NULL,false}
301 :
302 : struct ObjectOps
303 : {
304 : LookupGenericOp lookupGeneric;
305 : LookupPropOp lookupProperty;
306 : LookupElementOp lookupElement;
307 : LookupSpecialOp lookupSpecial;
308 : DefineGenericOp defineGeneric;
309 : DefinePropOp defineProperty;
310 : DefineElementOp defineElement;
311 : DefineSpecialOp defineSpecial;
312 : GenericIdOp getGeneric;
313 : PropertyIdOp getProperty;
314 : ElementIdOp getElement;
315 : ElementIfPresentOp getElementIfPresent; /* can be null */
316 : SpecialIdOp getSpecial;
317 : StrictGenericIdOp setGeneric;
318 : StrictPropertyIdOp setProperty;
319 : StrictElementIdOp setElement;
320 : StrictSpecialIdOp setSpecial;
321 : GenericAttributesOp getGenericAttributes;
322 : PropertyAttributesOp getPropertyAttributes;
323 : ElementAttributesOp getElementAttributes;
324 : SpecialAttributesOp getSpecialAttributes;
325 : GenericAttributesOp setGenericAttributes;
326 : PropertyAttributesOp setPropertyAttributes;
327 : ElementAttributesOp setElementAttributes;
328 : SpecialAttributesOp setSpecialAttributes;
329 : DeletePropertyOp deleteProperty;
330 : DeleteElementOp deleteElement;
331 : DeleteSpecialOp deleteSpecial;
332 :
333 : JSNewEnumerateOp enumerate;
334 : TypeOfOp typeOf;
335 : FixOp fix;
336 : ObjectOp thisObject;
337 : ClearOp clear;
338 : };
339 :
340 : #define JS_NULL_OBJECT_OPS \
341 : {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, \
342 : NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL, \
343 : NULL,NULL,NULL,NULL,NULL,NULL}
344 :
345 : struct Class
346 : {
347 : JS_CLASS_MEMBERS;
348 : ClassExtension ext;
349 : ObjectOps ops;
350 : uint8_t pad[sizeof(JSClass) - sizeof(ClassSizeMeasurement) -
351 : sizeof(ClassExtension) - sizeof(ObjectOps)];
352 :
353 : /* Class is not native and its map is not a scope. */
354 : static const uint32_t NON_NATIVE = JSCLASS_INTERNAL_FLAG2;
355 :
356 314590167 : bool isNative() const {
357 314590167 : return !(flags & NON_NATIVE);
358 : }
359 :
360 54740398 : bool hasPrivate() const {
361 54740398 : return !!(flags & JSCLASS_HAS_PRIVATE);
362 : }
363 : };
364 :
365 : JS_STATIC_ASSERT(offsetof(JSClass, name) == offsetof(Class, name));
366 : JS_STATIC_ASSERT(offsetof(JSClass, flags) == offsetof(Class, flags));
367 : JS_STATIC_ASSERT(offsetof(JSClass, addProperty) == offsetof(Class, addProperty));
368 : JS_STATIC_ASSERT(offsetof(JSClass, delProperty) == offsetof(Class, delProperty));
369 : JS_STATIC_ASSERT(offsetof(JSClass, getProperty) == offsetof(Class, getProperty));
370 : JS_STATIC_ASSERT(offsetof(JSClass, setProperty) == offsetof(Class, setProperty));
371 : JS_STATIC_ASSERT(offsetof(JSClass, enumerate) == offsetof(Class, enumerate));
372 : JS_STATIC_ASSERT(offsetof(JSClass, resolve) == offsetof(Class, resolve));
373 : JS_STATIC_ASSERT(offsetof(JSClass, convert) == offsetof(Class, convert));
374 : JS_STATIC_ASSERT(offsetof(JSClass, finalize) == offsetof(Class, finalize));
375 : JS_STATIC_ASSERT(offsetof(JSClass, checkAccess) == offsetof(Class, checkAccess));
376 : JS_STATIC_ASSERT(offsetof(JSClass, call) == offsetof(Class, call));
377 : JS_STATIC_ASSERT(offsetof(JSClass, construct) == offsetof(Class, construct));
378 : JS_STATIC_ASSERT(offsetof(JSClass, hasInstance) == offsetof(Class, hasInstance));
379 : JS_STATIC_ASSERT(offsetof(JSClass, trace) == offsetof(Class, trace));
380 : JS_STATIC_ASSERT(sizeof(JSClass) == sizeof(Class));
381 :
382 : static JS_ALWAYS_INLINE JSClass *
383 2337236 : Jsvalify(Class *c)
384 : {
385 2337236 : return (JSClass *)c;
386 : }
387 : static JS_ALWAYS_INLINE const JSClass *
388 : Jsvalify(const Class *c)
389 : {
390 : return (const JSClass *)c;
391 : }
392 :
393 : static JS_ALWAYS_INLINE Class *
394 1959193 : Valueify(JSClass *c)
395 : {
396 1959193 : return (Class *)c;
397 : }
398 : static JS_ALWAYS_INLINE const Class *
399 : Valueify(const JSClass *c)
400 : {
401 : return (const Class *)c;
402 : }
403 :
404 : /*
405 : * Enumeration describing possible values of the [[Class]] internal property
406 : * value of objects.
407 : */
408 : enum ESClassValue {
409 : ESClass_Array, ESClass_Number, ESClass_String, ESClass_Boolean, ESClass_RegExp
410 : };
411 :
412 : /*
413 : * Return whether the given object has the given [[Class]] internal property
414 : * value. Beware, this query says nothing about the js::Class of the JSObject
415 : * so the caller must not assume anything about obj's representation (e.g., obj
416 : * may be a proxy).
417 : */
418 : inline bool
419 : ObjectClassIs(JSObject &obj, ESClassValue classValue, JSContext *cx);
420 :
421 : /* Just a helper that checks v.isObject before calling ObjectClassIs. */
422 : inline bool
423 : IsObjectWithClass(const Value &v, ESClassValue classValue, JSContext *cx);
424 :
425 : } /* namespace js */
426 :
427 : #endif /* __cplusplus */
428 :
429 : #endif /* jsclass_h__ */
|