blob: 4ca842e5ea64b1dac6b442a4eee2712688f0a493 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block44f0eee2011-05-26 01:26:41 +01004
5#ifndef V8_ISOLATE_H_
6#define V8_ISOLATE_H_
7
Ben Murdoch61f157c2016-09-16 13:49:30 +01008#include <memory>
Emily Bernierd0a1eb72015-03-24 16:35:39 -04009#include <queue>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include <set>
11
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "include/v8-debug.h"
13#include "src/allocation.h"
14#include "src/assert-scope.h"
Ben Murdochda12d292016-06-02 14:46:10 +010015#include "src/base/accounting-allocator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "src/base/atomicops.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010017#include "src/base/hashmap.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/builtins.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/cancelable-task.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020#include "src/contexts.h"
21#include "src/date.h"
22#include "src/execution.h"
23#include "src/frames.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024#include "src/futex-emulation.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025#include "src/global-handles.h"
26#include "src/handles.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027#include "src/heap/heap.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028#include "src/messages.h"
29#include "src/optimizing-compile-dispatcher.h"
30#include "src/regexp/regexp-stack.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031#include "src/runtime-profiler.h"
Ben Murdochda12d292016-06-02 14:46:10 +010032#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033#include "src/zone.h"
Steve Block44f0eee2011-05-26 01:26:41 +010034
35namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036
37namespace base {
38class RandomNumberGenerator;
39}
40
Steve Block44f0eee2011-05-26 01:26:41 +010041namespace internal {
42
Emily Bernierd0a1eb72015-03-24 16:35:39 -040043class BasicBlockProfiler;
Steve Block44f0eee2011-05-26 01:26:41 +010044class Bootstrapper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045class CallInterfaceDescriptorData;
Ben Murdoch61f157c2016-09-16 13:49:30 +010046class CodeAgingHelper;
47class CodeEventDispatcher;
Steve Block44f0eee2011-05-26 01:26:41 +010048class CodeGenerator;
49class CodeRange;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050class CodeStubDescriptor;
51class CodeTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010052class CompilationCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040053class CompilationStatistics;
Steve Block44f0eee2011-05-26 01:26:41 +010054class ContextSlotCache;
Steve Block44f0eee2011-05-26 01:26:41 +010055class Counters;
56class CpuFeatures;
57class CpuProfiler;
58class DeoptimizerData;
59class Deserializer;
60class EmptyStatement;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061class ExternalCallbackScope;
Steve Block44f0eee2011-05-26 01:26:41 +010062class ExternalReferenceTable;
63class Factory;
Steve Block44f0eee2011-05-26 01:26:41 +010064class HandleScopeImplementer;
65class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066class HStatistics;
67class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010068class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010069class InnerPointerToCodeCache;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000070class Logger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071class MaterializedObjectStore;
Ben Murdoch61f157c2016-09-16 13:49:30 +010072class PositionsRecorder;
Steve Block44f0eee2011-05-26 01:26:41 +010073class RegExpStack;
74class SaveContext;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000075class StatsTable;
Steve Block44f0eee2011-05-26 01:26:41 +010076class StringTracker;
77class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010079class ThreadManager;
80class ThreadState;
81class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082class UnicodeCache;
83template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010084
85// 'void function pointer', used to roundtrip the
86// ExternalReference::ExternalReferenceRedirector since we can not include
87// assembler.h, where it is defined, here.
88typedef void* ExternalReferenceRedirectorPointer();
89
90
Steve Block44f0eee2011-05-26 01:26:41 +010091class Debug;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010093class Redirection;
94class Simulator;
Steve Block44f0eee2011-05-26 01:26:41 +010095
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000096namespace interpreter {
97class Interpreter;
98}
Steve Block44f0eee2011-05-26 01:26:41 +010099
100// Static indirection table for handles to constants. If a frame
101// element represents a constant, the data contains an index into
102// this table of handles to the actual constants.
103// Static indirection table for handles to constants. If a Result
104// represents a constant, the data contains an index into this table
105// of handles to the actual constants.
106typedef ZoneList<Handle<Object> > ZoneObjectList;
107
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100109 do { \
110 Isolate* __isolate__ = (isolate); \
111 if (__isolate__->has_scheduled_exception()) { \
112 return __isolate__->PromoteScheduledException(); \
113 } \
114 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100115
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116// Macros for MaybeHandle.
117
118#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
119 do { \
120 Isolate* __isolate__ = (isolate); \
121 if (__isolate__->has_scheduled_exception()) { \
122 __isolate__->PromoteScheduledException(); \
123 return value; \
124 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100125 } while (false)
126
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
128 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
129
Ben Murdoch61f157c2016-09-16 13:49:30 +0100130#define RETURN_RESULT_OR_FAILURE(isolate, call) \
131 do { \
132 Handle<Object> __result__; \
133 Isolate* __isolate__ = (isolate); \
134 if (!(call).ToHandle(&__result__)) { \
135 DCHECK(__isolate__->has_pending_exception()); \
136 return __isolate__->heap()->exception(); \
137 } \
138 return *__result__; \
139 } while (false)
140
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000141#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
142 do { \
143 if (!(call).ToHandle(&dst)) { \
144 DCHECK((isolate)->has_pending_exception()); \
145 return value; \
146 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100147 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100148
Ben Murdoch61f157c2016-09-16 13:49:30 +0100149#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
150 do { \
151 Isolate* __isolate__ = (isolate); \
152 ASSIGN_RETURN_ON_EXCEPTION_VALUE(__isolate__, dst, call, \
153 __isolate__->heap()->exception()); \
154 } while (false)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155
156#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
157 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
158
Ben Murdoch61f157c2016-09-16 13:49:30 +0100159#define THROW_NEW_ERROR(isolate, call, T) \
160 do { \
161 Isolate* __isolate__ = (isolate); \
162 return __isolate__->Throw<T>(__isolate__->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 } while (false)
164
Ben Murdoch61f157c2016-09-16 13:49:30 +0100165#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
166 do { \
167 Isolate* __isolate__ = (isolate); \
168 return __isolate__->Throw(*__isolate__->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 } while (false)
170
171#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
172 do { \
173 if ((call).is_null()) { \
174 DCHECK((isolate)->has_pending_exception()); \
175 return value; \
176 } \
177 } while (false)
178
Ben Murdoch61f157c2016-09-16 13:49:30 +0100179#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
180 do { \
181 Isolate* __isolate__ = (isolate); \
182 RETURN_ON_EXCEPTION_VALUE(__isolate__, call, \
183 __isolate__->heap()->exception()); \
184 } while (false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185
186#define RETURN_ON_EXCEPTION(isolate, call, T) \
187 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
188
Steve Block44f0eee2011-05-26 01:26:41 +0100189
Ben Murdoch589d6972011-11-30 16:04:58 +0000190#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
191 C(Handler, handler) \
192 C(CEntryFP, c_entry_fp) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400193 C(CFunction, c_function) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000194 C(Context, context) \
195 C(PendingException, pending_exception) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196 C(PendingHandlerContext, pending_handler_context) \
197 C(PendingHandlerCode, pending_handler_code) \
198 C(PendingHandlerOffset, pending_handler_offset) \
199 C(PendingHandlerFP, pending_handler_fp) \
200 C(PendingHandlerSP, pending_handler_sp) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000201 C(ExternalCaughtException, external_caught_exception) \
202 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100203
Ben Murdochda12d292016-06-02 14:46:10 +0100204#define FOR_WITH_HANDLE_SCOPE(isolate, loop_var_type, init, loop_var, \
205 limit_check, increment, body) \
206 do { \
207 loop_var_type init; \
208 loop_var_type for_with_handle_limit = loop_var; \
209 Isolate* for_with_handle_isolate = isolate; \
210 while (limit_check) { \
211 for_with_handle_limit += 1024; \
212 HandleScope loop_scope(for_with_handle_isolate); \
213 for (; limit_check && loop_var < for_with_handle_limit; increment) { \
214 body \
215 } \
216 } \
217 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100218
Ben Murdoch8b112d22011-06-08 16:22:53 +0100219// Platform-independent, reliable thread identifier.
220class ThreadId {
221 public:
222 // Creates an invalid ThreadId.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400223 ThreadId() { base::NoBarrier_Store(&id_, kInvalidId); }
224
225 ThreadId& operator=(const ThreadId& other) {
226 base::NoBarrier_Store(&id_, base::NoBarrier_Load(&other.id_));
227 return *this;
228 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100229
230 // Returns ThreadId for current thread.
231 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
232
233 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
234 static ThreadId Invalid() { return ThreadId(kInvalidId); }
235
236 // Compares ThreadIds for equality.
237 INLINE(bool Equals(const ThreadId& other) const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400238 return base::NoBarrier_Load(&id_) == base::NoBarrier_Load(&other.id_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100239 }
240
241 // Checks whether this ThreadId refers to any thread.
242 INLINE(bool IsValid() const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400243 return base::NoBarrier_Load(&id_) != kInvalidId;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100244 }
245
246 // Converts ThreadId to an integer representation
247 // (required for public API: V8::V8::GetCurrentThreadId).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400248 int ToInteger() const { return static_cast<int>(base::NoBarrier_Load(&id_)); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100249
250 // Converts ThreadId to an integer representation
251 // (required for public API: V8::V8::TerminateExecution).
252 static ThreadId FromInteger(int id) { return ThreadId(id); }
253
254 private:
255 static const int kInvalidId = -1;
256
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400257 explicit ThreadId(int id) { base::NoBarrier_Store(&id_, id); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100258
259 static int AllocateThreadId();
260
261 static int GetCurrentThreadId();
262
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400263 base::Atomic32 id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100264
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100266
267 friend class Isolate;
268};
269
270
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271#define FIELD_ACCESSOR(type, name) \
272 inline void set_##name(type v) { name##_ = v; } \
273 inline type name() const { return name##_; }
274
275
Steve Block44f0eee2011-05-26 01:26:41 +0100276class ThreadLocalTop BASE_EMBEDDED {
277 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100278 // Does early low-level initialization that does not depend on the
279 // isolate being present.
280 ThreadLocalTop();
281
Steve Block44f0eee2011-05-26 01:26:41 +0100282 // Initialize the thread data.
283 void Initialize();
284
285 // Get the top C++ try catch handler or NULL if none are registered.
286 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100288 // used for comparison with addresses into the JS stack. If such an
289 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000290 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100291
292 // Get the address of the top C++ try catch handler or NULL if
293 // none are registered.
294 //
295 // This method always returns an address that can be compared to
296 // pointers into the JavaScript stack. When running on actual
297 // hardware, try_catch_handler_address and TryCatchHandler return
298 // the same pointer. When running on a simulator with a separate JS
299 // stack, try_catch_handler_address returns a JS stack address that
300 // corresponds to the place on the JS stack where the C++ handler
301 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 Address try_catch_handler_address() {
303 return reinterpret_cast<Address>(
304 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100305 }
306
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000307 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100308
Ben Murdoch257744e2011-11-30 15:57:28 +0000309 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100310 // The context where the current execution method is created and for variable
311 // lookups.
312 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100313 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 Object* pending_exception_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315
316 // Communication channel between Isolate::FindHandler and the CEntryStub.
317 Context* pending_handler_context_;
318 Code* pending_handler_code_;
319 intptr_t pending_handler_offset_;
320 Address pending_handler_fp_;
321 Address pending_handler_sp_;
322
323 // Communication channel between Isolate::Throw and message consumers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000324 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100325 Object* pending_message_obj_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326
Steve Block44f0eee2011-05-26 01:26:41 +0100327 // Use a separate value for scheduled exceptions to preserve the
328 // invariants that hold about pending_exception. We may want to
329 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100331 bool external_caught_exception_;
332 SaveContext* save_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100333
334 // Stack.
335 Address c_entry_fp_; // the frame pointer of the top c entry frame
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 Address handler_; // try-blocks are chained through the stack
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400337 Address c_function_; // C function that was called at c entry.
Steve Block44f0eee2011-05-26 01:26:41 +0100338
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 // Throwing an exception may cause a Promise rejection. For this purpose
340 // we keep track of a stack of nested promises and the corresponding
341 // try-catch handlers.
342 PromiseOnStack* promise_on_stack_;
343
Steve Block44f0eee2011-05-26 01:26:41 +0100344#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100345 Simulator* simulator_;
346#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100347
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100348 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 // the external callback we're currently in
350 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100351 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100352
Steve Block44f0eee2011-05-26 01:26:41 +0100353 // Call back function to report unsafe JS accesses.
354 v8::FailedAccessCheckCallback failed_access_check_callback_;
355
356 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100357 void InitializeInternal();
358
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100360};
361
Steve Block44f0eee2011-05-26 01:26:41 +0100362
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000363#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100364
Ben Murdoch61f157c2016-09-16 13:49:30 +0100365#define ISOLATE_INIT_SIMULATOR_LIST(V) \
366 V(bool, simulator_initialized, false) \
367 V(base::HashMap*, simulator_i_cache, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100369#else
370
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100372
373#endif
374
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375
Steve Block44f0eee2011-05-26 01:26:41 +0100376#ifdef DEBUG
377
Ben Murdoch61f157c2016-09-16 13:49:30 +0100378#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
379 V(CommentStatistic, paged_space_comments_statistics, \
380 CommentStatistic::kMaxComments + 1) \
381 V(int, code_kind_statistics, AbstractCode::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100382#else
383
384#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
385
386#endif
387
Steve Block44f0eee2011-05-26 01:26:41 +0100388#define ISOLATE_INIT_ARRAY_LIST(V) \
389 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100391 V(int, bad_char_shift_table, kUC16AlphabetSize) \
392 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
393 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000394 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100395 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
396
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100398
Ben Murdoch61f157c2016-09-16 13:49:30 +0100399#define ISOLATE_INIT_LIST(V) \
400 /* Assembler state. */ \
401 V(FatalErrorCallback, exception_behavior, nullptr) \
402 V(LogEventCallback, event_logger, nullptr) \
403 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, nullptr) \
404 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, \
405 nullptr) \
406 /* State for Relocatable. */ \
407 V(Relocatable*, relocatable_top, nullptr) \
408 V(DebugObjectCache*, string_stream_debug_object_cache, nullptr) \
409 V(Object*, string_stream_current_security_token, nullptr) \
410 V(ExternalReferenceTable*, external_reference_table, nullptr) \
411 V(intptr_t*, api_external_references, nullptr) \
412 V(base::HashMap*, external_reference_map, nullptr) \
413 V(base::HashMap*, root_index_map, nullptr) \
414 V(int, pending_microtask_count, 0) \
415 V(HStatistics*, hstatistics, nullptr) \
416 V(CompilationStatistics*, turbo_statistics, nullptr) \
417 V(HTracer*, htracer, nullptr) \
418 V(CodeTracer*, code_tracer, nullptr) \
419 V(bool, fp_stubs_generated, false) \
420 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
421 V(PromiseRejectCallback, promise_reject_callback, nullptr) \
422 V(const v8::StartupData*, snapshot_blob, nullptr) \
423 V(int, code_and_metadata_size, 0) \
424 V(int, bytecode_and_metadata_size, 0) \
425 /* true if being profiled. Causes collection of extra compile info. */ \
426 V(bool, is_profiling, false) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 ISOLATE_INIT_SIMULATOR_LIST(V)
428
429#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
430 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
431 inline type name() const { return thread_local_top_.name##_; }
432
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433#define THREAD_LOCAL_TOP_ADDRESS(type, name) \
434 type* name##_address() { return &thread_local_top_.name##_; }
435
Steve Block44f0eee2011-05-26 01:26:41 +0100436
437class Isolate {
438 // These forward declarations are required to make the friend declarations in
439 // PerIsolateThreadData work on some older versions of gcc.
440 class ThreadDataTable;
441 class EntryStackItem;
442 public:
443 ~Isolate();
444
Steve Block44f0eee2011-05-26 01:26:41 +0100445 // A thread has a PerIsolateThreadData instance for each isolate that it has
446 // entered. That instance is allocated when the isolate is initially entered
447 // and reused on subsequent entries.
448 class PerIsolateThreadData {
449 public:
450 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
451 : isolate_(isolate),
452 thread_id_(thread_id),
453 stack_limit_(0),
454 thread_state_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100456 simulator_(NULL),
457#endif
458 next_(NULL),
459 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100461 Isolate* isolate() const { return isolate_; }
462 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100463
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 FIELD_ACCESSOR(uintptr_t, stack_limit)
465 FIELD_ACCESSOR(ThreadState*, thread_state)
466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467#if USE_SIMULATOR
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 FIELD_ACCESSOR(Simulator*, simulator)
Steve Block44f0eee2011-05-26 01:26:41 +0100469#endif
470
471 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100472 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100473 }
474
475 private:
476 Isolate* isolate_;
477 ThreadId thread_id_;
478 uintptr_t stack_limit_;
479 ThreadState* thread_state_;
480
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100482 Simulator* simulator_;
483#endif
484
485 PerIsolateThreadData* next_;
486 PerIsolateThreadData* prev_;
487
488 friend class Isolate;
489 friend class ThreadDataTable;
490 friend class EntryStackItem;
491
492 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
493 };
494
495
496 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000497#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
498 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000500 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100501 };
502
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 static void InitializeOncePerProcess();
504
Steve Block44f0eee2011-05-26 01:26:41 +0100505 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
506 // not currently set).
507 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
508 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100510 }
511
512 // Returns the isolate inside which the current thread is running.
513 INLINE(static Isolate* Current()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400514 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100515 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 base::Thread::GetExistingThreadLocal(isolate_key_));
517 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100518 return isolate;
519 }
520
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000521 // Usually called by Init(), but can be called early e.g. to allow
522 // testing components that require logging but not the whole
523 // isolate.
524 //
525 // Safe to call more than once.
526 void InitializeLoggingAndCounters();
527
Steve Block44f0eee2011-05-26 01:26:41 +0100528 bool Init(Deserializer* des);
529
Steve Block44f0eee2011-05-26 01:26:41 +0100530 // True if at least one thread Enter'ed this isolate.
531 bool IsInUse() { return entry_stack_ != NULL; }
532
533 // Destroys the non-default isolates.
534 // Sets default isolate into "has_been_disposed" state rather then destroying,
535 // for legacy API reasons.
536 void TearDown();
537
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100539
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000540 void ClearSerializerData();
541
Ben Murdoch257744e2011-11-30 15:57:28 +0000542 // Find the PerThread for this particular (isolate, thread) combination
543 // If one does not yet exist, return null.
544 PerIsolateThreadData* FindPerThreadDataForThisThread();
545
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000546 // Find the PerThread for given (isolate, thread) combination
547 // If one does not yet exist, return null.
548 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100549
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000550 // Discard the PerThread for this particular (isolate, thread) combination
551 // If one does not yet exist, no-op.
552 void DiscardPerThreadDataForThisThread();
553
Steve Block44f0eee2011-05-26 01:26:41 +0100554 // Returns the key used to store the pointer to the current isolate.
555 // Used internally for V8 threads that do not execute JavaScript but still
556 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100558 return isolate_key_;
559 }
Steve Block44f0eee2011-05-26 01:26:41 +0100560
561 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100563 return thread_id_key_;
564 }
Steve Block44f0eee2011-05-26 01:26:41 +0100565
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100567
Steve Block44f0eee2011-05-26 01:26:41 +0100568 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000570
Steve Block44f0eee2011-05-26 01:26:41 +0100571 Address get_address_from_id(AddressId id);
572
573 // Access to top context (where the current function object was created).
574 Context* context() { return thread_local_top_.context_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000575 inline void set_context(Context* context);
Steve Block44f0eee2011-05-26 01:26:41 +0100576 Context** context_address() { return &thread_local_top_.context_; }
577
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000578 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100579
580 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000581 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100582
583 // Interface to pending exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000584 inline Object* pending_exception();
585 inline void set_pending_exception(Object* exception_obj);
586 inline void clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000588 THREAD_LOCAL_TOP_ADDRESS(Object*, pending_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000590 inline bool has_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 THREAD_LOCAL_TOP_ADDRESS(Context*, pending_handler_context)
593 THREAD_LOCAL_TOP_ADDRESS(Code*, pending_handler_code)
594 THREAD_LOCAL_TOP_ADDRESS(intptr_t, pending_handler_offset)
595 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_fp)
596 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_sp)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597
598 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
599
Steve Block44f0eee2011-05-26 01:26:41 +0100600 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100602 }
Steve Block44f0eee2011-05-26 01:26:41 +0100603 bool* external_caught_exception_address() {
604 return &thread_local_top_.external_caught_exception_;
605 }
606
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 THREAD_LOCAL_TOP_ADDRESS(Object*, scheduled_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000609 inline void clear_pending_message();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000610 Address pending_message_obj_address() {
611 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
612 }
613
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 inline Object* scheduled_exception();
615 inline bool has_scheduled_exception();
616 inline void clear_scheduled_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000618 bool IsJavaScriptHandlerOnTop(Object* exception);
619 bool IsExternalHandlerOnTop(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000620
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000621 inline bool is_catchable_by_javascript(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622
Steve Block44f0eee2011-05-26 01:26:41 +0100623 // JS execution stack (see frames.h).
624 static Address c_entry_fp(ThreadLocalTop* thread) {
625 return thread->c_entry_fp_;
626 }
627 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400628 Address c_function() { return thread_local_top_.c_function_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100629
630 inline Address* c_entry_fp_address() {
631 return &thread_local_top_.c_entry_fp_;
632 }
633 inline Address* handler_address() { return &thread_local_top_.handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400634 inline Address* c_function_address() {
635 return &thread_local_top_.c_function_;
636 }
Steve Block44f0eee2011-05-26 01:26:41 +0100637
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 // Bottom JS entry.
639 Address js_entry_sp() {
640 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100641 }
642 inline Address* js_entry_sp_address() {
643 return &thread_local_top_.js_entry_sp_;
644 }
Steve Block44f0eee2011-05-26 01:26:41 +0100645
Steve Block44f0eee2011-05-26 01:26:41 +0100646 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100647 // a builtin object, or a JS global object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648 inline Handle<JSGlobalObject> global_object();
Steve Block44f0eee2011-05-26 01:26:41 +0100649
650 // Returns the global proxy object of the current context.
Ben Murdochc5610432016-08-08 18:44:38 +0100651 inline Handle<JSObject> global_proxy();
Steve Block44f0eee2011-05-26 01:26:41 +0100652
Steve Block44f0eee2011-05-26 01:26:41 +0100653 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
654 void FreeThreadResources() { thread_local_top_.Free(); }
655
656 // This method is called by the api after operations that may throw
657 // exceptions. If an exception was thrown and not handled by an external
658 // handler the exception is scheduled to be rethrown when we return to running
659 // JavaScript code. If an exception is scheduled true is returned.
660 bool OptionalRescheduleException(bool is_bottom_call);
661
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 // Push and pop a promise and the current try-catch handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000663 void PushPromise(Handle<JSObject> promise, Handle<JSFunction> function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 void PopPromise();
665 Handle<Object> GetPromiseOnStackOnThrow();
666
Ben Murdoch8b112d22011-06-08 16:22:53 +0100667 class ExceptionScope {
668 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000669 // Scope currently can only be used for regular exceptions,
670 // not termination exception.
671 inline explicit ExceptionScope(Isolate* isolate);
672 inline ~ExceptionScope();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100673
674 private:
675 Isolate* isolate_;
676 Handle<Object> pending_exception_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100677 };
678
Steve Block44f0eee2011-05-26 01:26:41 +0100679 void SetCaptureStackTraceForUncaughtExceptions(
680 bool capture,
681 int frame_limit,
682 StackTrace::StackTraceOptions options);
683
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 void SetAbortOnUncaughtExceptionCallback(
685 v8::Isolate::AbortOnUncaughtExceptionCallback callback);
686
687 enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
Steve Block44f0eee2011-05-26 01:26:41 +0100688 void PrintCurrentStackTrace(FILE* out);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000689 void PrintStack(StringStream* accumulator,
690 PrintStackMode mode = kPrintStackVerbose);
691 void PrintStack(FILE* out, PrintStackMode mode = kPrintStackVerbose);
Steve Block44f0eee2011-05-26 01:26:41 +0100692 Handle<String> StackTraceString();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000693 NO_INLINE(void PushStackTraceAndDie(unsigned int magic, void* ptr1,
694 void* ptr2, unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100695 Handle<JSArray> CaptureCurrentStackTrace(
696 int frame_limit,
697 StackTrace::StackTraceOptions options);
Ben Murdochda12d292016-06-02 14:46:10 +0100698 Handle<Object> CaptureSimpleStackTrace(Handle<JSReceiver> error_object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 Handle<Object> caller);
Ben Murdochda12d292016-06-02 14:46:10 +0100700 MaybeHandle<JSReceiver> CaptureAndSetDetailedStackTrace(
701 Handle<JSReceiver> error_object);
702 MaybeHandle<JSReceiver> CaptureAndSetSimpleStackTrace(
703 Handle<JSReceiver> error_object, Handle<Object> caller);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400704 Handle<JSArray> GetDetailedStackTrace(Handle<JSObject> error_object);
705 Handle<JSArray> GetDetailedFromSimpleStackTrace(
706 Handle<JSObject> error_object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100707
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000708 // Returns if the given context may access the given global object. If
Steve Block44f0eee2011-05-26 01:26:41 +0100709 // the result is false, the pending exception is guaranteed to be
710 // set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000711 bool MayAccess(Handle<Context> accessing_context, Handle<JSObject> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000712
Steve Block44f0eee2011-05-26 01:26:41 +0100713 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000714 void ReportFailedAccessCheck(Handle<JSObject> receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100715
716 // Exception throwing support. The caller should use the result
Ben Murdochc5610432016-08-08 18:44:38 +0100717 // of Throw() as its return vaue.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 Object* Throw(Object* exception, MessageLocation* location = NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000719 Object* ThrowIllegalOperation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000720
721 template <typename T>
722 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
723 MessageLocation* location = NULL) {
724 Throw(*exception, location);
725 return MaybeHandle<T>();
726 }
727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000728 // Re-throw an exception. This involves no error reporting since error
729 // reporting was handled when the exception was thrown originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000730 Object* ReThrow(Object* exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000731
732 // Find the correct handler for the current pending exception. This also
733 // clears and returns the current pending exception.
734 Object* UnwindAndFindHandler();
735
736 // Tries to predict whether an exception will be caught. Note that this can
737 // only produce an estimate, because it is undecidable whether a finally
738 // clause will consume or re-throw an exception. We conservatively assume any
739 // finally clause will behave as if the exception were consumed.
740 enum CatchType { NOT_CAUGHT, CAUGHT_BY_JAVASCRIPT, CAUGHT_BY_EXTERNAL };
741 CatchType PredictExceptionCatcher();
742
Steve Block44f0eee2011-05-26 01:26:41 +0100743 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000744 // Re-set pending message, script and positions reported to the TryCatch
745 // back to the TLS for re-use when rethrowing.
746 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
747 // Un-schedule an exception that was caught by a TryCatch handler.
748 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100749 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750 // Return pending location if any or unfilled structure.
751 MessageLocation GetMessageLocation();
Steve Block44f0eee2011-05-26 01:26:41 +0100752
753 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754 Object* PromoteScheduledException();
Steve Block44f0eee2011-05-26 01:26:41 +0100755
756 // Attempts to compute the current source location, storing the
757 // result in the target out parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000758 bool ComputeLocation(MessageLocation* target);
759 bool ComputeLocationFromException(MessageLocation* target,
760 Handle<Object> exception);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400761 bool ComputeLocationFromStackTrace(MessageLocation* target,
762 Handle<Object> exception);
763
764 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
765 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100766
Steve Block44f0eee2011-05-26 01:26:41 +0100767 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768 Object* StackOverflow();
769 Object* TerminateExecution();
770 void CancelTerminateExecution();
771
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400772 void RequestInterrupt(InterruptCallback callback, void* data);
773 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100774
775 // Administration
776 void Iterate(ObjectVisitor* v);
777 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
778 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100779 void IterateThread(ThreadVisitor* v, char* t);
780
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400781 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100783
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 // Returns the native context of the calling JavaScript code. That
785 // is, the native context of the top-most JavaScript frame.
786 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100787
788 void RegisterTryCatchHandler(v8::TryCatch* that);
789 void UnregisterTryCatchHandler(v8::TryCatch* that);
790
791 char* ArchiveThread(char* to);
792 char* RestoreThread(char* from);
793
Steve Block44f0eee2011-05-26 01:26:41 +0100794 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
795 static const int kBMMaxShift = 250; // See StringSearchBase.
796
797 // Accessors.
798#define GLOBAL_ACCESSOR(type, name, initialvalue) \
799 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000800 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100801 return name##_; \
802 } \
803 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100805 name##_ = value; \
806 }
807 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
808#undef GLOBAL_ACCESSOR
809
810#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
811 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100813 return &(name##_)[0]; \
814 }
815 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
816#undef GLOBAL_ARRAY_ACCESSOR
817
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
819 inline Handle<type> name(); \
820 inline bool is_##name(type* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
822#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100823
824 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000825 Counters* counters() {
826 // Call InitializeLoggingAndCounters() if logging is needed before
827 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000829 return counters_;
830 }
Steve Block44f0eee2011-05-26 01:26:41 +0100831 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
832 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000833 Logger* logger() {
834 // Call InitializeLoggingAndCounters() if logging is needed before
835 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000836 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000837 return logger_;
838 }
Steve Block44f0eee2011-05-26 01:26:41 +0100839 StackGuard* stack_guard() { return &stack_guard_; }
840 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000841 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100842 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000843 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100844 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100845 bool deoptimizer_lazy_throw() const { return deoptimizer_lazy_throw_; }
846 void set_deoptimizer_lazy_throw(bool value) {
847 deoptimizer_lazy_throw_ = value;
848 }
Steve Block44f0eee2011-05-26 01:26:41 +0100849 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850 MaterializedObjectStore* materialized_object_store() {
851 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100852 }
853
Steve Block44f0eee2011-05-26 01:26:41 +0100854 KeyedLookupCache* keyed_lookup_cache() {
855 return keyed_lookup_cache_;
856 }
857
858 ContextSlotCache* context_slot_cache() {
859 return context_slot_cache_;
860 }
861
862 DescriptorLookupCache* descriptor_lookup_cache() {
863 return descriptor_lookup_cache_;
864 }
865
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000866 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
867
Steve Block44f0eee2011-05-26 01:26:41 +0100868 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000869 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100870 return handle_scope_implementer_;
871 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000872 Zone* runtime_zone() { return &runtime_zone_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000873 Zone* interface_descriptor_zone() { return &interface_descriptor_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100874
Ben Murdoch8b112d22011-06-08 16:22:53 +0100875 UnicodeCache* unicode_cache() {
876 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100877 }
878
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100879 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
880 return inner_pointer_to_code_cache_;
881 }
Steve Block44f0eee2011-05-26 01:26:41 +0100882
Steve Block44f0eee2011-05-26 01:26:41 +0100883 GlobalHandles* global_handles() { return global_handles_; }
884
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 EternalHandles* eternal_handles() { return eternal_handles_; }
886
Steve Block44f0eee2011-05-26 01:26:41 +0100887 ThreadManager* thread_manager() { return thread_manager_; }
888
Steve Block44f0eee2011-05-26 01:26:41 +0100889 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
890 return &jsregexp_uncanonicalize_;
891 }
892
893 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
894 return &jsregexp_canonrange_;
895 }
896
Steve Block44f0eee2011-05-26 01:26:41 +0100897 RuntimeState* runtime_state() { return &runtime_state_; }
898
Steve Block44f0eee2011-05-26 01:26:41 +0100899 Builtins* builtins() { return &builtins_; }
900
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100901 void NotifyExtensionInstalled() {
902 has_installed_extensions_ = true;
903 }
904
905 bool has_installed_extensions() { return has_installed_extensions_; }
906
Steve Block44f0eee2011-05-26 01:26:41 +0100907 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
908 regexp_macro_assembler_canonicalize() {
909 return &regexp_macro_assembler_canonicalize_;
910 }
911
912 RegExpStack* regexp_stack() { return regexp_stack_; }
913
914 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
915 interp_canonicalize_mapping() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100916 return &regexp_macro_assembler_canonicalize_;
Steve Block44f0eee2011-05-26 01:26:41 +0100917 }
918
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000919 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100920
Ben Murdoch61f157c2016-09-16 13:49:30 +0100921 bool* is_profiling_address() { return &is_profiling_; }
922 CodeEventDispatcher* code_event_dispatcher() const {
923 return code_event_dispatcher_.get();
924 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
926 HeapProfiler* heap_profiler() const { return heap_profiler_; }
927
Steve Block44f0eee2011-05-26 01:26:41 +0100928#ifdef DEBUG
929 HistogramInfo* heap_histograms() { return heap_histograms_; }
930
931 JSObject::SpillInformation* js_spill_information() {
932 return &js_spill_information_;
933 }
Steve Block44f0eee2011-05-26 01:26:41 +0100934#endif
935
936 Factory* factory() { return reinterpret_cast<Factory*>(this); }
937
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100939
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100941
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
943
944 void SetData(uint32_t slot, void* data) {
945 DCHECK(slot < Internals::kNumIsolateDataSlots);
946 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100947 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 void* GetData(uint32_t slot) {
949 DCHECK(slot < Internals::kNumIsolateDataSlots);
950 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100951 }
Steve Block44f0eee2011-05-26 01:26:41 +0100952
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 bool serializer_enabled() const { return serializer_enabled_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000954 bool snapshot_available() const {
955 return snapshot_blob_ != NULL && snapshot_blob_->raw_size != 0;
956 }
Steve Block44f0eee2011-05-26 01:26:41 +0100957
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000958 bool IsDead() { return has_fatal_error_; }
959 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000960
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100962
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000963 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100964
965 double time_millis_since_init() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000966 return heap_.MonotonicallyIncreasingTimeInMs() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100967 }
968
969 DateCache* date_cache() {
970 return date_cache_;
971 }
972
973 void set_date_cache(DateCache* date_cache) {
974 if (date_cache != date_cache_) {
975 delete date_cache_;
976 }
977 date_cache_ = date_cache;
978 }
979
Ben Murdochda12d292016-06-02 14:46:10 +0100980 Map* get_initial_js_array_map(ElementsKind kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981
982 static const int kArrayProtectorValid = 1;
983 static const int kArrayProtectorInvalid = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984
985 bool IsFastArrayConstructorPrototypeChainIntact();
Ben Murdochda12d292016-06-02 14:46:10 +0100986 inline bool IsArraySpeciesLookupChainIntact();
Ben Murdochc5610432016-08-08 18:44:38 +0100987 inline bool IsHasInstanceLookupChainIntact();
988 bool IsIsConcatSpreadableLookupChainIntact();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000989
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000990 // On intent to set an element in object, make sure that appropriate
991 // notifications occur if the set is on the elements of the array or
992 // object prototype. Also ensure that changes to prototype chain between
993 // Array and Object fire notifications.
994 void UpdateArrayProtectorOnSetElement(Handle<JSObject> object);
995 void UpdateArrayProtectorOnSetLength(Handle<JSObject> object) {
996 UpdateArrayProtectorOnSetElement(object);
997 }
998 void UpdateArrayProtectorOnSetPrototype(Handle<JSObject> object) {
999 UpdateArrayProtectorOnSetElement(object);
1000 }
1001 void UpdateArrayProtectorOnNormalizeElements(Handle<JSObject> object) {
1002 UpdateArrayProtectorOnSetElement(object);
1003 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001004 void InvalidateArraySpeciesProtector();
Ben Murdochc5610432016-08-08 18:44:38 +01001005 void InvalidateHasInstanceProtector();
1006 void InvalidateIsConcatSpreadableProtector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001007
1008 // Returns true if array is the initial array prototype in any native context.
1009 bool IsAnyInitialArrayPrototype(Handle<JSArray> array);
1010
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 CallInterfaceDescriptorData* call_descriptor_data(int index);
1012
1013 void IterateDeferredHandles(ObjectVisitor* visitor);
1014 void LinkDeferredHandles(DeferredHandles* deferred_handles);
1015 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
1016
1017#ifdef DEBUG
1018 bool IsDeferredHandle(Object** location);
1019#endif // DEBUG
1020
1021 bool concurrent_recompilation_enabled() {
1022 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001023 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001025 return optimizing_compile_dispatcher_ != NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001026 }
1027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001028 OptimizingCompileDispatcher* optimizing_compile_dispatcher() {
1029 return optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 }
1031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032 int id() const { return static_cast<int>(id_); }
1033
1034 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001035 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036 HTracer* GetHTracer();
1037 CodeTracer* GetCodeTracer();
1038
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001039 void DumpAndResetCompilationStats();
1040
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1042 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1043 function_entry_hook_ = function_entry_hook;
1044 }
1045
1046 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001048 void* virtual_handler_register_address() {
1049 return &virtual_handler_register_;
1050 }
1051
1052 void* virtual_slot_register_address() { return &virtual_slot_register_; }
1053
1054 base::RandomNumberGenerator* random_number_generator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055
1056 // Given an address occupied by a live code object, return that object.
1057 Object* FindCodeObject(Address a);
1058
1059 int NextOptimizationId() {
1060 int id = next_optimization_id_++;
1061 if (!Smi::IsValid(next_optimization_id_)) {
1062 next_optimization_id_ = 0;
1063 }
1064 return id;
1065 }
1066
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001067 void IncrementJsCallsFromApiCounter() { ++js_calls_from_api_counter_; }
1068
1069 unsigned int js_calls_from_api_counter() {
1070 return js_calls_from_api_counter_;
1071 }
1072
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001073 // Get (and lazily initialize) the registry for per-isolate symbols.
1074 Handle<JSObject> GetSymbolRegistry();
1075
1076 void AddCallCompletedCallback(CallCompletedCallback callback);
1077 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1078 void FireCallCompletedCallback();
1079
Ben Murdoch097c5b22016-05-18 11:27:45 +01001080 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1081 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1082 void FireBeforeCallEnteredCallback();
1083
Ben Murdochda12d292016-06-02 14:46:10 +01001084 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1085 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1086 void FireMicrotasksCompletedCallback();
1087
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001088 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1089 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1090 v8::PromiseRejectEvent event);
1091
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092 void EnqueueMicrotask(Handle<Object> microtask);
1093 void RunMicrotasks();
Ben Murdochc5610432016-08-08 18:44:38 +01001094 bool IsRunningMicrotasks() const { return is_running_microtasks_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001095
1096 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1097 void CountUsage(v8::Isolate::UseCounterFeature feature);
1098
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001099 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1100 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1101
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001102 std::string GetTurboCfgFileName();
1103
1104#if TRACE_MAPS
1105 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1106#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107
Ben Murdochda12d292016-06-02 14:46:10 +01001108 // Support for dynamically disabling tail call elimination.
1109 Address is_tail_call_elimination_enabled_address() {
1110 return reinterpret_cast<Address>(&is_tail_call_elimination_enabled_);
1111 }
1112 bool is_tail_call_elimination_enabled() const {
1113 return is_tail_call_elimination_enabled_;
1114 }
1115 void SetTailCallEliminationEnabled(bool enabled);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001116
1117 void AddDetachedContext(Handle<Context> context);
1118 void CheckDetachedContextsAfterGC();
1119
1120 List<Object*>* partial_snapshot_cache() { return &partial_snapshot_cache_; }
1121
1122 void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
1123 array_buffer_allocator_ = allocator;
1124 }
1125 v8::ArrayBuffer::Allocator* array_buffer_allocator() const {
1126 return array_buffer_allocator_;
1127 }
1128
1129 FutexWaitListNode* futex_wait_list_node() { return &futex_wait_list_node_; }
1130
1131 CancelableTaskManager* cancelable_task_manager() {
1132 return cancelable_task_manager_;
1133 }
1134
1135 interpreter::Interpreter* interpreter() const { return interpreter_; }
1136
Ben Murdochda12d292016-06-02 14:46:10 +01001137 base::AccountingAllocator* allocator() { return &allocator_; }
1138
Ben Murdochc5610432016-08-08 18:44:38 +01001139 bool IsInAnyContext(Object* object, uint32_t index);
1140
Ben Murdoch61f157c2016-09-16 13:49:30 +01001141 void SetRAILMode(RAILMode rail_mode);
1142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001143 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001144 explicit Isolate(bool enable_serializer);
Ben Murdochc5610432016-08-08 18:44:38 +01001145 bool IsArrayOrObjectPrototype(Object* object);
Steve Block44f0eee2011-05-26 01:26:41 +01001146
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001147 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001148 friend struct GlobalState;
1149 friend struct InitializeGlobalState;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001150 Handle<JSObject> SetUpSubregistry(Handle<JSObject> registry, Handle<Map> map,
1151 const char* name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001152
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153 // These fields are accessed through the API, offsets must be kept in sync
1154 // with v8::internal::Internals (in include/v8.h) constants. This is also
1155 // verified in Isolate::Init() using runtime checks.
1156 void* embedder_data_[Internals::kNumIsolateDataSlots];
1157 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001158
Steve Block44f0eee2011-05-26 01:26:41 +01001159 // The per-process lock should be acquired before the ThreadDataTable is
1160 // modified.
1161 class ThreadDataTable {
1162 public:
1163 ThreadDataTable();
1164 ~ThreadDataTable();
1165
1166 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1167 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001168 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001169 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001170
1171 private:
1172 PerIsolateThreadData* list_;
1173 };
1174
1175 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1176 // the Isolate. The top of the stack points to a thread which is currently
1177 // running the Isolate. When the stack is empty, the Isolate is considered
1178 // not entered by any thread and can be Disposed.
1179 // If the same thread enters the Isolate more then once, the entry_count_
1180 // is incremented rather then a new item pushed to the stack.
1181 class EntryStackItem {
1182 public:
1183 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1184 Isolate* previous_isolate,
1185 EntryStackItem* previous_item)
1186 : entry_count(1),
1187 previous_thread_data(previous_thread_data),
1188 previous_isolate(previous_isolate),
1189 previous_item(previous_item) { }
1190
1191 int entry_count;
1192 PerIsolateThreadData* previous_thread_data;
1193 Isolate* previous_isolate;
1194 EntryStackItem* previous_item;
1195
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001196 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001197 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1198 };
1199
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001201
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001202 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1203 static base::Thread::LocalStorageKey isolate_key_;
1204 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001205 static ThreadDataTable* thread_data_table_;
1206
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001207 // A global counter for all generated Isolates, might overflow.
1208 static base::Atomic32 isolate_counter_;
1209
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001210#if DEBUG
1211 static base::Atomic32 isolate_key_created_;
1212#endif
1213
Steve Block44f0eee2011-05-26 01:26:41 +01001214 void Deinit();
1215
1216 static void SetIsolateThreadLocals(Isolate* isolate,
1217 PerIsolateThreadData* data);
1218
Steve Block44f0eee2011-05-26 01:26:41 +01001219 // Find the PerThread for this particular (isolate, thread) combination.
1220 // If one does not yet exist, allocate a new one.
1221 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1222
Steve Block44f0eee2011-05-26 01:26:41 +01001223 // Initializes the current thread to run this Isolate.
1224 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1225 // at the same time, this should be prevented using external locking.
1226 void Enter();
1227
1228 // Exits the current thread. The previosuly entered Isolate is restored
1229 // for the thread.
1230 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1231 // at the same time, this should be prevented using external locking.
1232 void Exit();
1233
Steve Block44f0eee2011-05-26 01:26:41 +01001234 void InitializeThreadLocal();
1235
Steve Block44f0eee2011-05-26 01:26:41 +01001236 void MarkCompactPrologue(bool is_compacting,
1237 ThreadLocalTop* archived_thread_data);
1238 void MarkCompactEpilogue(bool is_compacting,
1239 ThreadLocalTop* archived_thread_data);
1240
1241 void FillCache();
1242
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243 // Propagate pending exception message to the v8::TryCatch.
1244 // If there is no external try-catch or message was successfully propagated,
1245 // then return true.
1246 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001247
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001248 // Remove per-frame stored materialized objects when we are unwinding
1249 // the frame.
1250 void RemoveMaterializedObjectsOnUnwind(StackFrame* frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001251
Ben Murdochda12d292016-06-02 14:46:10 +01001252 void RunMicrotasksInternal();
1253
Ben Murdoch61f157c2016-09-16 13:49:30 +01001254 const char* RAILModeName(RAILMode rail_mode) const {
1255 switch (rail_mode) {
1256 case PERFORMANCE_DEFAULT:
1257 return "DEFAULT";
1258 case PERFORMANCE_RESPONSE:
1259 return "RESPONSE";
1260 case PERFORMANCE_ANIMATION:
1261 return "ANIMATION";
1262 case PERFORMANCE_IDLE:
1263 return "IDLE";
1264 case PERFORMANCE_LOAD:
1265 return "LOAD";
1266 default:
1267 UNREACHABLE();
1268 }
1269 return "";
1270 }
1271
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272 base::Atomic32 id_;
1273 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001274 int stack_trace_nesting_level_;
1275 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001276 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001277 Bootstrapper* bootstrapper_;
1278 RuntimeProfiler* runtime_profiler_;
1279 CompilationCache* compilation_cache_;
1280 Counters* counters_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001281 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001282 Logger* logger_;
1283 StackGuard stack_guard_;
1284 StatsTable* stats_table_;
1285 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001286 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001287 DeoptimizerData* deoptimizer_data_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001288 bool deoptimizer_lazy_throw_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001290 ThreadLocalTop thread_local_top_;
1291 bool capture_stack_trace_for_uncaught_exceptions_;
1292 int stack_trace_for_uncaught_exceptions_frame_limit_;
1293 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001294 KeyedLookupCache* keyed_lookup_cache_;
1295 ContextSlotCache* context_slot_cache_;
1296 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001298 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001299 UnicodeCache* unicode_cache_;
Ben Murdochda12d292016-06-02 14:46:10 +01001300 base::AccountingAllocator allocator_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301 Zone runtime_zone_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001302 Zone interface_descriptor_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001303 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001304 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001306 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001307 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001308 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001309 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001310 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1311 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001312 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1313 regexp_macro_assembler_canonicalize_;
1314 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001315 DateCache* date_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001316 CallInterfaceDescriptorData* call_descriptor_data_;
1317 base::RandomNumberGenerator* random_number_generator_;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001318 RAILMode rail_mode_;
Steve Block44f0eee2011-05-26 01:26:41 +01001319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 // Whether the isolate has been created for snapshotting.
1321 bool serializer_enabled_;
1322
1323 // True if fatal error has been signaled for this isolate.
1324 bool has_fatal_error_;
1325
1326 // True if this isolate was initialized from a snapshot.
1327 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001328
Ben Murdochda12d292016-06-02 14:46:10 +01001329 // True if ES2015 tail call elimination feature is enabled.
1330 bool is_tail_call_elimination_enabled_;
1331
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001332 // Time stamp at initialization.
1333 double time_millis_at_init_;
1334
Steve Block44f0eee2011-05-26 01:26:41 +01001335#ifdef DEBUG
1336 // A static array of histogram info for each type.
1337 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1338 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001339#endif
1340
Steve Block44f0eee2011-05-26 01:26:41 +01001341 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001342 CpuProfiler* cpu_profiler_;
1343 HeapProfiler* heap_profiler_;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001344 std::unique_ptr<CodeEventDispatcher> code_event_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001346
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001347 interpreter::Interpreter* interpreter_;
1348
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001349 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1350 std::queue<InterruptEntry> api_interrupts_queue_;
1351
Steve Block44f0eee2011-05-26 01:26:41 +01001352#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1353 type name##_;
1354 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1355#undef GLOBAL_BACKING_STORE
1356
1357#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1358 type name##_[length];
1359 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1360#undef GLOBAL_ARRAY_BACKING_STORE
1361
1362#ifdef DEBUG
1363 // This class is huge and has a number of fields controlled by
1364 // preprocessor defines. Make sure the offsets of these fields agree
1365 // between compilation units.
1366#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1367 static const intptr_t name##_debug_offset_;
1368 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1369 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1370#undef ISOLATE_FIELD_OFFSET
1371#endif
1372
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 DeferredHandles* deferred_handles_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001374 OptimizingCompileDispatcher* optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001375
1376 // Counts deopt points if deopt_every_n_times is enabled.
1377 unsigned int stress_deopt_count_;
1378
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001379 Address virtual_handler_register_;
1380 Address virtual_slot_register_;
1381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001382 int next_optimization_id_;
1383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001384 // Counts javascript calls from the API. Wraps around on overflow.
1385 unsigned int js_calls_from_api_counter_;
1386
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001387#if TRACE_MAPS
1388 int next_unique_sfi_id_;
1389#endif
1390
Ben Murdoch097c5b22016-05-18 11:27:45 +01001391 // List of callbacks before a Call starts execution.
1392 List<BeforeCallEnteredCallback> before_call_entered_callbacks_;
1393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 // List of callbacks when a Call completes.
1395 List<CallCompletedCallback> call_completed_callbacks_;
1396
Ben Murdochda12d292016-06-02 14:46:10 +01001397 // List of callbacks after microtasks were run.
1398 List<MicrotasksCompletedCallback> microtasks_completed_callbacks_;
Ben Murdochc5610432016-08-08 18:44:38 +01001399 bool is_running_microtasks_;
Ben Murdochda12d292016-06-02 14:46:10 +01001400
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001401 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001402 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001404 List<Object*> partial_snapshot_cache_;
1405
1406 v8::ArrayBuffer::Allocator* array_buffer_allocator_;
1407
1408 FutexWaitListNode futex_wait_list_node_;
1409
1410 CancelableTaskManager* cancelable_task_manager_;
1411
1412 v8::Isolate::AbortOnUncaughtExceptionCallback
1413 abort_on_uncaught_exception_callback_;
1414
Steve Block44f0eee2011-05-26 01:26:41 +01001415 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001416 friend class HandleScopeImplementer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001417 friend class OptimizingCompileDispatcher;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001419 friend class ThreadManager;
1420 friend class Simulator;
1421 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001422 friend class ThreadId;
Steve Block44f0eee2011-05-26 01:26:41 +01001423 friend class v8::Isolate;
1424 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001425 friend class v8::Unlocker;
Ben Murdoch61f157c2016-09-16 13:49:30 +01001426 friend class v8::SnapshotCreator;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001427 friend v8::StartupData v8::V8::CreateSnapshotDataBlob(const char*);
Ben Murdochda12d292016-06-02 14:46:10 +01001428 friend v8::StartupData v8::V8::WarmUpSnapshotDataBlob(v8::StartupData,
1429 const char*);
Steve Block44f0eee2011-05-26 01:26:41 +01001430
1431 DISALLOW_COPY_AND_ASSIGN(Isolate);
1432};
1433
1434
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001435#undef FIELD_ACCESSOR
1436#undef THREAD_LOCAL_TOP_ACCESSOR
1437
1438
1439class PromiseOnStack {
1440 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001441 PromiseOnStack(Handle<JSFunction> function, Handle<JSObject> promise,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001442 PromiseOnStack* prev)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001443 : function_(function), promise_(promise), prev_(prev) {}
1444 Handle<JSFunction> function() { return function_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 Handle<JSObject> promise() { return promise_; }
1446 PromiseOnStack* prev() { return prev_; }
1447
1448 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001449 Handle<JSFunction> function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 Handle<JSObject> promise_;
1451 PromiseOnStack* prev_;
1452};
1453
1454
Steve Block44f0eee2011-05-26 01:26:41 +01001455// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1456// class as a work around for a bug in the generated code found with these
1457// versions of GCC. See V8 issue 122 for details.
1458class SaveContext BASE_EMBEDDED {
1459 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001460 explicit SaveContext(Isolate* isolate);
1461 ~SaveContext();
Steve Block44f0eee2011-05-26 01:26:41 +01001462
1463 Handle<Context> context() { return context_; }
1464 SaveContext* prev() { return prev_; }
1465
1466 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001467 bool IsBelowFrame(JavaScriptFrame* frame) {
1468 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001469 }
1470
1471 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001472 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001473 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001474 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001475 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001476};
1477
1478
1479class AssertNoContextChange BASE_EMBEDDED {
1480#ifdef DEBUG
1481 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001482 explicit AssertNoContextChange(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001483 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001485 }
1486
1487 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001489 Handle<Context> context_;
1490#else
1491 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001492 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001493#endif
1494};
1495
1496
1497class ExecutionAccess BASE_EMBEDDED {
1498 public:
1499 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1500 Lock(isolate);
1501 }
1502 ~ExecutionAccess() { Unlock(isolate_); }
1503
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1505 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001506
1507 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001508 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001509 }
1510
1511 private:
1512 Isolate* isolate_;
1513};
1514
1515
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001517class StackLimitCheck BASE_EMBEDDED {
1518 public:
1519 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001521 // Use this to check for stack-overflows in C++ code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001522 bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001523 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001524 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001525 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001526
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001527 // Use this to check for interrupt request in C++ code.
1528 bool InterruptRequested() {
1529 StackGuard* stack_guard = isolate_->stack_guard();
1530 return GetCurrentStackPosition() < stack_guard->climit();
1531 }
1532
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 // Use this to check for stack-overflow when entering runtime from JS code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 bool JsHasOverflowed(uintptr_t gap = 0) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535
Steve Block44f0eee2011-05-26 01:26:41 +01001536 private:
1537 Isolate* isolate_;
1538};
1539
Ben Murdochc5610432016-08-08 18:44:38 +01001540#define STACK_CHECK(isolate, result_value) \
1541 do { \
1542 StackLimitCheck stack_check(isolate); \
1543 if (stack_check.HasOverflowed()) { \
1544 isolate->Throw(*isolate->factory()->NewRangeError( \
1545 MessageTemplate::kStackOverflow)); \
1546 return result_value; \
1547 } \
1548 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +01001549
1550// Support for temporarily postponing interrupts. When the outermost
1551// postpone scope is left the interrupts will be re-enabled and any
1552// interrupts that occurred while in the scope will be taken into
1553// account.
1554class PostponeInterruptsScope BASE_EMBEDDED {
1555 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001556 PostponeInterruptsScope(Isolate* isolate,
1557 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1558 : stack_guard_(isolate->stack_guard()),
1559 intercept_mask_(intercept_mask),
1560 intercepted_flags_(0) {
1561 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001562 }
1563
1564 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001566 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001567
1568 // Find the bottom-most scope that intercepts this interrupt.
1569 // Return whether the interrupt has been intercepted.
1570 bool Intercept(StackGuard::InterruptFlag flag);
1571
Steve Block44f0eee2011-05-26 01:26:41 +01001572 private:
1573 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574 int intercept_mask_;
1575 int intercepted_flags_;
1576 PostponeInterruptsScope* prev_;
1577
1578 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001579};
1580
1581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001582class CodeTracer final : public Malloced {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001583 public:
1584 explicit CodeTracer(int isolate_id)
1585 : file_(NULL),
1586 scope_depth_(0) {
1587 if (!ShouldRedirect()) {
1588 file_ = stdout;
1589 return;
1590 }
Steve Block44f0eee2011-05-26 01:26:41 +01001591
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592 if (FLAG_redirect_code_traces_to == NULL) {
1593 SNPrintF(filename_,
1594 "code-%d-%d.asm",
1595 base::OS::GetCurrentProcessId(),
1596 isolate_id);
1597 } else {
1598 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1599 }
Steve Block44f0eee2011-05-26 01:26:41 +01001600
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601 WriteChars(filename_.start(), "", 0, false);
1602 }
Steve Block44f0eee2011-05-26 01:26:41 +01001603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001604 class Scope {
1605 public:
1606 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1607 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001608
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001609 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001610
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611 private:
1612 CodeTracer* tracer_;
1613 };
1614
1615 void OpenFile() {
1616 if (!ShouldRedirect()) {
1617 return;
1618 }
1619
1620 if (file_ == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001621 file_ = base::OS::FOpen(filename_.start(), "ab");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622 }
1623
1624 scope_depth_++;
1625 }
1626
1627 void CloseFile() {
1628 if (!ShouldRedirect()) {
1629 return;
1630 }
1631
1632 if (--scope_depth_ == 0) {
1633 fclose(file_);
1634 file_ = NULL;
1635 }
1636 }
1637
1638 FILE* file() const { return file_; }
1639
1640 private:
1641 static bool ShouldRedirect() {
1642 return FLAG_redirect_code_traces;
1643 }
1644
1645 EmbeddedVector<char, 128> filename_;
1646 FILE* file_;
1647 int scope_depth_;
1648};
Steve Block44f0eee2011-05-26 01:26:41 +01001649
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001650} // namespace internal
1651} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01001652
Steve Block44f0eee2011-05-26 01:26:41 +01001653#endif // V8_ISOLATE_H_