blob: 2d74dc4a63a9b89d4542eb1ec8a8ab24662ddd4d [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 Murdoch4a90d5f2016-03-22 12:00:34 +00009#include <set>
10
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "include/v8-debug.h"
12#include "src/allocation.h"
13#include "src/assert-scope.h"
14#include "src/base/atomicops.h"
15#include "src/builtins.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016#include "src/cancelable-task.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017#include "src/contexts.h"
18#include "src/date.h"
19#include "src/execution.h"
20#include "src/frames.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000021#include "src/futex-emulation.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022#include "src/global-handles.h"
23#include "src/handles.h"
24#include "src/hashmap.h"
25#include "src/heap/heap.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000026#include "src/messages.h"
27#include "src/optimizing-compile-dispatcher.h"
28#include "src/regexp/regexp-stack.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040029#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/runtime-profiler.h"
31#include "src/zone.h"
Steve Block44f0eee2011-05-26 01:26:41 +010032
33namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034
35namespace base {
36class RandomNumberGenerator;
37}
38
Steve Block44f0eee2011-05-26 01:26:41 +010039namespace internal {
40
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041class BasicBlockProfiler;
Steve Block44f0eee2011-05-26 01:26:41 +010042class Bootstrapper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043class CallInterfaceDescriptorData;
Steve Block44f0eee2011-05-26 01:26:41 +010044class CodeGenerator;
45class CodeRange;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000046class CodeStubDescriptor;
47class CodeTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010048class CompilationCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049class CompilationStatistics;
Steve Block44f0eee2011-05-26 01:26:41 +010050class ContextSlotCache;
Steve Block44f0eee2011-05-26 01:26:41 +010051class Counters;
52class CpuFeatures;
53class CpuProfiler;
54class DeoptimizerData;
55class Deserializer;
56class EmptyStatement;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057class ExternalCallbackScope;
Steve Block44f0eee2011-05-26 01:26:41 +010058class ExternalReferenceTable;
59class Factory;
60class FunctionInfoListener;
61class HandleScopeImplementer;
62class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063class HStatistics;
64class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010065class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066class InnerPointerToCodeCache;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067class Logger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068class MaterializedObjectStore;
69class CodeAgingHelper;
Steve Block44f0eee2011-05-26 01:26:41 +010070class RegExpStack;
71class SaveContext;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072class StatsTable;
Steve Block44f0eee2011-05-26 01:26:41 +010073class StringTracker;
74class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010076class ThreadManager;
77class ThreadState;
78class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079class UnicodeCache;
80template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010081
82// 'void function pointer', used to roundtrip the
83// ExternalReference::ExternalReferenceRedirector since we can not include
84// assembler.h, where it is defined, here.
85typedef void* ExternalReferenceRedirectorPointer();
86
87
Steve Block44f0eee2011-05-26 01:26:41 +010088class Debug;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010090class Redirection;
91class Simulator;
Steve Block44f0eee2011-05-26 01:26:41 +010092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093namespace interpreter {
94class Interpreter;
95}
Steve Block44f0eee2011-05-26 01:26:41 +010096
97// Static indirection table for handles to constants. If a frame
98// element represents a constant, the data contains an index into
99// this table of handles to the actual constants.
100// Static indirection table for handles to constants. If a Result
101// represents a constant, the data contains an index into this table
102// of handles to the actual constants.
103typedef ZoneList<Handle<Object> > ZoneObjectList;
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106 do { \
107 Isolate* __isolate__ = (isolate); \
108 if (__isolate__->has_scheduled_exception()) { \
109 return __isolate__->PromoteScheduledException(); \
110 } \
111 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100112
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113// Macros for MaybeHandle.
114
115#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
116 do { \
117 Isolate* __isolate__ = (isolate); \
118 if (__isolate__->has_scheduled_exception()) { \
119 __isolate__->PromoteScheduledException(); \
120 return value; \
121 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100122 } while (false)
123
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
125 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
126
127#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
128 do { \
129 if (!(call).ToHandle(&dst)) { \
130 DCHECK((isolate)->has_pending_exception()); \
131 return value; \
132 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100133 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100134
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
136 ASSIGN_RETURN_ON_EXCEPTION_VALUE( \
137 isolate, dst, call, isolate->heap()->exception())
138
139#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
140 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142#define THROW_NEW_ERROR(isolate, call, T) \
143 do { \
144 return isolate->Throw<T>(isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 } while (false)
146
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
148 do { \
149 return isolate->Throw(*isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 } while (false)
151
152#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
153 do { \
154 if ((call).is_null()) { \
155 DCHECK((isolate)->has_pending_exception()); \
156 return value; \
157 } \
158 } while (false)
159
160#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
161 RETURN_ON_EXCEPTION_VALUE(isolate, call, isolate->heap()->exception())
162
163#define RETURN_ON_EXCEPTION(isolate, call, T) \
164 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
165
Steve Block44f0eee2011-05-26 01:26:41 +0100166
Ben Murdoch589d6972011-11-30 16:04:58 +0000167#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
168 C(Handler, handler) \
169 C(CEntryFP, c_entry_fp) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400170 C(CFunction, c_function) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000171 C(Context, context) \
172 C(PendingException, pending_exception) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 C(PendingHandlerContext, pending_handler_context) \
174 C(PendingHandlerCode, pending_handler_code) \
175 C(PendingHandlerOffset, pending_handler_offset) \
176 C(PendingHandlerFP, pending_handler_fp) \
177 C(PendingHandlerSP, pending_handler_sp) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000178 C(ExternalCaughtException, external_caught_exception) \
179 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100180
181
Ben Murdoch8b112d22011-06-08 16:22:53 +0100182// Platform-independent, reliable thread identifier.
183class ThreadId {
184 public:
185 // Creates an invalid ThreadId.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400186 ThreadId() { base::NoBarrier_Store(&id_, kInvalidId); }
187
188 ThreadId& operator=(const ThreadId& other) {
189 base::NoBarrier_Store(&id_, base::NoBarrier_Load(&other.id_));
190 return *this;
191 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100192
193 // Returns ThreadId for current thread.
194 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
195
196 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
197 static ThreadId Invalid() { return ThreadId(kInvalidId); }
198
199 // Compares ThreadIds for equality.
200 INLINE(bool Equals(const ThreadId& other) const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400201 return base::NoBarrier_Load(&id_) == base::NoBarrier_Load(&other.id_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100202 }
203
204 // Checks whether this ThreadId refers to any thread.
205 INLINE(bool IsValid() const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400206 return base::NoBarrier_Load(&id_) != kInvalidId;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100207 }
208
209 // Converts ThreadId to an integer representation
210 // (required for public API: V8::V8::GetCurrentThreadId).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400211 int ToInteger() const { return static_cast<int>(base::NoBarrier_Load(&id_)); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100212
213 // Converts ThreadId to an integer representation
214 // (required for public API: V8::V8::TerminateExecution).
215 static ThreadId FromInteger(int id) { return ThreadId(id); }
216
217 private:
218 static const int kInvalidId = -1;
219
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400220 explicit ThreadId(int id) { base::NoBarrier_Store(&id_, id); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100221
222 static int AllocateThreadId();
223
224 static int GetCurrentThreadId();
225
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 base::Atomic32 id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100227
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000228 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100229
230 friend class Isolate;
231};
232
233
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000234#define FIELD_ACCESSOR(type, name) \
235 inline void set_##name(type v) { name##_ = v; } \
236 inline type name() const { return name##_; }
237
238
Steve Block44f0eee2011-05-26 01:26:41 +0100239class ThreadLocalTop BASE_EMBEDDED {
240 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100241 // Does early low-level initialization that does not depend on the
242 // isolate being present.
243 ThreadLocalTop();
244
Steve Block44f0eee2011-05-26 01:26:41 +0100245 // Initialize the thread data.
246 void Initialize();
247
248 // Get the top C++ try catch handler or NULL if none are registered.
249 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100251 // used for comparison with addresses into the JS stack. If such an
252 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000253 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100254
255 // Get the address of the top C++ try catch handler or NULL if
256 // none are registered.
257 //
258 // This method always returns an address that can be compared to
259 // pointers into the JavaScript stack. When running on actual
260 // hardware, try_catch_handler_address and TryCatchHandler return
261 // the same pointer. When running on a simulator with a separate JS
262 // stack, try_catch_handler_address returns a JS stack address that
263 // corresponds to the place on the JS stack where the C++ handler
264 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 Address try_catch_handler_address() {
266 return reinterpret_cast<Address>(
267 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100268 }
269
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000270 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100271
Ben Murdoch257744e2011-11-30 15:57:28 +0000272 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100273 // The context where the current execution method is created and for variable
274 // lookups.
275 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100276 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 Object* pending_exception_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000278
279 // Communication channel between Isolate::FindHandler and the CEntryStub.
280 Context* pending_handler_context_;
281 Code* pending_handler_code_;
282 intptr_t pending_handler_offset_;
283 Address pending_handler_fp_;
284 Address pending_handler_sp_;
285
286 // Communication channel between Isolate::Throw and message consumers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100288 Object* pending_message_obj_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000289
Steve Block44f0eee2011-05-26 01:26:41 +0100290 // Use a separate value for scheduled exceptions to preserve the
291 // invariants that hold about pending_exception. We may want to
292 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100294 bool external_caught_exception_;
295 SaveContext* save_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100296
297 // Stack.
298 Address c_entry_fp_; // the frame pointer of the top c entry frame
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000299 Address handler_; // try-blocks are chained through the stack
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400300 Address c_function_; // C function that was called at c entry.
Steve Block44f0eee2011-05-26 01:26:41 +0100301
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 // Throwing an exception may cause a Promise rejection. For this purpose
303 // we keep track of a stack of nested promises and the corresponding
304 // try-catch handlers.
305 PromiseOnStack* promise_on_stack_;
306
Steve Block44f0eee2011-05-26 01:26:41 +0100307#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100308 Simulator* simulator_;
309#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100310
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100311 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000312 // the external callback we're currently in
313 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100314 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100315
Steve Block44f0eee2011-05-26 01:26:41 +0100316 // Call back function to report unsafe JS accesses.
317 v8::FailedAccessCheckCallback failed_access_check_callback_;
318
319 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100320 void InitializeInternal();
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100323};
324
Steve Block44f0eee2011-05-26 01:26:41 +0100325
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100327
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328#define ISOLATE_INIT_SIMULATOR_LIST(V) \
329 V(bool, simulator_initialized, false) \
330 V(HashMap*, simulator_i_cache, NULL) \
331 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100332#else
333
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100335
336#endif
337
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338
Steve Block44f0eee2011-05-26 01:26:41 +0100339#ifdef DEBUG
340
341#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
342 V(CommentStatistic, paged_space_comments_statistics, \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343 CommentStatistic::kMaxComments + 1) \
344 V(int, code_kind_statistics, Code::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100345#else
346
347#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
348
349#endif
350
Steve Block44f0eee2011-05-26 01:26:41 +0100351#define ISOLATE_INIT_ARRAY_LIST(V) \
352 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100354 V(int, bad_char_shift_table, kUC16AlphabetSize) \
355 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
356 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000357 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100358 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
359
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100361
362#define ISOLATE_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100363 /* Assembler state. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100364 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 V(LogEventCallback, event_logger, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000366 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100367 /* To distinguish the function templates, so that we can find them in the */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 /* function cache of the native context. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100369 V(int, next_serial_number, 0) \
370 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100371 /* Part of the state of liveedit. */ \
372 V(FunctionInfoListener*, active_function_info_listener, NULL) \
373 /* State for Relocatable. */ \
374 V(Relocatable*, relocatable_top, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100375 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
376 V(Object*, string_stream_current_security_token, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100377 V(ExternalReferenceTable*, external_reference_table, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000378 V(HashMap*, external_reference_map, NULL) \
379 V(HashMap*, root_index_map, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 V(int, pending_microtask_count, 0) \
381 V(bool, autorun_microtasks, true) \
382 V(HStatistics*, hstatistics, NULL) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400383 V(CompilationStatistics*, turbo_statistics, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 V(HTracer*, htracer, NULL) \
385 V(CodeTracer*, code_tracer, NULL) \
386 V(bool, fp_stubs_generated, false) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000387 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400388 V(PromiseRejectCallback, promise_reject_callback, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 V(const v8::StartupData*, snapshot_blob, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 ISOLATE_INIT_SIMULATOR_LIST(V)
391
392#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
393 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
394 inline type name() const { return thread_local_top_.name##_; }
395
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000396#define THREAD_LOCAL_TOP_ADDRESS(type, name) \
397 type* name##_address() { return &thread_local_top_.name##_; }
398
Steve Block44f0eee2011-05-26 01:26:41 +0100399
400class Isolate {
401 // These forward declarations are required to make the friend declarations in
402 // PerIsolateThreadData work on some older versions of gcc.
403 class ThreadDataTable;
404 class EntryStackItem;
405 public:
406 ~Isolate();
407
Steve Block44f0eee2011-05-26 01:26:41 +0100408 // A thread has a PerIsolateThreadData instance for each isolate that it has
409 // entered. That instance is allocated when the isolate is initially entered
410 // and reused on subsequent entries.
411 class PerIsolateThreadData {
412 public:
413 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
414 : isolate_(isolate),
415 thread_id_(thread_id),
416 stack_limit_(0),
417 thread_state_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000418#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100419 simulator_(NULL),
420#endif
421 next_(NULL),
422 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100424 Isolate* isolate() const { return isolate_; }
425 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100426
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 FIELD_ACCESSOR(uintptr_t, stack_limit)
428 FIELD_ACCESSOR(ThreadState*, thread_state)
429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430#if USE_SIMULATOR
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000431 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 Murdoch4a90d5f2016-03-22 12:00:34 +0000444#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100445 Simulator* simulator_;
446#endif
447
448 PerIsolateThreadData* next_;
449 PerIsolateThreadData* prev_;
450
451 friend class Isolate;
452 friend class ThreadDataTable;
453 friend class EntryStackItem;
454
455 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
456 };
457
458
459 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000460#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
461 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000463 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100464 };
465
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466 static void InitializeOncePerProcess();
467
Steve Block44f0eee2011-05-26 01:26:41 +0100468 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
469 // not currently set).
470 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
471 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100473 }
474
475 // Returns the isolate inside which the current thread is running.
476 INLINE(static Isolate* Current()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400477 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100478 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 base::Thread::GetExistingThreadLocal(isolate_key_));
480 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100481 return isolate;
482 }
483
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484 // Like Current, but skips the check that |isolate_key_| was initialized.
485 // Callers have to ensure that themselves.
486 // DO NOT USE. The only remaining callsite will be deleted soon.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 INLINE(static Isolate* UnsafeCurrent()) {
488 return reinterpret_cast<Isolate*>(
489 base::Thread::GetThreadLocal(isolate_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100490 }
491
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000492 // Usually called by Init(), but can be called early e.g. to allow
493 // testing components that require logging but not the whole
494 // isolate.
495 //
496 // Safe to call more than once.
497 void InitializeLoggingAndCounters();
498
Steve Block44f0eee2011-05-26 01:26:41 +0100499 bool Init(Deserializer* des);
500
Steve Block44f0eee2011-05-26 01:26:41 +0100501 // True if at least one thread Enter'ed this isolate.
502 bool IsInUse() { return entry_stack_ != NULL; }
503
504 // Destroys the non-default isolates.
505 // Sets default isolate into "has_been_disposed" state rather then destroying,
506 // for legacy API reasons.
507 void TearDown();
508
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000511 void ClearSerializerData();
512
Ben Murdoch257744e2011-11-30 15:57:28 +0000513 // Find the PerThread for this particular (isolate, thread) combination
514 // If one does not yet exist, return null.
515 PerIsolateThreadData* FindPerThreadDataForThisThread();
516
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 // Find the PerThread for given (isolate, thread) combination
518 // If one does not yet exist, return null.
519 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100520
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000521 // Discard the PerThread for this particular (isolate, thread) combination
522 // If one does not yet exist, no-op.
523 void DiscardPerThreadDataForThisThread();
524
Steve Block44f0eee2011-05-26 01:26:41 +0100525 // Returns the key used to store the pointer to the current isolate.
526 // Used internally for V8 threads that do not execute JavaScript but still
527 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100529 return isolate_key_;
530 }
Steve Block44f0eee2011-05-26 01:26:41 +0100531
532 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100534 return thread_id_key_;
535 }
Steve Block44f0eee2011-05-26 01:26:41 +0100536
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000537 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100538
Steve Block44f0eee2011-05-26 01:26:41 +0100539 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000541
Steve Block44f0eee2011-05-26 01:26:41 +0100542 Address get_address_from_id(AddressId id);
543
544 // Access to top context (where the current function object was created).
545 Context* context() { return thread_local_top_.context_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000546 inline void set_context(Context* context);
Steve Block44f0eee2011-05-26 01:26:41 +0100547 Context** context_address() { return &thread_local_top_.context_; }
548
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100550
551 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100553
554 // Interface to pending exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555 inline Object* pending_exception();
556 inline void set_pending_exception(Object* exception_obj);
557 inline void clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 THREAD_LOCAL_TOP_ADDRESS(Object*, pending_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000560
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561 inline bool has_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000563 THREAD_LOCAL_TOP_ADDRESS(Context*, pending_handler_context)
564 THREAD_LOCAL_TOP_ADDRESS(Code*, pending_handler_code)
565 THREAD_LOCAL_TOP_ADDRESS(intptr_t, pending_handler_offset)
566 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_fp)
567 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_sp)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568
569 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
570
Steve Block44f0eee2011-05-26 01:26:41 +0100571 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100573 }
Steve Block44f0eee2011-05-26 01:26:41 +0100574 bool* external_caught_exception_address() {
575 return &thread_local_top_.external_caught_exception_;
576 }
577
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000578 THREAD_LOCAL_TOP_ADDRESS(Object*, scheduled_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000579
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000580 inline void clear_pending_message();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 Address pending_message_obj_address() {
582 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
583 }
584
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000585 inline Object* scheduled_exception();
586 inline bool has_scheduled_exception();
587 inline void clear_scheduled_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589 bool IsJavaScriptHandlerOnTop(Object* exception);
590 bool IsExternalHandlerOnTop(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 inline bool is_catchable_by_javascript(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593
Steve Block44f0eee2011-05-26 01:26:41 +0100594 // JS execution stack (see frames.h).
595 static Address c_entry_fp(ThreadLocalTop* thread) {
596 return thread->c_entry_fp_;
597 }
598 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400599 Address c_function() { return thread_local_top_.c_function_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100600
601 inline Address* c_entry_fp_address() {
602 return &thread_local_top_.c_entry_fp_;
603 }
604 inline Address* handler_address() { return &thread_local_top_.handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400605 inline Address* c_function_address() {
606 return &thread_local_top_.c_function_;
607 }
Steve Block44f0eee2011-05-26 01:26:41 +0100608
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609 // Bottom JS entry.
610 Address js_entry_sp() {
611 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100612 }
613 inline Address* js_entry_sp_address() {
614 return &thread_local_top_.js_entry_sp_;
615 }
Steve Block44f0eee2011-05-26 01:26:41 +0100616
Steve Block44f0eee2011-05-26 01:26:41 +0100617 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100618 // a builtin object, or a JS global object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000619 inline Handle<JSGlobalObject> global_object();
Steve Block44f0eee2011-05-26 01:26:41 +0100620
621 // Returns the global proxy object of the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 JSObject* global_proxy() {
Steve Block44f0eee2011-05-26 01:26:41 +0100623 return context()->global_proxy();
624 }
625
Steve Block44f0eee2011-05-26 01:26:41 +0100626 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
627 void FreeThreadResources() { thread_local_top_.Free(); }
628
629 // This method is called by the api after operations that may throw
630 // exceptions. If an exception was thrown and not handled by an external
631 // handler the exception is scheduled to be rethrown when we return to running
632 // JavaScript code. If an exception is scheduled true is returned.
633 bool OptionalRescheduleException(bool is_bottom_call);
634
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 // Push and pop a promise and the current try-catch handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636 void PushPromise(Handle<JSObject> promise, Handle<JSFunction> function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 void PopPromise();
638 Handle<Object> GetPromiseOnStackOnThrow();
639
Ben Murdoch8b112d22011-06-08 16:22:53 +0100640 class ExceptionScope {
641 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000642 // Scope currently can only be used for regular exceptions,
643 // not termination exception.
644 inline explicit ExceptionScope(Isolate* isolate);
645 inline ~ExceptionScope();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100646
647 private:
648 Isolate* isolate_;
649 Handle<Object> pending_exception_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100650 };
651
Steve Block44f0eee2011-05-26 01:26:41 +0100652 void SetCaptureStackTraceForUncaughtExceptions(
653 bool capture,
654 int frame_limit,
655 StackTrace::StackTraceOptions options);
656
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000657 void SetAbortOnUncaughtExceptionCallback(
658 v8::Isolate::AbortOnUncaughtExceptionCallback callback);
659
660 enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
Steve Block44f0eee2011-05-26 01:26:41 +0100661 void PrintCurrentStackTrace(FILE* out);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000662 void PrintStack(StringStream* accumulator,
663 PrintStackMode mode = kPrintStackVerbose);
664 void PrintStack(FILE* out, PrintStackMode mode = kPrintStackVerbose);
Steve Block44f0eee2011-05-26 01:26:41 +0100665 Handle<String> StackTraceString();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000666 NO_INLINE(void PushStackTraceAndDie(unsigned int magic, void* ptr1,
667 void* ptr2, unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100668 Handle<JSArray> CaptureCurrentStackTrace(
669 int frame_limit,
670 StackTrace::StackTraceOptions options);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671 Handle<Object> CaptureSimpleStackTrace(Handle<JSObject> error_object,
672 Handle<Object> caller);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000673 MaybeHandle<JSObject> CaptureAndSetDetailedStackTrace(
674 Handle<JSObject> error_object);
675 MaybeHandle<JSObject> CaptureAndSetSimpleStackTrace(
676 Handle<JSObject> error_object, Handle<Object> caller);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400677 Handle<JSArray> GetDetailedStackTrace(Handle<JSObject> error_object);
678 Handle<JSArray> GetDetailedFromSimpleStackTrace(
679 Handle<JSObject> error_object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100680
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000681 // Returns if the given context may access the given global object. If
Steve Block44f0eee2011-05-26 01:26:41 +0100682 // the result is false, the pending exception is guaranteed to be
683 // set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 bool MayAccess(Handle<Context> accessing_context, Handle<JSObject> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685
Steve Block44f0eee2011-05-26 01:26:41 +0100686 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000687 void ReportFailedAccessCheck(Handle<JSObject> receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100688
689 // Exception throwing support. The caller should use the result
690 // of Throw() as its return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000691 Object* Throw(Object* exception, MessageLocation* location = NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000692 Object* ThrowIllegalOperation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693
694 template <typename T>
695 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
696 MessageLocation* location = NULL) {
697 Throw(*exception, location);
698 return MaybeHandle<T>();
699 }
700
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 // Re-throw an exception. This involves no error reporting since error
702 // reporting was handled when the exception was thrown originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000703 Object* ReThrow(Object* exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000704
705 // Find the correct handler for the current pending exception. This also
706 // clears and returns the current pending exception.
707 Object* UnwindAndFindHandler();
708
709 // Tries to predict whether an exception will be caught. Note that this can
710 // only produce an estimate, because it is undecidable whether a finally
711 // clause will consume or re-throw an exception. We conservatively assume any
712 // finally clause will behave as if the exception were consumed.
713 enum CatchType { NOT_CAUGHT, CAUGHT_BY_JAVASCRIPT, CAUGHT_BY_EXTERNAL };
714 CatchType PredictExceptionCatcher();
715
Steve Block44f0eee2011-05-26 01:26:41 +0100716 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 // Re-set pending message, script and positions reported to the TryCatch
718 // back to the TLS for re-use when rethrowing.
719 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
720 // Un-schedule an exception that was caught by a TryCatch handler.
721 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100722 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723 // Return pending location if any or unfilled structure.
724 MessageLocation GetMessageLocation();
Steve Block44f0eee2011-05-26 01:26:41 +0100725
726 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000727 Object* PromoteScheduledException();
Steve Block44f0eee2011-05-26 01:26:41 +0100728
729 // Attempts to compute the current source location, storing the
730 // result in the target out parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000731 bool ComputeLocation(MessageLocation* target);
732 bool ComputeLocationFromException(MessageLocation* target,
733 Handle<Object> exception);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400734 bool ComputeLocationFromStackTrace(MessageLocation* target,
735 Handle<Object> exception);
736
737 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
738 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100739
Steve Block44f0eee2011-05-26 01:26:41 +0100740 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000741 Object* StackOverflow();
742 Object* TerminateExecution();
743 void CancelTerminateExecution();
744
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400745 void RequestInterrupt(InterruptCallback callback, void* data);
746 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100747
748 // Administration
749 void Iterate(ObjectVisitor* v);
750 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
751 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100752 void IterateThread(ThreadVisitor* v, char* t);
753
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400754 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100756
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 // Returns the native context of the calling JavaScript code. That
758 // is, the native context of the top-most JavaScript frame.
759 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100760
761 void RegisterTryCatchHandler(v8::TryCatch* that);
762 void UnregisterTryCatchHandler(v8::TryCatch* that);
763
764 char* ArchiveThread(char* to);
765 char* RestoreThread(char* from);
766
767 static const char* const kStackOverflowMessage;
768
769 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
770 static const int kBMMaxShift = 250; // See StringSearchBase.
771
772 // Accessors.
773#define GLOBAL_ACCESSOR(type, name, initialvalue) \
774 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100776 return name##_; \
777 } \
778 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100780 name##_ = value; \
781 }
782 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
783#undef GLOBAL_ACCESSOR
784
785#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
786 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000787 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100788 return &(name##_)[0]; \
789 }
790 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
791#undef GLOBAL_ARRAY_ACCESSOR
792
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000793#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
794 inline Handle<type> name(); \
795 inline bool is_##name(type* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000796 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
797#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100798
799 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000800 Counters* counters() {
801 // Call InitializeLoggingAndCounters() if logging is needed before
802 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000803 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000804 return counters_;
805 }
Steve Block44f0eee2011-05-26 01:26:41 +0100806 CodeRange* code_range() { return code_range_; }
807 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
808 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000809 Logger* logger() {
810 // Call InitializeLoggingAndCounters() if logging is needed before
811 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000813 return logger_;
814 }
Steve Block44f0eee2011-05-26 01:26:41 +0100815 StackGuard* stack_guard() { return &stack_guard_; }
816 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000817 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100818 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000819 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100820 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100821 bool deoptimizer_lazy_throw() const { return deoptimizer_lazy_throw_; }
822 void set_deoptimizer_lazy_throw(bool value) {
823 deoptimizer_lazy_throw_ = value;
824 }
Steve Block44f0eee2011-05-26 01:26:41 +0100825 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826 MaterializedObjectStore* materialized_object_store() {
827 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100828 }
829
830 MemoryAllocator* memory_allocator() {
831 return memory_allocator_;
832 }
833
834 KeyedLookupCache* keyed_lookup_cache() {
835 return keyed_lookup_cache_;
836 }
837
838 ContextSlotCache* context_slot_cache() {
839 return context_slot_cache_;
840 }
841
842 DescriptorLookupCache* descriptor_lookup_cache() {
843 return descriptor_lookup_cache_;
844 }
845
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
847
Steve Block44f0eee2011-05-26 01:26:41 +0100848 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000849 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100850 return handle_scope_implementer_;
851 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 Zone* runtime_zone() { return &runtime_zone_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000853 Zone* interface_descriptor_zone() { return &interface_descriptor_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100854
Ben Murdoch8b112d22011-06-08 16:22:53 +0100855 UnicodeCache* unicode_cache() {
856 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100857 }
858
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100859 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
860 return inner_pointer_to_code_cache_;
861 }
Steve Block44f0eee2011-05-26 01:26:41 +0100862
Steve Block44f0eee2011-05-26 01:26:41 +0100863 GlobalHandles* global_handles() { return global_handles_; }
864
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000865 EternalHandles* eternal_handles() { return eternal_handles_; }
866
Steve Block44f0eee2011-05-26 01:26:41 +0100867 ThreadManager* thread_manager() { return thread_manager_; }
868
Steve Block44f0eee2011-05-26 01:26:41 +0100869 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
870 return &jsregexp_uncanonicalize_;
871 }
872
873 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
874 return &jsregexp_canonrange_;
875 }
876
Steve Block44f0eee2011-05-26 01:26:41 +0100877 RuntimeState* runtime_state() { return &runtime_state_; }
878
Steve Block44f0eee2011-05-26 01:26:41 +0100879 Builtins* builtins() { return &builtins_; }
880
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100881 void NotifyExtensionInstalled() {
882 has_installed_extensions_ = true;
883 }
884
885 bool has_installed_extensions() { return has_installed_extensions_; }
886
Steve Block44f0eee2011-05-26 01:26:41 +0100887 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
888 regexp_macro_assembler_canonicalize() {
889 return &regexp_macro_assembler_canonicalize_;
890 }
891
892 RegExpStack* regexp_stack() { return regexp_stack_; }
893
894 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
895 interp_canonicalize_mapping() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100896 return &regexp_macro_assembler_canonicalize_;
Steve Block44f0eee2011-05-26 01:26:41 +0100897 }
898
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100900
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
902 HeapProfiler* heap_profiler() const { return heap_profiler_; }
903
Steve Block44f0eee2011-05-26 01:26:41 +0100904#ifdef DEBUG
905 HistogramInfo* heap_histograms() { return heap_histograms_; }
906
907 JSObject::SpillInformation* js_spill_information() {
908 return &js_spill_information_;
909 }
Steve Block44f0eee2011-05-26 01:26:41 +0100910#endif
911
912 Factory* factory() { return reinterpret_cast<Factory*>(this); }
913
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100915
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100917
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
919
920 void SetData(uint32_t slot, void* data) {
921 DCHECK(slot < Internals::kNumIsolateDataSlots);
922 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100923 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 void* GetData(uint32_t slot) {
925 DCHECK(slot < Internals::kNumIsolateDataSlots);
926 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100927 }
Steve Block44f0eee2011-05-26 01:26:41 +0100928
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 bool serializer_enabled() const { return serializer_enabled_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000930 bool snapshot_available() const {
931 return snapshot_blob_ != NULL && snapshot_blob_->raw_size != 0;
932 }
Steve Block44f0eee2011-05-26 01:26:41 +0100933
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000934 bool IsDead() { return has_fatal_error_; }
935 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000936
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100938
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000939 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100940
941 double time_millis_since_init() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000942 return heap_.MonotonicallyIncreasingTimeInMs() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100943 }
944
945 DateCache* date_cache() {
946 return date_cache_;
947 }
948
949 void set_date_cache(DateCache* date_cache) {
950 if (date_cache != date_cache_) {
951 delete date_cache_;
952 }
953 date_cache_ = date_cache;
954 }
955
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000956 Map* get_initial_js_array_map(ElementsKind kind,
957 Strength strength = Strength::WEAK);
958
959 static const int kArrayProtectorValid = 1;
960 static const int kArrayProtectorInvalid = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961
962 bool IsFastArrayConstructorPrototypeChainIntact();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100963 bool IsArraySpeciesLookupChainIntact();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000965 // On intent to set an element in object, make sure that appropriate
966 // notifications occur if the set is on the elements of the array or
967 // object prototype. Also ensure that changes to prototype chain between
968 // Array and Object fire notifications.
969 void UpdateArrayProtectorOnSetElement(Handle<JSObject> object);
970 void UpdateArrayProtectorOnSetLength(Handle<JSObject> object) {
971 UpdateArrayProtectorOnSetElement(object);
972 }
973 void UpdateArrayProtectorOnSetPrototype(Handle<JSObject> object) {
974 UpdateArrayProtectorOnSetElement(object);
975 }
976 void UpdateArrayProtectorOnNormalizeElements(Handle<JSObject> object) {
977 UpdateArrayProtectorOnSetElement(object);
978 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100979 void InvalidateArraySpeciesProtector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000980
981 // Returns true if array is the initial array prototype in any native context.
982 bool IsAnyInitialArrayPrototype(Handle<JSArray> array);
983
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984 CallInterfaceDescriptorData* call_descriptor_data(int index);
985
986 void IterateDeferredHandles(ObjectVisitor* visitor);
987 void LinkDeferredHandles(DeferredHandles* deferred_handles);
988 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
989
990#ifdef DEBUG
991 bool IsDeferredHandle(Object** location);
992#endif // DEBUG
993
994 bool concurrent_recompilation_enabled() {
995 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000996 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000998 return optimizing_compile_dispatcher_ != NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000999 }
1000
1001 bool concurrent_osr_enabled() const {
1002 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001003 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001005 return optimizing_compile_dispatcher_ != NULL && FLAG_concurrent_osr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 }
1007
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001008 OptimizingCompileDispatcher* optimizing_compile_dispatcher() {
1009 return optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 }
1011
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001012 int id() const { return static_cast<int>(id_); }
1013
1014 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001015 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001016 HTracer* GetHTracer();
1017 CodeTracer* GetCodeTracer();
1018
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001019 void DumpAndResetCompilationStats();
1020
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001021 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1022 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1023 function_entry_hook_ = function_entry_hook;
1024 }
1025
1026 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028 void* virtual_handler_register_address() {
1029 return &virtual_handler_register_;
1030 }
1031
1032 void* virtual_slot_register_address() { return &virtual_slot_register_; }
1033
1034 base::RandomNumberGenerator* random_number_generator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001035
1036 // Given an address occupied by a live code object, return that object.
1037 Object* FindCodeObject(Address a);
1038
1039 int NextOptimizationId() {
1040 int id = next_optimization_id_++;
1041 if (!Smi::IsValid(next_optimization_id_)) {
1042 next_optimization_id_ = 0;
1043 }
1044 return id;
1045 }
1046
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001047 void IncrementJsCallsFromApiCounter() { ++js_calls_from_api_counter_; }
1048
1049 unsigned int js_calls_from_api_counter() {
1050 return js_calls_from_api_counter_;
1051 }
1052
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053 // Get (and lazily initialize) the registry for per-isolate symbols.
1054 Handle<JSObject> GetSymbolRegistry();
1055
1056 void AddCallCompletedCallback(CallCompletedCallback callback);
1057 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1058 void FireCallCompletedCallback();
1059
Ben Murdoch097c5b22016-05-18 11:27:45 +01001060 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1061 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1062 void FireBeforeCallEnteredCallback();
1063
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001064 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1065 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1066 v8::PromiseRejectEvent event);
1067
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001068 void EnqueueMicrotask(Handle<Object> microtask);
1069 void RunMicrotasks();
1070
1071 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1072 void CountUsage(v8::Isolate::UseCounterFeature feature);
1073
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001074 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1075 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1076
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001077 std::string GetTurboCfgFileName();
1078
1079#if TRACE_MAPS
1080 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1081#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001082
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001083
1084 void AddDetachedContext(Handle<Context> context);
1085 void CheckDetachedContextsAfterGC();
1086
1087 List<Object*>* partial_snapshot_cache() { return &partial_snapshot_cache_; }
1088
1089 void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
1090 array_buffer_allocator_ = allocator;
1091 }
1092 v8::ArrayBuffer::Allocator* array_buffer_allocator() const {
1093 return array_buffer_allocator_;
1094 }
1095
1096 FutexWaitListNode* futex_wait_list_node() { return &futex_wait_list_node_; }
1097
1098 CancelableTaskManager* cancelable_task_manager() {
1099 return cancelable_task_manager_;
1100 }
1101
1102 interpreter::Interpreter* interpreter() const { return interpreter_; }
1103
1104 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001105 explicit Isolate(bool enable_serializer);
Steve Block44f0eee2011-05-26 01:26:41 +01001106
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001107 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001108 friend struct GlobalState;
1109 friend struct InitializeGlobalState;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001110 Handle<JSObject> SetUpSubregistry(Handle<JSObject> registry, Handle<Map> map,
1111 const char* name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001112
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001113 // These fields are accessed through the API, offsets must be kept in sync
1114 // with v8::internal::Internals (in include/v8.h) constants. This is also
1115 // verified in Isolate::Init() using runtime checks.
1116 void* embedder_data_[Internals::kNumIsolateDataSlots];
1117 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001118
Steve Block44f0eee2011-05-26 01:26:41 +01001119 // The per-process lock should be acquired before the ThreadDataTable is
1120 // modified.
1121 class ThreadDataTable {
1122 public:
1123 ThreadDataTable();
1124 ~ThreadDataTable();
1125
1126 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1127 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001128 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001129 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001130
1131 private:
1132 PerIsolateThreadData* list_;
1133 };
1134
1135 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1136 // the Isolate. The top of the stack points to a thread which is currently
1137 // running the Isolate. When the stack is empty, the Isolate is considered
1138 // not entered by any thread and can be Disposed.
1139 // If the same thread enters the Isolate more then once, the entry_count_
1140 // is incremented rather then a new item pushed to the stack.
1141 class EntryStackItem {
1142 public:
1143 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1144 Isolate* previous_isolate,
1145 EntryStackItem* previous_item)
1146 : entry_count(1),
1147 previous_thread_data(previous_thread_data),
1148 previous_isolate(previous_isolate),
1149 previous_item(previous_item) { }
1150
1151 int entry_count;
1152 PerIsolateThreadData* previous_thread_data;
1153 Isolate* previous_isolate;
1154 EntryStackItem* previous_item;
1155
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001156 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001157 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1158 };
1159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001161
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001162 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1163 static base::Thread::LocalStorageKey isolate_key_;
1164 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001165 static ThreadDataTable* thread_data_table_;
1166
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001167 // A global counter for all generated Isolates, might overflow.
1168 static base::Atomic32 isolate_counter_;
1169
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001170#if DEBUG
1171 static base::Atomic32 isolate_key_created_;
1172#endif
1173
Steve Block44f0eee2011-05-26 01:26:41 +01001174 void Deinit();
1175
1176 static void SetIsolateThreadLocals(Isolate* isolate,
1177 PerIsolateThreadData* data);
1178
Steve Block44f0eee2011-05-26 01:26:41 +01001179 // Find the PerThread for this particular (isolate, thread) combination.
1180 // If one does not yet exist, allocate a new one.
1181 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1182
Steve Block44f0eee2011-05-26 01:26:41 +01001183 // Initializes the current thread to run this Isolate.
1184 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1185 // at the same time, this should be prevented using external locking.
1186 void Enter();
1187
1188 // Exits the current thread. The previosuly entered Isolate is restored
1189 // for the thread.
1190 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1191 // at the same time, this should be prevented using external locking.
1192 void Exit();
1193
Steve Block44f0eee2011-05-26 01:26:41 +01001194 void InitializeThreadLocal();
1195
Steve Block44f0eee2011-05-26 01:26:41 +01001196 void MarkCompactPrologue(bool is_compacting,
1197 ThreadLocalTop* archived_thread_data);
1198 void MarkCompactEpilogue(bool is_compacting,
1199 ThreadLocalTop* archived_thread_data);
1200
1201 void FillCache();
1202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001203 // Propagate pending exception message to the v8::TryCatch.
1204 // If there is no external try-catch or message was successfully propagated,
1205 // then return true.
1206 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001207
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001208 // Remove per-frame stored materialized objects when we are unwinding
1209 // the frame.
1210 void RemoveMaterializedObjectsOnUnwind(StackFrame* frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212 base::Atomic32 id_;
1213 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001214 int stack_trace_nesting_level_;
1215 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001216 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001217 Bootstrapper* bootstrapper_;
1218 RuntimeProfiler* runtime_profiler_;
1219 CompilationCache* compilation_cache_;
1220 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001221 CodeRange* code_range_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001223 Logger* logger_;
1224 StackGuard stack_guard_;
1225 StatsTable* stats_table_;
1226 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001228 DeoptimizerData* deoptimizer_data_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001229 bool deoptimizer_lazy_throw_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001230 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001231 ThreadLocalTop thread_local_top_;
1232 bool capture_stack_trace_for_uncaught_exceptions_;
1233 int stack_trace_for_uncaught_exceptions_frame_limit_;
1234 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001235 MemoryAllocator* memory_allocator_;
1236 KeyedLookupCache* keyed_lookup_cache_;
1237 ContextSlotCache* context_slot_cache_;
1238 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001240 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001241 UnicodeCache* unicode_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242 Zone runtime_zone_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001243 Zone interface_descriptor_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001244 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001245 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001247 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001248 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001249 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001250 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001251 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1252 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001253 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1254 regexp_macro_assembler_canonicalize_;
1255 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 DateCache* date_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257 CallInterfaceDescriptorData* call_descriptor_data_;
1258 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001259
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 // Whether the isolate has been created for snapshotting.
1261 bool serializer_enabled_;
1262
1263 // True if fatal error has been signaled for this isolate.
1264 bool has_fatal_error_;
1265
1266 // True if this isolate was initialized from a snapshot.
1267 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001268
1269 // Time stamp at initialization.
1270 double time_millis_at_init_;
1271
Steve Block44f0eee2011-05-26 01:26:41 +01001272#ifdef DEBUG
1273 // A static array of histogram info for each type.
1274 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1275 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001276#endif
1277
Steve Block44f0eee2011-05-26 01:26:41 +01001278 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 CpuProfiler* cpu_profiler_;
1280 HeapProfiler* heap_profiler_;
1281 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001282
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001283 interpreter::Interpreter* interpreter_;
1284
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001285 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1286 std::queue<InterruptEntry> api_interrupts_queue_;
1287
Steve Block44f0eee2011-05-26 01:26:41 +01001288#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1289 type name##_;
1290 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1291#undef GLOBAL_BACKING_STORE
1292
1293#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1294 type name##_[length];
1295 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1296#undef GLOBAL_ARRAY_BACKING_STORE
1297
1298#ifdef DEBUG
1299 // This class is huge and has a number of fields controlled by
1300 // preprocessor defines. Make sure the offsets of these fields agree
1301 // between compilation units.
1302#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1303 static const intptr_t name##_debug_offset_;
1304 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1305 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1306#undef ISOLATE_FIELD_OFFSET
1307#endif
1308
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001309 DeferredHandles* deferred_handles_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001310 OptimizingCompileDispatcher* optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001311
1312 // Counts deopt points if deopt_every_n_times is enabled.
1313 unsigned int stress_deopt_count_;
1314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001315 Address virtual_handler_register_;
1316 Address virtual_slot_register_;
1317
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 int next_optimization_id_;
1319
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001320 // Counts javascript calls from the API. Wraps around on overflow.
1321 unsigned int js_calls_from_api_counter_;
1322
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001323#if TRACE_MAPS
1324 int next_unique_sfi_id_;
1325#endif
1326
Ben Murdoch097c5b22016-05-18 11:27:45 +01001327 // List of callbacks before a Call starts execution.
1328 List<BeforeCallEnteredCallback> before_call_entered_callbacks_;
1329
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330 // List of callbacks when a Call completes.
1331 List<CallCompletedCallback> call_completed_callbacks_;
1332
1333 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001334 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001335
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001336 List<Object*> partial_snapshot_cache_;
1337
1338 v8::ArrayBuffer::Allocator* array_buffer_allocator_;
1339
1340 FutexWaitListNode futex_wait_list_node_;
1341
1342 CancelableTaskManager* cancelable_task_manager_;
1343
1344 v8::Isolate::AbortOnUncaughtExceptionCallback
1345 abort_on_uncaught_exception_callback_;
1346
Steve Block44f0eee2011-05-26 01:26:41 +01001347 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 friend class HandleScopeImplementer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001349 friend class OptimizingCompileDispatcher;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001350 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001351 friend class ThreadManager;
1352 friend class Simulator;
1353 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001354 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001355 friend class TestMemoryAllocatorScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356 friend class TestCodeRangeScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001357 friend class v8::Isolate;
1358 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001359 friend class v8::Unlocker;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001360 friend v8::StartupData v8::V8::CreateSnapshotDataBlob(const char*);
Steve Block44f0eee2011-05-26 01:26:41 +01001361
1362 DISALLOW_COPY_AND_ASSIGN(Isolate);
1363};
1364
1365
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001366#undef FIELD_ACCESSOR
1367#undef THREAD_LOCAL_TOP_ACCESSOR
1368
1369
1370class PromiseOnStack {
1371 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001372 PromiseOnStack(Handle<JSFunction> function, Handle<JSObject> promise,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 PromiseOnStack* prev)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001374 : function_(function), promise_(promise), prev_(prev) {}
1375 Handle<JSFunction> function() { return function_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376 Handle<JSObject> promise() { return promise_; }
1377 PromiseOnStack* prev() { return prev_; }
1378
1379 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001380 Handle<JSFunction> function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001381 Handle<JSObject> promise_;
1382 PromiseOnStack* prev_;
1383};
1384
1385
Steve Block44f0eee2011-05-26 01:26:41 +01001386// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1387// class as a work around for a bug in the generated code found with these
1388// versions of GCC. See V8 issue 122 for details.
1389class SaveContext BASE_EMBEDDED {
1390 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 explicit SaveContext(Isolate* isolate);
1392 ~SaveContext();
Steve Block44f0eee2011-05-26 01:26:41 +01001393
1394 Handle<Context> context() { return context_; }
1395 SaveContext* prev() { return prev_; }
1396
1397 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001398 bool IsBelowFrame(JavaScriptFrame* frame) {
1399 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001400 }
1401
1402 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001404 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001405 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001406 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001407};
1408
1409
1410class AssertNoContextChange BASE_EMBEDDED {
1411#ifdef DEBUG
1412 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001413 explicit AssertNoContextChange(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001414 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001416 }
1417
1418 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001419 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001420 Handle<Context> context_;
1421#else
1422 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001423 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001424#endif
1425};
1426
1427
1428class ExecutionAccess BASE_EMBEDDED {
1429 public:
1430 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1431 Lock(isolate);
1432 }
1433 ~ExecutionAccess() { Unlock(isolate_); }
1434
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001435 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1436 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001437
1438 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001439 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001440 }
1441
1442 private:
1443 Isolate* isolate_;
1444};
1445
1446
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001448class StackLimitCheck BASE_EMBEDDED {
1449 public:
1450 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1451
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001452 // Use this to check for stack-overflows in C++ code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001453 bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001454 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001455 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001456 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001458 // Use this to check for interrupt request in C++ code.
1459 bool InterruptRequested() {
1460 StackGuard* stack_guard = isolate_->stack_guard();
1461 return GetCurrentStackPosition() < stack_guard->climit();
1462 }
1463
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001464 // Use this to check for stack-overflow when entering runtime from JS code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001465 bool JsHasOverflowed(uintptr_t gap = 0) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001466
Steve Block44f0eee2011-05-26 01:26:41 +01001467 private:
1468 Isolate* isolate_;
1469};
1470
1471
1472// Support for temporarily postponing interrupts. When the outermost
1473// postpone scope is left the interrupts will be re-enabled and any
1474// interrupts that occurred while in the scope will be taken into
1475// account.
1476class PostponeInterruptsScope BASE_EMBEDDED {
1477 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478 PostponeInterruptsScope(Isolate* isolate,
1479 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1480 : stack_guard_(isolate->stack_guard()),
1481 intercept_mask_(intercept_mask),
1482 intercepted_flags_(0) {
1483 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001484 }
1485
1486 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001487 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001488 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001489
1490 // Find the bottom-most scope that intercepts this interrupt.
1491 // Return whether the interrupt has been intercepted.
1492 bool Intercept(StackGuard::InterruptFlag flag);
1493
Steve Block44f0eee2011-05-26 01:26:41 +01001494 private:
1495 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001496 int intercept_mask_;
1497 int intercepted_flags_;
1498 PostponeInterruptsScope* prev_;
1499
1500 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001501};
1502
1503
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001504class CodeTracer final : public Malloced {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001505 public:
1506 explicit CodeTracer(int isolate_id)
1507 : file_(NULL),
1508 scope_depth_(0) {
1509 if (!ShouldRedirect()) {
1510 file_ = stdout;
1511 return;
1512 }
Steve Block44f0eee2011-05-26 01:26:41 +01001513
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001514 if (FLAG_redirect_code_traces_to == NULL) {
1515 SNPrintF(filename_,
1516 "code-%d-%d.asm",
1517 base::OS::GetCurrentProcessId(),
1518 isolate_id);
1519 } else {
1520 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1521 }
Steve Block44f0eee2011-05-26 01:26:41 +01001522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 WriteChars(filename_.start(), "", 0, false);
1524 }
Steve Block44f0eee2011-05-26 01:26:41 +01001525
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001526 class Scope {
1527 public:
1528 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1529 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001530
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001531 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001532
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 private:
1534 CodeTracer* tracer_;
1535 };
1536
1537 void OpenFile() {
1538 if (!ShouldRedirect()) {
1539 return;
1540 }
1541
1542 if (file_ == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 file_ = base::OS::FOpen(filename_.start(), "ab");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 }
1545
1546 scope_depth_++;
1547 }
1548
1549 void CloseFile() {
1550 if (!ShouldRedirect()) {
1551 return;
1552 }
1553
1554 if (--scope_depth_ == 0) {
1555 fclose(file_);
1556 file_ = NULL;
1557 }
1558 }
1559
1560 FILE* file() const { return file_; }
1561
1562 private:
1563 static bool ShouldRedirect() {
1564 return FLAG_redirect_code_traces;
1565 }
1566
1567 EmbeddedVector<char, 128> filename_;
1568 FILE* file_;
1569 int scope_depth_;
1570};
Steve Block44f0eee2011-05-26 01:26:41 +01001571
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001572} // namespace internal
1573} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01001574
Steve Block44f0eee2011-05-26 01:26:41 +01001575#endif // V8_ISOLATE_H_