blob: 5bb504d2da3cf29371f2a8df35e4704c3e533b93 [file] [log] [blame]
Ben Murdoch8b112d22011-06-08 16:22:53 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Block44f0eee2011-05-26 01:26:41 +01002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#ifndef V8_ISOLATE_H_
29#define V8_ISOLATE_H_
30
31#include "../include/v8-debug.h"
32#include "allocation.h"
33#include "apiutils.h"
34#include "atomicops.h"
35#include "builtins.h"
36#include "contexts.h"
37#include "execution.h"
38#include "frames.h"
39#include "global-handles.h"
40#include "handles.h"
41#include "heap.h"
42#include "regexp-stack.h"
43#include "runtime-profiler.h"
44#include "runtime.h"
45#include "zone.h"
46
47namespace v8 {
48namespace internal {
49
50class AstSentinels;
51class Bootstrapper;
52class CodeGenerator;
53class CodeRange;
54class CompilationCache;
55class ContextSlotCache;
56class ContextSwitcher;
57class Counters;
58class CpuFeatures;
59class CpuProfiler;
60class DeoptimizerData;
61class Deserializer;
62class EmptyStatement;
63class ExternalReferenceTable;
64class Factory;
65class FunctionInfoListener;
66class HandleScopeImplementer;
67class HeapProfiler;
68class InlineRuntimeFunctionsTable;
69class NoAllocationStringAllocator;
70class PcToCodeCache;
71class PreallocatedMemoryThread;
Steve Block44f0eee2011-05-26 01:26:41 +010072class RegExpStack;
73class SaveContext;
Ben Murdoch8b112d22011-06-08 16:22:53 +010074class UnicodeCache;
Steve Block44f0eee2011-05-26 01:26:41 +010075class StringInputBuffer;
76class StringTracker;
77class StubCache;
78class ThreadManager;
79class ThreadState;
80class ThreadVisitor; // Defined in v8threads.h
81class VMState;
82
83// 'void function pointer', used to roundtrip the
84// ExternalReference::ExternalReferenceRedirector since we can not include
85// assembler.h, where it is defined, here.
86typedef void* ExternalReferenceRedirectorPointer();
87
88
89#ifdef ENABLE_DEBUGGER_SUPPORT
90class Debug;
91class Debugger;
92class DebuggerAgent;
93#endif
94
95#if !defined(__arm__) && defined(V8_TARGET_ARCH_ARM) || \
96 !defined(__mips__) && defined(V8_TARGET_ARCH_MIPS)
97class Redirection;
98class Simulator;
99#endif
100
101
102// Static indirection table for handles to constants. If a frame
103// element represents a constant, the data contains an index into
104// this table of handles to the actual constants.
105// Static indirection table for handles to constants. If a Result
106// represents a constant, the data contains an index into this table
107// of handles to the actual constants.
108typedef ZoneList<Handle<Object> > ZoneObjectList;
109
110#define RETURN_IF_SCHEDULED_EXCEPTION(isolate) \
111 if (isolate->has_scheduled_exception()) \
112 return isolate->PromoteScheduledException()
113
114#define RETURN_IF_EMPTY_HANDLE_VALUE(isolate, call, value) \
115 if (call.is_null()) { \
116 ASSERT(isolate->has_pending_exception()); \
117 return value; \
118 }
119
120#define RETURN_IF_EMPTY_HANDLE(isolate, call) \
121 RETURN_IF_EMPTY_HANDLE_VALUE(isolate, call, Failure::Exception())
122
123#define ISOLATE_ADDRESS_LIST(C) \
124 C(handler_address) \
125 C(c_entry_fp_address) \
126 C(context_address) \
127 C(pending_exception_address) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 C(external_caught_exception_address) \
Steve Block44f0eee2011-05-26 01:26:41 +0100129 C(js_entry_sp_address)
Steve Block44f0eee2011-05-26 01:26:41 +0100130
131
Ben Murdoch8b112d22011-06-08 16:22:53 +0100132// Platform-independent, reliable thread identifier.
133class ThreadId {
134 public:
135 // Creates an invalid ThreadId.
136 ThreadId() : id_(kInvalidId) {}
137
138 // Returns ThreadId for current thread.
139 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
140
141 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
142 static ThreadId Invalid() { return ThreadId(kInvalidId); }
143
144 // Compares ThreadIds for equality.
145 INLINE(bool Equals(const ThreadId& other) const) {
146 return id_ == other.id_;
147 }
148
149 // Checks whether this ThreadId refers to any thread.
150 INLINE(bool IsValid() const) {
151 return id_ != kInvalidId;
152 }
153
154 // Converts ThreadId to an integer representation
155 // (required for public API: V8::V8::GetCurrentThreadId).
156 int ToInteger() const { return id_; }
157
158 // Converts ThreadId to an integer representation
159 // (required for public API: V8::V8::TerminateExecution).
160 static ThreadId FromInteger(int id) { return ThreadId(id); }
161
162 private:
163 static const int kInvalidId = -1;
164
165 explicit ThreadId(int id) : id_(id) {}
166
167 static int AllocateThreadId();
168
169 static int GetCurrentThreadId();
170
171 int id_;
172
173 static Atomic32 highest_thread_id_;
174
175 friend class Isolate;
176};
177
178
Steve Block44f0eee2011-05-26 01:26:41 +0100179class ThreadLocalTop BASE_EMBEDDED {
180 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100181 // Does early low-level initialization that does not depend on the
182 // isolate being present.
183 ThreadLocalTop();
184
Steve Block44f0eee2011-05-26 01:26:41 +0100185 // Initialize the thread data.
186 void Initialize();
187
188 // Get the top C++ try catch handler or NULL if none are registered.
189 //
190 // This method is not guarenteed to return an address that can be
191 // used for comparison with addresses into the JS stack. If such an
192 // address is needed, use try_catch_handler_address.
193 v8::TryCatch* TryCatchHandler();
194
195 // Get the address of the top C++ try catch handler or NULL if
196 // none are registered.
197 //
198 // This method always returns an address that can be compared to
199 // pointers into the JavaScript stack. When running on actual
200 // hardware, try_catch_handler_address and TryCatchHandler return
201 // the same pointer. When running on a simulator with a separate JS
202 // stack, try_catch_handler_address returns a JS stack address that
203 // corresponds to the place on the JS stack where the C++ handler
204 // would have been if the stack were not separate.
205 inline Address try_catch_handler_address() {
206 return try_catch_handler_address_;
207 }
208
209 // Set the address of the top C++ try catch handler.
210 inline void set_try_catch_handler_address(Address address) {
211 try_catch_handler_address_ = address;
212 }
213
214 void Free() {
215 ASSERT(!has_pending_message_);
216 ASSERT(!external_caught_exception_);
217 ASSERT(try_catch_handler_address_ == NULL);
218 }
219
Ben Murdoch257744e2011-11-30 15:57:28 +0000220 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100221 // The context where the current execution method is created and for variable
222 // lookups.
223 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100224 ThreadId thread_id_;
Steve Block44f0eee2011-05-26 01:26:41 +0100225 MaybeObject* pending_exception_;
226 bool has_pending_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100227 Object* pending_message_obj_;
228 Script* pending_message_script_;
229 int pending_message_start_pos_;
230 int pending_message_end_pos_;
231 // Use a separate value for scheduled exceptions to preserve the
232 // invariants that hold about pending_exception. We may want to
233 // unify them later.
234 MaybeObject* scheduled_exception_;
235 bool external_caught_exception_;
236 SaveContext* save_context_;
237 v8::TryCatch* catcher_;
238
239 // Stack.
240 Address c_entry_fp_; // the frame pointer of the top c entry frame
241 Address handler_; // try-blocks are chained through the stack
242
243#ifdef USE_SIMULATOR
244#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
245 Simulator* simulator_;
246#endif
247#endif // USE_SIMULATOR
248
Steve Block44f0eee2011-05-26 01:26:41 +0100249 Address js_entry_sp_; // the stack pointer of the bottom js entry frame
250 Address external_callback_; // the external callback we're currently in
Steve Block44f0eee2011-05-26 01:26:41 +0100251 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100252
253 // Generated code scratch locations.
254 int32_t formal_count_;
255
256 // Call back function to report unsafe JS accesses.
257 v8::FailedAccessCheckCallback failed_access_check_callback_;
258
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000259 // Whether out of memory exceptions should be ignored.
260 bool ignore_out_of_memory_;
261
Steve Block44f0eee2011-05-26 01:26:41 +0100262 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100263 void InitializeInternal();
264
Steve Block44f0eee2011-05-26 01:26:41 +0100265 Address try_catch_handler_address_;
266};
267
268#if defined(V8_TARGET_ARCH_ARM) || defined(V8_TARGET_ARCH_MIPS)
269
270#define ISOLATE_PLATFORM_INIT_LIST(V) \
271 /* VirtualFrame::SpilledScope state */ \
272 V(bool, is_virtual_frame_in_spilled_scope, false) \
273 /* CodeGenerator::EmitNamedStore state */ \
274 V(int, inlined_write_barrier_size, -1)
275
276#if !defined(__arm__) && !defined(__mips__)
277class HashMap;
278#endif
279
280#else
281
282#define ISOLATE_PLATFORM_INIT_LIST(V)
283
284#endif
285
286#ifdef ENABLE_DEBUGGER_SUPPORT
287
288#define ISOLATE_DEBUGGER_INIT_LIST(V) \
289 V(v8::Debug::EventCallback, debug_event_callback, NULL) \
290 V(DebuggerAgent*, debugger_agent_instance, NULL)
291#else
292
293#define ISOLATE_DEBUGGER_INIT_LIST(V)
294
295#endif
296
297#ifdef DEBUG
298
299#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
300 V(CommentStatistic, paged_space_comments_statistics, \
301 CommentStatistic::kMaxComments + 1)
302#else
303
304#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
305
306#endif
307
Steve Block44f0eee2011-05-26 01:26:41 +0100308#define ISOLATE_INIT_ARRAY_LIST(V) \
309 /* SerializerDeserializer state. */ \
310 V(Object*, serialize_partial_snapshot_cache, kPartialSnapshotCacheCapacity) \
311 V(int, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
312 V(int, bad_char_shift_table, kUC16AlphabetSize) \
313 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
314 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000315 V(uint32_t, random_seed, 2) \
316 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100317 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
318
319typedef List<HeapObject*, PreallocatedStorage> DebugObjectCache;
320
321#define ISOLATE_INIT_LIST(V) \
322 /* AssertNoZoneAllocation state. */ \
323 V(bool, zone_allow_allocation, true) \
324 /* SerializerDeserializer state. */ \
325 V(int, serialize_partial_snapshot_cache_length, 0) \
326 /* Assembler state. */ \
327 /* A previously allocated buffer of kMinimalBufferSize bytes, or NULL. */ \
328 V(byte*, assembler_spare_buffer, NULL) \
329 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000330 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100331 V(v8::Debug::MessageHandler, message_handler, NULL) \
332 /* To distinguish the function templates, so that we can find them in the */ \
333 /* function cache of the global context. */ \
334 V(int, next_serial_number, 0) \
335 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
336 V(bool, always_allow_natives_syntax, false) \
337 /* Part of the state of liveedit. */ \
338 V(FunctionInfoListener*, active_function_info_listener, NULL) \
339 /* State for Relocatable. */ \
340 V(Relocatable*, relocatable_top, NULL) \
341 /* State for CodeEntry in profile-generator. */ \
342 V(CodeGenerator*, current_code_generator, NULL) \
343 V(bool, jump_target_compiling_deferred_code, false) \
344 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
345 V(Object*, string_stream_current_security_token, NULL) \
346 /* TODO(isolates): Release this on destruction? */ \
347 V(int*, irregexp_interpreter_backtrack_stack_cache, NULL) \
348 /* Serializer state. */ \
349 V(ExternalReferenceTable*, external_reference_table, NULL) \
350 /* AstNode state. */ \
351 V(unsigned, ast_node_id, 0) \
352 V(unsigned, ast_node_count, 0) \
Ben Murdoch8b112d22011-06-08 16:22:53 +0100353 /* SafeStackFrameIterator activations count. */ \
354 V(int, safe_stack_iterator_counter, 0) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000355 V(uint64_t, enabled_cpu_features, 0) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000356 V(CpuProfiler*, cpu_profiler, NULL) \
357 V(HeapProfiler*, heap_profiler, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100358 ISOLATE_PLATFORM_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100359 ISOLATE_DEBUGGER_INIT_LIST(V)
360
361class Isolate {
362 // These forward declarations are required to make the friend declarations in
363 // PerIsolateThreadData work on some older versions of gcc.
364 class ThreadDataTable;
365 class EntryStackItem;
366 public:
367 ~Isolate();
368
Steve Block44f0eee2011-05-26 01:26:41 +0100369 // A thread has a PerIsolateThreadData instance for each isolate that it has
370 // entered. That instance is allocated when the isolate is initially entered
371 // and reused on subsequent entries.
372 class PerIsolateThreadData {
373 public:
374 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
375 : isolate_(isolate),
376 thread_id_(thread_id),
377 stack_limit_(0),
378 thread_state_(NULL),
379#if !defined(__arm__) && defined(V8_TARGET_ARCH_ARM) || \
380 !defined(__mips__) && defined(V8_TARGET_ARCH_MIPS)
381 simulator_(NULL),
382#endif
383 next_(NULL),
384 prev_(NULL) { }
385 Isolate* isolate() const { return isolate_; }
386 ThreadId thread_id() const { return thread_id_; }
387 void set_stack_limit(uintptr_t value) { stack_limit_ = value; }
388 uintptr_t stack_limit() const { return stack_limit_; }
389 ThreadState* thread_state() const { return thread_state_; }
390 void set_thread_state(ThreadState* value) { thread_state_ = value; }
391
392#if !defined(__arm__) && defined(V8_TARGET_ARCH_ARM) || \
393 !defined(__mips__) && defined(V8_TARGET_ARCH_MIPS)
394 Simulator* simulator() const { return simulator_; }
395 void set_simulator(Simulator* simulator) {
396 simulator_ = simulator;
397 }
398#endif
399
400 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100401 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100402 }
403
404 private:
405 Isolate* isolate_;
406 ThreadId thread_id_;
407 uintptr_t stack_limit_;
408 ThreadState* thread_state_;
409
410#if !defined(__arm__) && defined(V8_TARGET_ARCH_ARM) || \
411 !defined(__mips__) && defined(V8_TARGET_ARCH_MIPS)
412 Simulator* simulator_;
413#endif
414
415 PerIsolateThreadData* next_;
416 PerIsolateThreadData* prev_;
417
418 friend class Isolate;
419 friend class ThreadDataTable;
420 friend class EntryStackItem;
421
422 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
423 };
424
425
426 enum AddressId {
427#define C(name) k_##name,
428 ISOLATE_ADDRESS_LIST(C)
Steve Block44f0eee2011-05-26 01:26:41 +0100429#undef C
430 k_isolate_address_count
431 };
432
433 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
434 // not currently set).
435 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
436 return reinterpret_cast<PerIsolateThreadData*>(
437 Thread::GetThreadLocal(per_isolate_thread_data_key_));
438 }
439
440 // Returns the isolate inside which the current thread is running.
441 INLINE(static Isolate* Current()) {
442 Isolate* isolate = reinterpret_cast<Isolate*>(
443 Thread::GetExistingThreadLocal(isolate_key_));
444 ASSERT(isolate != NULL);
445 return isolate;
446 }
447
448 INLINE(static Isolate* UncheckedCurrent()) {
449 return reinterpret_cast<Isolate*>(Thread::GetThreadLocal(isolate_key_));
450 }
451
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000452 // Usually called by Init(), but can be called early e.g. to allow
453 // testing components that require logging but not the whole
454 // isolate.
455 //
456 // Safe to call more than once.
457 void InitializeLoggingAndCounters();
458
Steve Block44f0eee2011-05-26 01:26:41 +0100459 bool Init(Deserializer* des);
460
461 bool IsInitialized() { return state_ == INITIALIZED; }
462
463 // True if at least one thread Enter'ed this isolate.
464 bool IsInUse() { return entry_stack_ != NULL; }
465
466 // Destroys the non-default isolates.
467 // Sets default isolate into "has_been_disposed" state rather then destroying,
468 // for legacy API reasons.
469 void TearDown();
470
471 bool IsDefaultIsolate() const { return this == default_isolate_; }
472
473 // Ensures that process-wide resources and the default isolate have been
474 // allocated. It is only necessary to call this method in rare casses, for
475 // example if you are using V8 from within the body of a static initializer.
476 // Safe to call multiple times.
477 static void EnsureDefaultIsolate();
478
Ben Murdoch257744e2011-11-30 15:57:28 +0000479 // Find the PerThread for this particular (isolate, thread) combination
480 // If one does not yet exist, return null.
481 PerIsolateThreadData* FindPerThreadDataForThisThread();
482
483#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100484 // Get the debugger from the default isolate. Preinitializes the
485 // default isolate if needed.
486 static Debugger* GetDefaultIsolateDebugger();
Ben Murdoch257744e2011-11-30 15:57:28 +0000487#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100488
489 // Get the stack guard from the default isolate. Preinitializes the
490 // default isolate if needed.
491 static StackGuard* GetDefaultIsolateStackGuard();
492
493 // Returns the key used to store the pointer to the current isolate.
494 // Used internally for V8 threads that do not execute JavaScript but still
495 // are part of the domain of an isolate (like the context switcher).
496 static Thread::LocalStorageKey isolate_key() {
497 return isolate_key_;
498 }
499
500 // Returns the key used to store process-wide thread IDs.
501 static Thread::LocalStorageKey thread_id_key() {
502 return thread_id_key_;
503 }
504
Steve Block44f0eee2011-05-26 01:26:41 +0100505 // If a client attempts to create a Locker without specifying an isolate,
506 // we assume that the client is using legacy behavior. Set up the current
507 // thread to be inside the implicit isolate (or fail a check if we have
508 // switched to non-legacy behavior).
509 static void EnterDefaultIsolate();
510
Steve Block44f0eee2011-05-26 01:26:41 +0100511 // Mutex for serializing access to break control structures.
512 Mutex* break_access() { return break_access_; }
513
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000514 // Mutex for serializing access to debugger.
515 Mutex* debugger_access() { return debugger_access_; }
516
Steve Block44f0eee2011-05-26 01:26:41 +0100517 Address get_address_from_id(AddressId id);
518
519 // Access to top context (where the current function object was created).
520 Context* context() { return thread_local_top_.context_; }
521 void set_context(Context* context) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000522 ASSERT(context == NULL || context->IsContext());
Steve Block44f0eee2011-05-26 01:26:41 +0100523 thread_local_top_.context_ = context;
524 }
525 Context** context_address() { return &thread_local_top_.context_; }
526
527 SaveContext* save_context() {return thread_local_top_.save_context_; }
528 void set_save_context(SaveContext* save) {
529 thread_local_top_.save_context_ = save;
530 }
531
532 // Access to current thread id.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100533 ThreadId thread_id() { return thread_local_top_.thread_id_; }
534 void set_thread_id(ThreadId id) { thread_local_top_.thread_id_ = id; }
Steve Block44f0eee2011-05-26 01:26:41 +0100535
536 // Interface to pending exception.
537 MaybeObject* pending_exception() {
538 ASSERT(has_pending_exception());
539 return thread_local_top_.pending_exception_;
540 }
541 bool external_caught_exception() {
542 return thread_local_top_.external_caught_exception_;
543 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100544 void set_external_caught_exception(bool value) {
545 thread_local_top_.external_caught_exception_ = value;
546 }
Steve Block44f0eee2011-05-26 01:26:41 +0100547 void set_pending_exception(MaybeObject* exception) {
548 thread_local_top_.pending_exception_ = exception;
549 }
550 void clear_pending_exception() {
551 thread_local_top_.pending_exception_ = heap_.the_hole_value();
552 }
553 MaybeObject** pending_exception_address() {
554 return &thread_local_top_.pending_exception_;
555 }
556 bool has_pending_exception() {
557 return !thread_local_top_.pending_exception_->IsTheHole();
558 }
559 void clear_pending_message() {
560 thread_local_top_.has_pending_message_ = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100561 thread_local_top_.pending_message_obj_ = heap_.the_hole_value();
562 thread_local_top_.pending_message_script_ = NULL;
563 }
564 v8::TryCatch* try_catch_handler() {
565 return thread_local_top_.TryCatchHandler();
566 }
567 Address try_catch_handler_address() {
568 return thread_local_top_.try_catch_handler_address();
569 }
570 bool* external_caught_exception_address() {
571 return &thread_local_top_.external_caught_exception_;
572 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100573 v8::TryCatch* catcher() {
574 return thread_local_top_.catcher_;
575 }
576 void set_catcher(v8::TryCatch* catcher) {
577 thread_local_top_.catcher_ = catcher;
578 }
Steve Block44f0eee2011-05-26 01:26:41 +0100579
580 MaybeObject** scheduled_exception_address() {
581 return &thread_local_top_.scheduled_exception_;
582 }
583 MaybeObject* scheduled_exception() {
584 ASSERT(has_scheduled_exception());
585 return thread_local_top_.scheduled_exception_;
586 }
587 bool has_scheduled_exception() {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000588 return thread_local_top_.scheduled_exception_ != heap_.the_hole_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100589 }
590 void clear_scheduled_exception() {
591 thread_local_top_.scheduled_exception_ = heap_.the_hole_value();
592 }
593
594 bool IsExternallyCaught();
595
596 bool is_catchable_by_javascript(MaybeObject* exception) {
597 return (exception != Failure::OutOfMemoryException()) &&
598 (exception != heap()->termination_exception());
599 }
600
601 // JS execution stack (see frames.h).
602 static Address c_entry_fp(ThreadLocalTop* thread) {
603 return thread->c_entry_fp_;
604 }
605 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
606
607 inline Address* c_entry_fp_address() {
608 return &thread_local_top_.c_entry_fp_;
609 }
610 inline Address* handler_address() { return &thread_local_top_.handler_; }
611
Steve Block44f0eee2011-05-26 01:26:41 +0100612 // Bottom JS entry (see StackTracer::Trace in log.cc).
613 static Address js_entry_sp(ThreadLocalTop* thread) {
614 return thread->js_entry_sp_;
615 }
616 inline Address* js_entry_sp_address() {
617 return &thread_local_top_.js_entry_sp_;
618 }
Steve Block44f0eee2011-05-26 01:26:41 +0100619
620 // Generated code scratch locations.
621 void* formal_count_address() { return &thread_local_top_.formal_count_; }
622
623 // Returns the global object of the current context. It could be
624 // a builtin object, or a js global object.
625 Handle<GlobalObject> global() {
626 return Handle<GlobalObject>(context()->global());
627 }
628
629 // Returns the global proxy object of the current context.
630 Object* global_proxy() {
631 return context()->global_proxy();
632 }
633
634 Handle<JSBuiltinsObject> js_builtins_object() {
635 return Handle<JSBuiltinsObject>(thread_local_top_.context_->builtins());
636 }
637
638 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
639 void FreeThreadResources() { thread_local_top_.Free(); }
640
641 // This method is called by the api after operations that may throw
642 // exceptions. If an exception was thrown and not handled by an external
643 // handler the exception is scheduled to be rethrown when we return to running
644 // JavaScript code. If an exception is scheduled true is returned.
645 bool OptionalRescheduleException(bool is_bottom_call);
646
Ben Murdoch8b112d22011-06-08 16:22:53 +0100647 class ExceptionScope {
648 public:
649 explicit ExceptionScope(Isolate* isolate) :
650 // Scope currently can only be used for regular exceptions, not
651 // failures like OOM or termination exception.
652 isolate_(isolate),
653 pending_exception_(isolate_->pending_exception()->ToObjectUnchecked()),
654 catcher_(isolate_->catcher())
655 { }
656
657 ~ExceptionScope() {
658 isolate_->set_catcher(catcher_);
659 isolate_->set_pending_exception(*pending_exception_);
660 }
661
662 private:
663 Isolate* isolate_;
664 Handle<Object> pending_exception_;
665 v8::TryCatch* catcher_;
666 };
667
Steve Block44f0eee2011-05-26 01:26:41 +0100668 void SetCaptureStackTraceForUncaughtExceptions(
669 bool capture,
670 int frame_limit,
671 StackTrace::StackTraceOptions options);
672
673 // Tells whether the current context has experienced an out of memory
674 // exception.
675 bool is_out_of_memory();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000676 bool ignore_out_of_memory() {
677 return thread_local_top_.ignore_out_of_memory_;
678 }
679 void set_ignore_out_of_memory(bool value) {
680 thread_local_top_.ignore_out_of_memory_ = value;
681 }
Steve Block44f0eee2011-05-26 01:26:41 +0100682
683 void PrintCurrentStackTrace(FILE* out);
684 void PrintStackTrace(FILE* out, char* thread_data);
685 void PrintStack(StringStream* accumulator);
686 void PrintStack();
687 Handle<String> StackTraceString();
688 Handle<JSArray> CaptureCurrentStackTrace(
689 int frame_limit,
690 StackTrace::StackTraceOptions options);
691
692 // Returns if the top context may access the given global object. If
693 // the result is false, the pending exception is guaranteed to be
694 // set.
695 bool MayNamedAccess(JSObject* receiver,
696 Object* key,
697 v8::AccessType type);
698 bool MayIndexedAccess(JSObject* receiver,
699 uint32_t index,
700 v8::AccessType type);
701
702 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
703 void ReportFailedAccessCheck(JSObject* receiver, v8::AccessType type);
704
705 // Exception throwing support. The caller should use the result
706 // of Throw() as its return value.
707 Failure* Throw(Object* exception, MessageLocation* location = NULL);
708 // Re-throw an exception. This involves no error reporting since
709 // error reporting was handled when the exception was thrown
710 // originally.
711 Failure* ReThrow(MaybeObject* exception, MessageLocation* location = NULL);
712 void ScheduleThrow(Object* exception);
713 void ReportPendingMessages();
714 Failure* ThrowIllegalOperation();
715
716 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
717 Failure* PromoteScheduledException();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100718 void DoThrow(MaybeObject* exception, MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100719 // Checks if exception should be reported and finds out if it's
720 // caught externally.
721 bool ShouldReportException(bool* can_be_caught_externally,
722 bool catchable_by_javascript);
723
724 // Attempts to compute the current source location, storing the
725 // result in the target out parameter.
726 void ComputeLocation(MessageLocation* target);
727
728 // Override command line flag.
729 void TraceException(bool flag);
730
731 // Out of resource exception helpers.
732 Failure* StackOverflow();
733 Failure* TerminateExecution();
734
735 // Administration
736 void Iterate(ObjectVisitor* v);
737 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
738 char* Iterate(ObjectVisitor* v, char* t);
739 void IterateThread(ThreadVisitor* v);
740 void IterateThread(ThreadVisitor* v, char* t);
741
742
743 // Returns the current global context.
744 Handle<Context> global_context();
745
746 // Returns the global context of the calling JavaScript code. That
747 // is, the global context of the top-most JavaScript frame.
748 Handle<Context> GetCallingGlobalContext();
749
750 void RegisterTryCatchHandler(v8::TryCatch* that);
751 void UnregisterTryCatchHandler(v8::TryCatch* that);
752
753 char* ArchiveThread(char* to);
754 char* RestoreThread(char* from);
755
756 static const char* const kStackOverflowMessage;
757
758 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
759 static const int kBMMaxShift = 250; // See StringSearchBase.
760
761 // Accessors.
762#define GLOBAL_ACCESSOR(type, name, initialvalue) \
763 inline type name() const { \
764 ASSERT(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
765 return name##_; \
766 } \
767 inline void set_##name(type value) { \
768 ASSERT(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
769 name##_ = value; \
770 }
771 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
772#undef GLOBAL_ACCESSOR
773
774#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
775 inline type* name() { \
776 ASSERT(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
777 return &(name##_)[0]; \
778 }
779 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
780#undef GLOBAL_ARRAY_ACCESSOR
781
782#define GLOBAL_CONTEXT_FIELD_ACCESSOR(index, type, name) \
783 Handle<type> name() { \
784 return Handle<type>(context()->global_context()->name()); \
785 }
786 GLOBAL_CONTEXT_FIELDS(GLOBAL_CONTEXT_FIELD_ACCESSOR)
787#undef GLOBAL_CONTEXT_FIELD_ACCESSOR
788
789 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000790 Counters* counters() {
791 // Call InitializeLoggingAndCounters() if logging is needed before
792 // the isolate is fully initialized.
793 ASSERT(counters_ != NULL);
794 return counters_;
795 }
Steve Block44f0eee2011-05-26 01:26:41 +0100796 CodeRange* code_range() { return code_range_; }
797 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
798 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000799 Logger* logger() {
800 // Call InitializeLoggingAndCounters() if logging is needed before
801 // the isolate is fully initialized.
802 ASSERT(logger_ != NULL);
803 return logger_;
804 }
Steve Block44f0eee2011-05-26 01:26:41 +0100805 StackGuard* stack_guard() { return &stack_guard_; }
806 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000807 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100808 StubCache* stub_cache() { return stub_cache_; }
809 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
810 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
811
812 TranscendentalCache* transcendental_cache() const {
813 return transcendental_cache_;
814 }
815
816 MemoryAllocator* memory_allocator() {
817 return memory_allocator_;
818 }
819
820 KeyedLookupCache* keyed_lookup_cache() {
821 return keyed_lookup_cache_;
822 }
823
824 ContextSlotCache* context_slot_cache() {
825 return context_slot_cache_;
826 }
827
828 DescriptorLookupCache* descriptor_lookup_cache() {
829 return descriptor_lookup_cache_;
830 }
831
832 v8::ImplementationUtilities::HandleScopeData* handle_scope_data() {
833 return &handle_scope_data_;
834 }
835 HandleScopeImplementer* handle_scope_implementer() {
836 ASSERT(handle_scope_implementer_);
837 return handle_scope_implementer_;
838 }
839 Zone* zone() { return &zone_; }
840
Ben Murdoch8b112d22011-06-08 16:22:53 +0100841 UnicodeCache* unicode_cache() {
842 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100843 }
844
845 PcToCodeCache* pc_to_code_cache() { return pc_to_code_cache_; }
846
847 StringInputBuffer* write_input_buffer() { return write_input_buffer_; }
848
849 GlobalHandles* global_handles() { return global_handles_; }
850
851 ThreadManager* thread_manager() { return thread_manager_; }
852
853 ContextSwitcher* context_switcher() { return context_switcher_; }
854
855 void set_context_switcher(ContextSwitcher* switcher) {
856 context_switcher_ = switcher;
857 }
858
859 StringTracker* string_tracker() { return string_tracker_; }
860
861 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
862 return &jsregexp_uncanonicalize_;
863 }
864
865 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
866 return &jsregexp_canonrange_;
867 }
868
869 StringInputBuffer* objects_string_compare_buffer_a() {
870 return &objects_string_compare_buffer_a_;
871 }
872
873 StringInputBuffer* objects_string_compare_buffer_b() {
874 return &objects_string_compare_buffer_b_;
875 }
876
877 StaticResource<StringInputBuffer>* objects_string_input_buffer() {
878 return &objects_string_input_buffer_;
879 }
880
881 AstSentinels* ast_sentinels() { return ast_sentinels_; }
882
883 RuntimeState* runtime_state() { return &runtime_state_; }
884
Steve Block44f0eee2011-05-26 01:26:41 +0100885 StaticResource<SafeStringInputBuffer>* compiler_safe_string_input_buffer() {
886 return &compiler_safe_string_input_buffer_;
887 }
888
889 Builtins* builtins() { return &builtins_; }
890
891 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
892 regexp_macro_assembler_canonicalize() {
893 return &regexp_macro_assembler_canonicalize_;
894 }
895
896 RegExpStack* regexp_stack() { return regexp_stack_; }
897
898 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
899 interp_canonicalize_mapping() {
900 return &interp_canonicalize_mapping_;
901 }
902
Steve Block44f0eee2011-05-26 01:26:41 +0100903 void* PreallocatedStorageNew(size_t size);
904 void PreallocatedStorageDelete(void* p);
905 void PreallocatedStorageInit(size_t size);
906
907#ifdef ENABLE_DEBUGGER_SUPPORT
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000908 Debugger* debugger() {
909 if (!NoBarrier_Load(&debugger_initialized_)) InitializeDebugger();
910 return debugger_;
911 }
912 Debug* debug() {
913 if (!NoBarrier_Load(&debugger_initialized_)) InitializeDebugger();
914 return debug_;
915 }
Steve Block44f0eee2011-05-26 01:26:41 +0100916#endif
917
Ben Murdoch257744e2011-11-30 15:57:28 +0000918 inline bool DebuggerHasBreakPoints();
919
Steve Block44f0eee2011-05-26 01:26:41 +0100920#ifdef DEBUG
921 HistogramInfo* heap_histograms() { return heap_histograms_; }
922
923 JSObject::SpillInformation* js_spill_information() {
924 return &js_spill_information_;
925 }
926
927 int* code_kind_statistics() { return code_kind_statistics_; }
928#endif
929
930#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
931 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
932 bool simulator_initialized() { return simulator_initialized_; }
933 void set_simulator_initialized(bool initialized) {
934 simulator_initialized_ = initialized;
935 }
936
937 HashMap* simulator_i_cache() { return simulator_i_cache_; }
938 void set_simulator_i_cache(HashMap* hash_map) {
939 simulator_i_cache_ = hash_map;
940 }
941
942 Redirection* simulator_redirection() {
943 return simulator_redirection_;
944 }
945 void set_simulator_redirection(Redirection* redirection) {
946 simulator_redirection_ = redirection;
947 }
948#endif
949
950 Factory* factory() { return reinterpret_cast<Factory*>(this); }
951
952 // SerializerDeserializer state.
953 static const int kPartialSnapshotCacheCapacity = 1400;
954
955 static const int kJSRegexpStaticOffsetsVectorSize = 50;
956
Steve Block44f0eee2011-05-26 01:26:41 +0100957 Address external_callback() {
958 return thread_local_top_.external_callback_;
959 }
960 void set_external_callback(Address callback) {
961 thread_local_top_.external_callback_ = callback;
962 }
Steve Block44f0eee2011-05-26 01:26:41 +0100963
Steve Block44f0eee2011-05-26 01:26:41 +0100964 StateTag current_vm_state() {
965 return thread_local_top_.current_vm_state_;
966 }
967
968 void SetCurrentVMState(StateTag state) {
969 if (RuntimeProfiler::IsEnabled()) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000970 // Make sure thread local top is initialized.
971 ASSERT(thread_local_top_.isolate_ == this);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100972 StateTag current_state = thread_local_top_.current_vm_state_;
973 if (current_state != JS && state == JS) {
974 // Non-JS -> JS transition.
Steve Block44f0eee2011-05-26 01:26:41 +0100975 RuntimeProfiler::IsolateEnteredJS(this);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100976 } else if (current_state == JS && state != JS) {
Steve Block44f0eee2011-05-26 01:26:41 +0100977 // JS -> non-JS transition.
978 ASSERT(RuntimeProfiler::IsSomeIsolateInJS());
979 RuntimeProfiler::IsolateExitedJS(this);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100980 } else {
981 // Other types of state transitions are not interesting to the
982 // runtime profiler, because they don't affect whether we're
983 // in JS or not.
984 ASSERT((current_state == JS) == (state == JS));
Steve Block44f0eee2011-05-26 01:26:41 +0100985 }
986 }
987 thread_local_top_.current_vm_state_ = state;
988 }
Steve Block44f0eee2011-05-26 01:26:41 +0100989
Ben Murdoch257744e2011-11-30 15:57:28 +0000990 void SetData(void* data) { embedder_data_ = data; }
991 void* GetData() { return embedder_data_; }
992
Steve Block44f0eee2011-05-26 01:26:41 +0100993 private:
994 Isolate();
995
996 // The per-process lock should be acquired before the ThreadDataTable is
997 // modified.
998 class ThreadDataTable {
999 public:
1000 ThreadDataTable();
1001 ~ThreadDataTable();
1002
1003 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1004 void Insert(PerIsolateThreadData* data);
1005 void Remove(Isolate* isolate, ThreadId thread_id);
1006 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001007 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001008
1009 private:
1010 PerIsolateThreadData* list_;
1011 };
1012
1013 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1014 // the Isolate. The top of the stack points to a thread which is currently
1015 // running the Isolate. When the stack is empty, the Isolate is considered
1016 // not entered by any thread and can be Disposed.
1017 // If the same thread enters the Isolate more then once, the entry_count_
1018 // is incremented rather then a new item pushed to the stack.
1019 class EntryStackItem {
1020 public:
1021 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1022 Isolate* previous_isolate,
1023 EntryStackItem* previous_item)
1024 : entry_count(1),
1025 previous_thread_data(previous_thread_data),
1026 previous_isolate(previous_isolate),
1027 previous_item(previous_item) { }
1028
1029 int entry_count;
1030 PerIsolateThreadData* previous_thread_data;
1031 Isolate* previous_isolate;
1032 EntryStackItem* previous_item;
1033
1034 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1035 };
1036
1037 // This mutex protects highest_thread_id_, thread_data_table_ and
1038 // default_isolate_.
1039 static Mutex* process_wide_mutex_;
1040
1041 static Thread::LocalStorageKey per_isolate_thread_data_key_;
1042 static Thread::LocalStorageKey isolate_key_;
1043 static Thread::LocalStorageKey thread_id_key_;
1044 static Isolate* default_isolate_;
1045 static ThreadDataTable* thread_data_table_;
Steve Block44f0eee2011-05-26 01:26:41 +01001046
Steve Block44f0eee2011-05-26 01:26:41 +01001047 void Deinit();
1048
1049 static void SetIsolateThreadLocals(Isolate* isolate,
1050 PerIsolateThreadData* data);
1051
1052 enum State {
1053 UNINITIALIZED, // Some components may not have been allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01001054 INITIALIZED // All components are fully initialized.
1055 };
1056
1057 State state_;
1058 EntryStackItem* entry_stack_;
1059
1060 // Allocate and insert PerIsolateThreadData into the ThreadDataTable
1061 // (regardless of whether such data already exists).
1062 PerIsolateThreadData* AllocatePerIsolateThreadData(ThreadId thread_id);
1063
1064 // Find the PerThread for this particular (isolate, thread) combination.
1065 // If one does not yet exist, allocate a new one.
1066 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1067
Ben Murdoch257744e2011-11-30 15:57:28 +00001068// PreInits and returns a default isolate. Needed when a new thread tries
Steve Block44f0eee2011-05-26 01:26:41 +01001069 // to create a Locker for the first time (the lock itself is in the isolate).
1070 static Isolate* GetDefaultIsolateForLocking();
1071
1072 // Initializes the current thread to run this Isolate.
1073 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1074 // at the same time, this should be prevented using external locking.
1075 void Enter();
1076
1077 // Exits the current thread. The previosuly entered Isolate is restored
1078 // for the thread.
1079 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1080 // at the same time, this should be prevented using external locking.
1081 void Exit();
1082
1083 void PreallocatedMemoryThreadStart();
1084 void PreallocatedMemoryThreadStop();
1085 void InitializeThreadLocal();
1086
1087 void PrintStackTrace(FILE* out, ThreadLocalTop* thread);
1088 void MarkCompactPrologue(bool is_compacting,
1089 ThreadLocalTop* archived_thread_data);
1090 void MarkCompactEpilogue(bool is_compacting,
1091 ThreadLocalTop* archived_thread_data);
1092
1093 void FillCache();
1094
Ben Murdoch8b112d22011-06-08 16:22:53 +01001095 void PropagatePendingExceptionToExternalTryCatch();
1096
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001097 void InitializeDebugger();
1098
Steve Block44f0eee2011-05-26 01:26:41 +01001099 int stack_trace_nesting_level_;
1100 StringStream* incomplete_message_;
1101 // The preallocated memory thread singleton.
1102 PreallocatedMemoryThread* preallocated_memory_thread_;
1103 Address isolate_addresses_[k_isolate_address_count + 1]; // NOLINT
1104 NoAllocationStringAllocator* preallocated_message_space_;
1105
1106 Bootstrapper* bootstrapper_;
1107 RuntimeProfiler* runtime_profiler_;
1108 CompilationCache* compilation_cache_;
1109 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001110 CodeRange* code_range_;
1111 Mutex* break_access_;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001112 Atomic32 debugger_initialized_;
1113 Mutex* debugger_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001114 Heap heap_;
1115 Logger* logger_;
1116 StackGuard stack_guard_;
1117 StatsTable* stats_table_;
1118 StubCache* stub_cache_;
1119 DeoptimizerData* deoptimizer_data_;
1120 ThreadLocalTop thread_local_top_;
1121 bool capture_stack_trace_for_uncaught_exceptions_;
1122 int stack_trace_for_uncaught_exceptions_frame_limit_;
1123 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
1124 TranscendentalCache* transcendental_cache_;
1125 MemoryAllocator* memory_allocator_;
1126 KeyedLookupCache* keyed_lookup_cache_;
1127 ContextSlotCache* context_slot_cache_;
1128 DescriptorLookupCache* descriptor_lookup_cache_;
1129 v8::ImplementationUtilities::HandleScopeData handle_scope_data_;
1130 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001131 UnicodeCache* unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001132 Zone zone_;
1133 PreallocatedStorage in_use_list_;
1134 PreallocatedStorage free_list_;
1135 bool preallocated_storage_preallocated_;
1136 PcToCodeCache* pc_to_code_cache_;
1137 StringInputBuffer* write_input_buffer_;
1138 GlobalHandles* global_handles_;
1139 ContextSwitcher* context_switcher_;
1140 ThreadManager* thread_manager_;
1141 AstSentinels* ast_sentinels_;
1142 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001143 StaticResource<SafeStringInputBuffer> compiler_safe_string_input_buffer_;
1144 Builtins builtins_;
1145 StringTracker* string_tracker_;
1146 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1147 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
1148 StringInputBuffer objects_string_compare_buffer_a_;
1149 StringInputBuffer objects_string_compare_buffer_b_;
1150 StaticResource<StringInputBuffer> objects_string_input_buffer_;
1151 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1152 regexp_macro_assembler_canonicalize_;
1153 RegExpStack* regexp_stack_;
1154 unibrow::Mapping<unibrow::Ecma262Canonicalize> interp_canonicalize_mapping_;
Ben Murdoch257744e2011-11-30 15:57:28 +00001155 void* embedder_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001156
1157#if defined(V8_TARGET_ARCH_ARM) && !defined(__arm__) || \
1158 defined(V8_TARGET_ARCH_MIPS) && !defined(__mips__)
1159 bool simulator_initialized_;
1160 HashMap* simulator_i_cache_;
1161 Redirection* simulator_redirection_;
1162#endif
1163
1164#ifdef DEBUG
1165 // A static array of histogram info for each type.
1166 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1167 JSObject::SpillInformation js_spill_information_;
1168 int code_kind_statistics_[Code::NUMBER_OF_KINDS];
1169#endif
1170
1171#ifdef ENABLE_DEBUGGER_SUPPORT
1172 Debugger* debugger_;
1173 Debug* debug_;
1174#endif
1175
Steve Block44f0eee2011-05-26 01:26:41 +01001176#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1177 type name##_;
1178 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1179#undef GLOBAL_BACKING_STORE
1180
1181#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1182 type name##_[length];
1183 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1184#undef GLOBAL_ARRAY_BACKING_STORE
1185
1186#ifdef DEBUG
1187 // This class is huge and has a number of fields controlled by
1188 // preprocessor defines. Make sure the offsets of these fields agree
1189 // between compilation units.
1190#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1191 static const intptr_t name##_debug_offset_;
1192 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1193 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1194#undef ISOLATE_FIELD_OFFSET
1195#endif
1196
1197 friend class ExecutionAccess;
1198 friend class IsolateInitializer;
Ben Murdoch257744e2011-11-30 15:57:28 +00001199 friend class ThreadManager;
1200 friend class Simulator;
1201 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001202 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001203 friend class TestMemoryAllocatorScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001204 friend class v8::Isolate;
1205 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001206 friend class v8::Unlocker;
Steve Block44f0eee2011-05-26 01:26:41 +01001207
1208 DISALLOW_COPY_AND_ASSIGN(Isolate);
1209};
1210
1211
1212// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1213// class as a work around for a bug in the generated code found with these
1214// versions of GCC. See V8 issue 122 for details.
1215class SaveContext BASE_EMBEDDED {
1216 public:
1217 explicit SaveContext(Isolate* isolate) : prev_(isolate->save_context()) {
1218 if (isolate->context() != NULL) {
1219 context_ = Handle<Context>(isolate->context());
1220#if __GNUC_VERSION__ >= 40100 && __GNUC_VERSION__ < 40300
1221 dummy_ = Handle<Context>(isolate->context());
1222#endif
1223 }
1224 isolate->set_save_context(this);
1225
1226 // If there is no JS frame under the current C frame, use the value 0.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001227 JavaScriptFrameIterator it(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001228 js_sp_ = it.done() ? 0 : it.frame()->sp();
1229 }
1230
1231 ~SaveContext() {
1232 if (context_.is_null()) {
1233 Isolate* isolate = Isolate::Current();
1234 isolate->set_context(NULL);
1235 isolate->set_save_context(prev_);
1236 } else {
1237 Isolate* isolate = context_->GetIsolate();
1238 isolate->set_context(*context_);
1239 isolate->set_save_context(prev_);
1240 }
1241 }
1242
1243 Handle<Context> context() { return context_; }
1244 SaveContext* prev() { return prev_; }
1245
1246 // Returns true if this save context is below a given JavaScript frame.
1247 bool below(JavaScriptFrame* frame) {
1248 return (js_sp_ == 0) || (frame->sp() < js_sp_);
1249 }
1250
1251 private:
1252 Handle<Context> context_;
1253#if __GNUC_VERSION__ >= 40100 && __GNUC_VERSION__ < 40300
1254 Handle<Context> dummy_;
1255#endif
1256 SaveContext* prev_;
1257 Address js_sp_; // The top JS frame's sp when saving context.
1258};
1259
1260
1261class AssertNoContextChange BASE_EMBEDDED {
1262#ifdef DEBUG
1263 public:
1264 AssertNoContextChange() :
1265 scope_(Isolate::Current()),
1266 context_(Isolate::Current()->context(), Isolate::Current()) {
1267 }
1268
1269 ~AssertNoContextChange() {
1270 ASSERT(Isolate::Current()->context() == *context_);
1271 }
1272
1273 private:
1274 HandleScope scope_;
1275 Handle<Context> context_;
1276#else
1277 public:
1278 AssertNoContextChange() { }
1279#endif
1280};
1281
1282
1283class ExecutionAccess BASE_EMBEDDED {
1284 public:
1285 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1286 Lock(isolate);
1287 }
1288 ~ExecutionAccess() { Unlock(isolate_); }
1289
1290 static void Lock(Isolate* isolate) { isolate->break_access_->Lock(); }
1291 static void Unlock(Isolate* isolate) { isolate->break_access_->Unlock(); }
1292
1293 static bool TryLock(Isolate* isolate) {
1294 return isolate->break_access_->TryLock();
1295 }
1296
1297 private:
1298 Isolate* isolate_;
1299};
1300
1301
1302// Support for checking for stack-overflows in C++ code.
1303class StackLimitCheck BASE_EMBEDDED {
1304 public:
1305 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1306
1307 bool HasOverflowed() const {
1308 StackGuard* stack_guard = isolate_->stack_guard();
1309 // Stack has overflowed in C++ code only if stack pointer exceeds the C++
1310 // stack guard and the limits are not set to interrupt values.
1311 // TODO(214): Stack overflows are ignored if a interrupt is pending. This
1312 // code should probably always use the initial C++ limit.
1313 return (reinterpret_cast<uintptr_t>(this) < stack_guard->climit()) &&
1314 stack_guard->IsStackOverflow();
1315 }
1316 private:
1317 Isolate* isolate_;
1318};
1319
1320
1321// Support for temporarily postponing interrupts. When the outermost
1322// postpone scope is left the interrupts will be re-enabled and any
1323// interrupts that occurred while in the scope will be taken into
1324// account.
1325class PostponeInterruptsScope BASE_EMBEDDED {
1326 public:
1327 explicit PostponeInterruptsScope(Isolate* isolate)
1328 : stack_guard_(isolate->stack_guard()) {
1329 stack_guard_->thread_local_.postpone_interrupts_nesting_++;
1330 stack_guard_->DisableInterrupts();
1331 }
1332
1333 ~PostponeInterruptsScope() {
1334 if (--stack_guard_->thread_local_.postpone_interrupts_nesting_ == 0) {
1335 stack_guard_->EnableInterrupts();
1336 }
1337 }
1338 private:
1339 StackGuard* stack_guard_;
1340};
1341
1342
1343// Temporary macros for accessing current isolate and its subobjects.
1344// They provide better readability, especially when used a lot in the code.
1345#define HEAP (v8::internal::Isolate::Current()->heap())
1346#define FACTORY (v8::internal::Isolate::Current()->factory())
1347#define ISOLATE (v8::internal::Isolate::Current())
1348#define ZONE (v8::internal::Isolate::Current()->zone())
1349#define LOGGER (v8::internal::Isolate::Current()->logger())
1350
1351
1352// Tells whether the global context is marked with out of memory.
1353inline bool Context::has_out_of_memory() {
1354 return global_context()->out_of_memory()->IsTrue();
1355}
1356
1357
1358// Mark the global context with out of memory.
1359inline void Context::mark_out_of_memory() {
1360 global_context()->set_out_of_memory(HEAP->true_value());
1361}
1362
1363
Steve Block44f0eee2011-05-26 01:26:41 +01001364} } // namespace v8::internal
1365
Steve Block44f0eee2011-05-26 01:26:41 +01001366#endif // V8_ISOLATE_H_