blob: 42a814ad729ecd7cda4bf34974ba01e0eafa5ed0 [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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04008#include <queue>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "include/v8-debug.h"
10#include "src/allocation.h"
11#include "src/assert-scope.h"
12#include "src/base/atomicops.h"
13#include "src/builtins.h"
14#include "src/contexts.h"
15#include "src/date.h"
16#include "src/execution.h"
17#include "src/frames.h"
18#include "src/global-handles.h"
19#include "src/handles.h"
20#include "src/hashmap.h"
21#include "src/heap/heap.h"
22#include "src/optimizing-compiler-thread.h"
23#include "src/regexp-stack.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040024#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025#include "src/runtime-profiler.h"
26#include "src/zone.h"
Steve Block44f0eee2011-05-26 01:26:41 +010027
28namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029
30namespace base {
31class RandomNumberGenerator;
32}
33
Steve Block44f0eee2011-05-26 01:26:41 +010034namespace internal {
35
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036class BasicBlockProfiler;
Steve Block44f0eee2011-05-26 01:26:41 +010037class Bootstrapper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038class CallInterfaceDescriptorData;
Steve Block44f0eee2011-05-26 01:26:41 +010039class CodeGenerator;
40class CodeRange;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041class CodeStubDescriptor;
42class CodeTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010043class CompilationCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040044class CompilationStatistics;
Steve Block44f0eee2011-05-26 01:26:41 +010045class ContextSlotCache;
Steve Block44f0eee2011-05-26 01:26:41 +010046class Counters;
47class CpuFeatures;
48class CpuProfiler;
49class DeoptimizerData;
50class Deserializer;
51class EmptyStatement;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052class ExternalCallbackScope;
Steve Block44f0eee2011-05-26 01:26:41 +010053class ExternalReferenceTable;
54class Factory;
55class FunctionInfoListener;
56class HandleScopeImplementer;
57class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058class HStatistics;
59class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010060class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010061class InnerPointerToCodeCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000062class MaterializedObjectStore;
63class CodeAgingHelper;
Steve Block44f0eee2011-05-26 01:26:41 +010064class RegExpStack;
65class SaveContext;
Steve Block44f0eee2011-05-26 01:26:41 +010066class StringTracker;
67class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010069class ThreadManager;
70class ThreadState;
71class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072class UnicodeCache;
73template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010074
75// 'void function pointer', used to roundtrip the
76// ExternalReference::ExternalReferenceRedirector since we can not include
77// assembler.h, where it is defined, here.
78typedef void* ExternalReferenceRedirectorPointer();
79
80
Steve Block44f0eee2011-05-26 01:26:41 +010081class Debug;
82class Debugger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010084
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
86 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
87 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
88 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +010089class Redirection;
90class Simulator;
91#endif
92
93
94// Static indirection table for handles to constants. If a frame
95// element represents a constant, the data contains an index into
96// this table of handles to the actual constants.
97// Static indirection table for handles to constants. If a Result
98// represents a constant, the data contains an index into this table
99// of handles to the actual constants.
100typedef ZoneList<Handle<Object> > ZoneObjectList;
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100103 do { \
104 Isolate* __isolate__ = (isolate); \
105 if (__isolate__->has_scheduled_exception()) { \
106 return __isolate__->PromoteScheduledException(); \
107 } \
108 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100109
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110// Macros for MaybeHandle.
111
112#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
113 do { \
114 Isolate* __isolate__ = (isolate); \
115 if (__isolate__->has_scheduled_exception()) { \
116 __isolate__->PromoteScheduledException(); \
117 return value; \
118 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100119 } while (false)
120
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
122 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
123
124#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
125 do { \
126 if (!(call).ToHandle(&dst)) { \
127 DCHECK((isolate)->has_pending_exception()); \
128 return value; \
129 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100130 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100131
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
133 ASSIGN_RETURN_ON_EXCEPTION_VALUE( \
134 isolate, dst, call, isolate->heap()->exception())
135
136#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
137 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
138
139#define THROW_NEW_ERROR(isolate, call, T) \
140 do { \
141 Handle<Object> __error__; \
142 ASSIGN_RETURN_ON_EXCEPTION(isolate, __error__, isolate->factory()->call, \
143 T); \
144 return isolate->Throw<T>(__error__); \
145 } while (false)
146
147#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
148 do { \
149 Handle<Object> __error__; \
150 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, __error__, \
151 isolate->factory()->call); \
152 return isolate->Throw(*__error__); \
153 } while (false)
154
155#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
156 do { \
157 if ((call).is_null()) { \
158 DCHECK((isolate)->has_pending_exception()); \
159 return value; \
160 } \
161 } while (false)
162
163#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
164 RETURN_ON_EXCEPTION_VALUE(isolate, call, isolate->heap()->exception())
165
166#define RETURN_ON_EXCEPTION(isolate, call, T) \
167 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
168
Steve Block44f0eee2011-05-26 01:26:41 +0100169
Ben Murdoch589d6972011-11-30 16:04:58 +0000170#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
171 C(Handler, handler) \
172 C(CEntryFP, c_entry_fp) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400173 C(CFunction, c_function) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000174 C(Context, context) \
175 C(PendingException, pending_exception) \
176 C(ExternalCaughtException, external_caught_exception) \
177 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100178
179
Ben Murdoch8b112d22011-06-08 16:22:53 +0100180// Platform-independent, reliable thread identifier.
181class ThreadId {
182 public:
183 // Creates an invalid ThreadId.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400184 ThreadId() { base::NoBarrier_Store(&id_, kInvalidId); }
185
186 ThreadId& operator=(const ThreadId& other) {
187 base::NoBarrier_Store(&id_, base::NoBarrier_Load(&other.id_));
188 return *this;
189 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100190
191 // Returns ThreadId for current thread.
192 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
193
194 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
195 static ThreadId Invalid() { return ThreadId(kInvalidId); }
196
197 // Compares ThreadIds for equality.
198 INLINE(bool Equals(const ThreadId& other) const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400199 return base::NoBarrier_Load(&id_) == base::NoBarrier_Load(&other.id_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100200 }
201
202 // Checks whether this ThreadId refers to any thread.
203 INLINE(bool IsValid() const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400204 return base::NoBarrier_Load(&id_) != kInvalidId;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100205 }
206
207 // Converts ThreadId to an integer representation
208 // (required for public API: V8::V8::GetCurrentThreadId).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400209 int ToInteger() const { return static_cast<int>(base::NoBarrier_Load(&id_)); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100210
211 // Converts ThreadId to an integer representation
212 // (required for public API: V8::V8::TerminateExecution).
213 static ThreadId FromInteger(int id) { return ThreadId(id); }
214
215 private:
216 static const int kInvalidId = -1;
217
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400218 explicit ThreadId(int id) { base::NoBarrier_Store(&id_, id); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100219
220 static int AllocateThreadId();
221
222 static int GetCurrentThreadId();
223
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400224 base::Atomic32 id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100225
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100227
228 friend class Isolate;
229};
230
231
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000232#define FIELD_ACCESSOR(type, name) \
233 inline void set_##name(type v) { name##_ = v; } \
234 inline type name() const { return name##_; }
235
236
Steve Block44f0eee2011-05-26 01:26:41 +0100237class ThreadLocalTop BASE_EMBEDDED {
238 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239 // Does early low-level initialization that does not depend on the
240 // isolate being present.
241 ThreadLocalTop();
242
Steve Block44f0eee2011-05-26 01:26:41 +0100243 // Initialize the thread data.
244 void Initialize();
245
246 // Get the top C++ try catch handler or NULL if none are registered.
247 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100249 // used for comparison with addresses into the JS stack. If such an
250 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100252
253 // Get the address of the top C++ try catch handler or NULL if
254 // none are registered.
255 //
256 // This method always returns an address that can be compared to
257 // pointers into the JavaScript stack. When running on actual
258 // hardware, try_catch_handler_address and TryCatchHandler return
259 // the same pointer. When running on a simulator with a separate JS
260 // stack, try_catch_handler_address returns a JS stack address that
261 // corresponds to the place on the JS stack where the C++ handler
262 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 Address try_catch_handler_address() {
264 return reinterpret_cast<Address>(
265 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100266 }
267
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100269
Ben Murdoch257744e2011-11-30 15:57:28 +0000270 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100271 // The context where the current execution method is created and for variable
272 // lookups.
273 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100274 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 Object* pending_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100276 bool has_pending_message_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100278 Object* pending_message_obj_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 Object* pending_message_script_;
Steve Block44f0eee2011-05-26 01:26:41 +0100280 int pending_message_start_pos_;
281 int pending_message_end_pos_;
282 // Use a separate value for scheduled exceptions to preserve the
283 // invariants that hold about pending_exception. We may want to
284 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100286 bool external_caught_exception_;
287 SaveContext* save_context_;
288 v8::TryCatch* catcher_;
289
290 // Stack.
291 Address c_entry_fp_; // the frame pointer of the top c entry frame
292 Address handler_; // try-blocks are chained through the stack
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400293 Address c_function_; // C function that was called at c entry.
Steve Block44f0eee2011-05-26 01:26:41 +0100294
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295 // Throwing an exception may cause a Promise rejection. For this purpose
296 // we keep track of a stack of nested promises and the corresponding
297 // try-catch handlers.
298 PromiseOnStack* promise_on_stack_;
299
Steve Block44f0eee2011-05-26 01:26:41 +0100300#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100301 Simulator* simulator_;
302#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100303
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100304 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000305 // the external callback we're currently in
306 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100307 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100308
309 // Generated code scratch locations.
310 int32_t formal_count_;
311
312 // Call back function to report unsafe JS accesses.
313 v8::FailedAccessCheckCallback failed_access_check_callback_;
314
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100315 // Head of the list of live LookupResults.
316 LookupResult* top_lookup_result_;
317
Steve Block44f0eee2011-05-26 01:26:41 +0100318 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100319 void InitializeInternal();
320
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000321 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100322};
323
Steve Block44f0eee2011-05-26 01:26:41 +0100324
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325#if V8_TARGET_ARCH_ARM && !defined(__arm__) || \
326 V8_TARGET_ARCH_ARM64 && !defined(__aarch64__) || \
327 V8_TARGET_ARCH_MIPS && !defined(__mips__) || \
328 V8_TARGET_ARCH_MIPS64 && !defined(__mips__)
Steve Block44f0eee2011-05-26 01:26:41 +0100329
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330#define ISOLATE_INIT_SIMULATOR_LIST(V) \
331 V(bool, simulator_initialized, false) \
332 V(HashMap*, simulator_i_cache, NULL) \
333 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100334#else
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100337
338#endif
339
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340
Steve Block44f0eee2011-05-26 01:26:41 +0100341#ifdef DEBUG
342
343#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
344 V(CommentStatistic, paged_space_comments_statistics, \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 CommentStatistic::kMaxComments + 1) \
346 V(int, code_kind_statistics, Code::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100347#else
348
349#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
350
351#endif
352
Steve Block44f0eee2011-05-26 01:26:41 +0100353#define ISOLATE_INIT_ARRAY_LIST(V) \
354 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100356 V(int, bad_char_shift_table, kUC16AlphabetSize) \
357 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
358 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000359 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100360 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
361
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100363
364#define ISOLATE_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100365 /* SerializerDeserializer state. */ \
366 V(int, serialize_partial_snapshot_cache_length, 0) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 V(int, serialize_partial_snapshot_cache_capacity, 0) \
368 V(Object**, serialize_partial_snapshot_cache, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100369 /* Assembler state. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100370 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 V(LogEventCallback, event_logger, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000372 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100373 /* To distinguish the function templates, so that we can find them in the */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374 /* function cache of the native context. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100375 V(int, next_serial_number, 0) \
376 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100377 /* Part of the state of liveedit. */ \
378 V(FunctionInfoListener*, active_function_info_listener, NULL) \
379 /* State for Relocatable. */ \
380 V(Relocatable*, relocatable_top, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100381 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
382 V(Object*, string_stream_current_security_token, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100383 /* Serializer state. */ \
384 V(ExternalReferenceTable*, external_reference_table, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000385 V(int, pending_microtask_count, 0) \
386 V(bool, autorun_microtasks, true) \
387 V(HStatistics*, hstatistics, NULL) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400388 V(CompilationStatistics*, turbo_statistics, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 V(HTracer*, htracer, NULL) \
390 V(CodeTracer*, code_tracer, NULL) \
391 V(bool, fp_stubs_generated, false) \
392 V(int, max_available_threads, 0) \
393 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400394 V(PromiseRejectCallback, promise_reject_callback, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 ISOLATE_INIT_SIMULATOR_LIST(V)
396
397#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
398 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
399 inline type name() const { return thread_local_top_.name##_; }
400
Steve Block44f0eee2011-05-26 01:26:41 +0100401
402class Isolate {
403 // These forward declarations are required to make the friend declarations in
404 // PerIsolateThreadData work on some older versions of gcc.
405 class ThreadDataTable;
406 class EntryStackItem;
407 public:
408 ~Isolate();
409
Steve Block44f0eee2011-05-26 01:26:41 +0100410 // A thread has a PerIsolateThreadData instance for each isolate that it has
411 // entered. That instance is allocated when the isolate is initially entered
412 // and reused on subsequent entries.
413 class PerIsolateThreadData {
414 public:
415 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
416 : isolate_(isolate),
417 thread_id_(thread_id),
418 stack_limit_(0),
419 thread_state_(NULL),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
421 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
422 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
423 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +0100424 simulator_(NULL),
425#endif
426 next_(NULL),
427 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100429 Isolate* isolate() const { return isolate_; }
430 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100431
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432 FIELD_ACCESSOR(uintptr_t, stack_limit)
433 FIELD_ACCESSOR(ThreadState*, thread_state)
434
435#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
436 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
437 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
438 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
439 FIELD_ACCESSOR(Simulator*, simulator)
Steve Block44f0eee2011-05-26 01:26:41 +0100440#endif
441
442 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100443 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100444 }
445
446 private:
447 Isolate* isolate_;
448 ThreadId thread_id_;
449 uintptr_t stack_limit_;
450 ThreadState* thread_state_;
451
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452#if !defined(__arm__) && V8_TARGET_ARCH_ARM || \
453 !defined(__aarch64__) && V8_TARGET_ARCH_ARM64 || \
454 !defined(__mips__) && V8_TARGET_ARCH_MIPS || \
455 !defined(__mips__) && V8_TARGET_ARCH_MIPS64
Steve Block44f0eee2011-05-26 01:26:41 +0100456 Simulator* simulator_;
457#endif
458
459 PerIsolateThreadData* next_;
460 PerIsolateThreadData* prev_;
461
462 friend class Isolate;
463 friend class ThreadDataTable;
464 friend class EntryStackItem;
465
466 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
467 };
468
469
470 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000471#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
472 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000474 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100475 };
476
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 static void InitializeOncePerProcess();
478
Steve Block44f0eee2011-05-26 01:26:41 +0100479 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
480 // not currently set).
481 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
482 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100484 }
485
486 // Returns the isolate inside which the current thread is running.
487 INLINE(static Isolate* Current()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400488 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100489 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 base::Thread::GetExistingThreadLocal(isolate_key_));
491 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100492 return isolate;
493 }
494
495 INLINE(static Isolate* UncheckedCurrent()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400496 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 return reinterpret_cast<Isolate*>(
498 base::Thread::GetThreadLocal(isolate_key_));
499 }
500
501 // Like UncheckedCurrent, but skips the check that |isolate_key_| was
502 // initialized. Callers have to ensure that themselves.
503 INLINE(static Isolate* UnsafeCurrent()) {
504 return reinterpret_cast<Isolate*>(
505 base::Thread::GetThreadLocal(isolate_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100506 }
507
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000508 // Usually called by Init(), but can be called early e.g. to allow
509 // testing components that require logging but not the whole
510 // isolate.
511 //
512 // Safe to call more than once.
513 void InitializeLoggingAndCounters();
514
Steve Block44f0eee2011-05-26 01:26:41 +0100515 bool Init(Deserializer* des);
516
Steve Block44f0eee2011-05-26 01:26:41 +0100517 // True if at least one thread Enter'ed this isolate.
518 bool IsInUse() { return entry_stack_ != NULL; }
519
520 // Destroys the non-default isolates.
521 // Sets default isolate into "has_been_disposed" state rather then destroying,
522 // for legacy API reasons.
523 void TearDown();
524
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000525 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100526
Ben Murdoch257744e2011-11-30 15:57:28 +0000527 // Find the PerThread for this particular (isolate, thread) combination
528 // If one does not yet exist, return null.
529 PerIsolateThreadData* FindPerThreadDataForThisThread();
530
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 // Find the PerThread for given (isolate, thread) combination
532 // If one does not yet exist, return null.
533 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100534
535 // Returns the key used to store the pointer to the current isolate.
536 // Used internally for V8 threads that do not execute JavaScript but still
537 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100539 return isolate_key_;
540 }
Steve Block44f0eee2011-05-26 01:26:41 +0100541
542 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100544 return thread_id_key_;
545 }
Steve Block44f0eee2011-05-26 01:26:41 +0100546
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100548
Steve Block44f0eee2011-05-26 01:26:41 +0100549 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000551
Steve Block44f0eee2011-05-26 01:26:41 +0100552 Address get_address_from_id(AddressId id);
553
554 // Access to top context (where the current function object was created).
555 Context* context() { return thread_local_top_.context_; }
556 void set_context(Context* context) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 DCHECK(context == NULL || context->IsContext());
Steve Block44f0eee2011-05-26 01:26:41 +0100558 thread_local_top_.context_ = context;
559 }
560 Context** context_address() { return &thread_local_top_.context_; }
561
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100563
564 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100566
567 // Interface to pending exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568 Object* pending_exception() {
569 DCHECK(has_pending_exception());
570 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100571 return thread_local_top_.pending_exception_;
572 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000573
574 void set_pending_exception(Object* exception_obj) {
575 DCHECK(!exception_obj->IsException());
576 thread_local_top_.pending_exception_ = exception_obj;
Steve Block44f0eee2011-05-26 01:26:41 +0100577 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578
Steve Block44f0eee2011-05-26 01:26:41 +0100579 void clear_pending_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100581 thread_local_top_.pending_exception_ = heap_.the_hole_value();
582 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583
584 Object** pending_exception_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100585 return &thread_local_top_.pending_exception_;
586 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587
Steve Block44f0eee2011-05-26 01:26:41 +0100588 bool has_pending_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 DCHECK(!thread_local_top_.pending_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100590 return !thread_local_top_.pending_exception_->IsTheHole();
591 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000592
593 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
594
Steve Block44f0eee2011-05-26 01:26:41 +0100595 void clear_pending_message() {
596 thread_local_top_.has_pending_message_ = false;
Steve Block44f0eee2011-05-26 01:26:41 +0100597 thread_local_top_.pending_message_obj_ = heap_.the_hole_value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598 thread_local_top_.pending_message_script_ = heap_.the_hole_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100599 }
600 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100602 }
603 Address try_catch_handler_address() {
604 return thread_local_top_.try_catch_handler_address();
605 }
606 bool* external_caught_exception_address() {
607 return &thread_local_top_.external_caught_exception_;
608 }
609
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 THREAD_LOCAL_TOP_ACCESSOR(v8::TryCatch*, catcher)
611
612 Object** scheduled_exception_address() {
Steve Block44f0eee2011-05-26 01:26:41 +0100613 return &thread_local_top_.scheduled_exception_;
614 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615
616 Address pending_message_obj_address() {
617 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
618 }
619
620 Address has_pending_message_address() {
621 return reinterpret_cast<Address>(&thread_local_top_.has_pending_message_);
622 }
623
624 Address pending_message_script_address() {
625 return reinterpret_cast<Address>(
626 &thread_local_top_.pending_message_script_);
627 }
628
629 Object* scheduled_exception() {
630 DCHECK(has_scheduled_exception());
631 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100632 return thread_local_top_.scheduled_exception_;
633 }
634 bool has_scheduled_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000636 return thread_local_top_.scheduled_exception_ != heap_.the_hole_value();
Steve Block44f0eee2011-05-26 01:26:41 +0100637 }
638 void clear_scheduled_exception() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639 DCHECK(!thread_local_top_.scheduled_exception_->IsException());
Steve Block44f0eee2011-05-26 01:26:41 +0100640 thread_local_top_.scheduled_exception_ = heap_.the_hole_value();
641 }
642
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000643 bool HasExternalTryCatch();
644 bool IsFinallyOnTop();
Steve Block44f0eee2011-05-26 01:26:41 +0100645
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 bool is_catchable_by_javascript(Object* exception) {
647 return exception != heap()->termination_exception();
Steve Block44f0eee2011-05-26 01:26:41 +0100648 }
649
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 // Serializer.
651 void PushToPartialSnapshotCache(Object* obj);
652
Steve Block44f0eee2011-05-26 01:26:41 +0100653 // JS execution stack (see frames.h).
654 static Address c_entry_fp(ThreadLocalTop* thread) {
655 return thread->c_entry_fp_;
656 }
657 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400658 Address c_function() { return thread_local_top_.c_function_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100659
660 inline Address* c_entry_fp_address() {
661 return &thread_local_top_.c_entry_fp_;
662 }
663 inline Address* handler_address() { return &thread_local_top_.handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400664 inline Address* c_function_address() {
665 return &thread_local_top_.c_function_;
666 }
Steve Block44f0eee2011-05-26 01:26:41 +0100667
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 // Bottom JS entry.
669 Address js_entry_sp() {
670 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100671 }
672 inline Address* js_entry_sp_address() {
673 return &thread_local_top_.js_entry_sp_;
674 }
Steve Block44f0eee2011-05-26 01:26:41 +0100675
676 // Generated code scratch locations.
677 void* formal_count_address() { return &thread_local_top_.formal_count_; }
678
679 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100680 // a builtin object, or a JS global object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681 Handle<GlobalObject> global_object() {
682 return Handle<GlobalObject>(context()->global_object());
Steve Block44f0eee2011-05-26 01:26:41 +0100683 }
684
685 // Returns the global proxy object of the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000686 JSObject* global_proxy() {
Steve Block44f0eee2011-05-26 01:26:41 +0100687 return context()->global_proxy();
688 }
689
690 Handle<JSBuiltinsObject> js_builtins_object() {
691 return Handle<JSBuiltinsObject>(thread_local_top_.context_->builtins());
692 }
693
694 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
695 void FreeThreadResources() { thread_local_top_.Free(); }
696
697 // This method is called by the api after operations that may throw
698 // exceptions. If an exception was thrown and not handled by an external
699 // handler the exception is scheduled to be rethrown when we return to running
700 // JavaScript code. If an exception is scheduled true is returned.
701 bool OptionalRescheduleException(bool is_bottom_call);
702
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000703 // Push and pop a promise and the current try-catch handler.
704 void PushPromise(Handle<JSObject> promise);
705 void PopPromise();
706 Handle<Object> GetPromiseOnStackOnThrow();
707
Ben Murdoch8b112d22011-06-08 16:22:53 +0100708 class ExceptionScope {
709 public:
710 explicit ExceptionScope(Isolate* isolate) :
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 // Scope currently can only be used for regular exceptions,
712 // not termination exception.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100713 isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 pending_exception_(isolate_->pending_exception(), isolate_),
Ben Murdoch8b112d22011-06-08 16:22:53 +0100715 catcher_(isolate_->catcher())
716 { }
717
718 ~ExceptionScope() {
719 isolate_->set_catcher(catcher_);
720 isolate_->set_pending_exception(*pending_exception_);
721 }
722
723 private:
724 Isolate* isolate_;
725 Handle<Object> pending_exception_;
726 v8::TryCatch* catcher_;
727 };
728
Steve Block44f0eee2011-05-26 01:26:41 +0100729 void SetCaptureStackTraceForUncaughtExceptions(
730 bool capture,
731 int frame_limit,
732 StackTrace::StackTraceOptions options);
733
Steve Block44f0eee2011-05-26 01:26:41 +0100734 void PrintCurrentStackTrace(FILE* out);
Steve Block44f0eee2011-05-26 01:26:41 +0100735 void PrintStack(StringStream* accumulator);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000736 void PrintStack(FILE* out);
Steve Block44f0eee2011-05-26 01:26:41 +0100737 Handle<String> StackTraceString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000738 NO_INLINE(void PushStackTraceAndDie(unsigned int magic,
739 Object* object,
740 Map* map,
741 unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100742 Handle<JSArray> CaptureCurrentStackTrace(
743 int frame_limit,
744 StackTrace::StackTraceOptions options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000745 Handle<Object> CaptureSimpleStackTrace(Handle<JSObject> error_object,
746 Handle<Object> caller);
747 void CaptureAndSetDetailedStackTrace(Handle<JSObject> error_object);
748 void CaptureAndSetSimpleStackTrace(Handle<JSObject> error_object,
749 Handle<Object> caller);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400750 Handle<JSArray> GetDetailedStackTrace(Handle<JSObject> error_object);
751 Handle<JSArray> GetDetailedFromSimpleStackTrace(
752 Handle<JSObject> error_object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100753
Steve Block44f0eee2011-05-26 01:26:41 +0100754 // Returns if the top context may access the given global object. If
755 // the result is false, the pending exception is guaranteed to be
756 // set.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757
758 bool MayNamedAccess(Handle<JSObject> receiver,
759 Handle<Object> key,
Steve Block44f0eee2011-05-26 01:26:41 +0100760 v8::AccessType type);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 bool MayIndexedAccess(Handle<JSObject> receiver,
Steve Block44f0eee2011-05-26 01:26:41 +0100762 uint32_t index,
763 v8::AccessType type);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400764 bool IsInternallyUsedPropertyName(Handle<Object> name);
765 bool IsInternallyUsedPropertyName(Object* name);
Steve Block44f0eee2011-05-26 01:26:41 +0100766
767 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 void ReportFailedAccessCheck(Handle<JSObject> receiver, v8::AccessType type);
Steve Block44f0eee2011-05-26 01:26:41 +0100769
770 // Exception throwing support. The caller should use the result
771 // of Throw() as its return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 Object* Throw(Object* exception, MessageLocation* location = NULL);
773
774 template <typename T>
775 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
776 MessageLocation* location = NULL) {
777 Throw(*exception, location);
778 return MaybeHandle<T>();
779 }
780
Steve Block44f0eee2011-05-26 01:26:41 +0100781 // Re-throw an exception. This involves no error reporting since
782 // error reporting was handled when the exception was thrown
783 // originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 Object* ReThrow(Object* exception);
Steve Block44f0eee2011-05-26 01:26:41 +0100785 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786 // Re-set pending message, script and positions reported to the TryCatch
787 // back to the TLS for re-use when rethrowing.
788 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
789 // Un-schedule an exception that was caught by a TryCatch handler.
790 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100791 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792 // Return pending location if any or unfilled structure.
793 MessageLocation GetMessageLocation();
794 Object* ThrowIllegalOperation();
Steve Block44f0eee2011-05-26 01:26:41 +0100795
796 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797 Object* PromoteScheduledException();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100798 void DoThrow(Object* exception, MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100799 // Checks if exception should be reported and finds out if it's
800 // caught externally.
801 bool ShouldReportException(bool* can_be_caught_externally,
802 bool catchable_by_javascript);
803
804 // Attempts to compute the current source location, storing the
805 // result in the target out parameter.
806 void ComputeLocation(MessageLocation* target);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400807 bool ComputeLocationFromStackTrace(MessageLocation* target,
808 Handle<Object> exception);
809
810 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
811 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100812
Steve Block44f0eee2011-05-26 01:26:41 +0100813 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000814 Object* StackOverflow();
815 Object* TerminateExecution();
816 void CancelTerminateExecution();
817
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400818 void RequestInterrupt(InterruptCallback callback, void* data);
819 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100820
821 // Administration
822 void Iterate(ObjectVisitor* v);
823 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
824 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100825 void IterateThread(ThreadVisitor* v, char* t);
826
827
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400828 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100830
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000831 // Returns the native context of the calling JavaScript code. That
832 // is, the native context of the top-most JavaScript frame.
833 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100834
835 void RegisterTryCatchHandler(v8::TryCatch* that);
836 void UnregisterTryCatchHandler(v8::TryCatch* that);
837
838 char* ArchiveThread(char* to);
839 char* RestoreThread(char* from);
840
841 static const char* const kStackOverflowMessage;
842
843 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
844 static const int kBMMaxShift = 250; // See StringSearchBase.
845
846 // Accessors.
847#define GLOBAL_ACCESSOR(type, name, initialvalue) \
848 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000849 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100850 return name##_; \
851 } \
852 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100854 name##_ = value; \
855 }
856 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
857#undef GLOBAL_ACCESSOR
858
859#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
860 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100862 return &(name##_)[0]; \
863 }
864 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
865#undef GLOBAL_ARRAY_ACCESSOR
866
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000867#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
868 Handle<type> name() { \
869 return Handle<type>(native_context()->name(), this); \
870 } \
871 bool is_##name(type* value) { \
872 return native_context()->is_##name(value); \
Steve Block44f0eee2011-05-26 01:26:41 +0100873 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000874 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
875#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100876
877 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000878 Counters* counters() {
879 // Call InitializeLoggingAndCounters() if logging is needed before
880 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000881 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000882 return counters_;
883 }
Steve Block44f0eee2011-05-26 01:26:41 +0100884 CodeRange* code_range() { return code_range_; }
885 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
886 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000887 Logger* logger() {
888 // Call InitializeLoggingAndCounters() if logging is needed before
889 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000890 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000891 return logger_;
892 }
Steve Block44f0eee2011-05-26 01:26:41 +0100893 StackGuard* stack_guard() { return &stack_guard_; }
894 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000895 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100896 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100898 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
899 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900 MaterializedObjectStore* materialized_object_store() {
901 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100902 }
903
904 MemoryAllocator* memory_allocator() {
905 return memory_allocator_;
906 }
907
908 KeyedLookupCache* keyed_lookup_cache() {
909 return keyed_lookup_cache_;
910 }
911
912 ContextSlotCache* context_slot_cache() {
913 return context_slot_cache_;
914 }
915
916 DescriptorLookupCache* descriptor_lookup_cache() {
917 return descriptor_lookup_cache_;
918 }
919
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
921
Steve Block44f0eee2011-05-26 01:26:41 +0100922 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100924 return handle_scope_implementer_;
925 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000926 Zone* runtime_zone() { return &runtime_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100927
Ben Murdoch8b112d22011-06-08 16:22:53 +0100928 UnicodeCache* unicode_cache() {
929 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100930 }
931
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100932 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
933 return inner_pointer_to_code_cache_;
934 }
Steve Block44f0eee2011-05-26 01:26:41 +0100935
Steve Block44f0eee2011-05-26 01:26:41 +0100936 GlobalHandles* global_handles() { return global_handles_; }
937
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 EternalHandles* eternal_handles() { return eternal_handles_; }
939
Steve Block44f0eee2011-05-26 01:26:41 +0100940 ThreadManager* thread_manager() { return thread_manager_; }
941
Steve Block44f0eee2011-05-26 01:26:41 +0100942 StringTracker* string_tracker() { return string_tracker_; }
943
944 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
945 return &jsregexp_uncanonicalize_;
946 }
947
948 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
949 return &jsregexp_canonrange_;
950 }
951
Steve Block44f0eee2011-05-26 01:26:41 +0100952 RuntimeState* runtime_state() { return &runtime_state_; }
953
Steve Block44f0eee2011-05-26 01:26:41 +0100954 Builtins* builtins() { return &builtins_; }
955
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100956 void NotifyExtensionInstalled() {
957 has_installed_extensions_ = true;
958 }
959
960 bool has_installed_extensions() { return has_installed_extensions_; }
961
Steve Block44f0eee2011-05-26 01:26:41 +0100962 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
963 regexp_macro_assembler_canonicalize() {
964 return &regexp_macro_assembler_canonicalize_;
965 }
966
967 RegExpStack* regexp_stack() { return regexp_stack_; }
968
969 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
970 interp_canonicalize_mapping() {
971 return &interp_canonicalize_mapping_;
972 }
973
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100975
Ben Murdoch257744e2011-11-30 15:57:28 +0000976 inline bool DebuggerHasBreakPoints();
977
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
979 HeapProfiler* heap_profiler() const { return heap_profiler_; }
980
Steve Block44f0eee2011-05-26 01:26:41 +0100981#ifdef DEBUG
982 HistogramInfo* heap_histograms() { return heap_histograms_; }
983
984 JSObject::SpillInformation* js_spill_information() {
985 return &js_spill_information_;
986 }
Steve Block44f0eee2011-05-26 01:26:41 +0100987#endif
988
989 Factory* factory() { return reinterpret_cast<Factory*>(this); }
990
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100992
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100994
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
996
997 void SetData(uint32_t slot, void* data) {
998 DCHECK(slot < Internals::kNumIsolateDataSlots);
999 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +01001000 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 void* GetData(uint32_t slot) {
1002 DCHECK(slot < Internals::kNumIsolateDataSlots);
1003 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +01001004 }
Steve Block44f0eee2011-05-26 01:26:41 +01001005
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 THREAD_LOCAL_TOP_ACCESSOR(LookupResult*, top_lookup_result)
1007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008 bool serializer_enabled() const { return serializer_enabled_; }
Steve Block44f0eee2011-05-26 01:26:41 +01001009
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 bool IsDead() { return has_fatal_error_; }
1011 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +00001012
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001014
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001015 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001016
1017 double time_millis_since_init() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 return base::OS::TimeCurrentMillis() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001019 }
1020
1021 DateCache* date_cache() {
1022 return date_cache_;
1023 }
1024
1025 void set_date_cache(DateCache* date_cache) {
1026 if (date_cache != date_cache_) {
1027 delete date_cache_;
1028 }
1029 date_cache_ = date_cache;
1030 }
1031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 Map* get_initial_js_array_map(ElementsKind kind);
1033
1034 bool IsFastArrayConstructorPrototypeChainIntact();
1035
1036 CallInterfaceDescriptorData* call_descriptor_data(int index);
1037
1038 void IterateDeferredHandles(ObjectVisitor* visitor);
1039 void LinkDeferredHandles(DeferredHandles* deferred_handles);
1040 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
1041
1042#ifdef DEBUG
1043 bool IsDeferredHandle(Object** location);
1044#endif // DEBUG
1045
1046 bool concurrent_recompilation_enabled() {
1047 // Thread is only available with flag enabled.
1048 DCHECK(optimizing_compiler_thread_ == NULL ||
1049 FLAG_concurrent_recompilation);
1050 return optimizing_compiler_thread_ != NULL;
1051 }
1052
1053 bool concurrent_osr_enabled() const {
1054 // Thread is only available with flag enabled.
1055 DCHECK(optimizing_compiler_thread_ == NULL ||
1056 FLAG_concurrent_recompilation);
1057 return optimizing_compiler_thread_ != NULL && FLAG_concurrent_osr;
1058 }
1059
1060 OptimizingCompilerThread* optimizing_compiler_thread() {
1061 return optimizing_compiler_thread_;
1062 }
1063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064 int id() const { return static_cast<int>(id_); }
1065
1066 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001067 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 HTracer* GetHTracer();
1069 CodeTracer* GetCodeTracer();
1070
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001071 void DumpAndResetCompilationStats();
1072
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1074 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1075 function_entry_hook_ = function_entry_hook;
1076 }
1077
1078 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1079
1080 inline base::RandomNumberGenerator* random_number_generator();
1081
1082 // Given an address occupied by a live code object, return that object.
1083 Object* FindCodeObject(Address a);
1084
1085 int NextOptimizationId() {
1086 int id = next_optimization_id_++;
1087 if (!Smi::IsValid(next_optimization_id_)) {
1088 next_optimization_id_ = 0;
1089 }
1090 return id;
1091 }
1092
1093 // Get (and lazily initialize) the registry for per-isolate symbols.
1094 Handle<JSObject> GetSymbolRegistry();
1095
1096 void AddCallCompletedCallback(CallCompletedCallback callback);
1097 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1098 void FireCallCompletedCallback();
1099
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001100 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1101 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1102 v8::PromiseRejectEvent event);
1103
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001104 void EnqueueMicrotask(Handle<Object> microtask);
1105 void RunMicrotasks();
1106
1107 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1108 void CountUsage(v8::Isolate::UseCounterFeature feature);
1109
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001110 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1111 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1112
1113 static Isolate* NewForTesting() { return new Isolate(false); }
1114
1115 std::string GetTurboCfgFileName();
1116
1117#if TRACE_MAPS
1118 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1119#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120
Steve Block44f0eee2011-05-26 01:26:41 +01001121 private:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001122 explicit Isolate(bool enable_serializer);
Steve Block44f0eee2011-05-26 01:26:41 +01001123
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001124 friend struct GlobalState;
1125 friend struct InitializeGlobalState;
1126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 // These fields are accessed through the API, offsets must be kept in sync
1128 // with v8::internal::Internals (in include/v8.h) constants. This is also
1129 // verified in Isolate::Init() using runtime checks.
1130 void* embedder_data_[Internals::kNumIsolateDataSlots];
1131 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132
Steve Block44f0eee2011-05-26 01:26:41 +01001133 // The per-process lock should be acquired before the ThreadDataTable is
1134 // modified.
1135 class ThreadDataTable {
1136 public:
1137 ThreadDataTable();
1138 ~ThreadDataTable();
1139
1140 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1141 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001142 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001143 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001144
1145 private:
1146 PerIsolateThreadData* list_;
1147 };
1148
1149 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1150 // the Isolate. The top of the stack points to a thread which is currently
1151 // running the Isolate. When the stack is empty, the Isolate is considered
1152 // not entered by any thread and can be Disposed.
1153 // If the same thread enters the Isolate more then once, the entry_count_
1154 // is incremented rather then a new item pushed to the stack.
1155 class EntryStackItem {
1156 public:
1157 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1158 Isolate* previous_isolate,
1159 EntryStackItem* previous_item)
1160 : entry_count(1),
1161 previous_thread_data(previous_thread_data),
1162 previous_isolate(previous_isolate),
1163 previous_item(previous_item) { }
1164
1165 int entry_count;
1166 PerIsolateThreadData* previous_thread_data;
1167 Isolate* previous_isolate;
1168 EntryStackItem* previous_item;
1169
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001170 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001171 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1172 };
1173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001174 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001175
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001176 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1177 static base::Thread::LocalStorageKey isolate_key_;
1178 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001179 static ThreadDataTable* thread_data_table_;
1180
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181 // A global counter for all generated Isolates, might overflow.
1182 static base::Atomic32 isolate_counter_;
1183
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001184#if DEBUG
1185 static base::Atomic32 isolate_key_created_;
1186#endif
1187
Steve Block44f0eee2011-05-26 01:26:41 +01001188 void Deinit();
1189
1190 static void SetIsolateThreadLocals(Isolate* isolate,
1191 PerIsolateThreadData* data);
1192
Steve Block44f0eee2011-05-26 01:26:41 +01001193 // Find the PerThread for this particular (isolate, thread) combination.
1194 // If one does not yet exist, allocate a new one.
1195 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1196
Steve Block44f0eee2011-05-26 01:26:41 +01001197 // Initializes the current thread to run this Isolate.
1198 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1199 // at the same time, this should be prevented using external locking.
1200 void Enter();
1201
1202 // Exits the current thread. The previosuly entered Isolate is restored
1203 // for the thread.
1204 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1205 // at the same time, this should be prevented using external locking.
1206 void Exit();
1207
Steve Block44f0eee2011-05-26 01:26:41 +01001208 void InitializeThreadLocal();
1209
Steve Block44f0eee2011-05-26 01:26:41 +01001210 void MarkCompactPrologue(bool is_compacting,
1211 ThreadLocalTop* archived_thread_data);
1212 void MarkCompactEpilogue(bool is_compacting,
1213 ThreadLocalTop* archived_thread_data);
1214
1215 void FillCache();
1216
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001217 // Propagate pending exception message to the v8::TryCatch.
1218 // If there is no external try-catch or message was successfully propagated,
1219 // then return true.
1220 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001221
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001222 // Traverse prototype chain to find out whether the object is derived from
1223 // the Error object.
1224 bool IsErrorObject(Handle<Object> obj);
1225
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001226 base::Atomic32 id_;
1227 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001228 int stack_trace_nesting_level_;
1229 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001230 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001231 Bootstrapper* bootstrapper_;
1232 RuntimeProfiler* runtime_profiler_;
1233 CompilationCache* compilation_cache_;
1234 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001235 CodeRange* code_range_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001237 Logger* logger_;
1238 StackGuard stack_guard_;
1239 StatsTable* stats_table_;
1240 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001242 DeoptimizerData* deoptimizer_data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001244 ThreadLocalTop thread_local_top_;
1245 bool capture_stack_trace_for_uncaught_exceptions_;
1246 int stack_trace_for_uncaught_exceptions_frame_limit_;
1247 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001248 MemoryAllocator* memory_allocator_;
1249 KeyedLookupCache* keyed_lookup_cache_;
1250 ContextSlotCache* context_slot_cache_;
1251 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001253 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001254 UnicodeCache* unicode_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 Zone runtime_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001257 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001259 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001260 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001261 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001262 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001263 StringTracker* string_tracker_;
1264 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1265 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001266 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1267 regexp_macro_assembler_canonicalize_;
1268 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001269 DateCache* date_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001270 unibrow::Mapping<unibrow::Ecma262Canonicalize> interp_canonicalize_mapping_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 CallInterfaceDescriptorData* call_descriptor_data_;
1272 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274 // Whether the isolate has been created for snapshotting.
1275 bool serializer_enabled_;
1276
1277 // True if fatal error has been signaled for this isolate.
1278 bool has_fatal_error_;
1279
1280 // True if this isolate was initialized from a snapshot.
1281 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001282
1283 // Time stamp at initialization.
1284 double time_millis_at_init_;
1285
Steve Block44f0eee2011-05-26 01:26:41 +01001286#ifdef DEBUG
1287 // A static array of histogram info for each type.
1288 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1289 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001290#endif
1291
Steve Block44f0eee2011-05-26 01:26:41 +01001292 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001293 CpuProfiler* cpu_profiler_;
1294 HeapProfiler* heap_profiler_;
1295 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001296
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001297 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1298 std::queue<InterruptEntry> api_interrupts_queue_;
1299
Steve Block44f0eee2011-05-26 01:26:41 +01001300#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1301 type name##_;
1302 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1303#undef GLOBAL_BACKING_STORE
1304
1305#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1306 type name##_[length];
1307 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1308#undef GLOBAL_ARRAY_BACKING_STORE
1309
1310#ifdef DEBUG
1311 // This class is huge and has a number of fields controlled by
1312 // preprocessor defines. Make sure the offsets of these fields agree
1313 // between compilation units.
1314#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1315 static const intptr_t name##_debug_offset_;
1316 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1317 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1318#undef ISOLATE_FIELD_OFFSET
1319#endif
1320
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001321 DeferredHandles* deferred_handles_head_;
1322 OptimizingCompilerThread* optimizing_compiler_thread_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323
1324 // Counts deopt points if deopt_every_n_times is enabled.
1325 unsigned int stress_deopt_count_;
1326
1327 int next_optimization_id_;
1328
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001329#if TRACE_MAPS
1330 int next_unique_sfi_id_;
1331#endif
1332
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 // List of callbacks when a Call completes.
1334 List<CallCompletedCallback> call_completed_callbacks_;
1335
1336 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001337 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338
Steve Block44f0eee2011-05-26 01:26:41 +01001339 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340 friend class HandleScopeImplementer;
Steve Block44f0eee2011-05-26 01:26:41 +01001341 friend class IsolateInitializer;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001342 friend class OptimizingCompilerThread;
1343 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001344 friend class ThreadManager;
1345 friend class Simulator;
1346 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001347 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001348 friend class TestMemoryAllocatorScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001349 friend class TestCodeRangeScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001350 friend class v8::Isolate;
1351 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001352 friend class v8::Unlocker;
Steve Block44f0eee2011-05-26 01:26:41 +01001353
1354 DISALLOW_COPY_AND_ASSIGN(Isolate);
1355};
1356
1357
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358#undef FIELD_ACCESSOR
1359#undef THREAD_LOCAL_TOP_ACCESSOR
1360
1361
1362class PromiseOnStack {
1363 public:
1364 PromiseOnStack(StackHandler* handler, Handle<JSObject> promise,
1365 PromiseOnStack* prev)
1366 : handler_(handler), promise_(promise), prev_(prev) {}
1367 StackHandler* handler() { return handler_; }
1368 Handle<JSObject> promise() { return promise_; }
1369 PromiseOnStack* prev() { return prev_; }
1370
1371 private:
1372 StackHandler* handler_;
1373 Handle<JSObject> promise_;
1374 PromiseOnStack* prev_;
1375};
1376
1377
Steve Block44f0eee2011-05-26 01:26:41 +01001378// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1379// class as a work around for a bug in the generated code found with these
1380// versions of GCC. See V8 issue 122 for details.
1381class SaveContext BASE_EMBEDDED {
1382 public:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001383 inline explicit SaveContext(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001384
1385 ~SaveContext() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001386 isolate_->set_context(context_.is_null() ? NULL : *context_);
1387 isolate_->set_save_context(prev_);
Steve Block44f0eee2011-05-26 01:26:41 +01001388 }
1389
1390 Handle<Context> context() { return context_; }
1391 SaveContext* prev() { return prev_; }
1392
1393 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001394 bool IsBelowFrame(JavaScriptFrame* frame) {
1395 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001396 }
1397
1398 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001399 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001400 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001401 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001402 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001403};
1404
1405
1406class AssertNoContextChange BASE_EMBEDDED {
1407#ifdef DEBUG
1408 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001409 explicit AssertNoContextChange(Isolate* isolate)
1410 : isolate_(isolate),
1411 context_(isolate->context(), isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001412 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001414 }
1415
1416 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001417 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001418 Handle<Context> context_;
1419#else
1420 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001422#endif
1423};
1424
1425
1426class ExecutionAccess BASE_EMBEDDED {
1427 public:
1428 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1429 Lock(isolate);
1430 }
1431 ~ExecutionAccess() { Unlock(isolate_); }
1432
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001433 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1434 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001435
1436 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001438 }
1439
1440 private:
1441 Isolate* isolate_;
1442};
1443
1444
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001446class StackLimitCheck BASE_EMBEDDED {
1447 public:
1448 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1449
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 // Use this to check for stack-overflows in C++ code.
1451 inline bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001452 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001453 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001454 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001455
1456 // Use this to check for stack-overflow when entering runtime from JS code.
1457 bool JsHasOverflowed() const;
1458
Steve Block44f0eee2011-05-26 01:26:41 +01001459 private:
1460 Isolate* isolate_;
1461};
1462
1463
1464// Support for temporarily postponing interrupts. When the outermost
1465// postpone scope is left the interrupts will be re-enabled and any
1466// interrupts that occurred while in the scope will be taken into
1467// account.
1468class PostponeInterruptsScope BASE_EMBEDDED {
1469 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470 PostponeInterruptsScope(Isolate* isolate,
1471 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1472 : stack_guard_(isolate->stack_guard()),
1473 intercept_mask_(intercept_mask),
1474 intercepted_flags_(0) {
1475 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001476 }
1477
1478 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001479 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001480 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001481
1482 // Find the bottom-most scope that intercepts this interrupt.
1483 // Return whether the interrupt has been intercepted.
1484 bool Intercept(StackGuard::InterruptFlag flag);
1485
Steve Block44f0eee2011-05-26 01:26:41 +01001486 private:
1487 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 int intercept_mask_;
1489 int intercepted_flags_;
1490 PostponeInterruptsScope* prev_;
1491
1492 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001493};
1494
1495
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001496class CodeTracer FINAL : public Malloced {
1497 public:
1498 explicit CodeTracer(int isolate_id)
1499 : file_(NULL),
1500 scope_depth_(0) {
1501 if (!ShouldRedirect()) {
1502 file_ = stdout;
1503 return;
1504 }
Steve Block44f0eee2011-05-26 01:26:41 +01001505
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001506 if (FLAG_redirect_code_traces_to == NULL) {
1507 SNPrintF(filename_,
1508 "code-%d-%d.asm",
1509 base::OS::GetCurrentProcessId(),
1510 isolate_id);
1511 } else {
1512 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1513 }
Steve Block44f0eee2011-05-26 01:26:41 +01001514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 WriteChars(filename_.start(), "", 0, false);
1516 }
Steve Block44f0eee2011-05-26 01:26:41 +01001517
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001518 class Scope {
1519 public:
1520 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1521 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001524
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525 private:
1526 CodeTracer* tracer_;
1527 };
1528
1529 void OpenFile() {
1530 if (!ShouldRedirect()) {
1531 return;
1532 }
1533
1534 if (file_ == NULL) {
1535 file_ = base::OS::FOpen(filename_.start(), "a");
1536 }
1537
1538 scope_depth_++;
1539 }
1540
1541 void CloseFile() {
1542 if (!ShouldRedirect()) {
1543 return;
1544 }
1545
1546 if (--scope_depth_ == 0) {
1547 fclose(file_);
1548 file_ = NULL;
1549 }
1550 }
1551
1552 FILE* file() const { return file_; }
1553
1554 private:
1555 static bool ShouldRedirect() {
1556 return FLAG_redirect_code_traces;
1557 }
1558
1559 EmbeddedVector<char, 128> filename_;
1560 FILE* file_;
1561 int scope_depth_;
1562};
Steve Block44f0eee2011-05-26 01:26:41 +01001563
Steve Block44f0eee2011-05-26 01:26:41 +01001564} } // namespace v8::internal
1565
Steve Block44f0eee2011-05-26 01:26:41 +01001566#endif // V8_ISOLATE_H_