/[jscoverage]/trunk/js/jscntxt.h
ViewVC logotype

Contents of /trunk/js/jscntxt.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 585 - (show annotations)
Sun Sep 12 15:13:23 2010 UTC (8 years, 9 months ago) by siliconforks
File MIME type: text/plain
File size: 61291 byte(s)
Update to SpiderMonkey from Firefox 3.6.9.

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 jscntxt_h___
42 #define jscntxt_h___
43 /*
44 * JS execution context.
45 */
46 #include "jsarena.h" /* Added by JSIFY */
47 #include "jsclist.h"
48 #include "jslong.h"
49 #include "jsatom.h"
50 #include "jsversion.h"
51 #include "jsdhash.h"
52 #include "jsgc.h"
53 #include "jsinterp.h"
54 #include "jsobj.h"
55 #include "jsprvtd.h"
56 #include "jspubtd.h"
57 #include "jsregexp.h"
58 #include "jsutil.h"
59 #include "jsarray.h"
60 #include "jstask.h"
61
62 /*
63 * js_GetSrcNote cache to avoid O(n^2) growth in finding a source note for a
64 * given pc in a script. We use the script->code pointer to tag the cache,
65 * instead of the script address itself, so that source notes are always found
66 * by offset from the bytecode with which they were generated.
67 */
68 typedef struct JSGSNCache {
69 jsbytecode *code;
70 JSDHashTable table;
71 #ifdef JS_GSNMETER
72 uint32 hits;
73 uint32 misses;
74 uint32 fills;
75 uint32 purges;
76 # define GSN_CACHE_METER(cache,cnt) (++(cache)->cnt)
77 #else
78 # define GSN_CACHE_METER(cache,cnt) /* nothing */
79 #endif
80 } JSGSNCache;
81
82 #define js_FinishGSNCache(cache) js_PurgeGSNCache(cache)
83
84 extern void
85 js_PurgeGSNCache(JSGSNCache *cache);
86
87 /* These helper macros take a cx as parameter and operate on its GSN cache. */
88 #define JS_PURGE_GSN_CACHE(cx) js_PurgeGSNCache(&JS_GSN_CACHE(cx))
89 #define JS_METER_GSN_CACHE(cx,cnt) GSN_CACHE_METER(&JS_GSN_CACHE(cx), cnt)
90
91 typedef struct InterpState InterpState;
92 typedef struct VMSideExit VMSideExit;
93
94 #ifdef __cplusplus
95 namespace nanojit {
96 class Assembler;
97 class CodeAlloc;
98 class Fragment;
99 class LirBuffer;
100 #ifdef DEBUG
101 class LabelMap;
102 #endif
103 extern "C++" {
104 template<typename K> class DefaultHash;
105 template<typename K, typename V, typename H> class HashMap;
106 template<typename T> class Seq;
107 }
108 }
109 #if defined(JS_JIT_SPEW) || defined(DEBUG)
110 struct FragPI;
111 typedef nanojit::HashMap<uint32, FragPI, nanojit::DefaultHash<uint32> > FragStatsMap;
112 #endif
113 class TraceRecorder;
114 class VMAllocator;
115 extern "C++" { template<typename T> class Queue; }
116 typedef Queue<uint16> SlotList;
117
118 # define CLS(T) T*
119 #else
120 # define CLS(T) void*
121 #endif
122
123 #define FRAGMENT_TABLE_SIZE 512
124 struct VMFragment;
125
126 #ifdef __cplusplus
127 struct REHashKey;
128 struct REHashFn;
129 typedef nanojit::HashMap<REHashKey, nanojit::Fragment*, REHashFn> REHashMap;
130 #endif
131
132 #define MONITOR_N_GLOBAL_STATES 4
133 struct GlobalState {
134 JSObject* globalObj;
135 uint32 globalShape;
136 CLS(SlotList) globalSlots;
137 };
138
139 /*
140 * Trace monitor. Every JSThread (if JS_THREADSAFE) or JSRuntime (if not
141 * JS_THREADSAFE) has an associated trace monitor that keeps track of loop
142 * frequencies for all JavaScript code loaded into that runtime.
143 */
144 struct JSTraceMonitor {
145 /*
146 * The context currently executing JIT-compiled code on this thread, or
147 * NULL if none. Among other things, this can in certain cases prevent
148 * last-ditch GC and suppress calls to JS_ReportOutOfMemory.
149 *
150 * !tracecx && !recorder: not on trace
151 * !tracecx && recorder: recording
152 * tracecx && !recorder: executing a trace
153 * tracecx && recorder: executing inner loop, recording outer loop
154 */
155 JSContext *tracecx;
156
157 CLS(VMAllocator) dataAlloc; /* A chunk allocator for LIR. */
158 CLS(VMAllocator) tempAlloc; /* A temporary chunk allocator. */
159 CLS(nanojit::CodeAlloc) codeAlloc; /* An allocator for native code. */
160 CLS(nanojit::Assembler) assembler;
161 CLS(nanojit::LirBuffer) lirbuf;
162 CLS(nanojit::LirBuffer) reLirBuf;
163 #ifdef DEBUG
164 CLS(nanojit::LabelMap) labels;
165 #endif
166
167 CLS(TraceRecorder) recorder;
168 jsval *reservedDoublePool;
169 jsval *reservedDoublePoolPtr;
170
171 struct GlobalState globalStates[MONITOR_N_GLOBAL_STATES];
172 struct VMFragment* vmfragments[FRAGMENT_TABLE_SIZE];
173 JSDHashTable recordAttempts;
174
175 /*
176 * Maximum size of the code cache before we start flushing. 1/16 of this
177 * size is used as threshold for the regular expression code cache.
178 */
179 uint32 maxCodeCacheBytes;
180
181 /*
182 * If nonzero, do not flush the JIT cache after a deep bail. That would
183 * free JITted code pages that we will later return to. Instead, set the
184 * needFlush flag so that it can be flushed later.
185 */
186 JSBool needFlush;
187
188 /*
189 * reservedObjects is a linked list (via fslots[0]) of preallocated JSObjects.
190 * The JIT uses this to ensure that leaving a trace tree can't fail.
191 */
192 JSBool useReservedObjects;
193 JSObject *reservedObjects;
194
195 /*
196 * Fragment map for the regular expression compiler.
197 */
198 CLS(REHashMap) reFragments;
199
200 /*
201 * A temporary allocator for RE recording.
202 */
203 CLS(VMAllocator) reTempAlloc;
204
205 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
206
207 #ifdef DEBUG
208 /* Fields needed for fragment/guard profiling. */
209 CLS(nanojit::Seq<nanojit::Fragment*>) branches;
210 uint32 lastFragID;
211 /*
212 * profAlloc has a lifetime which spans exactly from js_InitJIT to
213 * js_FinishJIT.
214 */
215 CLS(VMAllocator) profAlloc;
216 CLS(FragStatsMap) profTab;
217 #endif
218
219 /* Flush the JIT cache. */
220 void flush();
221
222 /* Mark all objects baked into native code in the code cache. */
223 void mark(JSTracer *trc);
224
225 #endif
226 };
227
228 typedef struct InterpStruct InterpStruct;
229
230 /*
231 * N.B. JS_ON_TRACE(cx) is true if JIT code is on the stack in the current
232 * thread, regardless of whether cx is the context in which that trace is
233 * executing. cx must be a context on the current thread.
234 */
235 #ifdef JS_TRACER
236 # define JS_ON_TRACE(cx) (JS_TRACE_MONITOR(cx).tracecx != NULL)
237 #else
238 # define JS_ON_TRACE(cx) JS_FALSE
239 #endif
240
241 #ifdef DEBUG
242 # define JS_EVAL_CACHE_METERING 1
243 # define JS_FUNCTION_METERING 1
244 #endif
245
246 /* Number of potentially reusable scriptsToGC to search for the eval cache. */
247 #ifndef JS_EVAL_CACHE_SHIFT
248 # define JS_EVAL_CACHE_SHIFT 6
249 #endif
250 #define JS_EVAL_CACHE_SIZE JS_BIT(JS_EVAL_CACHE_SHIFT)
251
252 #ifdef JS_EVAL_CACHE_METERING
253 # define EVAL_CACHE_METER_LIST(_) _(probe), _(hit), _(step), _(noscope)
254 # define identity(x) x
255
256 /* Have to typedef this for LiveConnect C code, which includes us. */
257 typedef struct JSEvalCacheMeter {
258 uint64 EVAL_CACHE_METER_LIST(identity);
259 } JSEvalCacheMeter;
260
261 # undef identity
262 #endif
263
264 #ifdef JS_FUNCTION_METERING
265 # define FUNCTION_KIND_METER_LIST(_) \
266 _(allfun), _(heavy), _(nofreeupvar), _(onlyfreevar), \
267 _(display), _(flat), _(setupvar), _(badfunarg)
268 # define identity(x) x
269
270 typedef struct JSFunctionMeter {
271 int32 FUNCTION_KIND_METER_LIST(identity);
272 } JSFunctionMeter;
273
274 # undef identity
275 #endif
276
277 struct JSThreadData {
278 /*
279 * The GSN cache is per thread since even multi-cx-per-thread embeddings
280 * do not interleave js_GetSrcNote calls.
281 */
282 JSGSNCache gsnCache;
283
284 /* Property cache for faster call/get/set invocation. */
285 JSPropertyCache propertyCache;
286
287 #ifdef JS_TRACER
288 /* Trace-tree JIT recorder/interpreter state. */
289 JSTraceMonitor traceMonitor;
290 #endif
291
292 /* Lock-free hashed lists of scripts created by eval to garbage-collect. */
293 JSScript *scriptsToGC[JS_EVAL_CACHE_SIZE];
294
295 #ifdef JS_EVAL_CACHE_METERING
296 JSEvalCacheMeter evalCacheMeter;
297 #endif
298
299 /*
300 * Thread-local version of JSRuntime.gcMallocBytes to avoid taking
301 * locks on each JS_malloc.
302 */
303 size_t gcMallocBytes;
304
305 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
306
307 /*
308 * Cache of reusable JSNativeEnumerators mapped by shape identifiers (as
309 * stored in scope->shape). This cache is nulled by the GC and protected
310 * by gcLock.
311 */
312 #define NATIVE_ENUM_CACHE_LOG2 8
313 #define NATIVE_ENUM_CACHE_MASK JS_BITMASK(NATIVE_ENUM_CACHE_LOG2)
314 #define NATIVE_ENUM_CACHE_SIZE JS_BIT(NATIVE_ENUM_CACHE_LOG2)
315
316 #define NATIVE_ENUM_CACHE_HASH(shape) \
317 ((((shape) >> NATIVE_ENUM_CACHE_LOG2) ^ (shape)) & NATIVE_ENUM_CACHE_MASK)
318
319 jsuword nativeEnumCache[NATIVE_ENUM_CACHE_SIZE];
320
321 #ifdef JS_THREADSAFE
322 /*
323 * Deallocator task for this thread.
324 */
325 JSFreePointerListTask *deallocatorTask;
326 #endif
327
328 void mark(JSTracer *trc) {
329 #ifdef JS_TRACER
330 traceMonitor.mark(trc);
331 #endif
332 }
333
334 #endif /* __cplusplus */
335 };
336
337 #ifdef JS_THREADSAFE
338
339 /*
340 * Structure uniquely representing a thread. It holds thread-private data
341 * that can be accessed without a global lock.
342 */
343 struct JSThread {
344 /* Linked list of all contexts in use on this thread. */
345 JSCList contextList;
346
347 /* Opaque thread-id, from NSPR's PR_GetCurrentThread(). */
348 jsword id;
349
350 /* Indicates that the thread is waiting in ClaimTitle from jslock.cpp. */
351 JSTitle *titleToShare;
352
353 JSGCThing *gcFreeLists[GC_NUM_FREELISTS];
354
355 /* Factored out of JSThread for !JS_THREADSAFE embedding in JSRuntime. */
356 JSThreadData data;
357 };
358
359 #define JS_THREAD_DATA(cx) (&(cx)->thread->data)
360
361 struct JSThreadsHashEntry {
362 JSDHashEntryHdr base;
363 JSThread *thread;
364 };
365
366 extern JSThread *
367 js_CurrentThread(JSRuntime *rt);
368
369 /*
370 * The function takes the GC lock and does not release in successful return.
371 * On error (out of memory) the function releases the lock but delegates
372 * the error reporting to the caller.
373 */
374 extern JSBool
375 js_InitContextThread(JSContext *cx);
376
377 /*
378 * On entrance the GC lock must be held and it will be held on exit.
379 */
380 extern void
381 js_ClearContextThread(JSContext *cx);
382
383 #endif /* JS_THREADSAFE */
384
385 typedef enum JSDestroyContextMode {
386 JSDCM_NO_GC,
387 JSDCM_MAYBE_GC,
388 JSDCM_FORCE_GC,
389 JSDCM_NEW_FAILED
390 } JSDestroyContextMode;
391
392 typedef enum JSRuntimeState {
393 JSRTS_DOWN,
394 JSRTS_LAUNCHING,
395 JSRTS_UP,
396 JSRTS_LANDING
397 } JSRuntimeState;
398
399 typedef enum JSBuiltinFunctionId {
400 JSBUILTIN_ObjectToIterator,
401 JSBUILTIN_CallIteratorNext,
402 JSBUILTIN_LIMIT
403 } JSBuiltinFunctionId;
404
405 typedef struct JSPropertyTreeEntry {
406 JSDHashEntryHdr hdr;
407 JSScopeProperty *child;
408 } JSPropertyTreeEntry;
409
410 typedef struct JSSetSlotRequest JSSetSlotRequest;
411
412 struct JSSetSlotRequest {
413 JSObject *obj; /* object containing slot to set */
414 JSObject *pobj; /* new proto or parent reference */
415 uint16 slot; /* which to set, proto or parent */
416 JSPackedBool cycle; /* true if a cycle was detected */
417 JSSetSlotRequest *next; /* next request in GC worklist */
418 };
419
420 struct JSRuntime {
421 /* Runtime state, synchronized by the stateChange/gcLock condvar/lock. */
422 JSRuntimeState state;
423
424 /* Context create/destroy callback. */
425 JSContextCallback cxCallback;
426
427 /*
428 * Shape regenerated whenever a prototype implicated by an "add property"
429 * property cache fill and induced trace guard has a readonly property or a
430 * setter defined on it. This number proxies for the shapes of all objects
431 * along the prototype chain of all objects in the runtime on which such an
432 * add-property result has been cached/traced.
433 *
434 * See bug 492355 for more details.
435 *
436 * This comes early in JSRuntime to minimize the immediate format used by
437 * trace-JITted code that reads it.
438 */
439 uint32 protoHazardShape;
440
441 /* Garbage collector state, used by jsgc.c. */
442 JSGCChunkInfo *gcChunkList;
443 JSGCArenaList gcArenaList[GC_NUM_FREELISTS];
444 JSGCDoubleArenaList gcDoubleArenaList;
445 JSDHashTable gcRootsHash;
446 JSDHashTable *gcLocksHash;
447 jsrefcount gcKeepAtoms;
448 size_t gcBytes;
449 size_t gcLastBytes;
450 size_t gcMaxBytes;
451 size_t gcMaxMallocBytes;
452 uint32 gcEmptyArenaPoolLifespan;
453 uint32 gcLevel;
454 uint32 gcNumber;
455 JSTracer *gcMarkingTracer;
456 uint32 gcTriggerFactor;
457 size_t gcTriggerBytes;
458 volatile JSBool gcIsNeeded;
459 volatile JSBool gcFlushCodeCaches;
460
461 /*
462 * NB: do not pack another flag here by claiming gcPadding unless the new
463 * flag is written only by the GC thread. Atomic updates to packed bytes
464 * are not guaranteed, so stores issued by one thread may be lost due to
465 * unsynchronized read-modify-write cycles on other threads.
466 */
467 JSPackedBool gcPoke;
468 JSPackedBool gcRunning;
469 JSPackedBool gcRegenShapes;
470
471 /*
472 * During gc, if rt->gcRegenShapes &&
473 * (scope->flags & JSScope::SHAPE_REGEN) == rt->gcRegenShapesScopeFlag,
474 * then the scope's shape has already been regenerated during this GC.
475 * To avoid having to sweep JSScopes, the bit's meaning toggles with each
476 * shape-regenerating GC.
477 *
478 * FIXME Once scopes are GC'd (bug 505004), this will be obsolete.
479 */
480 uint8 gcRegenShapesScopeFlag;
481
482 #ifdef JS_GC_ZEAL
483 jsrefcount gcZeal;
484 #endif
485
486 JSGCCallback gcCallback;
487 size_t gcMallocBytes;
488 JSGCArenaInfo *gcUntracedArenaStackTop;
489 #ifdef DEBUG
490 size_t gcTraceLaterCount;
491 #endif
492
493 /*
494 * Table for tracking iterators to ensure that we close iterator's state
495 * before finalizing the iterable object.
496 */
497 JSPtrTable gcIteratorTable;
498
499 /*
500 * The trace operation and its data argument to trace embedding-specific
501 * GC roots.
502 */
503 JSTraceDataOp gcExtraRootsTraceOp;
504 void *gcExtraRootsData;
505
506 /*
507 * Used to serialize cycle checks when setting __proto__ or __parent__ by
508 * requesting the GC handle the required cycle detection. If the GC hasn't
509 * been poked, it won't scan for garbage. This member is protected by
510 * rt->gcLock.
511 */
512 JSSetSlotRequest *setSlotRequests;
513
514 /* Well-known numbers held for use by this runtime's contexts. */
515 jsdouble *jsNaN;
516 jsdouble *jsNegativeInfinity;
517 jsdouble *jsPositiveInfinity;
518
519 #ifdef JS_THREADSAFE
520 JSLock *deflatedStringCacheLock;
521 #endif
522 JSHashTable *deflatedStringCache;
523 #ifdef DEBUG
524 uint32 deflatedStringCacheBytes;
525 #endif
526
527 JSString *emptyString;
528
529 /*
530 * Builtin functions, lazily created and held for use by the trace recorder.
531 *
532 * This field would be #ifdef JS_TRACER, but XPConnect is compiled without
533 * -DJS_TRACER and includes this header.
534 */
535 JSObject *builtinFunctions[JSBUILTIN_LIMIT];
536
537 /* List of active contexts sharing this runtime; protected by gcLock. */
538 JSCList contextList;
539
540 /* Per runtime debug hooks -- see jsprvtd.h and jsdbgapi.h. */
541 JSDebugHooks globalDebugHooks;
542
543 #ifdef JS_TRACER
544 /* True if any debug hooks not supported by the JIT are enabled. */
545 bool debuggerInhibitsJIT() const {
546 return (globalDebugHooks.interruptHandler ||
547 globalDebugHooks.callHook ||
548 globalDebugHooks.objectHook);
549 }
550 #endif
551
552 /* More debugging state, see jsdbgapi.c. */
553 JSCList trapList;
554 JSCList watchPointList;
555
556 /* Client opaque pointers */
557 void *data;
558
559 #ifdef JS_THREADSAFE
560 /* These combine to interlock the GC and new requests. */
561 PRLock *gcLock;
562 PRCondVar *gcDone;
563 PRCondVar *requestDone;
564 uint32 requestCount;
565 JSThread *gcThread;
566
567 /* Lock and owning thread pointer for JS_LOCK_RUNTIME. */
568 PRLock *rtLock;
569 #ifdef DEBUG
570 jsword rtLockOwner;
571 #endif
572
573 /* Used to synchronize down/up state change; protected by gcLock. */
574 PRCondVar *stateChange;
575
576 /*
577 * State for sharing single-threaded titles, once a second thread tries to
578 * lock a title. The titleSharingDone condvar is protected by rt->gcLock
579 * to minimize number of locks taken in JS_EndRequest.
580 *
581 * The titleSharingTodo linked list is likewise "global" per runtime, not
582 * one-list-per-context, to conserve space over all contexts, optimizing
583 * for the likely case that titles become shared rarely, and among a very
584 * small set of threads (contexts).
585 */
586 PRCondVar *titleSharingDone;
587 JSTitle *titleSharingTodo;
588
589 /*
590 * Magic terminator for the rt->titleSharingTodo linked list, threaded through
591 * title->u.link. This hack allows us to test whether a title is on the list
592 * by asking whether title->u.link is non-null. We use a large, likely bogus
593 * pointer here to distinguish this value from any valid u.count (small int)
594 * value.
595 */
596 #define NO_TITLE_SHARING_TODO ((JSTitle *) 0xfeedbeef)
597
598 /*
599 * Lock serializing trapList and watchPointList accesses, and count of all
600 * mutations to trapList and watchPointList made by debugger threads. To
601 * keep the code simple, we define debuggerMutations for the thread-unsafe
602 * case too.
603 */
604 PRLock *debuggerLock;
605
606 JSDHashTable threads;
607 #endif /* JS_THREADSAFE */
608 uint32 debuggerMutations;
609
610 /*
611 * Security callbacks set on the runtime are used by each context unless
612 * an override is set on the context.
613 */
614 JSSecurityCallbacks *securityCallbacks;
615
616 /*
617 * Shared scope property tree, and arena-pool for allocating its nodes.
618 * The propertyRemovals counter is incremented for every JSScope::clear,
619 * and for each JSScope::remove method call that frees a slot in an object.
620 * See js_NativeGet and js_NativeSet in jsobj.c.
621 */
622 JSDHashTable propertyTreeHash;
623 JSScopeProperty *propertyFreeList;
624 JSArenaPool propertyArenaPool;
625 int32 propertyRemovals;
626
627 /* Script filename table. */
628 struct JSHashTable *scriptFilenameTable;
629 JSCList scriptFilenamePrefixes;
630 #ifdef JS_THREADSAFE
631 PRLock *scriptFilenameTableLock;
632 #endif
633
634 /* Number localization, used by jsnum.c */
635 const char *thousandsSeparator;
636 const char *decimalSeparator;
637 const char *numGrouping;
638
639 /*
640 * Weak references to lazily-created, well-known XML singletons.
641 *
642 * NB: Singleton objects must be carefully disconnected from the rest of
643 * the object graph usually associated with a JSContext's global object,
644 * including the set of standard class objects. See jsxml.c for details.
645 */
646 JSObject *anynameObject;
647 JSObject *functionNamespaceObject;
648
649 #ifndef JS_THREADSAFE
650 JSThreadData threadData;
651
652 #define JS_THREAD_DATA(cx) (&(cx)->runtime->threadData)
653 #endif
654
655 /*
656 * Object shape (property cache structural type) identifier generator.
657 *
658 * Type 0 stands for the empty scope, and must not be regenerated due to
659 * uint32 wrap-around. Since js_GenerateShape (in jsinterp.cpp) uses
660 * atomic pre-increment, the initial value for the first typed non-empty
661 * scope will be 1.
662 *
663 * If this counter overflows into SHAPE_OVERFLOW_BIT (in jsinterp.h), the
664 * cache is disabled, to avoid aliasing two different types. It stays
665 * disabled until a triggered GC at some later moment compresses live
666 * types, minimizing rt->shapeGen in the process.
667 */
668 volatile uint32 shapeGen;
669
670 /* Literal table maintained by jsatom.c functions. */
671 JSAtomState atomState;
672
673 /*
674 * Various metering fields are defined at the end of JSRuntime. In this
675 * way there is no need to recompile all the code that refers to other
676 * fields of JSRuntime after enabling the corresponding metering macro.
677 */
678 #ifdef JS_DUMP_ENUM_CACHE_STATS
679 int32 nativeEnumProbes;
680 int32 nativeEnumMisses;
681 # define ENUM_CACHE_METER(name) JS_ATOMIC_INCREMENT(&cx->runtime->name)
682 #else
683 # define ENUM_CACHE_METER(name) ((void) 0)
684 #endif
685
686 #ifdef JS_DUMP_LOOP_STATS
687 /* Loop statistics, to trigger trace recording and compiling. */
688 JSBasicStats loopStats;
689 #endif
690
691 #if defined DEBUG || defined JS_DUMP_PROPTREE_STATS
692 /* Function invocation metering. */
693 jsrefcount inlineCalls;
694 jsrefcount nativeCalls;
695 jsrefcount nonInlineCalls;
696 jsrefcount constructs;
697
698 /* Title lock and scope property metering. */
699 jsrefcount claimAttempts;
700 jsrefcount claimedTitles;
701 jsrefcount deadContexts;
702 jsrefcount deadlocksAvoided;
703 jsrefcount liveScopes;
704 jsrefcount sharedTitles;
705 jsrefcount totalScopes;
706 jsrefcount liveScopeProps;
707 jsrefcount liveScopePropsPreSweep;
708 jsrefcount totalScopeProps;
709 jsrefcount livePropTreeNodes;
710 jsrefcount duplicatePropTreeNodes;
711 jsrefcount totalPropTreeNodes;
712 jsrefcount propTreeKidsChunks;
713 jsrefcount middleDeleteFixups;
714
715 /* String instrumentation. */
716 jsrefcount liveStrings;
717 jsrefcount totalStrings;
718 jsrefcount liveDependentStrings;
719 jsrefcount totalDependentStrings;
720 jsrefcount badUndependStrings;
721 double lengthSum;
722 double lengthSquaredSum;
723 double strdepLengthSum;
724 double strdepLengthSquaredSum;
725 #endif /* DEBUG || JS_DUMP_PROPTREE_STATS */
726
727 #ifdef JS_SCOPE_DEPTH_METER
728 /*
729 * Stats on runtime prototype chain lookups and scope chain depths, i.e.,
730 * counts of objects traversed on a chain until the wanted id is found.
731 */
732 JSBasicStats protoLookupDepthStats;
733 JSBasicStats scopeSearchDepthStats;
734
735 /*
736 * Stats on compile-time host environment and lexical scope chain lengths
737 * (maximum depths).
738 */
739 JSBasicStats hostenvScopeDepthStats;
740 JSBasicStats lexicalScopeDepthStats;
741 #endif
742
743 #ifdef JS_GCMETER
744 JSGCStats gcStats;
745 #endif
746
747 #ifdef JS_FUNCTION_METERING
748 JSFunctionMeter functionMeter;
749 char lastScriptFilename[1024];
750 #endif
751
752 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
753
754 void setGCTriggerFactor(uint32 factor);
755 void setGCLastBytes(size_t lastBytes);
756
757 inline void* malloc(size_t bytes) {
758 return ::js_malloc(bytes);
759 }
760
761 inline void* calloc(size_t bytes) {
762 return ::js_calloc(bytes);
763 }
764
765 inline void* realloc(void* p, size_t bytes) {
766 return ::js_realloc(p, bytes);
767 }
768
769 inline void free(void* p) {
770 ::js_free(p);
771 }
772
773 #ifdef JS_THREADSAFE
774 JSBackgroundThread *deallocatorThread;
775 #endif
776
777 #endif /* __cplusplus */
778 };
779
780 /* Common macros to access thread-local caches in JSThread or JSRuntime. */
781 #define JS_GSN_CACHE(cx) (JS_THREAD_DATA(cx)->gsnCache)
782 #define JS_PROPERTY_CACHE(cx) (JS_THREAD_DATA(cx)->propertyCache)
783 #define JS_TRACE_MONITOR(cx) (JS_THREAD_DATA(cx)->traceMonitor)
784 #define JS_SCRIPTS_TO_GC(cx) (JS_THREAD_DATA(cx)->scriptsToGC)
785
786 #ifdef JS_EVAL_CACHE_METERING
787 # define EVAL_CACHE_METER(x) (JS_THREAD_DATA(cx)->evalCacheMeter.x++)
788 #else
789 # define EVAL_CACHE_METER(x) ((void) 0)
790 #endif
791
792 #ifdef DEBUG
793 # define JS_RUNTIME_METER(rt, which) JS_ATOMIC_INCREMENT(&(rt)->which)
794 # define JS_RUNTIME_UNMETER(rt, which) JS_ATOMIC_DECREMENT(&(rt)->which)
795 #else
796 # define JS_RUNTIME_METER(rt, which) /* nothing */
797 # define JS_RUNTIME_UNMETER(rt, which) /* nothing */
798 #endif
799
800 #define JS_KEEP_ATOMS(rt) JS_ATOMIC_INCREMENT(&(rt)->gcKeepAtoms);
801 #define JS_UNKEEP_ATOMS(rt) JS_ATOMIC_DECREMENT(&(rt)->gcKeepAtoms);
802
803 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
804 /*
805 * Linked list mapping format strings for JS_{Convert,Push}Arguments{,VA} to
806 * formatter functions. Elements are sorted in non-increasing format string
807 * length order.
808 */
809 struct JSArgumentFormatMap {
810 const char *format;
811 size_t length;
812 JSArgumentFormatter formatter;
813 JSArgumentFormatMap *next;
814 };
815 #endif
816
817 struct JSStackHeader {
818 uintN nslots;
819 JSStackHeader *down;
820 };
821
822 #define JS_STACK_SEGMENT(sh) ((jsval *)(sh) + 2)
823
824 /*
825 * Key and entry types for the JSContext.resolvingTable hash table, typedef'd
826 * here because all consumers need to see these declarations (and not just the
827 * typedef names, as would be the case for an opaque pointer-to-typedef'd-type
828 * declaration), along with cx->resolvingTable.
829 */
830 typedef struct JSResolvingKey {
831 JSObject *obj;
832 jsid id;
833 } JSResolvingKey;
834
835 typedef struct JSResolvingEntry {
836 JSDHashEntryHdr hdr;
837 JSResolvingKey key;
838 uint32 flags;
839 } JSResolvingEntry;
840
841 #define JSRESFLAG_LOOKUP 0x1 /* resolving id from lookup */
842 #define JSRESFLAG_WATCH 0x2 /* resolving id from watch */
843
844 typedef struct JSLocalRootChunk JSLocalRootChunk;
845
846 #define JSLRS_CHUNK_SHIFT 8
847 #define JSLRS_CHUNK_SIZE JS_BIT(JSLRS_CHUNK_SHIFT)
848 #define JSLRS_CHUNK_MASK JS_BITMASK(JSLRS_CHUNK_SHIFT)
849
850 struct JSLocalRootChunk {
851 jsval roots[JSLRS_CHUNK_SIZE];
852 JSLocalRootChunk *down;
853 };
854
855 typedef struct JSLocalRootStack {
856 uint32 scopeMark;
857 uint32 rootCount;
858 JSLocalRootChunk *topChunk;
859 JSLocalRootChunk firstChunk;
860 } JSLocalRootStack;
861
862 #define JSLRS_NULL_MARK ((uint32) -1)
863
864 /*
865 * Macros to push/pop JSTempValueRooter instances to context-linked stack of
866 * temporary GC roots. If you need to protect a result value that flows out of
867 * a C function across several layers of other functions, use the
868 * js_LeaveLocalRootScopeWithResult internal API (see further below) instead.
869 *
870 * The macros also provide a simple way to get a single rooted pointer via
871 * JS_PUSH_TEMP_ROOT_<KIND>(cx, NULL, &tvr). Then &tvr.u.<kind> gives the
872 * necessary pointer.
873 *
874 * JSTempValueRooter.count defines the type of the rooted value referenced by
875 * JSTempValueRooter.u union of type JSTempValueUnion. When count is positive
876 * or zero, u.array points to a vector of jsvals. Otherwise it must be one of
877 * the following constants:
878 */
879 #define JSTVU_SINGLE (-1) /* u.value or u.<gcthing> is single jsval
880 or non-JSString GC-thing pointer */
881 #define JSTVU_TRACE (-2) /* u.trace is a hook to trace a custom
882 * structure */
883 #define JSTVU_SPROP (-3) /* u.sprop roots property tree node */
884 #define JSTVU_WEAK_ROOTS (-4) /* u.weakRoots points to saved weak roots */
885 #define JSTVU_COMPILER (-5) /* u.compiler roots JSCompiler* */
886 #define JSTVU_SCRIPT (-6) /* u.script roots JSScript* */
887 #define JSTVU_ENUMERATOR (-7) /* a pointer to JSTempValueRooter points
888 to an instance of JSAutoEnumStateRooter
889 with u.object storing the enumeration
890 object */
891
892 /*
893 * Here single JSTVU_SINGLE covers both jsval and pointers to almost (see note
894 * below) any GC-thing via reinterpreting the thing as JSVAL_OBJECT. This works
895 * because the GC-thing is aligned on a 0 mod 8 boundary, and object has the 0
896 * jsval tag. So any GC-heap-allocated thing pointer may be tagged as if it
897 * were an object and untagged, if it's then used only as an opaque pointer
898 * until discriminated by other means than tag bits. This is how, for example,
899 * js_GetGCThingTraceKind uses its |thing| parameter -- it consults GC-thing
900 * flags stored separately from the thing to decide the kind of thing.
901 *
902 * Note well that JSStrings may be statically allocated (see the intStringTable
903 * and unitStringTable static arrays), so this hack does not work for arbitrary
904 * GC-thing pointers.
905 */
906 #define JS_PUSH_TEMP_ROOT_COMMON(cx,x,tvr,cnt,kind) \
907 JS_BEGIN_MACRO \
908 JS_ASSERT((cx)->tempValueRooters != (tvr)); \
909 (tvr)->count = (cnt); \
910 (tvr)->u.kind = (x); \
911 (tvr)->down = (cx)->tempValueRooters; \
912 (cx)->tempValueRooters = (tvr); \
913 JS_END_MACRO
914
915 #define JS_POP_TEMP_ROOT(cx,tvr) \
916 JS_BEGIN_MACRO \
917 JS_ASSERT((cx)->tempValueRooters == (tvr)); \
918 (cx)->tempValueRooters = (tvr)->down; \
919 JS_END_MACRO
920
921 #define JS_PUSH_TEMP_ROOT(cx,cnt,arr,tvr) \
922 JS_BEGIN_MACRO \
923 JS_ASSERT((int)(cnt) >= 0); \
924 JS_PUSH_TEMP_ROOT_COMMON(cx, arr, tvr, (ptrdiff_t) (cnt), array); \
925 JS_END_MACRO
926
927 #define JS_PUSH_SINGLE_TEMP_ROOT(cx,val,tvr) \
928 JS_PUSH_TEMP_ROOT_COMMON(cx, val, tvr, JSTVU_SINGLE, value)
929
930 #define JS_PUSH_TEMP_ROOT_OBJECT(cx,obj,tvr) \
931 JS_PUSH_TEMP_ROOT_COMMON(cx, obj, tvr, JSTVU_SINGLE, object)
932
933 #define JS_PUSH_TEMP_ROOT_STRING(cx,str,tvr) \
934 JS_PUSH_SINGLE_TEMP_ROOT(cx, str ? STRING_TO_JSVAL(str) : JSVAL_NULL, tvr)
935
936 #define JS_PUSH_TEMP_ROOT_XML(cx,xml_,tvr) \
937 JS_PUSH_TEMP_ROOT_COMMON(cx, xml_, tvr, JSTVU_SINGLE, xml)
938
939 #define JS_PUSH_TEMP_ROOT_TRACE(cx,trace_,tvr) \
940 JS_PUSH_TEMP_ROOT_COMMON(cx, trace_, tvr, JSTVU_TRACE, trace)
941
942 #define JS_PUSH_TEMP_ROOT_SPROP(cx,sprop_,tvr) \
943 JS_PUSH_TEMP_ROOT_COMMON(cx, sprop_, tvr, JSTVU_SPROP, sprop)
944
945 #define JS_PUSH_TEMP_ROOT_WEAK_COPY(cx,weakRoots_,tvr) \
946 JS_PUSH_TEMP_ROOT_COMMON(cx, weakRoots_, tvr, JSTVU_WEAK_ROOTS, weakRoots)
947
948 #define JS_PUSH_TEMP_ROOT_COMPILER(cx,pc,tvr) \
949 JS_PUSH_TEMP_ROOT_COMMON(cx, pc, tvr, JSTVU_COMPILER, compiler)
950
951 #define JS_PUSH_TEMP_ROOT_SCRIPT(cx,script_,tvr) \
952 JS_PUSH_TEMP_ROOT_COMMON(cx, script_, tvr, JSTVU_SCRIPT, script)
953
954 #define JSRESOLVE_INFER 0xffff /* infer bits from current bytecode */
955
956 extern const JSDebugHooks js_NullDebugHooks; /* defined in jsdbgapi.cpp */
957
958 struct JSContext {
959 /*
960 * If this flag is set, we were asked to call back the operation callback
961 * as soon as possible.
962 */
963 volatile jsint operationCallbackFlag;
964
965 /* JSRuntime contextList linkage. */
966 JSCList link;
967
968 #if JS_HAS_XML_SUPPORT
969 /*
970 * Bit-set formed from binary exponentials of the XML_* tiny-ids defined
971 * for boolean settings in jsxml.c, plus an XSF_CACHE_VALID bit. Together
972 * these act as a cache of the boolean XML.ignore* and XML.prettyPrinting
973 * property values associated with this context's global object.
974 */
975 uint8 xmlSettingFlags;
976 uint8 padding;
977 #else
978 uint16 padding;
979 #endif
980
981 /*
982 * Classic Algol "display" static link optimization.
983 */
984 #define JS_DISPLAY_SIZE 16U
985
986 JSStackFrame *display[JS_DISPLAY_SIZE];
987
988 /* Runtime version control identifier. */
989 uint16 version;
990
991 /* Per-context options. */
992 uint32 options; /* see jsapi.h for JSOPTION_* */
993
994 /* Locale specific callbacks for string conversion. */
995 JSLocaleCallbacks *localeCallbacks;
996
997 /*
998 * cx->resolvingTable is non-null and non-empty if we are initializing
999 * standard classes lazily, or if we are otherwise recursing indirectly
1000 * from js_LookupProperty through a JSClass.resolve hook. It is used to
1001 * limit runaway recursion (see jsapi.c and jsobj.c).
1002 */
1003 JSDHashTable *resolvingTable;
1004
1005 #if JS_HAS_LVALUE_RETURN
1006 /*
1007 * Secondary return value from native method called on the left-hand side
1008 * of an assignment operator. The native should store the object in which
1009 * to set a property in *rval, and return the property's id expressed as a
1010 * jsval by calling JS_SetCallReturnValue2(cx, idval).
1011 */
1012 jsval rval2;
1013 JSPackedBool rval2set;
1014 #endif
1015
1016 /*
1017 * True if generating an error, to prevent runaway recursion.
1018 * NB: generatingError packs with rval2set, #if JS_HAS_LVALUE_RETURN;
1019 * with insideGCMarkCallback and with throwing below.
1020 */
1021 JSPackedBool generatingError;
1022
1023 /* Flag to indicate that we run inside gcCallback(cx, JSGC_MARK_END). */
1024 JSPackedBool insideGCMarkCallback;
1025
1026 /* Exception state -- the exception member is a GC root by definition. */
1027 JSPackedBool throwing; /* is there a pending exception? */
1028 jsval exception; /* most-recently-thrown exception */
1029
1030 /* Limit pointer for checking native stack consumption during recursion. */
1031 jsuword stackLimit;
1032
1033 /* Quota on the size of arenas used to compile and execute scripts. */
1034 size_t scriptStackQuota;
1035
1036 /* Data shared by threads in an address space. */
1037 JSRuntime * const runtime;
1038
1039 explicit JSContext(JSRuntime *rt) : runtime(rt) {}
1040
1041 /* Stack arena pool and frame pointer register. */
1042 JS_REQUIRES_STACK
1043 JSArenaPool stackPool;
1044
1045 JS_REQUIRES_STACK
1046 JSStackFrame *fp;
1047
1048 /* Temporary arena pool used while compiling and decompiling. */
1049 JSArenaPool tempPool;
1050
1051 /* Top-level object and pointer to top stack frame's scope chain. */
1052 JSObject *globalObject;
1053
1054 /* Storage to root recently allocated GC things and script result. */
1055 JSWeakRoots weakRoots;
1056
1057 /* Regular expression class statics (XXX not shared globally). */
1058 JSRegExpStatics regExpStatics;
1059
1060 /* State for object and array toSource conversion. */
1061 JSSharpObjectMap sharpObjectMap;
1062 JSHashTable *busyArrayTable;
1063
1064 /* Argument formatter support for JS_{Convert,Push}Arguments{,VA}. */
1065 JSArgumentFormatMap *argumentFormatMap;
1066
1067 /* Last message string and trace file for debugging. */
1068 char *lastMessage;
1069 #ifdef DEBUG
1070 void *tracefp;
1071 jsbytecode *tracePrevPc;
1072 #endif
1073
1074 /* Per-context optional error reporter. */
1075 JSErrorReporter errorReporter;
1076
1077 /* Branch callback. */
1078 JSOperationCallback operationCallback;
1079
1080 /* Interpreter activation count. */
1081 uintN interpLevel;
1082
1083 /* Client opaque pointers. */
1084 void *data;
1085 void *data2;
1086
1087 /* GC and thread-safe state. */
1088 JSStackFrame *dormantFrameChain; /* dormant stack frame to scan */
1089 #ifdef JS_THREADSAFE
1090 JSThread *thread;
1091 jsrefcount requestDepth;
1092 /* Same as requestDepth but ignoring JS_SuspendRequest/JS_ResumeRequest */
1093 jsrefcount outstandingRequests;
1094 JSTitle *lockedSealedTitle; /* weak ref, for low-cost sealed
1095 title locking */
1096 JSCList threadLinks; /* JSThread contextList linkage */
1097
1098 #define CX_FROM_THREAD_LINKS(tl) \
1099 ((JSContext *)((char *)(tl) - offsetof(JSContext, threadLinks)))
1100 #endif
1101
1102 /* PDL of stack headers describing stack slots not rooted by argv, etc. */
1103 JSStackHeader *stackHeaders;
1104
1105 /* Optional stack of heap-allocated scoped local GC roots. */
1106 JSLocalRootStack *localRootStack;
1107
1108 /* Stack of thread-stack-allocated temporary GC roots. */
1109 JSTempValueRooter *tempValueRooters;
1110
1111 /* List of pre-allocated doubles. */
1112 JSGCDoubleCell *doubleFreeList;
1113
1114 /* Debug hooks associated with the current context. */
1115 const JSDebugHooks *debugHooks;
1116
1117 /* Security callbacks that override any defined on the runtime. */
1118 JSSecurityCallbacks *securityCallbacks;
1119
1120 /* Pinned regexp pool used for regular expressions. */
1121 JSArenaPool regexpPool;
1122
1123 /* Stored here to avoid passing it around as a parameter. */
1124 uintN resolveFlags;
1125
1126 /* Random number generator state, used by jsmath.cpp. */
1127 int64 rngSeed;
1128
1129 #ifdef JS_TRACER
1130 /*
1131 * State for the current tree execution. bailExit is valid if the tree has
1132 * called back into native code via a _FAIL builtin and has not yet bailed,
1133 * else garbage (NULL in debug builds).
1134 */
1135 InterpState *interpState;
1136 VMSideExit *bailExit;
1137
1138 /*
1139 * True if traces may be executed. Invariant: The value of jitEnabled is
1140 * always equal to the expression in updateJITEnabled below.
1141 *
1142 * This flag and the fields accessed by updateJITEnabled are written only
1143 * in runtime->gcLock, to avoid race conditions that would leave the wrong
1144 * value in jitEnabled. (But the interpreter reads this without
1145 * locking. That can race against another thread setting debug hooks, but
1146 * we always read cx->debugHooks without locking anyway.)
1147 */
1148 bool jitEnabled;
1149 #endif
1150
1151 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files, */
1152
1153 /* Caller must be holding runtime->gcLock. */
1154 void updateJITEnabled() {
1155 #ifdef JS_TRACER
1156 jitEnabled = ((options & JSOPTION_JIT) &&
1157 (debugHooks == &js_NullDebugHooks ||
1158 (debugHooks == &runtime->globalDebugHooks &&
1159 !runtime->debuggerInhibitsJIT())));
1160 #endif
1161 }
1162
1163
1164 #ifdef JS_THREADSAFE
1165 inline void createDeallocatorTask() {
1166 JSThreadData* tls = JS_THREAD_DATA(this);
1167 JS_ASSERT(!tls->deallocatorTask);
1168 if (runtime->deallocatorThread && !runtime->deallocatorThread->busy())
1169 tls->deallocatorTask = new JSFreePointerListTask();
1170 }
1171
1172 inline void submitDeallocatorTask() {
1173 JSThreadData* tls = JS_THREAD_DATA(this);
1174 if (tls->deallocatorTask) {
1175 runtime->deallocatorThread->schedule(tls->deallocatorTask);
1176 tls->deallocatorTask = NULL;
1177 }
1178 }
1179 #endif
1180
1181 /* Call this after succesful malloc of memory for GC-related things. */
1182 inline void updateMallocCounter(size_t nbytes) {
1183 size_t *pbytes, bytes;
1184
1185 pbytes = &JS_THREAD_DATA(this)->gcMallocBytes;
1186 bytes = *pbytes;
1187 *pbytes = (size_t(-1) - bytes <= nbytes) ? size_t(-1) : bytes + nbytes;
1188 }
1189
1190 inline void* malloc(size_t bytes) {
1191 JS_ASSERT(bytes != 0);
1192 void *p = runtime->malloc(bytes);
1193 if (!p) {
1194 JS_ReportOutOfMemory(this);
1195 return NULL;
1196 }
1197 updateMallocCounter(bytes);
1198 return p;
1199 }
1200
1201 inline void* mallocNoReport(size_t bytes) {
1202 JS_ASSERT(bytes != 0);
1203 void *p = runtime->malloc(bytes);
1204 if (!p)
1205 return NULL;
1206 updateMallocCounter(bytes);
1207 return p;
1208 }
1209
1210 inline void* calloc(size_t bytes) {
1211 JS_ASSERT(bytes != 0);
1212 void *p = runtime->calloc(bytes);
1213 if (!p) {
1214 JS_ReportOutOfMemory(this);
1215 return NULL;
1216 }
1217 updateMallocCounter(bytes);
1218 return p;
1219 }
1220
1221 inline void* realloc(void* p, size_t bytes) {
1222 void *orig = p;
1223 p = runtime->realloc(p, bytes);
1224 if (!p) {
1225 JS_ReportOutOfMemory(this);
1226 return NULL;
1227 }
1228 if (!orig)
1229 updateMallocCounter(bytes);
1230 return p;
1231 }
1232
1233 #ifdef JS_THREADSAFE
1234 inline void free(void* p) {
1235 if (!p)
1236 return;
1237 if (thread) {
1238 JSFreePointerListTask* task = JS_THREAD_DATA(this)->deallocatorTask;
1239 if (task) {
1240 task->add(p);
1241 return;
1242 }
1243 }
1244 runtime->free(p);
1245 }
1246 #else
1247 inline void free(void* p) {
1248 if (!p)
1249 return;
1250 runtime->free(p);
1251 }
1252 #endif
1253
1254 /*
1255 * In the common case that we'd like to allocate the memory for an object
1256 * with cx->malloc/free, we cannot use overloaded C++ operators (no
1257 * placement delete). Factor the common workaround into one place.
1258 */
1259 #define CREATE_BODY(parms) \
1260 void *memory = this->malloc(sizeof(T)); \
1261 if (!memory) { \
1262 JS_ReportOutOfMemory(this); \
1263 return NULL; \
1264 } \
1265 return new(memory) T parms;
1266
1267 template <class T>
1268 JS_ALWAYS_INLINE T *create() {
1269 CREATE_BODY(())
1270 }
1271
1272 template <class T, class P1>
1273 JS_ALWAYS_INLINE T *create(const P1 &p1) {
1274 CREATE_BODY((p1))
1275 }
1276
1277 template <class T, class P1, class P2>
1278 JS_ALWAYS_INLINE T *create(const P1 &p1, const P2 &p2) {
1279 CREATE_BODY((p1, p2))
1280 }
1281
1282 template <class T, class P1, class P2, class P3>
1283 JS_ALWAYS_INLINE T *create(const P1 &p1, const P2 &p2, const P3 &p3) {
1284 CREATE_BODY((p1, p2, p3))
1285 }
1286 #undef CREATE_BODY
1287
1288 template <class T>
1289 JS_ALWAYS_INLINE void destroy(T *p) {
1290 p->~T();
1291 this->free(p);
1292 }
1293
1294 #endif /* __cplusplus */
1295 };
1296
1297 #ifdef JS_THREADSAFE
1298 # define JS_THREAD_ID(cx) ((cx)->thread ? (cx)->thread->id : 0)
1299 #endif
1300
1301 #ifdef __cplusplus
1302
1303 static inline JSAtom **
1304 FrameAtomBase(JSContext *cx, JSStackFrame *fp)
1305 {
1306 return fp->imacpc
1307 ? COMMON_ATOMS_START(&cx->runtime->atomState)
1308 : fp->script->atomMap.vector;
1309 }
1310
1311 /* FIXME(bug 332648): Move this into a public header. */
1312 class JSAutoTempValueRooter
1313 {
1314 public:
1315 JSAutoTempValueRooter(JSContext *cx, size_t len, jsval *vec
1316 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1317 : mContext(cx) {
1318 JS_GUARD_OBJECT_NOTIFIER_INIT;
1319 JS_PUSH_TEMP_ROOT(mContext, len, vec, &mTvr);
1320 }
1321 explicit JSAutoTempValueRooter(JSContext *cx, jsval v = JSVAL_NULL
1322 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1323 : mContext(cx) {
1324 JS_GUARD_OBJECT_NOTIFIER_INIT;
1325 JS_PUSH_SINGLE_TEMP_ROOT(mContext, v, &mTvr);
1326 }
1327 JSAutoTempValueRooter(JSContext *cx, JSString *str
1328 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1329 : mContext(cx) {
1330 JS_GUARD_OBJECT_NOTIFIER_INIT;
1331 JS_PUSH_TEMP_ROOT_STRING(mContext, str, &mTvr);
1332 }
1333 JSAutoTempValueRooter(JSContext *cx, JSObject *obj
1334 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1335 : mContext(cx) {
1336 JS_GUARD_OBJECT_NOTIFIER_INIT;
1337 JS_PUSH_TEMP_ROOT_OBJECT(mContext, obj, &mTvr);
1338 }
1339
1340 ~JSAutoTempValueRooter() {
1341 JS_POP_TEMP_ROOT(mContext, &mTvr);
1342 }
1343
1344 jsval value() { return mTvr.u.value; }
1345 jsval *addr() { return &mTvr.u.value; }
1346
1347 protected:
1348 JSContext *mContext;
1349
1350 private:
1351 #ifndef AIX
1352 static void *operator new(size_t);
1353 static void operator delete(void *, size_t);
1354 #endif
1355
1356 JSTempValueRooter mTvr;
1357 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1358 };
1359
1360 class JSAutoTempIdRooter
1361 {
1362 public:
1363 explicit JSAutoTempIdRooter(JSContext *cx, jsid id = INT_TO_JSID(0)
1364 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1365 : mContext(cx) {
1366 JS_GUARD_OBJECT_NOTIFIER_INIT;
1367 JS_PUSH_SINGLE_TEMP_ROOT(mContext, ID_TO_VALUE(id), &mTvr);
1368 }
1369
1370 ~JSAutoTempIdRooter() {
1371 JS_POP_TEMP_ROOT(mContext, &mTvr);
1372 }
1373
1374 jsid id() { return (jsid) mTvr.u.value; }
1375 jsid * addr() { return (jsid *) &mTvr.u.value; }
1376
1377 private:
1378 JSContext *mContext;
1379 JSTempValueRooter mTvr;
1380 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1381 };
1382
1383 /* The auto-root for enumeration object and its state. */
1384 class JSAutoEnumStateRooter : public JSTempValueRooter
1385 {
1386 public:
1387 JSAutoEnumStateRooter(JSContext *cx, JSObject *obj, jsval *statep
1388 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1389 : mContext(cx), mStatep(statep)
1390 {
1391 JS_GUARD_OBJECT_NOTIFIER_INIT;
1392 JS_ASSERT(obj);
1393 JS_ASSERT(statep);
1394 JS_PUSH_TEMP_ROOT_COMMON(cx, obj, this, JSTVU_ENUMERATOR, object);
1395 }
1396
1397 ~JSAutoEnumStateRooter() {
1398 JS_POP_TEMP_ROOT(mContext, this);
1399 }
1400
1401 void mark(JSTracer *trc) {
1402 JS_CALL_OBJECT_TRACER(trc, u.object, "enumerator_obj");
1403 js_MarkEnumeratorState(trc, u.object, *mStatep);
1404 }
1405
1406 private:
1407 JSContext *mContext;
1408 jsval *mStatep;
1409 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1410 };
1411
1412 class JSAutoResolveFlags
1413 {
1414 public:
1415 JSAutoResolveFlags(JSContext *cx, uintN flags
1416 JS_GUARD_OBJECT_NOTIFIER_PARAM)
1417 : mContext(cx), mSaved(cx->resolveFlags) {
1418 JS_GUARD_OBJECT_NOTIFIER_INIT;
1419 cx->resolveFlags = flags;
1420 }
1421
1422 ~JSAutoResolveFlags() { mContext->resolveFlags = mSaved; }
1423
1424 private:
1425 JSContext *mContext;
1426 uintN mSaved;
1427 JS_DECL_USE_GUARD_OBJECT_NOTIFIER
1428 };
1429
1430 #endif /* __cplusplus */
1431
1432 /*
1433 * Slightly more readable macros for testing per-context option settings (also
1434 * to hide bitset implementation detail).
1435 *
1436 * JSOPTION_XML must be handled specially in order to propagate from compile-
1437 * to run-time (from cx->options to script->version/cx->version). To do that,
1438 * we copy JSOPTION_XML from cx->options into cx->version as JSVERSION_HAS_XML
1439 * whenever options are set, and preserve this XML flag across version number
1440 * changes done via the JS_SetVersion API.
1441 *
1442 * But when executing a script or scripted function, the interpreter changes
1443 * cx->version, including the XML flag, to script->version. Thus JSOPTION_XML
1444 * is a compile-time option that causes a run-time version change during each
1445 * activation of the compiled script. That version change has the effect of
1446 * changing JS_HAS_XML_OPTION, so that any compiling done via eval enables XML
1447 * support. If an XML-enabled script or function calls a non-XML function,
1448 * the flag bit will be cleared during the callee's activation.
1449 *
1450 * Note that JS_SetVersion API calls never pass JSVERSION_HAS_XML or'd into
1451 * that API's version parameter.
1452 *
1453 * Note also that script->version must contain this XML option flag in order
1454 * for XDR'ed scripts to serialize and deserialize with that option preserved
1455 * for detection at run-time. We can't copy other compile-time options into
1456 * script->version because that would break backward compatibility (certain
1457 * other options, e.g. JSOPTION_VAROBJFIX, are analogous to JSOPTION_XML).
1458 */
1459 #define JS_HAS_OPTION(cx,option) (((cx)->options & (option)) != 0)
1460 #define JS_HAS_STRICT_OPTION(cx) JS_HAS_OPTION(cx, JSOPTION_STRICT)
1461 #define JS_HAS_WERROR_OPTION(cx) JS_HAS_OPTION(cx, JSOPTION_WERROR)
1462 #define JS_HAS_COMPILE_N_GO_OPTION(cx) JS_HAS_OPTION(cx, JSOPTION_COMPILE_N_GO)
1463 #define JS_HAS_ATLINE_OPTION(cx) JS_HAS_OPTION(cx, JSOPTION_ATLINE)
1464
1465 #define JSVERSION_MASK 0x0FFF /* see JSVersion in jspubtd.h */
1466 #define JSVERSION_HAS_XML 0x1000 /* flag induced by XML option */
1467 #define JSVERSION_ANONFUNFIX 0x2000 /* see jsapi.h, the comments
1468 for JSOPTION_ANONFUNFIX */
1469
1470 #define JSVERSION_NUMBER(cx) ((JSVersion)((cx)->version & \
1471 JSVERSION_MASK))
1472 #define JS_HAS_XML_OPTION(cx) ((cx)->version & JSVERSION_HAS_XML || \
1473 JSVERSION_NUMBER(cx) >= JSVERSION_1_6)
1474
1475 extern JSThreadData *
1476 js_CurrentThreadData(JSRuntime *rt);
1477
1478 extern JSBool
1479 js_InitThreads(JSRuntime *rt);
1480
1481 extern void
1482 js_FinishThreads(JSRuntime *rt);
1483
1484 extern void
1485 js_PurgeThreads(JSContext *cx);
1486
1487 extern void
1488 js_TraceThreads(JSRuntime *rt, JSTracer *trc);
1489
1490 /*
1491 * Ensures the JSOPTION_XML and JSOPTION_ANONFUNFIX bits of cx->options are
1492 * reflected in cx->version, since each bit must travel with a script that has
1493 * it set.
1494 */
1495 extern void
1496 js_SyncOptionsToVersion(JSContext *cx);
1497
1498 /*
1499 * Common subroutine of JS_SetVersion and js_SetVersion, to update per-context
1500 * data that depends on version.
1501 */
1502 extern void
1503 js_OnVersionChange(JSContext *cx);
1504
1505 /*
1506 * Unlike the JS_SetVersion API, this function stores JSVERSION_HAS_XML and
1507 * any future non-version-number flags induced by compiler options.
1508 */
1509 extern void
1510 js_SetVersion(JSContext *cx, JSVersion version);
1511
1512 /*
1513 * Create and destroy functions for JSContext, which is manually allocated
1514 * and exclusively owned.
1515 */
1516 extern JSContext *
1517 js_NewContext(JSRuntime *rt, size_t stackChunkSize);
1518
1519 extern void
1520 js_DestroyContext(JSContext *cx, JSDestroyContextMode mode);
1521
1522 /*
1523 * Return true if cx points to a context in rt->contextList, else return false.
1524 * NB: the caller (see jslock.c:ClaimTitle) must hold rt->gcLock.
1525 */
1526 extern JSBool
1527 js_ValidContextPointer(JSRuntime *rt, JSContext *cx);
1528
1529 static JS_INLINE JSContext *
1530 js_ContextFromLinkField(JSCList *link)
1531 {
1532 JS_ASSERT(link);
1533 return (JSContext *) ((uint8 *) link - offsetof(JSContext, link));
1534 }
1535
1536 /*
1537 * If unlocked, acquire and release rt->gcLock around *iterp update; otherwise
1538 * the caller must be holding rt->gcLock.
1539 */
1540 extern JSContext *
1541 js_ContextIterator(JSRuntime *rt, JSBool unlocked, JSContext **iterp);
1542
1543 /*
1544 * Iterate through contexts with active requests. The caller must be holding
1545 * rt->gcLock in case of a thread-safe build, or otherwise guarantee that the
1546 * context list is not alternated asynchroniously.
1547 */
1548 extern JS_FRIEND_API(JSContext *)
1549 js_NextActiveContext(JSRuntime *, JSContext *);
1550
1551 #ifdef JS_THREADSAFE
1552
1553 /*
1554 * Count the number of contexts entered requests on the current thread.
1555 */
1556 uint32
1557 js_CountThreadRequests(JSContext *cx);
1558
1559 /*
1560 * This is a helper for code at can potentially run outside JS request to
1561 * ensure that the GC is not running when the function returns.
1562 *
1563 * This function must be called with the GC lock held.
1564 */
1565 extern void
1566 js_WaitForGC(JSRuntime *rt);
1567
1568 /*
1569 * If we're in one or more requests (possibly on more than one context)
1570 * running on the current thread, indicate, temporarily, that all these
1571 * requests are inactive so a possible GC can proceed on another thread.
1572 * This function returns the number of discounted requests. The number must
1573 * be passed later to js_ActivateRequestAfterGC to reactivate the requests.
1574 *
1575 * This function must be called with the GC lock held.
1576 */
1577 uint32
1578 js_DiscountRequestsForGC(JSContext *cx);
1579
1580 /*
1581 * This function must be called with the GC lock held.
1582 */
1583 void
1584 js_RecountRequestsAfterGC(JSRuntime *rt, uint32 requestDebit);
1585
1586 #else /* !JS_THREADSAFE */
1587
1588 # define js_WaitForGC(rt) ((void) 0)
1589
1590 #endif
1591
1592 /*
1593 * JSClass.resolve and watchpoint recursion damping machinery.
1594 */
1595 extern JSBool
1596 js_StartResolving(JSContext *cx, JSResolvingKey *key, uint32 flag,
1597 JSResolvingEntry **entryp);
1598
1599 extern void
1600 js_StopResolving(JSContext *cx, JSResolvingKey *key, uint32 flag,
1601 JSResolvingEntry *entry, uint32 generation);
1602
1603 /*
1604 * Local root set management.
1605 *
1606 * NB: the jsval parameters below may be properly tagged jsvals, or GC-thing
1607 * pointers cast to (jsval). This relies on JSObject's tag being zero, but
1608 * on the up side it lets us push int-jsval-encoded scopeMark values on the
1609 * local root stack.
1610 */
1611 extern JSBool
1612 js_EnterLocalRootScope(JSContext *cx);
1613
1614 #define js_LeaveLocalRootScope(cx) \
1615 js_LeaveLocalRootScopeWithResult(cx, JSVAL_NULL)
1616
1617 extern void
1618 js_LeaveLocalRootScopeWithResult(JSContext *cx, jsval rval);
1619
1620 extern void
1621 js_ForgetLocalRoot(JSContext *cx, jsval v);
1622
1623 extern int
1624 js_PushLocalRoot(JSContext *cx, JSLocalRootStack *lrs, jsval v);
1625
1626 extern void
1627 js_TraceLocalRoots(JSTracer *trc, JSLocalRootStack *lrs);
1628
1629 /*
1630 * Report an exception, which is currently realized as a printf-style format
1631 * string and its arguments.
1632 */
1633 typedef enum JSErrNum {
1634 #define MSG_DEF(name, number, count, exception, format) \
1635 name = number,
1636 #include "js.msg"
1637 #undef MSG_DEF
1638 JSErr_Limit
1639 } JSErrNum;
1640
1641 extern JS_FRIEND_API(const JSErrorFormatString *)
1642 js_GetErrorMessage(void *userRef, const char *locale, const uintN errorNumber);
1643
1644 #ifdef va_start
1645 extern JSBool
1646 js_ReportErrorVA(JSContext *cx, uintN flags, const char *format, va_list ap);
1647
1648 extern JSBool
1649 js_ReportErrorNumberVA(JSContext *cx, uintN flags, JSErrorCallback callback,
1650 void *userRef, const uintN errorNumber,
1651 JSBool charArgs, va_list ap);
1652
1653 extern JSBool
1654 js_ExpandErrorArguments(JSContext *cx, JSErrorCallback callback,
1655 void *userRef, const uintN errorNumber,
1656 char **message, JSErrorReport *reportp,
1657 JSBool *warningp, JSBool charArgs, va_list ap);
1658 #endif
1659
1660 extern void
1661 js_ReportOutOfMemory(JSContext *cx);
1662
1663 /*
1664 * Report that cx->scriptStackQuota is exhausted.
1665 */
1666 extern void
1667 js_ReportOutOfScriptQuota(JSContext *cx);
1668
1669 extern void
1670 js_ReportOverRecursed(JSContext *cx);
1671
1672 extern void
1673 js_ReportAllocationOverflow(JSContext *cx);
1674
1675 #define JS_CHECK_RECURSION(cx, onerror) \
1676 JS_BEGIN_MACRO \
1677 int stackDummy_; \
1678 \
1679 if (!JS_CHECK_STACK_SIZE(cx, stackDummy_)) { \
1680 js_ReportOverRecursed(cx); \
1681 onerror; \
1682 } \
1683 JS_END_MACRO
1684
1685 /*
1686 * Report an exception using a previously composed JSErrorReport.
1687 * XXXbe remove from "friend" API
1688 */
1689 extern JS_FRIEND_API(void)
1690 js_ReportErrorAgain(JSContext *cx, const char *message, JSErrorReport *report);
1691
1692 extern void
1693 js_ReportIsNotDefined(JSContext *cx, const char *name);
1694
1695 /*
1696 * Report an attempt to access the property of a null or undefined value (v).
1697 */
1698 extern JSBool
1699 js_ReportIsNullOrUndefined(JSContext *cx, intN spindex, jsval v,
1700 JSString *fallback);
1701
1702 extern void
1703 js_ReportMissingArg(JSContext *cx, jsval *vp, uintN arg);
1704
1705 /*
1706 * Report error using js_DecompileValueGenerator(cx, spindex, v, fallback) as
1707 * the first argument for the error message. If the error message has less
1708 * then 3 arguments, use null for arg1 or arg2.
1709 */
1710 extern JSBool
1711 js_ReportValueErrorFlags(JSContext *cx, uintN flags, const uintN errorNumber,
1712 intN spindex, jsval v, JSString *fallback,
1713 const char *arg1, const char *arg2);
1714
1715 #define js_ReportValueError(cx,errorNumber,spindex,v,fallback) \
1716 ((void)js_ReportValueErrorFlags(cx, JSREPORT_ERROR, errorNumber, \
1717 spindex, v, fallback, NULL, NULL))
1718
1719 #define js_ReportValueError2(cx,errorNumber,spindex,v,fallback,arg1) \
1720 ((void)js_ReportValueErrorFlags(cx, JSREPORT_ERROR, errorNumber, \
1721 spindex, v, fallback, arg1, NULL))
1722
1723 #define js_ReportValueError3(cx,errorNumber,spindex,v,fallback,arg1,arg2) \
1724 ((void)js_ReportValueErrorFlags(cx, JSREPORT_ERROR, errorNumber, \
1725 spindex, v, fallback, arg1, arg2))
1726
1727 extern JSErrorFormatString js_ErrorFormatString[JSErr_Limit];
1728
1729 /*
1730 * See JS_SetThreadStackLimit in jsapi.c, where we check that the stack grows
1731 * in the expected direction. On Unix-y systems, JS_STACK_GROWTH_DIRECTION is
1732 * computed on the build host by jscpucfg.c and written into jsautocfg.h. The
1733 * macro is hardcoded in jscpucfg.h on Windows and Mac systems (for historical
1734 * reasons pre-dating autoconf usage).
1735 */
1736 #if JS_STACK_GROWTH_DIRECTION > 0
1737 # define JS_CHECK_STACK_SIZE(cx, lval) ((jsuword)&(lval) < (cx)->stackLimit)
1738 #else
1739 # define JS_CHECK_STACK_SIZE(cx, lval) ((jsuword)&(lval) > (cx)->stackLimit)
1740 #endif
1741
1742 /*
1743 * If the operation callback flag was set, call the operation callback.
1744 * This macro can run the full GC. Return true if it is OK to continue and
1745 * false otherwise.
1746 */
1747 #define JS_CHECK_OPERATION_LIMIT(cx) \
1748 (!(cx)->operationCallbackFlag || js_InvokeOperationCallback(cx))
1749
1750 /*
1751 * Invoke the operation callback and return false if the current execution
1752 * is to be terminated.
1753 */
1754 extern JSBool
1755 js_InvokeOperationCallback(JSContext *cx);
1756
1757 #ifndef JS_THREADSAFE
1758 # define js_TriggerAllOperationCallbacks(rt, gcLocked) \
1759 js_TriggerAllOperationCallbacks (rt)
1760 #endif
1761
1762 void
1763 js_TriggerAllOperationCallbacks(JSRuntime *rt, JSBool gcLocked);
1764
1765 extern JSStackFrame *
1766 js_GetScriptedCaller(JSContext *cx, JSStackFrame *fp);
1767
1768 extern jsbytecode*
1769 js_GetCurrentBytecodePC(JSContext* cx);
1770
1771 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
1772 extern bool
1773 #else
1774 extern JSBool
1775 #endif
1776 js_CurrentPCIsInImacro(JSContext *cx);
1777
1778 #ifdef JS_TRACER
1779 /*
1780 * Reconstruct the JS stack and clear cx->tracecx. We must be currently in a
1781 * _FAIL builtin from trace on cx or another context on the same thread. The
1782 * machine code for the trace remains on the C stack when js_DeepBail returns.
1783 *
1784 * Implemented in jstracer.cpp.
1785 */
1786 JS_FORCES_STACK JS_FRIEND_API(void)
1787 js_DeepBail(JSContext *cx);
1788 #endif
1789
1790 static JS_FORCES_STACK JS_INLINE void
1791 js_LeaveTrace(JSContext *cx)
1792 {
1793 #ifdef JS_TRACER
1794 if (JS_ON_TRACE(cx))
1795 js_DeepBail(cx);
1796 #endif
1797 }
1798
1799 static JS_INLINE void
1800 js_LeaveTraceIfGlobalObject(JSContext *cx, JSObject *obj)
1801 {
1802 if (!obj->fslots[JSSLOT_PARENT])
1803 js_LeaveTrace(cx);
1804 }
1805
1806 static JS_INLINE JSBool
1807 js_CanLeaveTrace(JSContext *cx)
1808 {
1809 JS_ASSERT(JS_ON_TRACE(cx));
1810 #ifdef JS_TRACER
1811 return cx->bailExit != NULL;
1812 #else
1813 return JS_FALSE;
1814 #endif
1815 }
1816
1817 /*
1818 * Get the current cx->fp, first lazily instantiating stack frames if needed.
1819 * (Do not access cx->fp directly except in JS_REQUIRES_STACK code.)
1820 *
1821 * Defined in jstracer.cpp if JS_TRACER is defined.
1822 */
1823 static JS_FORCES_STACK JS_INLINE JSStackFrame *
1824 js_GetTopStackFrame(JSContext *cx)
1825 {
1826 js_LeaveTrace(cx);
1827 return cx->fp;
1828 }
1829
1830 static JS_INLINE JSBool
1831 js_IsPropertyCacheDisabled(JSContext *cx)
1832 {
1833 return cx->runtime->shapeGen >= SHAPE_OVERFLOW_BIT;
1834 }
1835
1836 static JS_INLINE uint32
1837 js_RegenerateShapeForGC(JSContext *cx)
1838 {
1839 JS_ASSERT(cx->runtime->gcRunning);
1840 JS_ASSERT(cx->runtime->gcRegenShapes);
1841
1842 /*
1843 * Under the GC, compared with js_GenerateShape, we don't need to use
1844 * atomic increments but we still must make sure that after an overflow
1845 * the shape stays such.
1846 */
1847 uint32 shape = cx->runtime->shapeGen;
1848 shape = (shape + 1) | (shape & SHAPE_OVERFLOW_BIT);
1849 cx->runtime->shapeGen = shape;
1850 return shape;
1851 }
1852
1853 #endif /* jscntxt_h___ */

  ViewVC Help
Powered by ViewVC 1.1.24