Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1147 lines (942 sloc) 39.824 kb
a821f8a @dustin Import pristine Tracemonkey from Firefox 3.7a3
dustin authored
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=78:
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 Communicator client code, released
18 * March 31, 1998.
19 *
20 * The Initial Developer of the Original Code is
21 * Netscape Communications Corporation.
22 * Portions created by the Initial Developer are Copyright (C) 1998
23 * the Initial Developer. All Rights Reserved.
24 *
25 * Contributor(s):
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 #ifndef jsobj_h___
42 #define jsobj_h___
43 /*
44 * JS object definitions.
45 *
46 * A JS object consists of a possibly-shared object descriptor containing
47 * ordered property names, called the map; and a dense vector of property
48 * values, called slots. The map/slot pointer pair is GC'ed, while the map
49 * is reference counted and the slot vector is malloc'ed.
50 */
51 #include "jshash.h" /* Added by JSIFY */
52 #include "jspubtd.h"
53 #include "jsprvtd.h"
54
55 /*
56 * A representation of ECMA-262 ed. 5's internal property descriptor data
57 * structure.
58 */
59 struct PropertyDescriptor {
60 friend class AutoDescriptorArray;
61
62 private:
63 PropertyDescriptor();
64
65 public:
66 /* 8.10.5 ToPropertyDescriptor(Obj) */
67 bool initialize(JSContext* cx, jsid id, jsval v);
68
69 /* 8.10.1 IsAccessorDescriptor(desc) */
70 bool isAccessorDescriptor() const {
71 return hasGet || hasSet;
72 }
73
74 /* 8.10.2 IsDataDescriptor(desc) */
75 bool isDataDescriptor() const {
76 return hasValue || hasWritable;
77 }
78
79 /* 8.10.3 IsGenericDescriptor(desc) */
80 bool isGenericDescriptor() const {
81 return !isAccessorDescriptor() && !isDataDescriptor();
82 }
83
84 bool configurable() const {
85 return (attrs & JSPROP_PERMANENT) == 0;
86 }
87
88 bool enumerable() const {
89 return (attrs & JSPROP_ENUMERATE) != 0;
90 }
91
92 bool writable() const {
93 return (attrs & JSPROP_READONLY) == 0;
94 }
95
96 JSObject* getterObject() const {
97 return get != JSVAL_VOID ? JSVAL_TO_OBJECT(get) : NULL;
98 }
99 JSObject* setterObject() const {
100 return set != JSVAL_VOID ? JSVAL_TO_OBJECT(set) : NULL;
101 }
102
103 jsval getterValue() const {
104 return get;
105 }
106 jsval setterValue() const {
107 return set;
108 }
109
110 JSPropertyOp getter() const {
111 return js_CastAsPropertyOp(getterObject());
112 }
113 JSPropertyOp setter() const {
114 return js_CastAsPropertyOp(setterObject());
115 }
116
117 static void traceDescriptorArray(JSTracer* trc, JSObject* obj);
118 static void finalizeDescriptorArray(JSContext* cx, JSObject* obj);
119
120 jsid id;
121 jsval value, get, set;
122
123 /* Property descriptor boolean fields. */
124 uint8 attrs;
125
126 /* Bits indicating which values are set. */
127 bool hasGet : 1;
128 bool hasSet : 1;
129 bool hasValue : 1;
130 bool hasWritable : 1;
131 bool hasEnumerable : 1;
132 bool hasConfigurable : 1;
133 };
134
135 JS_BEGIN_EXTERN_C
136
137 /* For detailed comments on these function pointer types, see jsprvtd.h. */
138 struct JSObjectOps {
139 /*
140 * Custom shared object map for non-native objects. For native objects
141 * this should be null indicating, that JSObject.map is an instance of
142 * JSScope.
143 */
144 const JSObjectMap *objectMap;
145
146 /* Mandatory non-null function pointer members. */
147 JSLookupPropOp lookupProperty;
148 JSDefinePropOp defineProperty;
149 JSPropertyIdOp getProperty;
150 JSPropertyIdOp setProperty;
151 JSAttributesOp getAttributes;
152 JSAttributesOp setAttributes;
153 JSPropertyIdOp deleteProperty;
154 JSConvertOp defaultValue;
155 JSNewEnumerateOp enumerate;
156 JSCheckAccessIdOp checkAccess;
157 JSTypeOfOp typeOf;
158 JSTraceOp trace;
159
160 /* Optionally non-null members start here. */
161 JSObjectOp thisObject;
162 JSPropertyRefOp dropProperty;
163 JSNative call;
164 JSNative construct;
165 JSHasInstanceOp hasInstance;
166 JSFinalizeOp clear;
167
168 bool inline isNative() const;
169 };
170
171 extern JS_FRIEND_DATA(JSObjectOps) js_ObjectOps;
172 extern JS_FRIEND_DATA(JSObjectOps) js_WithObjectOps;
173
174 /*
175 * Test whether the ops is native. FIXME bug 492938: consider how it would
176 * affect the performance to do just the !objectMap check.
177 */
178 inline bool
179 JSObjectOps::isNative() const
180 {
181 return JS_LIKELY(this == &js_ObjectOps) || !objectMap;
182 }
183
184 struct JSObjectMap {
185 const JSObjectOps * const ops; /* high level object operation vtable */
186 uint32 shape; /* shape identifier */
187
188 explicit JSObjectMap(const JSObjectOps *ops, uint32 shape) : ops(ops), shape(shape) {}
189
190 enum { SHAPELESS = 0xffffffff };
191 };
192
193 const uint32 JS_INITIAL_NSLOTS = 5;
194
195 const uint32 JSSLOT_PROTO = 0;
196 const uint32 JSSLOT_PARENT = 1;
197
198 /*
199 * The first available slot to store generic value. For JSCLASS_HAS_PRIVATE
200 * classes the slot stores a pointer to private data reinterpreted as jsval.
201 * Such pointer is stored as is without an overhead of PRIVATE_TO_JSVAL
202 * tagging and should be accessed using the (get|set)Private methods of
203 * JSObject.
204 */
205 const uint32 JSSLOT_PRIVATE = 2;
206
207 const uint32 JSSLOT_PRIMITIVE_THIS = JSSLOT_PRIVATE;
208
209 const uintptr_t JSSLOT_CLASS_MASK_BITS = 3;
210
211 /*
212 * JSObject struct, with members sized to fit in 32 bytes on 32-bit targets,
213 * 64 bytes on 64-bit systems. The JSFunction struct is an extension of this
214 * struct allocated from a larger GC size-class.
215 *
216 * The classword member stores the JSClass pointer for this object, with the
217 * least two bits encoding whether this object is a "delegate" or a "system"
218 * object. We do *not* synchronize updates of classword -- API clients must
219 * take care.
220 *
221 * An object is a delegate if it is on another object's prototype (linked by
222 * JSSLOT_PROTO) or scope (JSSLOT_PARENT) chain, and therefore the delegate
223 * might be asked implicitly to get or set a property on behalf of another
224 * object. Delegates may be accessed directly too, as may any object, but only
225 * those objects linked after the head of any prototype or scope chain are
226 * flagged as delegates. This definition helps to optimize shape-based property
227 * cache invalidation (see Purge{Scope,Proto}Chain in jsobj.cpp).
228 *
229 * The meaning of the system object bit is defined by the API client. It is
230 * set in JS_NewSystemObject and is queried by JS_IsSystemObject (jsdbgapi.h),
231 * but it has no intrinsic meaning to SpiderMonkey. Further, JSFILENAME_SYSTEM
232 * and JS_FlagScriptFilenamePrefix (also exported via jsdbgapi.h) are intended
233 * to be complementary to this bit, but it is up to the API client to implement
234 * any such association.
235 *
236 * Both these classword tag bits are initially zero; they may be set or queried
237 * using the (is|set)(Delegate|System) inline methods.
238 *
239 * The dslots member is null or a pointer into a dynamically allocated vector
240 * of jsvals for reserved and dynamic slots. If dslots is not null, dslots[-1]
241 * records the number of available slots.
242 */
243 struct JSObject {
244 JSObjectMap *map; /* property map, see jsscope.h */
245 jsuword classword; /* JSClass ptr | bits, see above */
246 jsval fslots[JS_INITIAL_NSLOTS]; /* small number of fixed slots */
247 jsval *dslots; /* dynamically allocated slots */
248
249 bool isNative() const { return map->ops->isNative(); }
250
251 JSClass *getClass() const {
252 return (JSClass *) (classword & ~JSSLOT_CLASS_MASK_BITS);
253 }
254
255 bool hasClass(const JSClass *clasp) const {
256 return clasp == getClass();
257 }
258
259 bool isDelegate() const {
260 return (classword & jsuword(1)) != jsuword(0);
261 }
262
263 void setDelegate() {
264 classword |= jsuword(1);
265 }
266
267 static void setDelegateNullSafe(JSObject *obj) {
268 if (obj)
269 obj->setDelegate();
270 }
271
272 bool isSystem() const {
273 return (classword & jsuword(2)) != jsuword(0);
274 }
275
276 void setSystem() {
277 classword |= jsuword(2);
278 }
279
280 JSObject *getProto() const {
281 return JSVAL_TO_OBJECT(fslots[JSSLOT_PROTO]);
282 }
283
284 void clearProto() {
285 fslots[JSSLOT_PROTO] = JSVAL_NULL;
286 }
287
288 void setProto(JSObject *newProto) {
289 setDelegateNullSafe(newProto);
290 fslots[JSSLOT_PROTO] = OBJECT_TO_JSVAL(newProto);
291 }
292
293 JSObject *getParent() const {
294 return JSVAL_TO_OBJECT(fslots[JSSLOT_PARENT]);
295 }
296
297 void clearParent() {
298 fslots[JSSLOT_PARENT] = JSVAL_NULL;
299 }
300
301 void setParent(JSObject *newParent) {
302 setDelegateNullSafe(newParent);
303 fslots[JSSLOT_PARENT] = OBJECT_TO_JSVAL(newParent);
304 }
305
306 void traceProtoAndParent(JSTracer *trc) const {
307 JSObject *proto = getProto();
308 if (proto)
309 JS_CALL_OBJECT_TRACER(trc, proto, "__proto__");
310
311 JSObject *parent = getParent();
312 if (parent)
313 JS_CALL_OBJECT_TRACER(trc, parent, "__parent__");
314 }
315
316 void *getPrivate() const {
317 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
318 jsval v = fslots[JSSLOT_PRIVATE];
319 JS_ASSERT((v & jsval(1)) == jsval(0));
320 return reinterpret_cast<void *>(v);
321 }
322
323 void setPrivate(void *data) {
324 JS_ASSERT(getClass()->flags & JSCLASS_HAS_PRIVATE);
325 jsval v = reinterpret_cast<jsval>(data);
326 JS_ASSERT((v & jsval(1)) == jsval(0));
327 fslots[JSSLOT_PRIVATE] = v;
328 }
329
330 static jsval defaultPrivate(JSClass *clasp) {
331 return (clasp->flags & JSCLASS_HAS_PRIVATE)
332 ? JSVAL_NULL
333 : JSVAL_VOID;
334 }
335
336 bool isCallable();
337
338 /* The map field is not initialized here and should be set separately. */
339 void init(JSClass *clasp, JSObject *proto, JSObject *parent,
340 jsval privateSlotValue) {
341 JS_ASSERT(((jsuword) clasp & 3) == 0);
342 JS_STATIC_ASSERT(JSSLOT_PRIVATE + 3 == JS_INITIAL_NSLOTS);
343 JS_ASSERT_IF(clasp->flags & JSCLASS_HAS_PRIVATE,
344 (privateSlotValue & jsval(1)) == jsval(0));
345
346 classword = jsuword(clasp);
347 JS_ASSERT(!isDelegate());
348 JS_ASSERT(!isSystem());
349
350 setProto(proto);
351 setParent(parent);
352 fslots[JSSLOT_PRIVATE] = privateSlotValue;
353 fslots[JSSLOT_PRIVATE + 1] = JSVAL_VOID;
354 fslots[JSSLOT_PRIVATE + 2] = JSVAL_VOID;
355 dslots = NULL;
356 }
357
358 /*
359 * Like init, but also initializes map. The catch: proto must be the result
360 * of a call to js_InitClass(...clasp, ...).
361 */
362 inline void initSharingEmptyScope(JSClass *clasp, JSObject *proto, JSObject *parent,
363 jsval privateSlotValue);
364
365 inline bool hasSlotsArray() const { return dslots; }
366
367 /* This method can only be called when hasSlotsArray() returns true. */
368 inline void freeSlotsArray(JSContext *cx);
369
370 JSBool lookupProperty(JSContext *cx, jsid id,
371 JSObject **objp, JSProperty **propp) {
372 return map->ops->lookupProperty(cx, this, id, objp, propp);
373 }
374
375 JSBool defineProperty(JSContext *cx, jsid id, jsval value,
376 JSPropertyOp getter = JS_PropertyStub,
377 JSPropertyOp setter = JS_PropertyStub,
378 uintN attrs = JSPROP_ENUMERATE) {
379 return map->ops->defineProperty(cx, this, id, value, getter, setter, attrs);
380 }
381
382 JSBool getProperty(JSContext *cx, jsid id, jsval *vp) {
383 return map->ops->getProperty(cx, this, id, vp);
384 }
385
386 JSBool setProperty(JSContext *cx, jsid id, jsval *vp) {
387 return map->ops->setProperty(cx, this, id, vp);
388 }
389
390 JSBool getAttributes(JSContext *cx, jsid id, JSProperty *prop,
391 uintN *attrsp) {
392 return map->ops->getAttributes(cx, this, id, prop, attrsp);
393 }
394
395 JSBool setAttributes(JSContext *cx, jsid id, JSProperty *prop,
396 uintN *attrsp) {
397 return map->ops->setAttributes(cx, this, id, prop, attrsp);
398 }
399
400 JSBool deleteProperty(JSContext *cx, jsid id, jsval *rval) {
401 return map->ops->deleteProperty(cx, this, id, rval);
402 }
403
404 JSBool defaultValue(JSContext *cx, JSType hint, jsval *vp) {
405 return map->ops->defaultValue(cx, this, hint, vp);
406 }
407
408 JSBool enumerate(JSContext *cx, JSIterateOp op, jsval *statep,
409 jsid *idp) {
410 return map->ops->enumerate(cx, this, op, statep, idp);
411 }
412
413 JSBool checkAccess(JSContext *cx, jsid id, JSAccessMode mode, jsval *vp,
414 uintN *attrsp) {
415 return map->ops->checkAccess(cx, this, id, mode, vp, attrsp);
416 }
417
418 JSType typeOf(JSContext *cx) {
419 return map->ops->typeOf(cx, this);
420 }
421
422 /* These four are time-optimized to avoid stub calls. */
423 JSObject *thisObject(JSContext *cx) {
424 return map->ops->thisObject ? map->ops->thisObject(cx, this) : this;
425 }
426
427 void dropProperty(JSContext *cx, JSProperty *prop) {
428 if (map->ops->dropProperty)
429 map->ops->dropProperty(cx, this, prop);
430 }
431
432 inline bool isArray() const;
433 inline bool isDenseArray() const;
434 inline bool isFunction() const;
435 inline bool isRegExp() const;
436 inline bool isXML() const;
437
438 inline bool unbrand(JSContext *cx);
439 };
440
441 /* Compatibility macros. */
442 #define OBJ_IS_NATIVE(obj) ((obj)->isNative())
443
444 #define STOBJ_GET_PROTO(obj) ((obj)->getProto())
445 #define STOBJ_SET_PROTO(obj,proto) ((obj)->setProto(proto))
446 #define STOBJ_CLEAR_PROTO(obj) ((obj)->clearProto())
447
448 #define STOBJ_GET_PARENT(obj) ((obj)->getParent())
449 #define STOBJ_SET_PARENT(obj,parent) ((obj)->setParent(parent))
450 #define STOBJ_CLEAR_PARENT(obj) ((obj)->clearParent())
451
452 #define OBJ_GET_PROTO(cx,obj) STOBJ_GET_PROTO(obj)
453 #define OBJ_SET_PROTO(cx,obj,proto) STOBJ_SET_PROTO(obj, proto)
454 #define OBJ_CLEAR_PROTO(cx,obj) STOBJ_CLEAR_PROTO(obj)
455
456 #define OBJ_GET_PARENT(cx,obj) STOBJ_GET_PARENT(obj)
457 #define OBJ_SET_PARENT(cx,obj,parent) STOBJ_SET_PARENT(obj, parent)
458 #define OBJ_CLEAR_PARENT(cx,obj) STOBJ_CLEAR_PARENT(obj)
459
460 #define JSSLOT_START(clasp) (((clasp)->flags & JSCLASS_HAS_PRIVATE) \
461 ? JSSLOT_PRIVATE + 1 \
462 : JSSLOT_PRIVATE)
463
464 #define JSSLOT_FREE(clasp) (JSSLOT_START(clasp) \
465 + JSCLASS_RESERVED_SLOTS(clasp))
466
467 /*
468 * Maximum capacity of the obj->dslots vector, net of the hidden slot at
469 * obj->dslots[-1] that is used to store the length of the vector biased by
470 * JS_INITIAL_NSLOTS (and again net of the slot at index -1).
471 */
472 #define MAX_DSLOTS_LENGTH (JS_MAX(~uint32(0), ~size_t(0)) / sizeof(jsval) - 1)
473 #define MAX_DSLOTS_LENGTH32 (~uint32(0) / sizeof(jsval) - 1)
474
475 /*
476 * STOBJ prefix means Single Threaded Object. Use the following fast macros to
477 * directly manipulate slots in obj when only one thread can access obj, or
478 * when accessing read-only slots within JS_INITIAL_NSLOTS.
479 */
480
481 #define STOBJ_NSLOTS(obj) \
482 ((obj)->dslots ? (uint32)(obj)->dslots[-1] : (uint32)JS_INITIAL_NSLOTS)
483
484 inline jsval&
485 STOBJ_GET_SLOT(JSObject *obj, uintN slot)
486 {
487 return (slot < JS_INITIAL_NSLOTS)
488 ? obj->fslots[slot]
489 : (JS_ASSERT(slot < (uint32)obj->dslots[-1]),
490 obj->dslots[slot - JS_INITIAL_NSLOTS]);
491 }
492
493 inline void
494 STOBJ_SET_SLOT(JSObject *obj, uintN slot, jsval value)
495 {
496 if (slot < JS_INITIAL_NSLOTS) {
497 obj->fslots[slot] = value;
498 } else {
499 JS_ASSERT(slot < (uint32)obj->dslots[-1]);
500 obj->dslots[slot - JS_INITIAL_NSLOTS] = value;
501 }
502 }
503
504 inline JSClass*
505 STOBJ_GET_CLASS(const JSObject* obj)
506 {
507 return obj->getClass();
508 }
509
510 #define OBJ_CHECK_SLOT(obj,slot) \
511 (JS_ASSERT(obj->isNative()), JS_ASSERT(slot < OBJ_SCOPE(obj)->freeslot))
512
513 #define LOCKED_OBJ_GET_SLOT(obj,slot) \
514 (OBJ_CHECK_SLOT(obj, slot), STOBJ_GET_SLOT(obj, slot))
515 #define LOCKED_OBJ_SET_SLOT(obj,slot,value) \
516 (OBJ_CHECK_SLOT(obj, slot), STOBJ_SET_SLOT(obj, slot, value))
517
518 #ifdef JS_THREADSAFE
519
520 /* Thread-safe functions and wrapper macros for accessing slots in obj. */
521 #define OBJ_GET_SLOT(cx,obj,slot) \
522 (OBJ_CHECK_SLOT(obj, slot), \
523 (OBJ_SCOPE(obj)->title.ownercx == cx) \
524 ? LOCKED_OBJ_GET_SLOT(obj, slot) \
525 : js_GetSlotThreadSafe(cx, obj, slot))
526
527 #define OBJ_SET_SLOT(cx,obj,slot,value) \
528 JS_BEGIN_MACRO \
529 OBJ_CHECK_SLOT(obj, slot); \
530 if (OBJ_SCOPE(obj)->title.ownercx == cx) \
531 LOCKED_OBJ_SET_SLOT(obj, slot, value); \
532 else \
533 js_SetSlotThreadSafe(cx, obj, slot, value); \
534 JS_END_MACRO
535
536 /*
537 * If thread-safe, define an OBJ_GET_SLOT wrapper that bypasses, for a native
538 * object, the lock-free "fast path" test of (OBJ_SCOPE(obj)->ownercx == cx),
539 * to avoid needlessly switching from lock-free to lock-full scope when doing
540 * GC on a different context from the last one to own the scope. The caller
541 * in this case is probably a JSClass.mark function, e.g., fun_mark, or maybe
542 * a finalizer.
543 *
544 * The GC runs only when all threads except the one on which the GC is active
545 * are suspended at GC-safe points, so calling STOBJ_GET_SLOT from the GC's
546 * thread is safe when rt->gcRunning is set. See jsgc.c for details.
547 */
548 #define THREAD_IS_RUNNING_GC(rt, thread) \
549 ((rt)->gcRunning && (rt)->gcThread == (thread))
550
551 #define CX_THREAD_IS_RUNNING_GC(cx) \
552 THREAD_IS_RUNNING_GC((cx)->runtime, (cx)->thread)
553
554 #else /* !JS_THREADSAFE */
555
556 #define OBJ_GET_SLOT(cx,obj,slot) LOCKED_OBJ_GET_SLOT(obj,slot)
557 #define OBJ_SET_SLOT(cx,obj,slot,value) LOCKED_OBJ_SET_SLOT(obj,slot,value)
558
559 #endif /* !JS_THREADSAFE */
560
561 /*
562 * Class is invariant and comes from the fixed clasp member. Thus no locking
563 * is necessary to read it. Same for the private slot.
564 */
565 #define OBJ_GET_CLASS(cx,obj) STOBJ_GET_CLASS(obj)
566
567 #ifdef __cplusplus
568 inline void
569 OBJ_TO_INNER_OBJECT(JSContext *cx, JSObject *&obj)
570 {
571 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
572 if (clasp->flags & JSCLASS_IS_EXTENDED) {
573 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
574 if (xclasp->innerObject)
575 obj = xclasp->innerObject(cx, obj);
576 }
577 }
578
579 /*
580 * The following function has been copied to jsd/jsd_val.c. If making changes to
581 * OBJ_TO_OUTER_OBJECT, please update jsd/jsd_val.c as well.
582 */
583 inline void
584 OBJ_TO_OUTER_OBJECT(JSContext *cx, JSObject *&obj)
585 {
586 JSClass *clasp = OBJ_GET_CLASS(cx, obj);
587 if (clasp->flags & JSCLASS_IS_EXTENDED) {
588 JSExtendedClass *xclasp = (JSExtendedClass *) clasp;
589 if (xclasp->outerObject)
590 obj = xclasp->outerObject(cx, obj);
591 }
592 }
593 #endif
594
595 extern JSClass js_ObjectClass;
596 extern JSClass js_WithClass;
597 extern JSClass js_BlockClass;
598
599 /*
600 * Block scope object macros. The slots reserved by js_BlockClass are:
601 *
602 * JSSLOT_PRIVATE JSStackFrame * active frame pointer or null
603 * JSSLOT_BLOCK_DEPTH int depth of block slots in frame
604 *
605 * After JSSLOT_BLOCK_DEPTH come one or more slots for the block locals.
606 *
607 * A With object is like a Block object, in that both have one reserved slot
608 * telling the stack depth of the relevant slots (the slot whose value is the
609 * object named in the with statement, the slots containing the block's local
610 * variables); and both have a private slot referring to the JSStackFrame in
611 * whose activation they were created (or null if the with or block object
612 * outlives the frame).
613 */
614 #define JSSLOT_BLOCK_DEPTH (JSSLOT_PRIVATE + 1)
615
616 static inline bool
617 OBJ_IS_CLONED_BLOCK(JSObject *obj)
618 {
619 return obj->getProto() != NULL;
620 }
621
622 extern JSBool
623 js_DefineBlockVariable(JSContext *cx, JSObject *obj, jsid id, intN index);
624
625 #define OBJ_BLOCK_COUNT(cx,obj) \
626 (OBJ_SCOPE(OBJ_IS_CLONED_BLOCK(obj) ? obj->getProto() : obj)->entryCount)
627 #define OBJ_BLOCK_DEPTH(cx,obj) \
628 JSVAL_TO_INT(STOBJ_GET_SLOT(obj, JSSLOT_BLOCK_DEPTH))
629 #define OBJ_SET_BLOCK_DEPTH(cx,obj,depth) \
630 STOBJ_SET_SLOT(obj, JSSLOT_BLOCK_DEPTH, INT_TO_JSVAL(depth))
631
632 /*
633 * To make sure this slot is well-defined, always call js_NewWithObject to
634 * create a With object, don't call js_NewObject directly. When creating a
635 * With object that does not correspond to a stack slot, pass -1 for depth.
636 *
637 * When popping the stack across this object's "with" statement, client code
638 * must call withobj->setPrivate(NULL).
639 */
640 extern JS_REQUIRES_STACK JSObject *
641 js_NewWithObject(JSContext *cx, JSObject *proto, JSObject *parent, jsint depth);
642
643 /*
644 * Create a new block scope object not linked to any proto or parent object.
645 * Blocks are created by the compiler to reify let blocks and comprehensions.
646 * Only when dynamic scope is captured do they need to be cloned and spliced
647 * into an active scope chain.
648 */
649 extern JSObject *
650 js_NewBlockObject(JSContext *cx);
651
652 extern JSObject *
653 js_CloneBlockObject(JSContext *cx, JSObject *proto, JSStackFrame *fp);
654
655 extern JS_REQUIRES_STACK JSBool
656 js_PutBlockObject(JSContext *cx, JSBool normalUnwind);
657
658 JSBool
659 js_XDRBlockObject(JSXDRState *xdr, JSObject **objp);
660
661 struct JSSharpObjectMap {
662 jsrefcount depth;
663 jsatomid sharpgen;
664 JSHashTable *table;
665 };
666
667 #define SHARP_BIT ((jsatomid) 1)
668 #define BUSY_BIT ((jsatomid) 2)
669 #define SHARP_ID_SHIFT 2
670 #define IS_SHARP(he) (JS_PTR_TO_UINT32((he)->value) & SHARP_BIT)
671 #define MAKE_SHARP(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|SHARP_BIT))
672 #define IS_BUSY(he) (JS_PTR_TO_UINT32((he)->value) & BUSY_BIT)
673 #define MAKE_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)|BUSY_BIT))
674 #define CLEAR_BUSY(he) ((he)->value = JS_UINT32_TO_PTR(JS_PTR_TO_UINT32((he)->value)&~BUSY_BIT))
675
676 extern JSHashEntry *
677 js_EnterSharpObject(JSContext *cx, JSObject *obj, JSIdArray **idap,
678 jschar **sp);
679
680 extern void
681 js_LeaveSharpObject(JSContext *cx, JSIdArray **idap);
682
683 /*
684 * Mark objects stored in map if GC happens between js_EnterSharpObject
685 * and js_LeaveSharpObject. GC calls this when map->depth > 0.
686 */
687 extern void
688 js_TraceSharpMap(JSTracer *trc, JSSharpObjectMap *map);
689
690 extern JSBool
691 js_HasOwnPropertyHelper(JSContext *cx, JSLookupPropOp lookup, uintN argc,
692 jsval *vp);
693
694 extern JSBool
695 js_HasOwnProperty(JSContext *cx, JSLookupPropOp lookup, JSObject *obj, jsid id,
696 JSObject **objp, JSProperty **propp);
697
698 extern JSBool
699 js_PropertyIsEnumerable(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
700
701 extern JSObject *
702 js_InitEval(JSContext *cx, JSObject *obj);
703
704 extern JSObject *
705 js_InitObjectClass(JSContext *cx, JSObject *obj);
706
707 extern JSObject *
708 js_InitClass(JSContext *cx, JSObject *obj, JSObject *parent_proto,
709 JSClass *clasp, JSNative constructor, uintN nargs,
710 JSPropertySpec *ps, JSFunctionSpec *fs,
711 JSPropertySpec *static_ps, JSFunctionSpec *static_fs);
712
713 /*
714 * Select Object.prototype method names shared between jsapi.cpp and jsobj.cpp.
715 */
716 extern const char js_watch_str[];
717 extern const char js_unwatch_str[];
718 extern const char js_hasOwnProperty_str[];
719 extern const char js_isPrototypeOf_str[];
720 extern const char js_propertyIsEnumerable_str[];
721 extern const char js_defineGetter_str[];
722 extern const char js_defineSetter_str[];
723 extern const char js_lookupGetter_str[];
724 extern const char js_lookupSetter_str[];
725
726 extern JSObject *
727 js_NewObject(JSContext *cx, JSClass *clasp, JSObject *proto,
728 JSObject *parent, size_t objectSize = 0);
729
730 /*
731 * See jsapi.h, JS_NewObjectWithGivenProto.
732 */
733 extern JSObject *
734 js_NewObjectWithGivenProto(JSContext *cx, JSClass *clasp, JSObject *proto,
735 JSObject *parent, size_t objectSize = 0);
736
737 /*
738 * Allocate a new native object with the given value of the proto and private
739 * slots. The parent slot is set to the value of proto's parent slot.
740 *
741 * clasp must be a native class. proto must be the result of a call to
742 * js_InitClass(...clasp, ...).
743 *
744 * Note that this is the correct global object for native class instances, but
745 * not for user-defined functions called as constructors. Functions used as
746 * constructors must create instances parented by the parent of the function
747 * object, not by the parent of its .prototype object value.
748 */
749 extern JSObject*
750 js_NewObjectWithClassProto(JSContext *cx, JSClass *clasp, JSObject *proto,
751 jsval privateSlotValue);
752
753 /*
754 * Fast access to immutable standard objects (constructors and prototypes).
755 */
756 extern JSBool
757 js_GetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key,
758 JSObject **objp);
759
760 extern JSBool
761 js_SetClassObject(JSContext *cx, JSObject *obj, JSProtoKey key, JSObject *cobj);
762
763 /*
764 * If protoKey is not JSProto_Null, then clasp is ignored. If protoKey is
765 * JSProto_Null, clasp must non-null.
766 */
767 extern JSBool
768 js_FindClassObject(JSContext *cx, JSObject *start, JSProtoKey key, jsval *vp,
769 JSClass *clasp = NULL);
770
771 extern JSObject *
772 js_ConstructObject(JSContext *cx, JSClass *clasp, JSObject *proto,
773 JSObject *parent, uintN argc, jsval *argv);
774
775 extern JSBool
776 js_AllocSlot(JSContext *cx, JSObject *obj, uint32 *slotp);
777
778 extern void
779 js_FreeSlot(JSContext *cx, JSObject *obj, uint32 slot);
780
781 extern bool
782 js_GrowSlots(JSContext *cx, JSObject *obj, size_t nslots);
783
784 extern void
785 js_ShrinkSlots(JSContext *cx, JSObject *obj, size_t nslots);
786
787 /*
788 * Ensure that the object has at least JSCLASS_RESERVED_SLOTS(clasp)+nreserved
789 * slots. The function can be called only for native objects just created with
790 * js_NewObject or its forms. In particular, the object should not be shared
791 * between threads and its dslots array must be null. nreserved must match the
792 * value that JSClass.reserveSlots (if any) would return after the object is
793 * fully initialized.
794 */
795 bool
796 js_EnsureReservedSlots(JSContext *cx, JSObject *obj, size_t nreserved);
797
798 extern jsid
799 js_CheckForStringIndex(jsid id);
800
801 /*
802 * js_PurgeScopeChain does nothing if obj is not itself a prototype or parent
803 * scope, else it reshapes the scope and prototype chains it links. It calls
804 * js_PurgeScopeChainHelper, which asserts that obj is flagged as a delegate
805 * (i.e., obj has ever been on a prototype or parent chain).
806 */
807 extern void
808 js_PurgeScopeChainHelper(JSContext *cx, JSObject *obj, jsid id);
809
810 #ifdef __cplusplus /* Aargh, libgjs, bug 492720. */
811 static JS_INLINE void
812 js_PurgeScopeChain(JSContext *cx, JSObject *obj, jsid id)
813 {
814 if (obj->isDelegate())
815 js_PurgeScopeChainHelper(cx, obj, id);
816 }
817 #endif
818
819 /*
820 * Find or create a property named by id in obj's scope, with the given getter
821 * and setter, slot, attributes, and other members.
822 */
823 extern JSScopeProperty *
824 js_AddNativeProperty(JSContext *cx, JSObject *obj, jsid id,
825 JSPropertyOp getter, JSPropertyOp setter, uint32 slot,
826 uintN attrs, uintN flags, intN shortid);
827
828 /*
829 * Change sprop to have the given attrs, getter, and setter in scope, morphing
830 * it into a potentially new JSScopeProperty. Return a pointer to the changed
831 * or identical property.
832 */
833 extern JSScopeProperty *
834 js_ChangeNativePropertyAttrs(JSContext *cx, JSObject *obj,
835 JSScopeProperty *sprop, uintN attrs, uintN mask,
836 JSPropertyOp getter, JSPropertyOp setter);
837
838 extern JSBool
839 js_DefineProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
840 JSPropertyOp getter, JSPropertyOp setter, uintN attrs);
841
842 /*
843 * Flags for the defineHow parameter of js_DefineNativeProperty.
844 */
845 const uintN JSDNP_CACHE_RESULT = 1; /* an interpreter call from JSOP_INITPROP */
846 const uintN JSDNP_DONT_PURGE = 2; /* suppress js_PurgeScopeChain */
847 const uintN JSDNP_SET_METHOD = 4; /* js_{DefineNativeProperty,SetPropertyHelper}
848 must pass the JSScopeProperty::METHOD
849 flag on to js_AddScopeProperty */
850
851 /*
852 * On error, return false. On success, if propp is non-null, return true with
853 * obj locked and with a held property in *propp; if propp is null, return true
854 * but release obj's lock first. Therefore all callers who pass non-null propp
855 * result parameters must later call obj->dropProperty(cx, *propp) both to drop
856 * the held property, and to release the lock on obj.
857 */
858 extern JSBool
859 js_DefineNativeProperty(JSContext *cx, JSObject *obj, jsid id, jsval value,
860 JSPropertyOp getter, JSPropertyOp setter, uintN attrs,
861 uintN flags, intN shortid, JSProperty **propp,
862 uintN defineHow = 0);
863
864 /*
865 * Unlike js_DefineNativeProperty, propp must be non-null. On success, and if
866 * id was found, return true with *objp non-null and locked, and with a held
867 * property stored in *propp. If successful but id was not found, return true
868 * with both *objp and *propp null. Therefore all callers who receive a
869 * non-null *propp must later call (*objp)->dropProperty(cx, *propp).
870 */
871 extern JS_FRIEND_API(JSBool)
872 js_LookupProperty(JSContext *cx, JSObject *obj, jsid id, JSObject **objp,
873 JSProperty **propp);
874
875 /*
876 * Specialized subroutine that allows caller to preset JSRESOLVE_* flags and
877 * returns the index along the prototype chain in which *propp was found, or
878 * the last index if not found, or -1 on error.
879 */
880 extern int
881 js_LookupPropertyWithFlags(JSContext *cx, JSObject *obj, jsid id, uintN flags,
882 JSObject **objp, JSProperty **propp);
883
884
885 /*
886 * We cache name lookup results only for the global object or for native
887 * non-global objects without prototype or with prototype that never mutates,
888 * see bug 462734 and bug 487039.
889 */
890 static inline bool
891 js_IsCacheableNonGlobalScope(JSObject *obj)
892 {
893 extern JS_FRIEND_DATA(JSClass) js_CallClass;
894 extern JS_FRIEND_DATA(JSClass) js_DeclEnvClass;
895 JS_ASSERT(STOBJ_GET_PARENT(obj));
896
897 JSClass *clasp = STOBJ_GET_CLASS(obj);
898 bool cacheable = (clasp == &js_CallClass ||
899 clasp == &js_BlockClass ||
900 clasp == &js_DeclEnvClass);
901
902 JS_ASSERT_IF(cacheable, obj->map->ops->lookupProperty == js_LookupProperty);
903 return cacheable;
904 }
905
906 /*
907 * If cacheResult is false, return JS_NO_PROP_CACHE_FILL on success.
908 */
909 extern JSPropCacheEntry *
910 js_FindPropertyHelper(JSContext *cx, jsid id, JSBool cacheResult,
911 JSObject **objp, JSObject **pobjp, JSProperty **propp);
912
913 /*
914 * Return the index along the scope chain in which id was found, or the last
915 * index if not found, or -1 on error.
916 */
917 extern JS_FRIEND_API(JSBool)
918 js_FindProperty(JSContext *cx, jsid id, JSObject **objp, JSObject **pobjp,
919 JSProperty **propp);
920
921 extern JS_REQUIRES_STACK JSObject *
922 js_FindIdentifierBase(JSContext *cx, JSObject *scopeChain, jsid id);
923
924 extern JSObject *
925 js_FindVariableScope(JSContext *cx, JSFunction **funp);
926
927 /*
928 * JSGET_CACHE_RESULT is the analogue of JSDNP_CACHE_RESULT for js_GetMethod.
929 *
930 * JSGET_METHOD_BARRIER (the default, hence 0 but provided for documentation)
931 * enables a read barrier that preserves standard function object semantics (by
932 * default we assume our caller won't leak a joined callee to script, where it
933 * would create hazardous mutable object sharing as well as observable identity
934 * according to == and ===.
935 *
936 * JSGET_NO_METHOD_BARRIER avoids the performance overhead of the method read
937 * barrier, which is not needed when invoking a lambda that otherwise does not
938 * leak its callee reference (via arguments.callee or its name).
939 */
940 const uintN JSGET_CACHE_RESULT = 1; // from a caching interpreter opcode
941 const uintN JSGET_METHOD_BARRIER = 0; // get can leak joined function object
942 const uintN JSGET_NO_METHOD_BARRIER = 2; // call to joined function can't leak
943
944 /*
945 * NB: js_NativeGet and js_NativeSet are called with the scope containing sprop
946 * (pobj's scope for Get, obj's for Set) locked, and on successful return, that
947 * scope is again locked. But on failure, both functions return false with the
948 * scope containing sprop unlocked.
949 */
950 extern JSBool
951 js_NativeGet(JSContext *cx, JSObject *obj, JSObject *pobj,
952 JSScopeProperty *sprop, uintN getHow, jsval *vp);
953
954 extern JSBool
955 js_NativeSet(JSContext *cx, JSObject *obj, JSScopeProperty *sprop, bool added,
956 jsval *vp);
957
958 extern JSBool
959 js_GetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN getHow,
960 jsval *vp);
961
962 extern JSBool
963 js_GetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
964
965 extern JSBool
966 js_GetMethod(JSContext *cx, JSObject *obj, jsid id, uintN getHow, jsval *vp);
967
968 /*
969 * Check whether it is OK to assign an undeclared property of the global
970 * object at the current script PC.
971 */
972 extern JS_FRIEND_API(bool)
973 js_CheckUndeclaredVarAssignment(JSContext *cx);
974
975 extern JSBool
976 js_SetPropertyHelper(JSContext *cx, JSObject *obj, jsid id, uintN defineHow,
977 jsval *vp);
978
979 extern JSBool
980 js_SetProperty(JSContext *cx, JSObject *obj, jsid id, jsval *vp);
981
982 extern JSBool
983 js_GetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
984 uintN *attrsp);
985
986 extern JSBool
987 js_SetAttributes(JSContext *cx, JSObject *obj, jsid id, JSProperty *prop,
988 uintN *attrsp);
989
990 extern JSBool
991 js_DeleteProperty(JSContext *cx, JSObject *obj, jsid id, jsval *rval);
992
993 extern JSBool
994 js_DefaultValue(JSContext *cx, JSObject *obj, JSType hint, jsval *vp);
995
996 extern JSBool
997 js_Enumerate(JSContext *cx, JSObject *obj, JSIterateOp enum_op,
998 jsval *statep, jsid *idp);
999
1000 extern void
1001 js_MarkEnumeratorState(JSTracer *trc, JSObject *obj, jsval state);
1002
1003 extern void
1004 js_PurgeCachedNativeEnumerators(JSContext *cx, JSThreadData *data);
1005
1006 extern JSBool
1007 js_CheckAccess(JSContext *cx, JSObject *obj, jsid id, JSAccessMode mode,
1008 jsval *vp, uintN *attrsp);
1009
1010 extern JSType
1011 js_TypeOf(JSContext *cx, JSObject *obj);
1012
1013 extern JSBool
1014 js_Call(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
1015
1016 extern JSBool
1017 js_Construct(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
1018 jsval *rval);
1019
1020 extern JSBool
1021 js_HasInstance(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
1022
1023 extern JSBool
1024 js_SetProtoOrParent(JSContext *cx, JSObject *obj, uint32 slot, JSObject *pobj,
1025 JSBool checkForCycles);
1026
1027 extern JSBool
1028 js_IsDelegate(JSContext *cx, JSObject *obj, jsval v, JSBool *bp);
1029
1030 /*
1031 * If protoKey is not JSProto_Null, then clasp is ignored. If protoKey is
1032 * JSProto_Null, clasp must non-null.
1033 */
1034 extern JSBool
1035 js_GetClassPrototype(JSContext *cx, JSObject *scope, JSProtoKey protoKey,
1036 JSObject **protop, JSClass *clasp = NULL);
1037
1038 extern JSBool
1039 js_SetClassPrototype(JSContext *cx, JSObject *ctor, JSObject *proto,
1040 uintN attrs);
1041
1042 /*
1043 * Wrap boolean, number or string as Boolean, Number or String object.
1044 * *vp must not be an object, null or undefined.
1045 */
1046 extern JSBool
1047 js_PrimitiveToObject(JSContext *cx, jsval *vp);
1048
1049 extern JSBool
1050 js_ValueToObject(JSContext *cx, jsval v, JSObject **objp);
1051
1052 extern JSObject *
1053 js_ValueToNonNullObject(JSContext *cx, jsval v);
1054
1055 extern JSBool
1056 js_TryValueOf(JSContext *cx, JSObject *obj, JSType type, jsval *rval);
1057
1058 extern JSBool
1059 js_TryMethod(JSContext *cx, JSObject *obj, JSAtom *atom,
1060 uintN argc, jsval *argv, jsval *rval);
1061
1062 extern JSBool
1063 js_XDRObject(JSXDRState *xdr, JSObject **objp);
1064
1065 extern void
1066 js_TraceObject(JSTracer *trc, JSObject *obj);
1067
1068 extern void
1069 js_PrintObjectSlotName(JSTracer *trc, char *buf, size_t bufsize);
1070
1071 extern void
1072 js_Clear(JSContext *cx, JSObject *obj);
1073
1074 #ifdef JS_THREADSAFE
1075 #define NATIVE_DROP_PROPERTY js_DropProperty
1076
1077 extern void
1078 js_DropProperty(JSContext *cx, JSObject *obj, JSProperty *prop);
1079 #else
1080 #define NATIVE_DROP_PROPERTY NULL
1081 #endif
1082
1083 extern bool
1084 js_GetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval *vp);
1085
1086 bool
1087 js_SetReservedSlot(JSContext *cx, JSObject *obj, uint32 index, jsval v);
1088
1089 /*
1090 * Precondition: obj must be locked.
1091 */
1092 extern JSBool
1093 js_ReallocSlots(JSContext *cx, JSObject *obj, uint32 nslots,
1094 JSBool exactAllocation);
1095
1096 extern JSObject *
1097 js_CheckScopeChainValidity(JSContext *cx, JSObject *scopeobj, const char *caller);
1098
1099 extern JSBool
1100 js_CheckPrincipalsAccess(JSContext *cx, JSObject *scopeobj,
1101 JSPrincipals *principals, JSAtom *caller);
1102
1103 /* For CSP -- checks if eval() and friends are allowed to run. */
1104 extern JSBool
1105 js_CheckContentSecurityPolicy(JSContext *cx);
1106
1107 /* Infallible -- returns its argument if there is no wrapped object. */
1108 extern JSObject *
1109 js_GetWrappedObject(JSContext *cx, JSObject *obj);
1110
1111 /* NB: Infallible. */
1112 extern const char *
1113 js_ComputeFilename(JSContext *cx, JSStackFrame *caller,
1114 JSPrincipals *principals, uintN *linenop);
1115
1116 static inline bool
1117 js_IsCallable(jsval v) {
1118 return !JSVAL_IS_PRIMITIVE(v) && JSVAL_TO_OBJECT(v)->isCallable();
1119 }
1120
1121 extern JSBool
1122 js_ReportGetterOnlyAssignment(JSContext *cx);
1123
1124 extern JS_FRIEND_API(JSBool)
1125 js_GetterOnlyPropertyStub(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
1126
1127 #ifdef DEBUG
1128 JS_FRIEND_API(void) js_DumpChars(const jschar *s, size_t n);
1129 JS_FRIEND_API(void) js_DumpString(JSString *str);
1130 JS_FRIEND_API(void) js_DumpAtom(JSAtom *atom);
1131 JS_FRIEND_API(void) js_DumpValue(jsval val);
1132 JS_FRIEND_API(void) js_DumpId(jsid id);
1133 JS_FRIEND_API(void) js_DumpObject(JSObject *obj);
1134 JS_FRIEND_API(void) js_DumpStackFrame(JSStackFrame *fp);
1135 #endif
1136
1137 extern uintN
1138 js_InferFlags(JSContext *cx, uintN defaultFlags);
1139
1140 /* Object constructor native. Exposed only so the JIT can know its address. */
1141 JSBool
1142 js_Object(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval);
1143
1144 JS_END_EXTERN_C
1145
1146 #endif /* jsobj_h___ */
Something went wrong with that request. Please try again.