blob: 24d4b0898de5d5cdb36327f12d24c3283b21e355 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block44f0eee2011-05-26 01:26:41 +01004
5#ifndef V8_ISOLATE_H_
6#define V8_ISOLATE_H_
7
Ben Murdochb8a8cc12014-11-26 15:28:44 +00008#include "include/v8-debug.h"
9#include "src/allocation.h"
10#include "src/assert-scope.h"
11#include "src/base/atomicops.h"
12#include "src/builtins.h"
13#include "src/contexts.h"
14#include "src/date.h"
15#include "src/execution.h"
16#include "src/frames.h"
17#include "src/global-handles.h"
18#include "src/handles.h"
19#include "src/hashmap.h"
20#include "src/heap/heap.h"
21#include "src/optimizing-compiler-thread.h"
22#include "src/regexp-stack.h"
23#include "src/runtime.h"
24#include "src/runtime-profiler.h"
25#include "src/zone.h"
Steve Block44f0eee2011-05-26 01:26:41 +010026
27namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028
29namespace base {
30class RandomNumberGenerator;
31}
32
Steve Block44f0eee2011-05-26 01:26:41 +010033namespace internal {
34
Steve Block44f0eee2011-05-26 01:26:41 +010035class Bootstrapper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036class CallInterfaceDescriptorData;
Steve Block44f0eee2011-05-26 01:26:41 +010037class CodeGenerator;
38class CodeRange;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039class CodeStubDescriptor;
40class CodeTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010041class CompilationCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042class ConsStringIteratorOp;
Steve Block44f0eee2011-05-26 01:26:41 +010043class ContextSlotCache;
Steve Block44f0eee2011-05-26 01:26:41 +010044class Counters;
45class CpuFeatures;
46class CpuProfiler;
47class DeoptimizerData;
48class Deserializer;
49class EmptyStatement;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050class ExternalCallbackScope;
Steve Block44f0eee2011-05-26 01:26:41 +010051class ExternalReferenceTable;
52class Factory;
53class FunctionInfoListener;
54class HandleScopeImplementer;
55class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000056class HStatistics;
57class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010058class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010059class InnerPointerToCodeCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060class MaterializedObjectStore;
61class CodeAgingHelper;
Steve Block44f0eee2011-05-26 01:26:41 +010062class RegExpStack;
63class SaveContext;
Steve Block44f0eee2011-05-26 01:26:41 +010064class StringTracker;
65class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010067class ThreadManager;
68class ThreadState;
69class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070class UnicodeCache;
71template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010072
73// 'void function pointer', used to roundtrip the
74// ExternalReference::ExternalReferenceRedirector since we can not include
75// assembler.h, where it is defined, here.
76typedef void* ExternalReferenceRedirectorPointer();
77
78
Steve Block44f0eee2011-05-26 01:26:41 +010079class Debug;
80class Debugger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010082
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
84 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
85 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
86 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +010087class Redirection;
88class Simulator;
89#endif
90
91
92// Static indirection table for handles to constants. If a frame
93// element represents a constant, the data contains an index into
94// this table of handles to the actual constants.
95// Static indirection table for handles to constants. If a Result
96// represents a constant, the data contains an index into this table
97// of handles to the actual constants.
98typedef ZoneList<Handle<Object> > ZoneObjectList;
99
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100101 do { \
102 Isolate* __isolate__ = (isolate); \
103 if (__isolate__->has_scheduled_exception()) { \
104 return __isolate__->PromoteScheduledException(); \
105 } \
106 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108// Macros for MaybeHandle.
109
110#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
111 do { \
112 Isolate* __isolate__ = (isolate); \
113 if (__isolate__->has_scheduled_exception()) { \
114 __isolate__->PromoteScheduledException(); \
115 return value; \
116 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100117 } while (false)
118
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
120 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
121
122#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
123 do { \
124 if (!(call).ToHandle(&dst)) { \
125 DCHECK((isolate)->has_pending_exception()); \
126 return value; \
127 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100128 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100129
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000130#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
131 ASSIGN_RETURN_ON_EXCEPTION_VALUE( \
132 isolate, dst, call, isolate->heap()->exception())
133
134#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
135 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
136
137#define THROW_NEW_ERROR(isolate, call, T) \
138 do { \
139 Handle<Object> __error__; \
140 ASSIGN_RETURN_ON_EXCEPTION(isolate, __error__, isolate->factory()->call, \
141 T); \
142 return isolate->Throw<T>(__error__); \
143 } while (false)
144
145#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
146 do { \
147 Handle<Object> __error__; \
148 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, __error__, \
149 isolate->factory()->call); \
150 return isolate->Throw(*__error__); \
151 } while (false)
152
153#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
154 do { \
155 if ((call).is_null()) { \
156 DCHECK((isolate)->has_pending_exception()); \
157 return value; \
158 } \
159 } while (false)
160
161#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
162 RETURN_ON_EXCEPTION_VALUE(isolate, call, isolate->heap()->exception())
163
164#define RETURN_ON_EXCEPTION(isolate, call, T) \
165 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
166
Steve Block44f0eee2011-05-26 01:26:41 +0100167
Ben Murdoch589d6972011-11-30 16:04:58 +0000168#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
169 C(Handler, handler) \
170 C(CEntryFP, c_entry_fp) \
171 C(Context, context) \
172 C(PendingException, pending_exception) \
173 C(ExternalCaughtException, external_caught_exception) \
174 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100175
176
Ben Murdoch8b112d22011-06-08 16:22:53 +0100177// Platform-independent, reliable thread identifier.
178class ThreadId {
179 public:
180 // Creates an invalid ThreadId.
181 ThreadId() : id_(kInvalidId) {}
182
183 // Returns ThreadId for current thread.
184 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
185
186 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
187 static ThreadId Invalid() { return ThreadId(kInvalidId); }
188
189 // Compares ThreadIds for equality.
190 INLINE(bool Equals(const ThreadId& other) const) {
191 return id_ == other.id_;
192 }
193
194 // Checks whether this ThreadId refers to any thread.
195 INLINE(bool IsValid() const) {
196 return id_ != kInvalidId;
197 }
198
199 // Converts ThreadId to an integer representation
200 // (required for public API: V8::V8::GetCurrentThreadId).
201 int ToInteger() const { return id_; }
202
203 // Converts ThreadId to an integer representation
204 // (required for public API: V8::V8::TerminateExecution).
205 static ThreadId FromInteger(int id) { return ThreadId(id); }
206
207 private:
208 static const int kInvalidId = -1;
209
210 explicit ThreadId(int id) : id_(id) {}
211
212 static int AllocateThreadId();
213
214 static int GetCurrentThreadId();
215
216 int id_;
217
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100219
220 friend class Isolate;
221};
222
223
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224#define FIELD_ACCESSOR(type, name) \
225 inline void set_##name(type v) { name##_ = v; } \
226 inline type name() const { return name##_; }
227
228
Steve Block44f0eee2011-05-26 01:26:41 +0100229class ThreadLocalTop BASE_EMBEDDED {
230 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100231 // Does early low-level initialization that does not depend on the
232 // isolate being present.
233 ThreadLocalTop();
234
Steve Block44f0eee2011-05-26 01:26:41 +0100235 // Initialize the thread data.
236 void Initialize();
237
238 // Get the top C++ try catch handler or NULL if none are registered.
239 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100241 // used for comparison with addresses into the JS stack. If such an
242 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100244
245 // Get the address of the top C++ try catch handler or NULL if
246 // none are registered.
247 //
248 // This method always returns an address that can be compared to
249 // pointers into the JavaScript stack. When running on actual
250 // hardware, try_catch_handler_address and TryCatchHandler return
251 // the same pointer. When running on a simulator with a separate JS
252 // stack, try_catch_handler_address returns a JS stack address that
253 // corresponds to the place on the JS stack where the C++ handler
254 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 Address try_catch_handler_address() {
256 return reinterpret_cast<Address>(
257 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100258 }
259
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000260 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100261
Ben Murdoch257744e2011-11-30 15:57:28 +0000262 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100263 // The context where the current execution method is created and for variable
264 // lookups.
265 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100266 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 Object* pending_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100268 bool has_pending_message_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100270 Object* pending_message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271 Object* pending_message_script_;
Steve Block44f0eee2011-05-26 01:26:41 +0100272 int pending_message_start_pos_;
273 int pending_message_end_pos_;
274 // Use a separate value for scheduled exceptions to preserve the
275 // invariants that hold about pending_exception. We may want to
276 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100278 bool external_caught_exception_;
279 SaveContext* save_context_;
280 v8::TryCatch* catcher_;
281
282 // Stack.
283 Address c_entry_fp_; // the frame pointer of the top c entry frame
284 Address handler_; // try-blocks are chained through the stack
285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 // Throwing an exception may cause a Promise rejection. For this purpose
287 // we keep track of a stack of nested promises and the corresponding
288 // try-catch handlers.
289 PromiseOnStack* promise_on_stack_;
290
Steve Block44f0eee2011-05-26 01:26:41 +0100291#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100292 Simulator* simulator_;
293#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100294
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100295 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 // the external callback we're currently in
297 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100298 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100299
300 // Generated code scratch locations.
301 int32_t formal_count_;
302
303 // Call back function to report unsafe JS accesses.
304 v8::FailedAccessCheckCallback failed_access_check_callback_;
305
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100306 // Head of the list of live LookupResults.
307 LookupResult* top_lookup_result_;
308
Steve Block44f0eee2011-05-26 01:26:41 +0100309 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100310 void InitializeInternal();
311
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100313};
314
Steve Block44f0eee2011-05-26 01:26:41 +0100315
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316#if V8_TARGET_ARCH_ARM && !defined(__arm__) || \
317 V8_TARGET_ARCH_ARM64 && !defined(__aarch64__) || \
318 V8_TARGET_ARCH_MIPS && !defined(__mips__) || \
319 V8_TARGET_ARCH_MIPS64 && !defined(__mips__)
Steve Block44f0eee2011-05-26 01:26:41 +0100320
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321#define ISOLATE_INIT_SIMULATOR_LIST(V) \
322 V(bool, simulator_initialized, false) \
323 V(HashMap*, simulator_i_cache, NULL) \
324 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100325#else
326
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100328
329#endif
330
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000331
Steve Block44f0eee2011-05-26 01:26:41 +0100332#ifdef DEBUG
333
334#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
335 V(CommentStatistic, paged_space_comments_statistics, \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 CommentStatistic::kMaxComments + 1) \
337 V(int, code_kind_statistics, Code::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100338#else
339
340#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
341
342#endif
343
Steve Block44f0eee2011-05-26 01:26:41 +0100344#define ISOLATE_INIT_ARRAY_LIST(V) \
345 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100347 V(int, bad_char_shift_table, kUC16AlphabetSize) \
348 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
349 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000350 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100351 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
352
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100354
355#define ISOLATE_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100356 /* SerializerDeserializer state. */ \
357 V(int, serialize_partial_snapshot_cache_length, 0) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358 V(int, serialize_partial_snapshot_cache_capacity, 0) \
359 V(Object**, serialize_partial_snapshot_cache, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100360 /* Assembler state. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100361 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362 V(LogEventCallback, event_logger, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000363 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100364 /* To distinguish the function templates, so that we can find them in the */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 /* function cache of the native context. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100366 V(int, next_serial_number, 0) \
367 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100368 /* Part of the state of liveedit. */ \
369 V(FunctionInfoListener*, active_function_info_listener, NULL) \
370 /* State for Relocatable. */ \
371 V(Relocatable*, relocatable_top, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100372 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
373 V(Object*, string_stream_current_security_token, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100374 /* Serializer state. */ \
375 V(ExternalReferenceTable*, external_reference_table, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 V(int, pending_microtask_count, 0) \
377 V(bool, autorun_microtasks, true) \
378 V(HStatistics*, hstatistics, NULL) \
379 V(HStatistics*, tstatistics, NULL) \
380 V(HTracer*, htracer, NULL) \
381 V(CodeTracer*, code_tracer, NULL) \
382 V(bool, fp_stubs_generated, false) \
383 V(int, max_available_threads, 0) \
384 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
385 V(InterruptCallback, api_interrupt_callback, NULL) \
386 V(void*, api_interrupt_callback_data, NULL) \
387 ISOLATE_INIT_SIMULATOR_LIST(V)
388
389#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
390 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
391 inline type name() const { return thread_local_top_.name##_; }
392
Steve Block44f0eee2011-05-26 01:26:41 +0100393
394class Isolate {
395 // These forward declarations are required to make the friend declarations in
396 // PerIsolateThreadData work on some older versions of gcc.
397 class ThreadDataTable;
398 class EntryStackItem;
399 public:
400 ~Isolate();
401
Steve Block44f0eee2011-05-26 01:26:41 +0100402 // A thread has a PerIsolateThreadData instance for each isolate that it has
403 // entered. That instance is allocated when the isolate is initially entered
404 // and reused on subsequent entries.
405 class PerIsolateThreadData {
406 public:
407 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
408 : isolate_(isolate),
409 thread_id_(thread_id),
410 stack_limit_(0),
411 thread_state_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
413 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
414 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
415 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +0100416 simulator_(NULL),
417#endif
418 next_(NULL),
419 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100421 Isolate* isolate() const { return isolate_; }
422 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100423
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424 FIELD_ACCESSOR(uintptr_t, stack_limit)
425 FIELD_ACCESSOR(ThreadState*, thread_state)
426
427#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
428 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
429 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
430 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
431 FIELD_ACCESSOR(Simulator*, simulator)
Steve Block44f0eee2011-05-26 01:26:41 +0100432#endif
433
434 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100435 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100436 }
437
438 private:
439 Isolate* isolate_;
440 ThreadId thread_id_;
441 uintptr_t stack_limit_;
442 ThreadState* thread_state_;
443
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000444#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
445 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
446 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
447 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +0100448 Simulator* simulator_;
449#endif
450
451 PerIsolateThreadData* next_;
452 PerIsolateThreadData* prev_;
453
454 friend class Isolate;
455 friend class ThreadDataTable;
456 friend class EntryStackItem;
457
458 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
459 };
460
461
462 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000463#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
464 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000466 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100467 };
468
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 static void InitializeOncePerProcess();
470
Steve Block44f0eee2011-05-26 01:26:41 +0100471 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
472 // not currently set).
473 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
474 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100476 }
477
478 // Returns the isolate inside which the current thread is running.
479 INLINE(static Isolate* Current()) {
480 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 base::Thread::GetExistingThreadLocal(isolate_key_));
482 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100483 return isolate;
484 }
485
486 INLINE(static Isolate* UncheckedCurrent()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 return reinterpret_cast<Isolate*>(
488 base::Thread::GetThreadLocal(isolate_key_));
489 }
490
491 // Like UncheckedCurrent, but skips the check that |isolate_key_| was
492 // initialized. Callers have to ensure that themselves.
493 INLINE(static Isolate* UnsafeCurrent()) {
494 return reinterpret_cast<Isolate*>(
495 base::Thread::GetThreadLocal(isolate_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100496 }
497
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000498 // Usually called by Init(), but can be called early e.g. to allow
499 // testing components that require logging but not the whole
500 // isolate.
501 //
502 // Safe to call more than once.
503 void InitializeLoggingAndCounters();
504
Steve Block44f0eee2011-05-26 01:26:41 +0100505 bool Init(Deserializer* des);
506
507 bool IsInitialized() { return state_ == INITIALIZED; }
508
509 // True if at least one thread Enter'ed this isolate.
510 bool IsInUse() { return entry_stack_ != NULL; }
511
512 // Destroys the non-default isolates.
513 // Sets default isolate into "has_been_disposed" state rather then destroying,
514 // for legacy API reasons.
515 void TearDown();
516
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100518
Ben Murdoch257744e2011-11-30 15:57:28 +0000519 // Find the PerThread for this particular (isolate, thread) combination
520 // If one does not yet exist, return null.
521 PerIsolateThreadData* FindPerThreadDataForThisThread();
522
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 // Find the PerThread for given (isolate, thread) combination
524 // If one does not yet exist, return null.
525 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100526
527 // Returns the key used to store the pointer to the current isolate.
528 // Used internally for V8 threads that do not execute JavaScript but still
529 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100531 return isolate_key_;
532 }
Steve Block44f0eee2011-05-26 01:26:41 +0100533
534 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100536 return thread_id_key_;
537 }
Steve Block44f0eee2011-05-26 01:26:41 +0100538
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100540
Steve Block44f0eee2011-05-26 01:26:41 +0100541 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000543
Steve Block44f0eee2011-05-26 01:26:41 +0100544 Address get_address_from_id(AddressId id);
545
546 // Access to top context (where the current function object was created).
547 Context* context() { return thread_local_top_.context_; }
548 void set_context(Context* context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 DCHECK(context == NULL || context->IsContext());
Steve Block44f0eee2011-05-26 01:26:41 +0100550 thread_local_top_.context_ = context;
551 }
552 Context** context_address() { return &thread_local_top_.context_; }
553
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100555
556 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100558
559 // Interface to pending exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560 Object* pending_exception() {
561 DCHECK(has_pending_exception());
562 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100563 return thread_local_top_.pending_exception_;
564 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565
566 void set_pending_exception(Object* exception_obj) {
567 DCHECK(!exception_obj->IsException());
568 thread_local_top_.pending_exception_ = exception_obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100569 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570
Steve Block44f0eee2011-05-26 01:26:41 +0100571 void clear_pending_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100573 thread_local_top_.pending_exception_ = heap_.the_hole_value();
574 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575
576 Object** pending_exception_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100577 return &thread_local_top_.pending_exception_;
578 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579
Steve Block44f0eee2011-05-26 01:26:41 +0100580 bool has_pending_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100582 return !thread_local_top_.pending_exception_->IsTheHole();
583 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000584
585 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
586
Steve Block44f0eee2011-05-26 01:26:41 +0100587 void clear_pending_message() {
588 thread_local_top_.has_pending_message_ = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100589 thread_local_top_.pending_message_obj_ = heap_.the_hole_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 thread_local_top_.pending_message_script_ = heap_.the_hole_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100591 }
592 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100594 }
595 Address try_catch_handler_address() {
596 return thread_local_top_.try_catch_handler_address();
597 }
598 bool* external_caught_exception_address() {
599 return &thread_local_top_.external_caught_exception_;
600 }
601
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 THREAD_LOCAL_TOP_ACCESSOR(v8::TryCatch*, catcher)
603
604 Object** scheduled_exception_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100605 return &thread_local_top_.scheduled_exception_;
606 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607
608 Address pending_message_obj_address() {
609 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
610 }
611
612 Address has_pending_message_address() {
613 return reinterpret_cast<Address>(&thread_local_top_.has_pending_message_);
614 }
615
616 Address pending_message_script_address() {
617 return reinterpret_cast<Address>(
618 &thread_local_top_.pending_message_script_);
619 }
620
621 Object* scheduled_exception() {
622 DCHECK(has_scheduled_exception());
623 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100624 return thread_local_top_.scheduled_exception_;
625 }
626 bool has_scheduled_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000628 return thread_local_top_.scheduled_exception_ != heap_.the_hole_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100629 }
630 void clear_scheduled_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100632 thread_local_top_.scheduled_exception_ = heap_.the_hole_value();
633 }
634
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 bool HasExternalTryCatch();
636 bool IsFinallyOnTop();
Steve Block44f0eee2011-05-26 01:26:41 +0100637
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 bool is_catchable_by_javascript(Object* exception) {
639 return exception != heap()->termination_exception();
Steve Block44f0eee2011-05-26 01:26:41 +0100640 }
641
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000642 // Serializer.
643 void PushToPartialSnapshotCache(Object* obj);
644
Steve Block44f0eee2011-05-26 01:26:41 +0100645 // JS execution stack (see frames.h).
646 static Address c_entry_fp(ThreadLocalTop* thread) {
647 return thread->c_entry_fp_;
648 }
649 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
650
651 inline Address* c_entry_fp_address() {
652 return &thread_local_top_.c_entry_fp_;
653 }
654 inline Address* handler_address() { return &thread_local_top_.handler_; }
655
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656 // Bottom JS entry.
657 Address js_entry_sp() {
658 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100659 }
660 inline Address* js_entry_sp_address() {
661 return &thread_local_top_.js_entry_sp_;
662 }
Steve Block44f0eee2011-05-26 01:26:41 +0100663
664 // Generated code scratch locations.
665 void* formal_count_address() { return &thread_local_top_.formal_count_; }
666
667 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100668 // a builtin object, or a JS global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669 Handle<GlobalObject> global_object() {
670 return Handle<GlobalObject>(context()->global_object());
Steve Block44f0eee2011-05-26 01:26:41 +0100671 }
672
673 // Returns the global proxy object of the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 JSObject* global_proxy() {
Steve Block44f0eee2011-05-26 01:26:41 +0100675 return context()->global_proxy();
676 }
677
678 Handle<JSBuiltinsObject> js_builtins_object() {
679 return Handle<JSBuiltinsObject>(thread_local_top_.context_->builtins());
680 }
681
682 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
683 void FreeThreadResources() { thread_local_top_.Free(); }
684
685 // This method is called by the api after operations that may throw
686 // exceptions. If an exception was thrown and not handled by an external
687 // handler the exception is scheduled to be rethrown when we return to running
688 // JavaScript code. If an exception is scheduled true is returned.
689 bool OptionalRescheduleException(bool is_bottom_call);
690
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000691 // Push and pop a promise and the current try-catch handler.
692 void PushPromise(Handle<JSObject> promise);
693 void PopPromise();
694 Handle<Object> GetPromiseOnStackOnThrow();
695
Ben Murdoch8b112d22011-06-08 16:22:53 +0100696 class ExceptionScope {
697 public:
698 explicit ExceptionScope(Isolate* isolate) :
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 // Scope currently can only be used for regular exceptions,
700 // not termination exception.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100701 isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000702 pending_exception_(isolate_->pending_exception(), isolate_),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100703 catcher_(isolate_->catcher())
704 { }
705
706 ~ExceptionScope() {
707 isolate_->set_catcher(catcher_);
708 isolate_->set_pending_exception(*pending_exception_);
709 }
710
711 private:
712 Isolate* isolate_;
713 Handle<Object> pending_exception_;
714 v8::TryCatch* catcher_;
715 };
716
Steve Block44f0eee2011-05-26 01:26:41 +0100717 void SetCaptureStackTraceForUncaughtExceptions(
718 bool capture,
719 int frame_limit,
720 StackTrace::StackTraceOptions options);
721
Steve Block44f0eee2011-05-26 01:26:41 +0100722 void PrintCurrentStackTrace(FILE* out);
Steve Block44f0eee2011-05-26 01:26:41 +0100723 void PrintStack(StringStream* accumulator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000724 void PrintStack(FILE* out);
Steve Block44f0eee2011-05-26 01:26:41 +0100725 Handle<String> StackTraceString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000726 NO_INLINE(void PushStackTraceAndDie(unsigned int magic,
727 Object* object,
728 Map* map,
729 unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100730 Handle<JSArray> CaptureCurrentStackTrace(
731 int frame_limit,
732 StackTrace::StackTraceOptions options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 Handle<Object> CaptureSimpleStackTrace(Handle<JSObject> error_object,
734 Handle<Object> caller);
735 void CaptureAndSetDetailedStackTrace(Handle<JSObject> error_object);
736 void CaptureAndSetSimpleStackTrace(Handle<JSObject> error_object,
737 Handle<Object> caller);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100738
Steve Block44f0eee2011-05-26 01:26:41 +0100739 // Returns if the top context may access the given global object. If
740 // the result is false, the pending exception is guaranteed to be
741 // set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742
743 bool MayNamedAccess(Handle<JSObject> receiver,
744 Handle<Object> key,
Steve Block44f0eee2011-05-26 01:26:41 +0100745 v8::AccessType type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 bool MayIndexedAccess(Handle<JSObject> receiver,
Steve Block44f0eee2011-05-26 01:26:41 +0100747 uint32_t index,
748 v8::AccessType type);
749
750 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751 void ReportFailedAccessCheck(Handle<JSObject> receiver, v8::AccessType type);
Steve Block44f0eee2011-05-26 01:26:41 +0100752
753 // Exception throwing support. The caller should use the result
754 // of Throw() as its return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 Object* Throw(Object* exception, MessageLocation* location = NULL);
756
757 template <typename T>
758 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
759 MessageLocation* location = NULL) {
760 Throw(*exception, location);
761 return MaybeHandle<T>();
762 }
763
Steve Block44f0eee2011-05-26 01:26:41 +0100764 // Re-throw an exception. This involves no error reporting since
765 // error reporting was handled when the exception was thrown
766 // originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000767 Object* ReThrow(Object* exception);
Steve Block44f0eee2011-05-26 01:26:41 +0100768 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 // Re-set pending message, script and positions reported to the TryCatch
770 // back to the TLS for re-use when rethrowing.
771 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
772 // Un-schedule an exception that was caught by a TryCatch handler.
773 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100774 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 // Return pending location if any or unfilled structure.
776 MessageLocation GetMessageLocation();
777 Object* ThrowIllegalOperation();
Steve Block44f0eee2011-05-26 01:26:41 +0100778
779 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 Object* PromoteScheduledException();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100781 void DoThrow(Object* exception, MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100782 // Checks if exception should be reported and finds out if it's
783 // caught externally.
784 bool ShouldReportException(bool* can_be_caught_externally,
785 bool catchable_by_javascript);
786
787 // Attempts to compute the current source location, storing the
788 // result in the target out parameter.
789 void ComputeLocation(MessageLocation* target);
790
Steve Block44f0eee2011-05-26 01:26:41 +0100791 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792 Object* StackOverflow();
793 Object* TerminateExecution();
794 void CancelTerminateExecution();
795
796 void InvokeApiInterruptCallback();
Steve Block44f0eee2011-05-26 01:26:41 +0100797
798 // Administration
799 void Iterate(ObjectVisitor* v);
800 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
801 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100802 void IterateThread(ThreadVisitor* v, char* t);
803
804
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 // Returns the current native and global context.
806 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100807 Handle<Context> global_context();
808
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 // Returns the native context of the calling JavaScript code. That
810 // is, the native context of the top-most JavaScript frame.
811 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100812
813 void RegisterTryCatchHandler(v8::TryCatch* that);
814 void UnregisterTryCatchHandler(v8::TryCatch* that);
815
816 char* ArchiveThread(char* to);
817 char* RestoreThread(char* from);
818
819 static const char* const kStackOverflowMessage;
820
821 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
822 static const int kBMMaxShift = 250; // See StringSearchBase.
823
824 // Accessors.
825#define GLOBAL_ACCESSOR(type, name, initialvalue) \
826 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100828 return name##_; \
829 } \
830 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100832 name##_ = value; \
833 }
834 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
835#undef GLOBAL_ACCESSOR
836
837#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
838 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000839 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100840 return &(name##_)[0]; \
841 }
842 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
843#undef GLOBAL_ARRAY_ACCESSOR
844
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000845#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
846 Handle<type> name() { \
847 return Handle<type>(native_context()->name(), this); \
848 } \
849 bool is_##name(type* value) { \
850 return native_context()->is_##name(value); \
Steve Block44f0eee2011-05-26 01:26:41 +0100851 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
853#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100854
855 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000856 Counters* counters() {
857 // Call InitializeLoggingAndCounters() if logging is needed before
858 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000860 return counters_;
861 }
Steve Block44f0eee2011-05-26 01:26:41 +0100862 CodeRange* code_range() { return code_range_; }
863 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
864 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000865 Logger* logger() {
866 // Call InitializeLoggingAndCounters() if logging is needed before
867 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000869 return logger_;
870 }
Steve Block44f0eee2011-05-26 01:26:41 +0100871 StackGuard* stack_guard() { return &stack_guard_; }
872 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000873 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100874 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100876 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
877 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878 MaterializedObjectStore* materialized_object_store() {
879 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100880 }
881
882 MemoryAllocator* memory_allocator() {
883 return memory_allocator_;
884 }
885
886 KeyedLookupCache* keyed_lookup_cache() {
887 return keyed_lookup_cache_;
888 }
889
890 ContextSlotCache* context_slot_cache() {
891 return context_slot_cache_;
892 }
893
894 DescriptorLookupCache* descriptor_lookup_cache() {
895 return descriptor_lookup_cache_;
896 }
897
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
899
Steve Block44f0eee2011-05-26 01:26:41 +0100900 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100902 return handle_scope_implementer_;
903 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000904 Zone* runtime_zone() { return &runtime_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100905
Ben Murdoch8b112d22011-06-08 16:22:53 +0100906 UnicodeCache* unicode_cache() {
907 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100908 }
909
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100910 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
911 return inner_pointer_to_code_cache_;
912 }
Steve Block44f0eee2011-05-26 01:26:41 +0100913
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 ConsStringIteratorOp* write_iterator() { return write_iterator_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100915
916 GlobalHandles* global_handles() { return global_handles_; }
917
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918 EternalHandles* eternal_handles() { return eternal_handles_; }
919
Steve Block44f0eee2011-05-26 01:26:41 +0100920 ThreadManager* thread_manager() { return thread_manager_; }
921
Steve Block44f0eee2011-05-26 01:26:41 +0100922 StringTracker* string_tracker() { return string_tracker_; }
923
924 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
925 return &jsregexp_uncanonicalize_;
926 }
927
928 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
929 return &jsregexp_canonrange_;
930 }
931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 ConsStringIteratorOp* objects_string_compare_iterator_a() {
933 return &objects_string_compare_iterator_a_;
Steve Block44f0eee2011-05-26 01:26:41 +0100934 }
935
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 ConsStringIteratorOp* objects_string_compare_iterator_b() {
937 return &objects_string_compare_iterator_b_;
Steve Block44f0eee2011-05-26 01:26:41 +0100938 }
939
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 StaticResource<ConsStringIteratorOp>* objects_string_iterator() {
941 return &objects_string_iterator_;
Steve Block44f0eee2011-05-26 01:26:41 +0100942 }
943
Steve Block44f0eee2011-05-26 01:26:41 +0100944 RuntimeState* runtime_state() { return &runtime_state_; }
945
Steve Block44f0eee2011-05-26 01:26:41 +0100946 Builtins* builtins() { return &builtins_; }
947
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100948 void NotifyExtensionInstalled() {
949 has_installed_extensions_ = true;
950 }
951
952 bool has_installed_extensions() { return has_installed_extensions_; }
953
Steve Block44f0eee2011-05-26 01:26:41 +0100954 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
955 regexp_macro_assembler_canonicalize() {
956 return &regexp_macro_assembler_canonicalize_;
957 }
958
959 RegExpStack* regexp_stack() { return regexp_stack_; }
960
961 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
962 interp_canonicalize_mapping() {
963 return &interp_canonicalize_mapping_;
964 }
965
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000966 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100967
Ben Murdoch257744e2011-11-30 15:57:28 +0000968 inline bool DebuggerHasBreakPoints();
969
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
971 HeapProfiler* heap_profiler() const { return heap_profiler_; }
972
Steve Block44f0eee2011-05-26 01:26:41 +0100973#ifdef DEBUG
974 HistogramInfo* heap_histograms() { return heap_histograms_; }
975
976 JSObject::SpillInformation* js_spill_information() {
977 return &js_spill_information_;
978 }
Steve Block44f0eee2011-05-26 01:26:41 +0100979#endif
980
981 Factory* factory() { return reinterpret_cast<Factory*>(this); }
982
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000983 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100984
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100986
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000987 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
988
989 void SetData(uint32_t slot, void* data) {
990 DCHECK(slot < Internals::kNumIsolateDataSlots);
991 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100992 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993 void* GetData(uint32_t slot) {
994 DCHECK(slot < Internals::kNumIsolateDataSlots);
995 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100996 }
Steve Block44f0eee2011-05-26 01:26:41 +0100997
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998 THREAD_LOCAL_TOP_ACCESSOR(LookupResult*, top_lookup_result)
999
1000 void enable_serializer() {
1001 // The serializer can only be enabled before the isolate init.
1002 DCHECK(state_ != INITIALIZED);
1003 serializer_enabled_ = true;
Steve Block44f0eee2011-05-26 01:26:41 +01001004 }
1005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 bool serializer_enabled() const { return serializer_enabled_; }
Steve Block44f0eee2011-05-26 01:26:41 +01001007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008 bool IsDead() { return has_fatal_error_; }
1009 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +00001010
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001012
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001014
1015 double time_millis_since_init() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001016 return base::OS::TimeCurrentMillis() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001017 }
1018
1019 DateCache* date_cache() {
1020 return date_cache_;
1021 }
1022
1023 void set_date_cache(DateCache* date_cache) {
1024 if (date_cache != date_cache_) {
1025 delete date_cache_;
1026 }
1027 date_cache_ = date_cache;
1028 }
1029
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 Map* get_initial_js_array_map(ElementsKind kind);
1031
1032 bool IsFastArrayConstructorPrototypeChainIntact();
1033
1034 CallInterfaceDescriptorData* call_descriptor_data(int index);
1035
1036 void IterateDeferredHandles(ObjectVisitor* visitor);
1037 void LinkDeferredHandles(DeferredHandles* deferred_handles);
1038 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
1039
1040#ifdef DEBUG
1041 bool IsDeferredHandle(Object** location);
1042#endif // DEBUG
1043
1044 bool concurrent_recompilation_enabled() {
1045 // Thread is only available with flag enabled.
1046 DCHECK(optimizing_compiler_thread_ == NULL ||
1047 FLAG_concurrent_recompilation);
1048 return optimizing_compiler_thread_ != NULL;
1049 }
1050
1051 bool concurrent_osr_enabled() const {
1052 // Thread is only available with flag enabled.
1053 DCHECK(optimizing_compiler_thread_ == NULL ||
1054 FLAG_concurrent_recompilation);
1055 return optimizing_compiler_thread_ != NULL && FLAG_concurrent_osr;
1056 }
1057
1058 OptimizingCompilerThread* optimizing_compiler_thread() {
1059 return optimizing_compiler_thread_;
1060 }
1061
1062 int num_sweeper_threads() const {
1063 return num_sweeper_threads_;
1064 }
1065
1066 SweeperThread** sweeper_threads() {
1067 return sweeper_thread_;
1068 }
1069
1070 int id() const { return static_cast<int>(id_); }
1071
1072 HStatistics* GetHStatistics();
1073 HStatistics* GetTStatistics();
1074 HTracer* GetHTracer();
1075 CodeTracer* GetCodeTracer();
1076
1077 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1078 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1079 function_entry_hook_ = function_entry_hook;
1080 }
1081
1082 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1083
1084 inline base::RandomNumberGenerator* random_number_generator();
1085
1086 // Given an address occupied by a live code object, return that object.
1087 Object* FindCodeObject(Address a);
1088
1089 int NextOptimizationId() {
1090 int id = next_optimization_id_++;
1091 if (!Smi::IsValid(next_optimization_id_)) {
1092 next_optimization_id_ = 0;
1093 }
1094 return id;
1095 }
1096
1097 // Get (and lazily initialize) the registry for per-isolate symbols.
1098 Handle<JSObject> GetSymbolRegistry();
1099
1100 void AddCallCompletedCallback(CallCompletedCallback callback);
1101 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1102 void FireCallCompletedCallback();
1103
1104 void EnqueueMicrotask(Handle<Object> microtask);
1105 void RunMicrotasks();
1106
1107 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1108 void CountUsage(v8::Isolate::UseCounterFeature feature);
1109
1110 static Isolate* NewForTesting() { return new Isolate(); }
1111
Steve Block44f0eee2011-05-26 01:26:41 +01001112 private:
1113 Isolate();
1114
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001115 friend struct GlobalState;
1116 friend struct InitializeGlobalState;
1117
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001118 enum State {
1119 UNINITIALIZED, // Some components may not have been allocated.
1120 INITIALIZED // All components are fully initialized.
1121 };
1122
1123 // These fields are accessed through the API, offsets must be kept in sync
1124 // with v8::internal::Internals (in include/v8.h) constants. This is also
1125 // verified in Isolate::Init() using runtime checks.
1126 void* embedder_data_[Internals::kNumIsolateDataSlots];
1127 Heap heap_;
1128 State state_; // Will be padded to kApiPointerSize.
1129
Steve Block44f0eee2011-05-26 01:26:41 +01001130 // The per-process lock should be acquired before the ThreadDataTable is
1131 // modified.
1132 class ThreadDataTable {
1133 public:
1134 ThreadDataTable();
1135 ~ThreadDataTable();
1136
1137 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1138 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001139 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001140 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001141
1142 private:
1143 PerIsolateThreadData* list_;
1144 };
1145
1146 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1147 // the Isolate. The top of the stack points to a thread which is currently
1148 // running the Isolate. When the stack is empty, the Isolate is considered
1149 // not entered by any thread and can be Disposed.
1150 // If the same thread enters the Isolate more then once, the entry_count_
1151 // is incremented rather then a new item pushed to the stack.
1152 class EntryStackItem {
1153 public:
1154 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1155 Isolate* previous_isolate,
1156 EntryStackItem* previous_item)
1157 : entry_count(1),
1158 previous_thread_data(previous_thread_data),
1159 previous_isolate(previous_isolate),
1160 previous_item(previous_item) { }
1161
1162 int entry_count;
1163 PerIsolateThreadData* previous_thread_data;
1164 Isolate* previous_isolate;
1165 EntryStackItem* previous_item;
1166
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001167 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001168 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1169 };
1170
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001171 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001172
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001173 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1174 static base::Thread::LocalStorageKey isolate_key_;
1175 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001176 static ThreadDataTable* thread_data_table_;
1177
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001178 // A global counter for all generated Isolates, might overflow.
1179 static base::Atomic32 isolate_counter_;
1180
Steve Block44f0eee2011-05-26 01:26:41 +01001181 void Deinit();
1182
1183 static void SetIsolateThreadLocals(Isolate* isolate,
1184 PerIsolateThreadData* data);
1185
Steve Block44f0eee2011-05-26 01:26:41 +01001186 // Find the PerThread for this particular (isolate, thread) combination.
1187 // If one does not yet exist, allocate a new one.
1188 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1189
Steve Block44f0eee2011-05-26 01:26:41 +01001190 // Initializes the current thread to run this Isolate.
1191 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1192 // at the same time, this should be prevented using external locking.
1193 void Enter();
1194
1195 // Exits the current thread. The previosuly entered Isolate is restored
1196 // for the thread.
1197 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1198 // at the same time, this should be prevented using external locking.
1199 void Exit();
1200
Steve Block44f0eee2011-05-26 01:26:41 +01001201 void InitializeThreadLocal();
1202
Steve Block44f0eee2011-05-26 01:26:41 +01001203 void MarkCompactPrologue(bool is_compacting,
1204 ThreadLocalTop* archived_thread_data);
1205 void MarkCompactEpilogue(bool is_compacting,
1206 ThreadLocalTop* archived_thread_data);
1207
1208 void FillCache();
1209
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 // Propagate pending exception message to the v8::TryCatch.
1211 // If there is no external try-catch or message was successfully propagated,
1212 // then return true.
1213 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001214
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001215 // Traverse prototype chain to find out whether the object is derived from
1216 // the Error object.
1217 bool IsErrorObject(Handle<Object> obj);
1218
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 base::Atomic32 id_;
1220 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001221 int stack_trace_nesting_level_;
1222 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001223 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001224 Bootstrapper* bootstrapper_;
1225 RuntimeProfiler* runtime_profiler_;
1226 CompilationCache* compilation_cache_;
1227 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001228 CodeRange* code_range_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001229 base::RecursiveMutex break_access_;
1230 base::Atomic32 debugger_initialized_;
Steve Block44f0eee2011-05-26 01:26:41 +01001231 Logger* logger_;
1232 StackGuard stack_guard_;
1233 StatsTable* stats_table_;
1234 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001235 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001236 DeoptimizerData* deoptimizer_data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001237 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001238 ThreadLocalTop thread_local_top_;
1239 bool capture_stack_trace_for_uncaught_exceptions_;
1240 int stack_trace_for_uncaught_exceptions_frame_limit_;
1241 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001242 MemoryAllocator* memory_allocator_;
1243 KeyedLookupCache* keyed_lookup_cache_;
1244 ContextSlotCache* context_slot_cache_;
1245 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001247 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001248 UnicodeCache* unicode_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 Zone runtime_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001250 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001251 ConsStringIteratorOp* write_iterator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001252 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001253 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001254 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001255 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001256 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001257 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001258 StringTracker* string_tracker_;
1259 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1260 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 ConsStringIteratorOp objects_string_compare_iterator_a_;
1262 ConsStringIteratorOp objects_string_compare_iterator_b_;
1263 StaticResource<ConsStringIteratorOp> objects_string_iterator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001264 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1265 regexp_macro_assembler_canonicalize_;
1266 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001267 DateCache* date_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001268 unibrow::Mapping<unibrow::Ecma262Canonicalize> interp_canonicalize_mapping_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001269 CallInterfaceDescriptorData* call_descriptor_data_;
1270 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001271
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272 // Whether the isolate has been created for snapshotting.
1273 bool serializer_enabled_;
1274
1275 // True if fatal error has been signaled for this isolate.
1276 bool has_fatal_error_;
1277
1278 // True if this isolate was initialized from a snapshot.
1279 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001280
1281 // Time stamp at initialization.
1282 double time_millis_at_init_;
1283
Steve Block44f0eee2011-05-26 01:26:41 +01001284#ifdef DEBUG
1285 // A static array of histogram info for each type.
1286 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1287 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001288#endif
1289
Steve Block44f0eee2011-05-26 01:26:41 +01001290 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001291 CpuProfiler* cpu_profiler_;
1292 HeapProfiler* heap_profiler_;
1293 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001294
Steve Block44f0eee2011-05-26 01:26:41 +01001295#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1296 type name##_;
1297 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1298#undef GLOBAL_BACKING_STORE
1299
1300#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1301 type name##_[length];
1302 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1303#undef GLOBAL_ARRAY_BACKING_STORE
1304
1305#ifdef DEBUG
1306 // This class is huge and has a number of fields controlled by
1307 // preprocessor defines. Make sure the offsets of these fields agree
1308 // between compilation units.
1309#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1310 static const intptr_t name##_debug_offset_;
1311 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1312 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1313#undef ISOLATE_FIELD_OFFSET
1314#endif
1315
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001316 DeferredHandles* deferred_handles_head_;
1317 OptimizingCompilerThread* optimizing_compiler_thread_;
1318 SweeperThread** sweeper_thread_;
1319 int num_sweeper_threads_;
1320
1321 // Counts deopt points if deopt_every_n_times is enabled.
1322 unsigned int stress_deopt_count_;
1323
1324 int next_optimization_id_;
1325
1326 // List of callbacks when a Call completes.
1327 List<CallCompletedCallback> call_completed_callbacks_;
1328
1329 v8::Isolate::UseCounterCallback use_counter_callback_;
1330
Steve Block44f0eee2011-05-26 01:26:41 +01001331 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 friend class HandleScopeImplementer;
Steve Block44f0eee2011-05-26 01:26:41 +01001333 friend class IsolateInitializer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 friend class OptimizingCompilerThread;
1335 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001336 friend class ThreadManager;
1337 friend class Simulator;
1338 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001339 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001340 friend class TestMemoryAllocatorScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 friend class TestCodeRangeScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001342 friend class v8::Isolate;
1343 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001344 friend class v8::Unlocker;
Steve Block44f0eee2011-05-26 01:26:41 +01001345
1346 DISALLOW_COPY_AND_ASSIGN(Isolate);
1347};
1348
1349
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001350#undef FIELD_ACCESSOR
1351#undef THREAD_LOCAL_TOP_ACCESSOR
1352
1353
1354class PromiseOnStack {
1355 public:
1356 PromiseOnStack(StackHandler* handler, Handle<JSObject> promise,
1357 PromiseOnStack* prev)
1358 : handler_(handler), promise_(promise), prev_(prev) {}
1359 StackHandler* handler() { return handler_; }
1360 Handle<JSObject> promise() { return promise_; }
1361 PromiseOnStack* prev() { return prev_; }
1362
1363 private:
1364 StackHandler* handler_;
1365 Handle<JSObject> promise_;
1366 PromiseOnStack* prev_;
1367};
1368
1369
Steve Block44f0eee2011-05-26 01:26:41 +01001370// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1371// class as a work around for a bug in the generated code found with these
1372// versions of GCC. See V8 issue 122 for details.
1373class SaveContext BASE_EMBEDDED {
1374 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001375 inline explicit SaveContext(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001376
1377 ~SaveContext() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378 isolate_->set_context(context_.is_null() ? NULL : *context_);
1379 isolate_->set_save_context(prev_);
Steve Block44f0eee2011-05-26 01:26:41 +01001380 }
1381
1382 Handle<Context> context() { return context_; }
1383 SaveContext* prev() { return prev_; }
1384
1385 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001386 bool IsBelowFrame(JavaScriptFrame* frame) {
1387 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001388 }
1389
1390 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001392 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001393 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001394 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001395};
1396
1397
1398class AssertNoContextChange BASE_EMBEDDED {
1399#ifdef DEBUG
1400 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001401 explicit AssertNoContextChange(Isolate* isolate)
1402 : isolate_(isolate),
1403 context_(isolate->context(), isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001404 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001406 }
1407
1408 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001409 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001410 Handle<Context> context_;
1411#else
1412 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001414#endif
1415};
1416
1417
1418class ExecutionAccess BASE_EMBEDDED {
1419 public:
1420 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1421 Lock(isolate);
1422 }
1423 ~ExecutionAccess() { Unlock(isolate_); }
1424
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001425 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1426 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001427
1428 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001429 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001430 }
1431
1432 private:
1433 Isolate* isolate_;
1434};
1435
1436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001438class StackLimitCheck BASE_EMBEDDED {
1439 public:
1440 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1441
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001442 // Use this to check for stack-overflows in C++ code.
1443 inline bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001444 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001446 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447
1448 // Use this to check for stack-overflow when entering runtime from JS code.
1449 bool JsHasOverflowed() const;
1450
Steve Block44f0eee2011-05-26 01:26:41 +01001451 private:
1452 Isolate* isolate_;
1453};
1454
1455
1456// Support for temporarily postponing interrupts. When the outermost
1457// postpone scope is left the interrupts will be re-enabled and any
1458// interrupts that occurred while in the scope will be taken into
1459// account.
1460class PostponeInterruptsScope BASE_EMBEDDED {
1461 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001462 PostponeInterruptsScope(Isolate* isolate,
1463 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1464 : stack_guard_(isolate->stack_guard()),
1465 intercept_mask_(intercept_mask),
1466 intercepted_flags_(0) {
1467 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001468 }
1469
1470 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001471 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001472 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001473
1474 // Find the bottom-most scope that intercepts this interrupt.
1475 // Return whether the interrupt has been intercepted.
1476 bool Intercept(StackGuard::InterruptFlag flag);
1477
Steve Block44f0eee2011-05-26 01:26:41 +01001478 private:
1479 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001480 int intercept_mask_;
1481 int intercepted_flags_;
1482 PostponeInterruptsScope* prev_;
1483
1484 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001485};
1486
1487
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488class CodeTracer FINAL : public Malloced {
1489 public:
1490 explicit CodeTracer(int isolate_id)
1491 : file_(NULL),
1492 scope_depth_(0) {
1493 if (!ShouldRedirect()) {
1494 file_ = stdout;
1495 return;
1496 }
Steve Block44f0eee2011-05-26 01:26:41 +01001497
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498 if (FLAG_redirect_code_traces_to == NULL) {
1499 SNPrintF(filename_,
1500 "code-%d-%d.asm",
1501 base::OS::GetCurrentProcessId(),
1502 isolate_id);
1503 } else {
1504 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1505 }
Steve Block44f0eee2011-05-26 01:26:41 +01001506
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001507 WriteChars(filename_.start(), "", 0, false);
1508 }
Steve Block44f0eee2011-05-26 01:26:41 +01001509
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001510 class Scope {
1511 public:
1512 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1513 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001516
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001517 private:
1518 CodeTracer* tracer_;
1519 };
1520
1521 void OpenFile() {
1522 if (!ShouldRedirect()) {
1523 return;
1524 }
1525
1526 if (file_ == NULL) {
1527 file_ = base::OS::FOpen(filename_.start(), "a");
1528 }
1529
1530 scope_depth_++;
1531 }
1532
1533 void CloseFile() {
1534 if (!ShouldRedirect()) {
1535 return;
1536 }
1537
1538 if (--scope_depth_ == 0) {
1539 fclose(file_);
1540 file_ = NULL;
1541 }
1542 }
1543
1544 FILE* file() const { return file_; }
1545
1546 private:
1547 static bool ShouldRedirect() {
1548 return FLAG_redirect_code_traces;
1549 }
1550
1551 EmbeddedVector<char, 128> filename_;
1552 FILE* file_;
1553 int scope_depth_;
1554};
Steve Block44f0eee2011-05-26 01:26:41 +01001555
Steve Block44f0eee2011-05-26 01:26:41 +01001556} } // namespace v8::internal
1557
Steve Block44f0eee2011-05-26 01:26:41 +01001558#endif // V8_ISOLATE_H_