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

Contents of /trunk/js/jscntxt.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 535 - (show annotations)
Fri Apr 2 16:18:55 2010 UTC (9 years, 8 months ago) by siliconforks
File MIME type: text/plain
File size: 61291 byte(s)
Update SpiderMonkey from Firefox 3.6.2.

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 /* Random number generator state, used by jsmath.cpp. */
288 int64 rngSeed;
289
290 #ifdef JS_TRACER
291 /* Trace-tree JIT recorder/interpreter state. */
292 JSTraceMonitor traceMonitor;
293 #endif
294
295 /* Lock-free hashed lists of scripts created by eval to garbage-collect. */
296 JSScript *scriptsToGC[JS_EVAL_CACHE_SIZE];
297
298 #ifdef JS_EVAL_CACHE_METERING
299 JSEvalCacheMeter evalCacheMeter;
300 #endif
301
302 /*
303 * Thread-local version of JSRuntime.gcMallocBytes to avoid taking
304 * locks on each JS_malloc.
305 */
306 size_t gcMallocBytes;
307
308 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
309
310 /*
311 * Cache of reusable JSNativeEnumerators mapped by shape identifiers (as
312 * stored in scope->shape). This cache is nulled by the GC and protected
313 * by gcLock.
314 */
315 #define NATIVE_ENUM_CACHE_LOG2 8
316 #define NATIVE_ENUM_CACHE_MASK JS_BITMASK(NATIVE_ENUM_CACHE_LOG2)
317 #define NATIVE_ENUM_CACHE_SIZE JS_BIT(NATIVE_ENUM_CACHE_LOG2)
318
319 #define NATIVE_ENUM_CACHE_HASH(shape) \
320 ((((shape) >> NATIVE_ENUM_CACHE_LOG2) ^ (shape)) & NATIVE_ENUM_CACHE_MASK)
321
322 jsuword nativeEnumCache[NATIVE_ENUM_CACHE_SIZE];
323
324 #ifdef JS_THREADSAFE
325 /*
326 * Deallocator task for this thread.
327 */
328 JSFreePointerListTask *deallocatorTask;
329 #endif
330
331 void mark(JSTracer *trc) {
332 #ifdef JS_TRACER
333 traceMonitor.mark(trc);
334 #endif
335 }
336
337 #endif /* __cplusplus */
338 };
339
340 #ifdef JS_THREADSAFE
341
342 /*
343 * Structure uniquely representing a thread. It holds thread-private data
344 * that can be accessed without a global lock.
345 */
346 struct JSThread {
347 /* Linked list of all contexts in use on this thread. */
348 JSCList contextList;
349
350 /* Opaque thread-id, from NSPR's PR_GetCurrentThread(). */
351 jsword id;
352
353 /* Indicates that the thread is waiting in ClaimTitle from jslock.cpp. */
354 JSTitle *titleToShare;
355
356 JSGCThing *gcFreeLists[GC_NUM_FREELISTS];
357
358 /* Factored out of JSThread for !JS_THREADSAFE embedding in JSRuntime. */
359 JSThreadData data;
360 };
361
362 #define JS_THREAD_DATA(cx) (&(cx)->thread->data)
363
364 struct JSThreadsHashEntry {
365 JSDHashEntryHdr base;
366 JSThread *thread;
367 };
368
369 extern JSThread *
370 js_CurrentThread(JSRuntime *rt);
371
372 /*
373 * The function takes the GC lock and does not release in successful return.
374 * On error (out of memory) the function releases the lock but delegates
375 * the error reporting to the caller.
376 */
377 extern JSBool
378 js_InitContextThread(JSContext *cx);
379
380 /*
381 * On entrance the GC lock must be held and it will be held on exit.
382 */
383 extern void
384 js_ClearContextThread(JSContext *cx);
385
386 #endif /* JS_THREADSAFE */
387
388 typedef enum JSDestroyContextMode {
389 JSDCM_NO_GC,
390 JSDCM_MAYBE_GC,
391 JSDCM_FORCE_GC,
392 JSDCM_NEW_FAILED
393 } JSDestroyContextMode;
394
395 typedef enum JSRuntimeState {
396 JSRTS_DOWN,
397 JSRTS_LAUNCHING,
398 JSRTS_UP,
399 JSRTS_LANDING
400 } JSRuntimeState;
401
402 typedef enum JSBuiltinFunctionId {
403 JSBUILTIN_ObjectToIterator,
404 JSBUILTIN_CallIteratorNext,
405 JSBUILTIN_LIMIT
406 } JSBuiltinFunctionId;
407
408 typedef struct JSPropertyTreeEntry {
409 JSDHashEntryHdr hdr;
410 JSScopeProperty *child;
411 } JSPropertyTreeEntry;
412
413 typedef struct JSSetSlotRequest JSSetSlotRequest;
414
415 struct JSSetSlotRequest {
416 JSObject *obj; /* object containing slot to set */
417 JSObject *pobj; /* new proto or parent reference */
418 uint16 slot; /* which to set, proto or parent */
419 JSPackedBool cycle; /* true if a cycle was detected */
420 JSSetSlotRequest *next; /* next request in GC worklist */
421 };
422
423 struct JSRuntime {
424 /* Runtime state, synchronized by the stateChange/gcLock condvar/lock. */
425 JSRuntimeState state;
426
427 /* Context create/destroy callback. */
428 JSContextCallback cxCallback;
429
430 /*
431 * Shape regenerated whenever a prototype implicated by an "add property"
432 * property cache fill and induced trace guard has a readonly property or a
433 * setter defined on it. This number proxies for the shapes of all objects
434 * along the prototype chain of all objects in the runtime on which such an
435 * add-property result has been cached/traced.
436 *
437 * See bug 492355 for more details.
438 *
439 * This comes early in JSRuntime to minimize the immediate format used by
440 * trace-JITted code that reads it.
441 */
442 uint32 protoHazardShape;
443
444 /* Garbage collector state, used by jsgc.c. */
445 JSGCChunkInfo *gcChunkList;
446 JSGCArenaList gcArenaList[GC_NUM_FREELISTS];
447 JSGCDoubleArenaList gcDoubleArenaList;
448 JSDHashTable gcRootsHash;
449 JSDHashTable *gcLocksHash;
450 jsrefcount gcKeepAtoms;
451 size_t gcBytes;
452 size_t gcLastBytes;
453 size_t gcMaxBytes;
454 size_t gcMaxMallocBytes;
455 uint32 gcEmptyArenaPoolLifespan;
456 uint32 gcLevel;
457 uint32 gcNumber;
458 JSTracer *gcMarkingTracer;
459 uint32 gcTriggerFactor;
460 size_t gcTriggerBytes;
461 volatile JSBool gcIsNeeded;
462 volatile JSBool gcFlushCodeCaches;
463
464 /*
465 * NB: do not pack another flag here by claiming gcPadding unless the new
466 * flag is written only by the GC thread. Atomic updates to packed bytes
467 * are not guaranteed, so stores issued by one thread may be lost due to
468 * unsynchronized read-modify-write cycles on other threads.
469 */
470 JSPackedBool gcPoke;
471 JSPackedBool gcRunning;
472 JSPackedBool gcRegenShapes;
473
474 /*
475 * During gc, if rt->gcRegenShapes &&
476 * (scope->flags & JSScope::SHAPE_REGEN) == rt->gcRegenShapesScopeFlag,
477 * then the scope's shape has already been regenerated during this GC.
478 * To avoid having to sweep JSScopes, the bit's meaning toggles with each
479 * shape-regenerating GC.
480 *
481 * FIXME Once scopes are GC'd (bug 505004), this will be obsolete.
482 */
483 uint8 gcRegenShapesScopeFlag;
484
485 #ifdef JS_GC_ZEAL
486 jsrefcount gcZeal;
487 #endif
488
489 JSGCCallback gcCallback;
490 size_t gcMallocBytes;
491 JSGCArenaInfo *gcUntracedArenaStackTop;
492 #ifdef DEBUG
493 size_t gcTraceLaterCount;
494 #endif
495
496 /*
497 * Table for tracking iterators to ensure that we close iterator's state
498 * before finalizing the iterable object.
499 */
500 JSPtrTable gcIteratorTable;
501
502 /*
503 * The trace operation and its data argument to trace embedding-specific
504 * GC roots.
505 */
506 JSTraceDataOp gcExtraRootsTraceOp;
507 void *gcExtraRootsData;
508
509 /*
510 * Used to serialize cycle checks when setting __proto__ or __parent__ by
511 * requesting the GC handle the required cycle detection. If the GC hasn't
512 * been poked, it won't scan for garbage. This member is protected by
513 * rt->gcLock.
514 */
515 JSSetSlotRequest *setSlotRequests;
516
517 /* Well-known numbers held for use by this runtime's contexts. */
518 jsdouble *jsNaN;
519 jsdouble *jsNegativeInfinity;
520 jsdouble *jsPositiveInfinity;
521
522 #ifdef JS_THREADSAFE
523 JSLock *deflatedStringCacheLock;
524 #endif
525 JSHashTable *deflatedStringCache;
526 #ifdef DEBUG
527 uint32 deflatedStringCacheBytes;
528 #endif
529
530 JSString *emptyString;
531
532 /*
533 * Builtin functions, lazily created and held for use by the trace recorder.
534 *
535 * This field would be #ifdef JS_TRACER, but XPConnect is compiled without
536 * -DJS_TRACER and includes this header.
537 */
538 JSObject *builtinFunctions[JSBUILTIN_LIMIT];
539
540 /* List of active contexts sharing this runtime; protected by gcLock. */
541 JSCList contextList;
542
543 /* Per runtime debug hooks -- see jsprvtd.h and jsdbgapi.h. */
544 JSDebugHooks globalDebugHooks;
545
546 #ifdef JS_TRACER
547 /* True if any debug hooks not supported by the JIT are enabled. */
548 bool debuggerInhibitsJIT() const {
549 return (globalDebugHooks.interruptHandler ||
550 globalDebugHooks.callHook ||
551 globalDebugHooks.objectHook);
552 }
553 #endif
554
555 /* More debugging state, see jsdbgapi.c. */
556 JSCList trapList;
557 JSCList watchPointList;
558
559 /* Client opaque pointers */
560 void *data;
561
562 #ifdef JS_THREADSAFE
563 /* These combine to interlock the GC and new requests. */
564 PRLock *gcLock;
565 PRCondVar *gcDone;
566 PRCondVar *requestDone;
567 uint32 requestCount;
568 JSThread *gcThread;
569
570 /* Lock and owning thread pointer for JS_LOCK_RUNTIME. */
571 PRLock *rtLock;
572 #ifdef DEBUG
573 jsword rtLockOwner;
574 #endif
575
576 /* Used to synchronize down/up state change; protected by gcLock. */
577 PRCondVar *stateChange;
578
579 /*
580 * State for sharing single-threaded titles, once a second thread tries to
581 * lock a title. The titleSharingDone condvar is protected by rt->gcLock
582 * to minimize number of locks taken in JS_EndRequest.
583 *
584 * The titleSharingTodo linked list is likewise "global" per runtime, not
585 * one-list-per-context, to conserve space over all contexts, optimizing
586 * for the likely case that titles become shared rarely, and among a very
587 * small set of threads (contexts).
588 */
589 PRCondVar *titleSharingDone;
590 JSTitle *titleSharingTodo;
591
592 /*
593 * Magic terminator for the rt->titleSharingTodo linked list, threaded through
594 * title->u.link. This hack allows us to test whether a title is on the list
595 * by asking whether title->u.link is non-null. We use a large, likely bogus
596 * pointer here to distinguish this value from any valid u.count (small int)
597 * value.
598 */
599 #define NO_TITLE_SHARING_TODO ((JSTitle *) 0xfeedbeef)
600
601 /*
602 * Lock serializing trapList and watchPointList accesses, and count of all
603 * mutations to trapList and watchPointList made by debugger threads. To
604 * keep the code simple, we define debuggerMutations for the thread-unsafe
605 * case too.
606 */
607 PRLock *debuggerLock;
608
609 JSDHashTable threads;
610 #endif /* JS_THREADSAFE */
611 uint32 debuggerMutations;
612
613 /*
614 * Security callbacks set on the runtime are used by each context unless
615 * an override is set on the context.
616 */
617 JSSecurityCallbacks *securityCallbacks;
618
619 /*
620 * Shared scope property tree, and arena-pool for allocating its nodes.
621 * The propertyRemovals counter is incremented for every JSScope::clear,
622 * and for each JSScope::remove method call that frees a slot in an object.
623 * See js_NativeGet and js_NativeSet in jsobj.c.
624 */
625 JSDHashTable propertyTreeHash;
626 JSScopeProperty *propertyFreeList;
627 JSArenaPool propertyArenaPool;
628 int32 propertyRemovals;
629
630 /* Script filename table. */
631 struct JSHashTable *scriptFilenameTable;
632 JSCList scriptFilenamePrefixes;
633 #ifdef JS_THREADSAFE
634 PRLock *scriptFilenameTableLock;
635 #endif
636
637 /* Number localization, used by jsnum.c */
638 const char *thousandsSeparator;
639 const char *decimalSeparator;
640 const char *numGrouping;
641
642 /*
643 * Weak references to lazily-created, well-known XML singletons.
644 *
645 * NB: Singleton objects must be carefully disconnected from the rest of
646 * the object graph usually associated with a JSContext's global object,
647 * including the set of standard class objects. See jsxml.c for details.
648 */
649 JSObject *anynameObject;
650 JSObject *functionNamespaceObject;
651
652 #ifndef JS_THREADSAFE
653 JSThreadData threadData;
654
655 #define JS_THREAD_DATA(cx) (&(cx)->runtime->threadData)
656 #endif
657
658 /*
659 * Object shape (property cache structural type) identifier generator.
660 *
661 * Type 0 stands for the empty scope, and must not be regenerated due to
662 * uint32 wrap-around. Since js_GenerateShape (in jsinterp.cpp) uses
663 * atomic pre-increment, the initial value for the first typed non-empty
664 * scope will be 1.
665 *
666 * If this counter overflows into SHAPE_OVERFLOW_BIT (in jsinterp.h), the
667 * cache is disabled, to avoid aliasing two different types. It stays
668 * disabled until a triggered GC at some later moment compresses live
669 * types, minimizing rt->shapeGen in the process.
670 */
671 volatile uint32 shapeGen;
672
673 /* Literal table maintained by jsatom.c functions. */
674 JSAtomState atomState;
675
676 /*
677 * Various metering fields are defined at the end of JSRuntime. In this
678 * way there is no need to recompile all the code that refers to other
679 * fields of JSRuntime after enabling the corresponding metering macro.
680 */
681 #ifdef JS_DUMP_ENUM_CACHE_STATS
682 int32 nativeEnumProbes;
683 int32 nativeEnumMisses;
684 # define ENUM_CACHE_METER(name) JS_ATOMIC_INCREMENT(&cx->runtime->name)
685 #else
686 # define ENUM_CACHE_METER(name) ((void) 0)
687 #endif
688
689 #ifdef JS_DUMP_LOOP_STATS
690 /* Loop statistics, to trigger trace recording and compiling. */
691 JSBasicStats loopStats;
692 #endif
693
694 #if defined DEBUG || defined JS_DUMP_PROPTREE_STATS
695 /* Function invocation metering. */
696 jsrefcount inlineCalls;
697 jsrefcount nativeCalls;
698 jsrefcount nonInlineCalls;
699 jsrefcount constructs;
700
701 /* Title lock and scope property metering. */
702 jsrefcount claimAttempts;
703 jsrefcount claimedTitles;
704 jsrefcount deadContexts;
705 jsrefcount deadlocksAvoided;
706 jsrefcount liveScopes;
707 jsrefcount sharedTitles;
708 jsrefcount totalScopes;
709 jsrefcount liveScopeProps;
710 jsrefcount liveScopePropsPreSweep;
711 jsrefcount totalScopeProps;
712 jsrefcount livePropTreeNodes;
713 jsrefcount duplicatePropTreeNodes;
714 jsrefcount totalPropTreeNodes;
715 jsrefcount propTreeKidsChunks;
716 jsrefcount middleDeleteFixups;
717
718 /* String instrumentation. */
719 jsrefcount liveStrings;
720 jsrefcount totalStrings;
721 jsrefcount liveDependentStrings;
722 jsrefcount totalDependentStrings;
723 jsrefcount badUndependStrings;
724 double lengthSum;
725 double lengthSquaredSum;
726 double strdepLengthSum;
727 double strdepLengthSquaredSum;
728 #endif /* DEBUG || JS_DUMP_PROPTREE_STATS */
729
730 #ifdef JS_SCOPE_DEPTH_METER
731 /*
732 * Stats on runtime prototype chain lookups and scope chain depths, i.e.,
733 * counts of objects traversed on a chain until the wanted id is found.
734 */
735 JSBasicStats protoLookupDepthStats;
736 JSBasicStats scopeSearchDepthStats;
737
738 /*
739 * Stats on compile-time host environment and lexical scope chain lengths
740 * (maximum depths).
741 */
742 JSBasicStats hostenvScopeDepthStats;
743 JSBasicStats lexicalScopeDepthStats;
744 #endif
745
746 #ifdef JS_GCMETER
747 JSGCStats gcStats;
748 #endif
749
750 #ifdef JS_FUNCTION_METERING
751 JSFunctionMeter functionMeter;
752 char lastScriptFilename[1024];
753 #endif
754
755 #ifdef __cplusplus /* Allow inclusion from LiveConnect C files. */
756
757 void setGCTriggerFactor(uint32 factor);
758 void setGCLastBytes(size_t lastBytes);
759
760 inline void* malloc(size_t bytes) {
761 return ::js_malloc(bytes);
762 }
763
764 inline void* calloc(size_t bytes) {
765 return ::js_calloc(bytes);
766 }
767
768 inline void* realloc(void* p, size_t bytes) {
769 return ::js_realloc(p, bytes);
770 }
771
772 inline void free(void* p) {
773 ::js_free(p);
774 }
775
776 #ifdef JS_THREADSAFE
777 JSBackgroundThread *deallocatorThread;
778 #endif
779
780 #endif /* __cplusplus */
781 };
782
783 /* Common macros to access thread-local caches in JSThread or JSRuntime. */
784 #define JS_GSN_CACHE(cx) (JS_THREAD_DATA(cx)->gsnCache)
785 #define JS_PROPERTY_CACHE(cx) (JS_THREAD_DATA(cx)->propertyCache)
786 #define JS_TRACE_MONITOR(cx) (JS_THREAD_DATA(cx)->traceMonitor)
787 #define JS_SCRIPTS_TO_GC(cx) (JS_THREAD_DATA(cx)->scriptsToGC)
788
789 #ifdef JS_EVAL_CACHE_METERING
790 # define EVAL_CACHE_METER(x) (JS_THREAD_DATA(cx)->evalCacheMeter.x++)
791 #else
792 # define EVAL_CACHE_METER(x) ((void) 0)
793 #endif
794
795 #ifdef DEBUG
796 # define JS_RUNTIME_METER(rt, which) JS_ATOMIC_INCREMENT(&(rt)->which)
797 # define JS_RUNTIME_UNMETER(rt, which) JS_ATOMIC_DECREMENT(&(rt)->which)
798 #else
799 # define JS_RUNTIME_METER(rt, which) /* nothing */
800 # define JS_RUNTIME_UNMETER(rt, which) /* nothing */
801 #endif
802
803 #define JS_KEEP_ATOMS(rt) JS_ATOMIC_INCREMENT(&(rt)->gcKeepAtoms);
804 #define JS_UNKEEP_ATOMS(rt) JS_ATOMIC_DECREMENT(&(rt)->gcKeepAtoms);
805
806 #ifdef JS_ARGUMENT_FORMATTER_DEFINED
807 /*
808 * Linked list mapping format strings for JS_{Convert,Push}Arguments{,VA} to
809 * formatter functions. Elements are sorted in non-increasing format string
810 * length order.
811 */
812 struct JSArgumentFormatMap {
813 const char *format;
814 size_t length;
815 JSArgumentFormatter formatter;
816 JSArgumentFormatMap *next;
817 };
818 #endif
819
820 struct JSStackHeader {
821 uintN nslots;
822 JSStackHeader *down;
823 };
824
825 #define JS_STACK_SEGMENT(sh) ((jsval *)(sh) + 2)
826
827 /*
828 * Key and entry types for the JSContext.resolvingTable hash table, typedef'd
829 * here because all consumers need to see these declarations (and not just the
830 * typedef names, as would be the case for an opaque pointer-to-typedef'd-type
831 * declaration), along with cx->resolvingTable.
832 */
833 typedef struct JSResolvingKey {
834 JSObject *obj;
835 jsid id;
836 } JSResolvingKey;
837
838 typedef struct JSResolvingEntry {
839 JSDHashEntryHdr hdr;
840 JSResolvingKey key;
841 uint32 flags;
842 } JSResolvingEntry;
843
844 #define JSRESFLAG_LOOKUP 0x1 /* resolving id from lookup */
845 #define JSRESFLAG_WATCH 0x2 /* resolving id from watch */
846
847 typedef struct JSLocalRootChunk JSLocalRootChunk;
848
849 #define JSLRS_CHUNK_SHIFT 8
850 #define JSLRS_CHUNK_SIZE JS_BIT(JSLRS_CHUNK_SHIFT)
851 #define JSLRS_CHUNK_MASK JS_BITMASK(JSLRS_CHUNK_SHIFT)
852
853 struct JSLocalRootChunk {
854 jsval roots[JSLRS_CHUNK_SIZE];
855 JSLocalRootChunk *down;
856 };
857
858 typedef struct JSLocalRootStack {
859 uint32 scopeMark;
860 uint32 rootCount;
861 JSLocalRootChunk *topChunk;
862 JSLocalRootChunk firstChunk;
863 } JSLocalRootStack;
864
865 #define JSLRS_NULL_MARK ((uint32) -1)
866
867 /*
868 * Macros to push/pop JSTempValueRooter instances to context-linked stack of
869 * temporary GC roots. If you need to protect a result value that flows out of
870 * a C function across several layers of other functions, use the
871 * js_LeaveLocalRootScopeWithResult internal API (see further below) instead.
872 *
873 * The macros also provide a simple way to get a single rooted pointer via
874 * JS_PUSH_TEMP_ROOT_<KIND>(cx, NULL, &tvr). Then &tvr.u.<kind> gives the
875 * necessary pointer.
876 *
877 * JSTempValueRooter.count defines the type of the rooted value referenced by
878 * JSTempValueRooter.u union of type JSTempValueUnion. When count is positive
879 * or zero, u.array points to a vector of jsvals. Otherwise it must be one of
880 * the following constants:
881 */
882 #define JSTVU_SINGLE (-1) /* u.value or u.<gcthing> is single jsval
883 or non-JSString GC-thing pointer */
884 #define JSTVU_TRACE (-2) /* u.trace is a hook to trace a custom
885 * structure */
886 #define JSTVU_SPROP (-3) /* u.sprop roots property tree node */
887 #define JSTVU_WEAK_ROOTS (-4) /* u.weakRoots points to saved weak roots */
888 #define JSTVU_COMPILER (-5) /* u.compiler roots JSCompiler* */
889 #define JSTVU_SCRIPT (-6) /* u.script roots JSScript* */
890 #define JSTVU_ENUMERATOR (-7) /* a pointer to JSTempValueRooter points
891 to an instance of JSAutoEnumStateRooter
892 with u.object storing the enumeration
893 object */
894
895 /*
896 * Here single JSTVU_SINGLE covers both jsval and pointers to almost (see note
897 * below) any GC-thing via reinterpreting the thing as JSVAL_OBJECT. This works
898 * because the GC-thing is aligned on a 0 mod 8 boundary, and object has the 0
899 * jsval tag. So any GC-heap-allocated thing pointer may be tagged as if it
900 * were an object and untagged, if it's then used only as an opaque pointer
901 * until discriminated by other means than tag bits. This is how, for example,
902 * js_GetGCThingTraceKind uses its |thing| parameter -- it consults GC-thing
903 * flags stored separately from the thing to decide the kind of thing.
904 *
905 * Note well that JSStrings may be statically allocated (see the intStringTable
906 * and unitStringTable static arrays), so this hack does not work for arbitrary
907 * GC-thing pointers.
908 */
909 #define JS_PUSH_TEMP_ROOT_COMMON(cx,x,tvr,cnt,kind) \
910 JS_BEGIN_MACRO \
911 JS_ASSERT((cx)->tempValueRooters != (tvr)); \
912 (tvr)->count = (cnt); \
913 (tvr)->u.kind = (x); \
914 (tvr)->down = (cx)->tempValueRooters; \
915 (cx)->tempValueRooters = (tvr); \
916 JS_END_MACRO
917
918 #define JS_POP_TEMP_ROOT(cx,tvr) \
919 JS_BEGIN_MACRO \
920 JS_ASSERT((cx)->tempValueRooters == (tvr)); \
921 (cx)->tempValueRooters = (tvr)->down; \
922 JS_END_MACRO
923
924 #define JS_PUSH_TEMP_ROOT(cx,cnt,arr,tvr) \
925 JS_BEGIN_MACRO \
926 JS_ASSERT((int)(cnt) >= 0); \
927 JS_PUSH_TEMP_ROOT_COMMON(cx, arr, tvr, (ptrdiff_t) (cnt), array); \
928 JS_END_MACRO
929
930 #define JS_PUSH_SINGLE_TEMP_ROOT(cx,val,tvr) \
931 JS_PUSH_TEMP_ROOT_COMMON(cx, val, tvr, JSTVU_SINGLE, value)
932
933 #define JS_PUSH_TEMP_ROOT_OBJECT(cx,obj,tvr) \
934 JS_PUSH_TEMP_ROOT_COMMON(cx, obj, tvr, JSTVU_SINGLE, object)
935
936 #define JS_PUSH_TEMP_ROOT_STRING(cx,str,tvr) \
937 JS_PUSH_SINGLE_TEMP_ROOT(cx, str ? STRING_TO_JSVAL(str) : JSVAL_NULL, tvr)
938
939 #define JS_PUSH_TEMP_ROOT_XML(cx,xml_,tvr) \
940 JS_PUSH_TEMP_ROOT_COMMON(cx, xml_, tvr, JSTVU_SINGLE, xml)
941
942 #define JS_PUSH_TEMP_ROOT_TRACE(cx,trace_,tvr) \
943 JS_PUSH_TEMP_ROOT_COMMON(cx, trace_, tvr, JSTVU_TRACE, trace)
944
945 #define JS_PUSH_TEMP_ROOT_SPROP(cx,sprop_,tvr) \
946 JS_PUSH_TEMP_ROOT_COMMON(cx, sprop_, tvr, JSTVU_SPROP, sprop)
947
948 #define JS_PUSH_TEMP_ROOT_WEAK_COPY(cx,weakRoots_,tvr) \
949 JS_PUSH_TEMP_ROOT_COMMON(cx, weakRoots_, tvr, JSTVU_WEAK_ROOTS, weakRoots)
950
951 #define JS_PUSH_TEMP_ROOT_COMPILER(cx,pc,tvr) \
952 JS_PUSH_TEMP_ROOT_COMMON(cx, pc, tvr, JSTVU_COMPILER, compiler)
953
954 #define JS_PUSH_TEMP_ROOT_SCRIPT(cx,script_,tvr) \
955 JS_PUSH_TEMP_ROOT_COMMON(cx, script_, tvr, JSTVU_SCRIPT, script)
956
957 #define JSRESOLVE_INFER 0xffff /* infer bits from current bytecode */
958
959 extern const JSDebugHooks js_NullDebugHooks; /* defined in jsdbgapi.cpp */
960
961 struct JSContext {
962 /*
963 * If this flag is set, we were asked to call back the operation callback
964 * as soon as possible.
965 */
966 volatile jsint operationCallbackFlag;
967
968 /* JSRuntime contextList linkage. */
969 JSCList link;
970
971 #if JS_HAS_XML_SUPPORT
972 /*
973 * Bit-set formed from binary exponentials of the XML_* tiny-ids defined
974 * for boolean settings in jsxml.c, plus an XSF_CACHE_VALID bit. Together
975 * these act as a cache of the boolean XML.ignore* and XML.prettyPrinting
976 * property values associated with this context's global object.
977 */
978 uint8 xmlSettingFlags;
979 uint8 padding;
980 #else
981 uint16 padding;
982 #endif
983
984 /*
985 * Classic Algol "display" static link optimization.
986 */
987 #define JS_DISPLAY_SIZE 16U
988
989 JSStackFrame *display[JS_DISPLAY_SIZE];
990
991 /* Runtime version control identifier. */
992 uint16 version;
993
994 /* Per-context options. */
995 uint32 options; /* see jsapi.h for JSOPTION_* */
996
997 /* Locale specific callbacks for string conversion. */
998 JSLocaleCallbacks *localeCallbacks;
999
1000 /*
1001 * cx->resolvingTable is non-null and non-empty if we are initializing
1002 * standard classes lazily, or if we are otherwise recursing indirectly
1003 * from js_LookupProperty through a JSClass.resolve hook. It is used to
1004 * limit runaway recursion (see jsapi.c and jsobj.c).
1005 */
1006 JSDHashTable *resolvingTable;
1007
1008 #if JS_HAS_LVALUE_RETURN
1009 /*
1010 * Secondary return value from native method called on the left-hand side
1011 * of an assignment operator. The native should store the object in which
1012 * to set a property in *rval, and return the property's id expressed as a
1013 * jsval by calling JS_SetCallReturnValue2(cx, idval).
1014 */
1015 jsval rval2;
1016 JSPackedBool rval2set;
1017 #endif
1018
1019 /*
1020 * True if generating an error, to prevent runaway recursion.
1021 * NB: generatingError packs with rval2set, #if JS_HAS_LVALUE_RETURN;
1022 * with insideGCMarkCallback and with throwing below.
1023 */
1024 JSPackedBool generatingError;
1025
1026 /* Flag to indicate that we run inside gcCallback(cx, JSGC_MARK_END). */
1027 JSPackedBool insideGCMarkCallback;
1028
1029 /* Exception state -- the exception member is a GC root by definition. */
1030 JSPackedBool throwing; /* is there a pending exception? */
1031 jsval exception; /* most-recently-thrown exception */
1032
1033 /* Limit pointer for checking native stack consumption during recursion. */
1034 jsuword stackLimit;
1035
1036 /* Quota on the size of arenas used to compile and execute scripts. */
1037 size_t scriptStackQuota;
1038
1039 /* Data shared by threads in an address space. */
1040 JSRuntime * const runtime;
1041
1042 explicit JSContext(JSRuntime *rt) : runtime(rt) {}
1043
1044 /* Stack arena pool and frame pointer register. */
1045 JS_REQUIRES_STACK
1046 JSArenaPool stackPool;
1047
1048 JS_REQUIRES_STACK
1049 JSStackFrame *fp;
1050
1051 /* Temporary arena pool used while compiling and decompiling. */
1052 JSArenaPool tempPool;
1053
1054 /* Top-level object and pointer to top stack frame's scope chain. */
1055 JSObject *globalObject;
1056
1057 /* Storage to root recently allocated GC things and script result. */
1058 JSWeakRoots weakRoots;
1059
1060 /* Regular expression class statics (XXX not shared globally). */
1061 JSRegExpStatics regExpStatics;
1062
1063 /* State for object and array toSource conversion. */
1064 JSSharpObjectMap sharpObjectMap;
1065 JSHashTable *busyArrayTable;
1066
1067 /* Argument formatter support for JS_{Convert,Push}Arguments{,VA}. */
1068 JSArgumentFormatMap *argumentFormatMap;
1069
1070 /* Last message string and trace file for debugging. */
1071 char *lastMessage;
1072 #ifdef DEBUG
1073 void *tracefp;
1074 jsbytecode *tracePrevPc;
1075 #endif
1076
1077 /* Per-context optional error reporter. */
1078 JSErrorReporter errorReporter;
1079
1080 /* Branch callback. */
1081 JSOperationCallback operationCallback;
1082
1083 /* Interpreter activation count. */
1084 uintN interpLevel;
1085
1086 /* Client opaque pointers. */
1087 void *data;
1088 void *data2;
1089
1090 /* GC and thread-safe state. */
1091 JSStackFrame *dormantFrameChain; /* dormant stack frame to scan */
1092 #ifdef JS_THREADSAFE
1093 JSThread *thread;
1094 jsrefcount requestDepth;
1095 /* Same as requestDepth but ignoring JS_SuspendRequest/JS_ResumeRequest */
1096 jsrefcount outstandingRequests;
1097 JSTitle *lockedSealedTitle; /* weak ref, for low-cost sealed
1098 title locking */
1099 JSCList threadLinks; /* JSThread contextList linkage */
1100
1101 #define CX_FROM_THREAD_LINKS(tl) \
1102 ((JSContext *)((char *)(tl) - offsetof(JSContext, threadLinks)))
1103 #endif
1104
1105 /* PDL of stack headers describing stack slots not rooted by argv, etc. */
1106 JSStackHeader *stackHeaders;
1107
1108 /* Optional stack of heap-allocated scoped local GC roots. */
1109 JSLocalRootStack *localRootStack;
1110
1111 /* Stack of thread-stack-allocated temporary GC roots. */
1112 JSTempValueRooter *tempValueRooters;
1113
1114 /* List of pre-allocated doubles. */
1115 JSGCDoubleCell *doubleFreeList;
1116
1117 /* Debug hooks associated with the current context. */
1118 const JSDebugHooks *debugHooks;
1119
1120 /* Security callbacks that override any defined on the runtime. */
1121 JSSecurityCallbacks *securityCallbacks;
1122
1123 /* Pinned regexp pool used for regular expressions. */
1124 JSArenaPool regexpPool;
1125
1126 /* Stored here to avoid passing it around as a parameter. */
1127 uintN resolveFlags;
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