blob: 40c815716569c438e42cd94383f377fe683ccf07 [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
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400686 bool IsInternallyUsedPropertyName(Handle<Object> name);
Steve Block44f0eee2011-05-26 01:26:41 +0100687
688 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000689 void ReportFailedAccessCheck(Handle<JSObject> receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100690
691 // Exception throwing support. The caller should use the result
692 // of Throw() as its return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693 Object* Throw(Object* exception, MessageLocation* location = NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000694 Object* ThrowIllegalOperation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000695
696 template <typename T>
697 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
698 MessageLocation* location = NULL) {
699 Throw(*exception, location);
700 return MaybeHandle<T>();
701 }
702
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000703 // Re-throw an exception. This involves no error reporting since error
704 // reporting was handled when the exception was thrown originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000705 Object* ReThrow(Object* exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706
707 // Find the correct handler for the current pending exception. This also
708 // clears and returns the current pending exception.
709 Object* UnwindAndFindHandler();
710
711 // Tries to predict whether an exception will be caught. Note that this can
712 // only produce an estimate, because it is undecidable whether a finally
713 // clause will consume or re-throw an exception. We conservatively assume any
714 // finally clause will behave as if the exception were consumed.
715 enum CatchType { NOT_CAUGHT, CAUGHT_BY_JAVASCRIPT, CAUGHT_BY_EXTERNAL };
716 CatchType PredictExceptionCatcher();
717
Steve Block44f0eee2011-05-26 01:26:41 +0100718 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 // Re-set pending message, script and positions reported to the TryCatch
720 // back to the TLS for re-use when rethrowing.
721 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
722 // Un-schedule an exception that was caught by a TryCatch handler.
723 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100724 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725 // Return pending location if any or unfilled structure.
726 MessageLocation GetMessageLocation();
Steve Block44f0eee2011-05-26 01:26:41 +0100727
728 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 Object* PromoteScheduledException();
Steve Block44f0eee2011-05-26 01:26:41 +0100730
731 // Attempts to compute the current source location, storing the
732 // result in the target out parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000733 bool ComputeLocation(MessageLocation* target);
734 bool ComputeLocationFromException(MessageLocation* target,
735 Handle<Object> exception);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400736 bool ComputeLocationFromStackTrace(MessageLocation* target,
737 Handle<Object> exception);
738
739 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
740 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100741
Steve Block44f0eee2011-05-26 01:26:41 +0100742 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743 Object* StackOverflow();
744 Object* TerminateExecution();
745 void CancelTerminateExecution();
746
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400747 void RequestInterrupt(InterruptCallback callback, void* data);
748 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100749
750 // Administration
751 void Iterate(ObjectVisitor* v);
752 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
753 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100754 void IterateThread(ThreadVisitor* v, char* t);
755
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400756 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100758
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000759 // Returns the native context of the calling JavaScript code. That
760 // is, the native context of the top-most JavaScript frame.
761 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100762
763 void RegisterTryCatchHandler(v8::TryCatch* that);
764 void UnregisterTryCatchHandler(v8::TryCatch* that);
765
766 char* ArchiveThread(char* to);
767 char* RestoreThread(char* from);
768
769 static const char* const kStackOverflowMessage;
770
771 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
772 static const int kBMMaxShift = 250; // See StringSearchBase.
773
774 // Accessors.
775#define GLOBAL_ACCESSOR(type, name, initialvalue) \
776 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100778 return name##_; \
779 } \
780 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100782 name##_ = value; \
783 }
784 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
785#undef GLOBAL_ACCESSOR
786
787#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
788 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000789 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100790 return &(name##_)[0]; \
791 }
792 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
793#undef GLOBAL_ARRAY_ACCESSOR
794
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000795#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
796 inline Handle<type> name(); \
797 inline bool is_##name(type* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
799#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100800
801 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000802 Counters* counters() {
803 // Call InitializeLoggingAndCounters() if logging is needed before
804 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000806 return counters_;
807 }
Steve Block44f0eee2011-05-26 01:26:41 +0100808 CodeRange* code_range() { return code_range_; }
809 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
810 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000811 Logger* logger() {
812 // Call InitializeLoggingAndCounters() if logging is needed before
813 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000814 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000815 return logger_;
816 }
Steve Block44f0eee2011-05-26 01:26:41 +0100817 StackGuard* stack_guard() { return &stack_guard_; }
818 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000819 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100820 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100822 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
823 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000824 MaterializedObjectStore* materialized_object_store() {
825 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100826 }
827
828 MemoryAllocator* memory_allocator() {
829 return memory_allocator_;
830 }
831
832 KeyedLookupCache* keyed_lookup_cache() {
833 return keyed_lookup_cache_;
834 }
835
836 ContextSlotCache* context_slot_cache() {
837 return context_slot_cache_;
838 }
839
840 DescriptorLookupCache* descriptor_lookup_cache() {
841 return descriptor_lookup_cache_;
842 }
843
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000844 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
845
Steve Block44f0eee2011-05-26 01:26:41 +0100846 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000847 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100848 return handle_scope_implementer_;
849 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850 Zone* runtime_zone() { return &runtime_zone_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000851 Zone* interface_descriptor_zone() { return &interface_descriptor_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100852
Ben Murdoch8b112d22011-06-08 16:22:53 +0100853 UnicodeCache* unicode_cache() {
854 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100855 }
856
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100857 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
858 return inner_pointer_to_code_cache_;
859 }
Steve Block44f0eee2011-05-26 01:26:41 +0100860
Steve Block44f0eee2011-05-26 01:26:41 +0100861 GlobalHandles* global_handles() { return global_handles_; }
862
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 EternalHandles* eternal_handles() { return eternal_handles_; }
864
Steve Block44f0eee2011-05-26 01:26:41 +0100865 ThreadManager* thread_manager() { return thread_manager_; }
866
Steve Block44f0eee2011-05-26 01:26:41 +0100867 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
868 return &jsregexp_uncanonicalize_;
869 }
870
871 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
872 return &jsregexp_canonrange_;
873 }
874
Steve Block44f0eee2011-05-26 01:26:41 +0100875 RuntimeState* runtime_state() { return &runtime_state_; }
876
Steve Block44f0eee2011-05-26 01:26:41 +0100877 Builtins* builtins() { return &builtins_; }
878
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100879 void NotifyExtensionInstalled() {
880 has_installed_extensions_ = true;
881 }
882
883 bool has_installed_extensions() { return has_installed_extensions_; }
884
Steve Block44f0eee2011-05-26 01:26:41 +0100885 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
886 regexp_macro_assembler_canonicalize() {
887 return &regexp_macro_assembler_canonicalize_;
888 }
889
890 RegExpStack* regexp_stack() { return regexp_stack_; }
891
892 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
893 interp_canonicalize_mapping() {
894 return &interp_canonicalize_mapping_;
895 }
896
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100898
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000899 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
900 HeapProfiler* heap_profiler() const { return heap_profiler_; }
901
Steve Block44f0eee2011-05-26 01:26:41 +0100902#ifdef DEBUG
903 HistogramInfo* heap_histograms() { return heap_histograms_; }
904
905 JSObject::SpillInformation* js_spill_information() {
906 return &js_spill_information_;
907 }
Steve Block44f0eee2011-05-26 01:26:41 +0100908#endif
909
910 Factory* factory() { return reinterpret_cast<Factory*>(this); }
911
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100913
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100915
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000916 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
917
918 void SetData(uint32_t slot, void* data) {
919 DCHECK(slot < Internals::kNumIsolateDataSlots);
920 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100921 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000922 void* GetData(uint32_t slot) {
923 DCHECK(slot < Internals::kNumIsolateDataSlots);
924 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100925 }
Steve Block44f0eee2011-05-26 01:26:41 +0100926
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 bool serializer_enabled() const { return serializer_enabled_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000928 bool snapshot_available() const {
929 return snapshot_blob_ != NULL && snapshot_blob_->raw_size != 0;
930 }
Steve Block44f0eee2011-05-26 01:26:41 +0100931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 bool IsDead() { return has_fatal_error_; }
933 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000934
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100936
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000937 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100938
939 double time_millis_since_init() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000940 return heap_.MonotonicallyIncreasingTimeInMs() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100941 }
942
943 DateCache* date_cache() {
944 return date_cache_;
945 }
946
947 void set_date_cache(DateCache* date_cache) {
948 if (date_cache != date_cache_) {
949 delete date_cache_;
950 }
951 date_cache_ = date_cache;
952 }
953
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000954 Map* get_initial_js_array_map(ElementsKind kind,
955 Strength strength = Strength::WEAK);
956
957 static const int kArrayProtectorValid = 1;
958 static const int kArrayProtectorInvalid = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000959
960 bool IsFastArrayConstructorPrototypeChainIntact();
961
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000962 // On intent to set an element in object, make sure that appropriate
963 // notifications occur if the set is on the elements of the array or
964 // object prototype. Also ensure that changes to prototype chain between
965 // Array and Object fire notifications.
966 void UpdateArrayProtectorOnSetElement(Handle<JSObject> object);
967 void UpdateArrayProtectorOnSetLength(Handle<JSObject> object) {
968 UpdateArrayProtectorOnSetElement(object);
969 }
970 void UpdateArrayProtectorOnSetPrototype(Handle<JSObject> object) {
971 UpdateArrayProtectorOnSetElement(object);
972 }
973 void UpdateArrayProtectorOnNormalizeElements(Handle<JSObject> object) {
974 UpdateArrayProtectorOnSetElement(object);
975 }
976
977 // Returns true if array is the initial array prototype in any native context.
978 bool IsAnyInitialArrayPrototype(Handle<JSArray> array);
979
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000980 CallInterfaceDescriptorData* call_descriptor_data(int index);
981
982 void IterateDeferredHandles(ObjectVisitor* visitor);
983 void LinkDeferredHandles(DeferredHandles* deferred_handles);
984 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
985
986#ifdef DEBUG
987 bool IsDeferredHandle(Object** location);
988#endif // DEBUG
989
990 bool concurrent_recompilation_enabled() {
991 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000992 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000993 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000994 return optimizing_compile_dispatcher_ != NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 }
996
997 bool concurrent_osr_enabled() const {
998 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000999 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001000 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001001 return optimizing_compile_dispatcher_ != NULL && FLAG_concurrent_osr;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 }
1003
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001004 OptimizingCompileDispatcher* optimizing_compile_dispatcher() {
1005 return optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001006 }
1007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008 int id() const { return static_cast<int>(id_); }
1009
1010 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001011 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001012 HTracer* GetHTracer();
1013 CodeTracer* GetCodeTracer();
1014
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001015 void DumpAndResetCompilationStats();
1016
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1018 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1019 function_entry_hook_ = function_entry_hook;
1020 }
1021
1022 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1023
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001024 void* virtual_handler_register_address() {
1025 return &virtual_handler_register_;
1026 }
1027
1028 void* virtual_slot_register_address() { return &virtual_slot_register_; }
1029
1030 base::RandomNumberGenerator* random_number_generator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001031
1032 // Given an address occupied by a live code object, return that object.
1033 Object* FindCodeObject(Address a);
1034
1035 int NextOptimizationId() {
1036 int id = next_optimization_id_++;
1037 if (!Smi::IsValid(next_optimization_id_)) {
1038 next_optimization_id_ = 0;
1039 }
1040 return id;
1041 }
1042
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001043 void IncrementJsCallsFromApiCounter() { ++js_calls_from_api_counter_; }
1044
1045 unsigned int js_calls_from_api_counter() {
1046 return js_calls_from_api_counter_;
1047 }
1048
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001049 // Get (and lazily initialize) the registry for per-isolate symbols.
1050 Handle<JSObject> GetSymbolRegistry();
1051
1052 void AddCallCompletedCallback(CallCompletedCallback callback);
1053 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1054 void FireCallCompletedCallback();
1055
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001056 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1057 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1058 v8::PromiseRejectEvent event);
1059
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001060 void EnqueueMicrotask(Handle<Object> microtask);
1061 void RunMicrotasks();
1062
1063 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1064 void CountUsage(v8::Isolate::UseCounterFeature feature);
1065
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001066 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1067 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1068
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001069 std::string GetTurboCfgFileName();
1070
1071#if TRACE_MAPS
1072 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1073#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001074
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001075
1076 void AddDetachedContext(Handle<Context> context);
1077 void CheckDetachedContextsAfterGC();
1078
1079 List<Object*>* partial_snapshot_cache() { return &partial_snapshot_cache_; }
1080
1081 void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
1082 array_buffer_allocator_ = allocator;
1083 }
1084 v8::ArrayBuffer::Allocator* array_buffer_allocator() const {
1085 return array_buffer_allocator_;
1086 }
1087
1088 FutexWaitListNode* futex_wait_list_node() { return &futex_wait_list_node_; }
1089
1090 CancelableTaskManager* cancelable_task_manager() {
1091 return cancelable_task_manager_;
1092 }
1093
1094 interpreter::Interpreter* interpreter() const { return interpreter_; }
1095
1096 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001097 explicit Isolate(bool enable_serializer);
Steve Block44f0eee2011-05-26 01:26:41 +01001098
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001099 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001100 friend struct GlobalState;
1101 friend struct InitializeGlobalState;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001102 Handle<JSObject> SetUpSubregistry(Handle<JSObject> registry, Handle<Map> map,
1103 const char* name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001104
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001105 // These fields are accessed through the API, offsets must be kept in sync
1106 // with v8::internal::Internals (in include/v8.h) constants. This is also
1107 // verified in Isolate::Init() using runtime checks.
1108 void* embedder_data_[Internals::kNumIsolateDataSlots];
1109 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001110
Steve Block44f0eee2011-05-26 01:26:41 +01001111 // The per-process lock should be acquired before the ThreadDataTable is
1112 // modified.
1113 class ThreadDataTable {
1114 public:
1115 ThreadDataTable();
1116 ~ThreadDataTable();
1117
1118 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1119 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001120 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001121 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001122
1123 private:
1124 PerIsolateThreadData* list_;
1125 };
1126
1127 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1128 // the Isolate. The top of the stack points to a thread which is currently
1129 // running the Isolate. When the stack is empty, the Isolate is considered
1130 // not entered by any thread and can be Disposed.
1131 // If the same thread enters the Isolate more then once, the entry_count_
1132 // is incremented rather then a new item pushed to the stack.
1133 class EntryStackItem {
1134 public:
1135 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1136 Isolate* previous_isolate,
1137 EntryStackItem* previous_item)
1138 : entry_count(1),
1139 previous_thread_data(previous_thread_data),
1140 previous_isolate(previous_isolate),
1141 previous_item(previous_item) { }
1142
1143 int entry_count;
1144 PerIsolateThreadData* previous_thread_data;
1145 Isolate* previous_isolate;
1146 EntryStackItem* previous_item;
1147
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001148 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001149 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1150 };
1151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001152 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001153
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001154 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1155 static base::Thread::LocalStorageKey isolate_key_;
1156 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001157 static ThreadDataTable* thread_data_table_;
1158
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159 // A global counter for all generated Isolates, might overflow.
1160 static base::Atomic32 isolate_counter_;
1161
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001162#if DEBUG
1163 static base::Atomic32 isolate_key_created_;
1164#endif
1165
Steve Block44f0eee2011-05-26 01:26:41 +01001166 void Deinit();
1167
1168 static void SetIsolateThreadLocals(Isolate* isolate,
1169 PerIsolateThreadData* data);
1170
Steve Block44f0eee2011-05-26 01:26:41 +01001171 // Find the PerThread for this particular (isolate, thread) combination.
1172 // If one does not yet exist, allocate a new one.
1173 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1174
Steve Block44f0eee2011-05-26 01:26:41 +01001175 // Initializes the current thread to run this Isolate.
1176 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1177 // at the same time, this should be prevented using external locking.
1178 void Enter();
1179
1180 // Exits the current thread. The previosuly entered Isolate is restored
1181 // for the thread.
1182 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1183 // at the same time, this should be prevented using external locking.
1184 void Exit();
1185
Steve Block44f0eee2011-05-26 01:26:41 +01001186 void InitializeThreadLocal();
1187
Steve Block44f0eee2011-05-26 01:26:41 +01001188 void MarkCompactPrologue(bool is_compacting,
1189 ThreadLocalTop* archived_thread_data);
1190 void MarkCompactEpilogue(bool is_compacting,
1191 ThreadLocalTop* archived_thread_data);
1192
1193 void FillCache();
1194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195 // Propagate pending exception message to the v8::TryCatch.
1196 // If there is no external try-catch or message was successfully propagated,
1197 // then return true.
1198 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001199
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001200 // Remove per-frame stored materialized objects when we are unwinding
1201 // the frame.
1202 void RemoveMaterializedObjectsOnUnwind(StackFrame* frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001203
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204 base::Atomic32 id_;
1205 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001206 int stack_trace_nesting_level_;
1207 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001208 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001209 Bootstrapper* bootstrapper_;
1210 RuntimeProfiler* runtime_profiler_;
1211 CompilationCache* compilation_cache_;
1212 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001213 CodeRange* code_range_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001214 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001215 Logger* logger_;
1216 StackGuard stack_guard_;
1217 StatsTable* stats_table_;
1218 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001220 DeoptimizerData* deoptimizer_data_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001221 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001222 ThreadLocalTop thread_local_top_;
1223 bool capture_stack_trace_for_uncaught_exceptions_;
1224 int stack_trace_for_uncaught_exceptions_frame_limit_;
1225 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001226 MemoryAllocator* memory_allocator_;
1227 KeyedLookupCache* keyed_lookup_cache_;
1228 ContextSlotCache* context_slot_cache_;
1229 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001230 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001231 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001232 UnicodeCache* unicode_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 Zone runtime_zone_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001234 Zone interface_descriptor_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001235 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001236 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001237 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001238 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001239 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001240 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001241 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001242 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1243 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001244 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1245 regexp_macro_assembler_canonicalize_;
1246 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001247 DateCache* date_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001248 unibrow::Mapping<unibrow::Ecma262Canonicalize> interp_canonicalize_mapping_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 CallInterfaceDescriptorData* call_descriptor_data_;
1250 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001251
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252 // Whether the isolate has been created for snapshotting.
1253 bool serializer_enabled_;
1254
1255 // True if fatal error has been signaled for this isolate.
1256 bool has_fatal_error_;
1257
1258 // True if this isolate was initialized from a snapshot.
1259 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001260
1261 // Time stamp at initialization.
1262 double time_millis_at_init_;
1263
Steve Block44f0eee2011-05-26 01:26:41 +01001264#ifdef DEBUG
1265 // A static array of histogram info for each type.
1266 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1267 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001268#endif
1269
Steve Block44f0eee2011-05-26 01:26:41 +01001270 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 CpuProfiler* cpu_profiler_;
1272 HeapProfiler* heap_profiler_;
1273 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001274
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001275 interpreter::Interpreter* interpreter_;
1276
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001277 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1278 std::queue<InterruptEntry> api_interrupts_queue_;
1279
Steve Block44f0eee2011-05-26 01:26:41 +01001280#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1281 type name##_;
1282 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1283#undef GLOBAL_BACKING_STORE
1284
1285#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1286 type name##_[length];
1287 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1288#undef GLOBAL_ARRAY_BACKING_STORE
1289
1290#ifdef DEBUG
1291 // This class is huge and has a number of fields controlled by
1292 // preprocessor defines. Make sure the offsets of these fields agree
1293 // between compilation units.
1294#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1295 static const intptr_t name##_debug_offset_;
1296 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1297 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1298#undef ISOLATE_FIELD_OFFSET
1299#endif
1300
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301 DeferredHandles* deferred_handles_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001302 OptimizingCompileDispatcher* optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001303
1304 // Counts deopt points if deopt_every_n_times is enabled.
1305 unsigned int stress_deopt_count_;
1306
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001307 Address virtual_handler_register_;
1308 Address virtual_slot_register_;
1309
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001310 int next_optimization_id_;
1311
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001312 // Counts javascript calls from the API. Wraps around on overflow.
1313 unsigned int js_calls_from_api_counter_;
1314
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001315#if TRACE_MAPS
1316 int next_unique_sfi_id_;
1317#endif
1318
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001319 // List of callbacks when a Call completes.
1320 List<CallCompletedCallback> call_completed_callbacks_;
1321
1322 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001323 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001325 List<Object*> partial_snapshot_cache_;
1326
1327 v8::ArrayBuffer::Allocator* array_buffer_allocator_;
1328
1329 FutexWaitListNode futex_wait_list_node_;
1330
1331 CancelableTaskManager* cancelable_task_manager_;
1332
1333 v8::Isolate::AbortOnUncaughtExceptionCallback
1334 abort_on_uncaught_exception_callback_;
1335
Steve Block44f0eee2011-05-26 01:26:41 +01001336 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337 friend class HandleScopeImplementer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001338 friend class OptimizingCompileDispatcher;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001340 friend class ThreadManager;
1341 friend class Simulator;
1342 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001343 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001344 friend class TestMemoryAllocatorScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345 friend class TestCodeRangeScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001346 friend class v8::Isolate;
1347 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001348 friend class v8::Unlocker;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001349 friend v8::StartupData v8::V8::CreateSnapshotDataBlob(const char*);
Steve Block44f0eee2011-05-26 01:26:41 +01001350
1351 DISALLOW_COPY_AND_ASSIGN(Isolate);
1352};
1353
1354
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001355#undef FIELD_ACCESSOR
1356#undef THREAD_LOCAL_TOP_ACCESSOR
1357
1358
1359class PromiseOnStack {
1360 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001361 PromiseOnStack(Handle<JSFunction> function, Handle<JSObject> promise,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362 PromiseOnStack* prev)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001363 : function_(function), promise_(promise), prev_(prev) {}
1364 Handle<JSFunction> function() { return function_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 Handle<JSObject> promise() { return promise_; }
1366 PromiseOnStack* prev() { return prev_; }
1367
1368 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001369 Handle<JSFunction> function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001370 Handle<JSObject> promise_;
1371 PromiseOnStack* prev_;
1372};
1373
1374
Steve Block44f0eee2011-05-26 01:26:41 +01001375// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1376// class as a work around for a bug in the generated code found with these
1377// versions of GCC. See V8 issue 122 for details.
1378class SaveContext BASE_EMBEDDED {
1379 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001380 explicit SaveContext(Isolate* isolate);
1381 ~SaveContext();
Steve Block44f0eee2011-05-26 01:26:41 +01001382
1383 Handle<Context> context() { return context_; }
1384 SaveContext* prev() { return prev_; }
1385
1386 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001387 bool IsBelowFrame(JavaScriptFrame* frame) {
1388 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001389 }
1390
1391 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001392 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001393 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001394 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001395 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001396};
1397
1398
1399class AssertNoContextChange BASE_EMBEDDED {
1400#ifdef DEBUG
1401 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001402 explicit AssertNoContextChange(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001403 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001405 }
1406
1407 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001408 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001409 Handle<Context> context_;
1410#else
1411 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001412 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001413#endif
1414};
1415
1416
1417class ExecutionAccess BASE_EMBEDDED {
1418 public:
1419 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1420 Lock(isolate);
1421 }
1422 ~ExecutionAccess() { Unlock(isolate_); }
1423
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001424 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1425 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001426
1427 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001428 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001429 }
1430
1431 private:
1432 Isolate* isolate_;
1433};
1434
1435
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001436// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001437class StackLimitCheck BASE_EMBEDDED {
1438 public:
1439 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1440
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001441 // Use this to check for stack-overflows in C++ code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001442 bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001443 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001444 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001445 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001447 // Use this to check for interrupt request in C++ code.
1448 bool InterruptRequested() {
1449 StackGuard* stack_guard = isolate_->stack_guard();
1450 return GetCurrentStackPosition() < stack_guard->climit();
1451 }
1452
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001453 // Use this to check for stack-overflow when entering runtime from JS code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001454 bool JsHasOverflowed(uintptr_t gap = 0) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001455
Steve Block44f0eee2011-05-26 01:26:41 +01001456 private:
1457 Isolate* isolate_;
1458};
1459
1460
1461// Support for temporarily postponing interrupts. When the outermost
1462// postpone scope is left the interrupts will be re-enabled and any
1463// interrupts that occurred while in the scope will be taken into
1464// account.
1465class PostponeInterruptsScope BASE_EMBEDDED {
1466 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467 PostponeInterruptsScope(Isolate* isolate,
1468 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1469 : stack_guard_(isolate->stack_guard()),
1470 intercept_mask_(intercept_mask),
1471 intercepted_flags_(0) {
1472 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001473 }
1474
1475 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001476 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001477 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478
1479 // Find the bottom-most scope that intercepts this interrupt.
1480 // Return whether the interrupt has been intercepted.
1481 bool Intercept(StackGuard::InterruptFlag flag);
1482
Steve Block44f0eee2011-05-26 01:26:41 +01001483 private:
1484 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001485 int intercept_mask_;
1486 int intercepted_flags_;
1487 PostponeInterruptsScope* prev_;
1488
1489 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001490};
1491
1492
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001493class CodeTracer final : public Malloced {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001494 public:
1495 explicit CodeTracer(int isolate_id)
1496 : file_(NULL),
1497 scope_depth_(0) {
1498 if (!ShouldRedirect()) {
1499 file_ = stdout;
1500 return;
1501 }
Steve Block44f0eee2011-05-26 01:26:41 +01001502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001503 if (FLAG_redirect_code_traces_to == NULL) {
1504 SNPrintF(filename_,
1505 "code-%d-%d.asm",
1506 base::OS::GetCurrentProcessId(),
1507 isolate_id);
1508 } else {
1509 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1510 }
Steve Block44f0eee2011-05-26 01:26:41 +01001511
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001512 WriteChars(filename_.start(), "", 0, false);
1513 }
Steve Block44f0eee2011-05-26 01:26:41 +01001514
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001515 class Scope {
1516 public:
1517 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1518 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001519
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001520 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001521
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001522 private:
1523 CodeTracer* tracer_;
1524 };
1525
1526 void OpenFile() {
1527 if (!ShouldRedirect()) {
1528 return;
1529 }
1530
1531 if (file_ == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532 file_ = base::OS::FOpen(filename_.start(), "ab");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 }
1534
1535 scope_depth_++;
1536 }
1537
1538 void CloseFile() {
1539 if (!ShouldRedirect()) {
1540 return;
1541 }
1542
1543 if (--scope_depth_ == 0) {
1544 fclose(file_);
1545 file_ = NULL;
1546 }
1547 }
1548
1549 FILE* file() const { return file_; }
1550
1551 private:
1552 static bool ShouldRedirect() {
1553 return FLAG_redirect_code_traces;
1554 }
1555
1556 EmbeddedVector<char, 128> filename_;
1557 FILE* file_;
1558 int scope_depth_;
1559};
Steve Block44f0eee2011-05-26 01:26:41 +01001560
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001561} // namespace internal
1562} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01001563
Steve Block44f0eee2011-05-26 01:26:41 +01001564#endif // V8_ISOLATE_H_