blob: 88471640121dd300293fc13153c8602b60494144 [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"
Ben Murdochda12d292016-06-02 14:46:10 +010014#include "src/base/accounting-allocator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/base/atomicops.h"
16#include "src/builtins.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000017#include "src/cancelable-task.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018#include "src/contexts.h"
19#include "src/date.h"
20#include "src/execution.h"
21#include "src/frames.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000022#include "src/futex-emulation.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000023#include "src/global-handles.h"
24#include "src/handles.h"
25#include "src/hashmap.h"
26#include "src/heap/heap.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027#include "src/messages.h"
28#include "src/optimizing-compile-dispatcher.h"
29#include "src/regexp/regexp-stack.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include "src/runtime-profiler.h"
Ben Murdochda12d292016-06-02 14:46:10 +010031#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/zone.h"
Steve Block44f0eee2011-05-26 01:26:41 +010033
34namespace v8 {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035
36namespace base {
37class RandomNumberGenerator;
38}
39
Steve Block44f0eee2011-05-26 01:26:41 +010040namespace internal {
41
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042class BasicBlockProfiler;
Steve Block44f0eee2011-05-26 01:26:41 +010043class Bootstrapper;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044class CallInterfaceDescriptorData;
Steve Block44f0eee2011-05-26 01:26:41 +010045class CodeGenerator;
46class CodeRange;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047class CodeStubDescriptor;
48class CodeTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010049class CompilationCache;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040050class CompilationStatistics;
Steve Block44f0eee2011-05-26 01:26:41 +010051class ContextSlotCache;
Steve Block44f0eee2011-05-26 01:26:41 +010052class Counters;
53class CpuFeatures;
54class CpuProfiler;
55class DeoptimizerData;
56class Deserializer;
57class EmptyStatement;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058class ExternalCallbackScope;
Steve Block44f0eee2011-05-26 01:26:41 +010059class ExternalReferenceTable;
60class Factory;
61class FunctionInfoListener;
62class HandleScopeImplementer;
63class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064class HStatistics;
65class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010066class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067class InnerPointerToCodeCache;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000068class Logger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069class MaterializedObjectStore;
70class CodeAgingHelper;
Steve Block44f0eee2011-05-26 01:26:41 +010071class RegExpStack;
72class SaveContext;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073class StatsTable;
Steve Block44f0eee2011-05-26 01:26:41 +010074class StringTracker;
75class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010077class ThreadManager;
78class ThreadState;
79class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000080class UnicodeCache;
81template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010082
83// 'void function pointer', used to roundtrip the
84// ExternalReference::ExternalReferenceRedirector since we can not include
85// assembler.h, where it is defined, here.
86typedef void* ExternalReferenceRedirectorPointer();
87
88
Steve Block44f0eee2011-05-26 01:26:41 +010089class Debug;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010091class Redirection;
92class Simulator;
Steve Block44f0eee2011-05-26 01:26:41 +010093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094namespace interpreter {
95class Interpreter;
96}
Steve Block44f0eee2011-05-26 01:26:41 +010097
98// Static indirection table for handles to constants. If a frame
99// element represents a constant, the data contains an index into
100// this table of handles to the actual constants.
101// Static indirection table for handles to constants. If a Result
102// represents a constant, the data contains an index into this table
103// of handles to the actual constants.
104typedef ZoneList<Handle<Object> > ZoneObjectList;
105
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100107 do { \
108 Isolate* __isolate__ = (isolate); \
109 if (__isolate__->has_scheduled_exception()) { \
110 return __isolate__->PromoteScheduledException(); \
111 } \
112 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100113
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114// Macros for MaybeHandle.
115
116#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
117 do { \
118 Isolate* __isolate__ = (isolate); \
119 if (__isolate__->has_scheduled_exception()) { \
120 __isolate__->PromoteScheduledException(); \
121 return value; \
122 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100123 } while (false)
124
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
126 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
127
128#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
129 do { \
130 if (!(call).ToHandle(&dst)) { \
131 DCHECK((isolate)->has_pending_exception()); \
132 return value; \
133 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100134 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
137 ASSIGN_RETURN_ON_EXCEPTION_VALUE( \
138 isolate, dst, call, isolate->heap()->exception())
139
140#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
141 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143#define THROW_NEW_ERROR(isolate, call, T) \
144 do { \
145 return isolate->Throw<T>(isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 } while (false)
147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000148#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
149 do { \
150 return isolate->Throw(*isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151 } while (false)
152
153#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
154 do { \
155 if ((call).is_null()) { \
156 DCHECK((isolate)->has_pending_exception()); \
157 return value; \
158 } \
159 } while (false)
160
161#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
162 RETURN_ON_EXCEPTION_VALUE(isolate, call, isolate->heap()->exception())
163
164#define RETURN_ON_EXCEPTION(isolate, call, T) \
165 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
166
Steve Block44f0eee2011-05-26 01:26:41 +0100167
Ben Murdoch589d6972011-11-30 16:04:58 +0000168#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
169 C(Handler, handler) \
170 C(CEntryFP, c_entry_fp) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400171 C(CFunction, c_function) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000172 C(Context, context) \
173 C(PendingException, pending_exception) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 C(PendingHandlerContext, pending_handler_context) \
175 C(PendingHandlerCode, pending_handler_code) \
176 C(PendingHandlerOffset, pending_handler_offset) \
177 C(PendingHandlerFP, pending_handler_fp) \
178 C(PendingHandlerSP, pending_handler_sp) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000179 C(ExternalCaughtException, external_caught_exception) \
180 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100181
Ben Murdochda12d292016-06-02 14:46:10 +0100182#define FOR_WITH_HANDLE_SCOPE(isolate, loop_var_type, init, loop_var, \
183 limit_check, increment, body) \
184 do { \
185 loop_var_type init; \
186 loop_var_type for_with_handle_limit = loop_var; \
187 Isolate* for_with_handle_isolate = isolate; \
188 while (limit_check) { \
189 for_with_handle_limit += 1024; \
190 HandleScope loop_scope(for_with_handle_isolate); \
191 for (; limit_check && loop_var < for_with_handle_limit; increment) { \
192 body \
193 } \
194 } \
195 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100196
Ben Murdoch8b112d22011-06-08 16:22:53 +0100197// Platform-independent, reliable thread identifier.
198class ThreadId {
199 public:
200 // Creates an invalid ThreadId.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400201 ThreadId() { base::NoBarrier_Store(&id_, kInvalidId); }
202
203 ThreadId& operator=(const ThreadId& other) {
204 base::NoBarrier_Store(&id_, base::NoBarrier_Load(&other.id_));
205 return *this;
206 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100207
208 // Returns ThreadId for current thread.
209 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
210
211 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
212 static ThreadId Invalid() { return ThreadId(kInvalidId); }
213
214 // Compares ThreadIds for equality.
215 INLINE(bool Equals(const ThreadId& other) const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400216 return base::NoBarrier_Load(&id_) == base::NoBarrier_Load(&other.id_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100217 }
218
219 // Checks whether this ThreadId refers to any thread.
220 INLINE(bool IsValid() const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400221 return base::NoBarrier_Load(&id_) != kInvalidId;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100222 }
223
224 // Converts ThreadId to an integer representation
225 // (required for public API: V8::V8::GetCurrentThreadId).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 int ToInteger() const { return static_cast<int>(base::NoBarrier_Load(&id_)); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100227
228 // Converts ThreadId to an integer representation
229 // (required for public API: V8::V8::TerminateExecution).
230 static ThreadId FromInteger(int id) { return ThreadId(id); }
231
232 private:
233 static const int kInvalidId = -1;
234
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400235 explicit ThreadId(int id) { base::NoBarrier_Store(&id_, id); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100236
237 static int AllocateThreadId();
238
239 static int GetCurrentThreadId();
240
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400241 base::Atomic32 id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100242
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100244
245 friend class Isolate;
246};
247
248
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249#define FIELD_ACCESSOR(type, name) \
250 inline void set_##name(type v) { name##_ = v; } \
251 inline type name() const { return name##_; }
252
253
Steve Block44f0eee2011-05-26 01:26:41 +0100254class ThreadLocalTop BASE_EMBEDDED {
255 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100256 // Does early low-level initialization that does not depend on the
257 // isolate being present.
258 ThreadLocalTop();
259
Steve Block44f0eee2011-05-26 01:26:41 +0100260 // Initialize the thread data.
261 void Initialize();
262
263 // Get the top C++ try catch handler or NULL if none are registered.
264 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000265 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100266 // used for comparison with addresses into the JS stack. If such an
267 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100269
270 // Get the address of the top C++ try catch handler or NULL if
271 // none are registered.
272 //
273 // This method always returns an address that can be compared to
274 // pointers into the JavaScript stack. When running on actual
275 // hardware, try_catch_handler_address and TryCatchHandler return
276 // the same pointer. When running on a simulator with a separate JS
277 // stack, try_catch_handler_address returns a JS stack address that
278 // corresponds to the place on the JS stack where the C++ handler
279 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 Address try_catch_handler_address() {
281 return reinterpret_cast<Address>(
282 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100283 }
284
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100286
Ben Murdoch257744e2011-11-30 15:57:28 +0000287 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100288 // The context where the current execution method is created and for variable
289 // lookups.
290 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100291 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 Object* pending_exception_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000293
294 // Communication channel between Isolate::FindHandler and the CEntryStub.
295 Context* pending_handler_context_;
296 Code* pending_handler_code_;
297 intptr_t pending_handler_offset_;
298 Address pending_handler_fp_;
299 Address pending_handler_sp_;
300
301 // Communication channel between Isolate::Throw and message consumers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100303 Object* pending_message_obj_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000304
Steve Block44f0eee2011-05-26 01:26:41 +0100305 // Use a separate value for scheduled exceptions to preserve the
306 // invariants that hold about pending_exception. We may want to
307 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100309 bool external_caught_exception_;
310 SaveContext* save_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100311
312 // Stack.
313 Address c_entry_fp_; // the frame pointer of the top c entry frame
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314 Address handler_; // try-blocks are chained through the stack
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400315 Address c_function_; // C function that was called at c entry.
Steve Block44f0eee2011-05-26 01:26:41 +0100316
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 // Throwing an exception may cause a Promise rejection. For this purpose
318 // we keep track of a stack of nested promises and the corresponding
319 // try-catch handlers.
320 PromiseOnStack* promise_on_stack_;
321
Steve Block44f0eee2011-05-26 01:26:41 +0100322#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100323 Simulator* simulator_;
324#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100325
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100326 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000327 // the external callback we're currently in
328 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100329 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100330
Steve Block44f0eee2011-05-26 01:26:41 +0100331 // Call back function to report unsafe JS accesses.
332 v8::FailedAccessCheckCallback failed_access_check_callback_;
333
334 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100335 void InitializeInternal();
336
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000337 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100338};
339
Steve Block44f0eee2011-05-26 01:26:41 +0100340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100342
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000343#define ISOLATE_INIT_SIMULATOR_LIST(V) \
344 V(bool, simulator_initialized, false) \
345 V(HashMap*, simulator_i_cache, NULL) \
346 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100347#else
348
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100350
351#endif
352
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353
Steve Block44f0eee2011-05-26 01:26:41 +0100354#ifdef DEBUG
355
356#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
357 V(CommentStatistic, paged_space_comments_statistics, \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358 CommentStatistic::kMaxComments + 1) \
359 V(int, code_kind_statistics, Code::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100360#else
361
362#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
363
364#endif
365
Steve Block44f0eee2011-05-26 01:26:41 +0100366#define ISOLATE_INIT_ARRAY_LIST(V) \
367 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100369 V(int, bad_char_shift_table, kUC16AlphabetSize) \
370 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
371 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000372 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100373 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
374
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000375typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100376
377#define ISOLATE_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100378 /* Assembler state. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100379 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000380 V(LogEventCallback, event_logger, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000381 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100382 /* To distinguish the function templates, so that we can find them in the */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 /* function cache of the native context. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100384 V(int, next_serial_number, 0) \
385 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100386 /* Part of the state of liveedit. */ \
387 V(FunctionInfoListener*, active_function_info_listener, NULL) \
388 /* State for Relocatable. */ \
389 V(Relocatable*, relocatable_top, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100390 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
391 V(Object*, string_stream_current_security_token, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100392 V(ExternalReferenceTable*, external_reference_table, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393 V(HashMap*, external_reference_map, NULL) \
394 V(HashMap*, root_index_map, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 V(int, pending_microtask_count, 0) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 V(HStatistics*, hstatistics, NULL) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397 V(CompilationStatistics*, turbo_statistics, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 V(HTracer*, htracer, NULL) \
399 V(CodeTracer*, code_tracer, NULL) \
400 V(bool, fp_stubs_generated, false) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400402 V(PromiseRejectCallback, promise_reject_callback, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403 V(const v8::StartupData*, snapshot_blob, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 ISOLATE_INIT_SIMULATOR_LIST(V)
405
406#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
407 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
408 inline type name() const { return thread_local_top_.name##_; }
409
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410#define THREAD_LOCAL_TOP_ADDRESS(type, name) \
411 type* name##_address() { return &thread_local_top_.name##_; }
412
Steve Block44f0eee2011-05-26 01:26:41 +0100413
414class Isolate {
415 // These forward declarations are required to make the friend declarations in
416 // PerIsolateThreadData work on some older versions of gcc.
417 class ThreadDataTable;
418 class EntryStackItem;
419 public:
420 ~Isolate();
421
Steve Block44f0eee2011-05-26 01:26:41 +0100422 // A thread has a PerIsolateThreadData instance for each isolate that it has
423 // entered. That instance is allocated when the isolate is initially entered
424 // and reused on subsequent entries.
425 class PerIsolateThreadData {
426 public:
427 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
428 : isolate_(isolate),
429 thread_id_(thread_id),
430 stack_limit_(0),
431 thread_state_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100433 simulator_(NULL),
434#endif
435 next_(NULL),
436 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100438 Isolate* isolate() const { return isolate_; }
439 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100440
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 FIELD_ACCESSOR(uintptr_t, stack_limit)
442 FIELD_ACCESSOR(ThreadState*, thread_state)
443
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444#if USE_SIMULATOR
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 FIELD_ACCESSOR(Simulator*, simulator)
Steve Block44f0eee2011-05-26 01:26:41 +0100446#endif
447
448 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100449 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100450 }
451
452 private:
453 Isolate* isolate_;
454 ThreadId thread_id_;
455 uintptr_t stack_limit_;
456 ThreadState* thread_state_;
457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000458#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100459 Simulator* simulator_;
460#endif
461
462 PerIsolateThreadData* next_;
463 PerIsolateThreadData* prev_;
464
465 friend class Isolate;
466 friend class ThreadDataTable;
467 friend class EntryStackItem;
468
469 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
470 };
471
472
473 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000474#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
475 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000476#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000477 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100478 };
479
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 static void InitializeOncePerProcess();
481
Steve Block44f0eee2011-05-26 01:26:41 +0100482 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
483 // not currently set).
484 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
485 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100487 }
488
489 // Returns the isolate inside which the current thread is running.
490 INLINE(static Isolate* Current()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400491 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100492 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 base::Thread::GetExistingThreadLocal(isolate_key_));
494 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100495 return isolate;
496 }
497
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000498 // Like Current, but skips the check that |isolate_key_| was initialized.
499 // Callers have to ensure that themselves.
500 // DO NOT USE. The only remaining callsite will be deleted soon.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501 INLINE(static Isolate* UnsafeCurrent()) {
502 return reinterpret_cast<Isolate*>(
503 base::Thread::GetThreadLocal(isolate_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100504 }
505
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000506 // Usually called by Init(), but can be called early e.g. to allow
507 // testing components that require logging but not the whole
508 // isolate.
509 //
510 // Safe to call more than once.
511 void InitializeLoggingAndCounters();
512
Steve Block44f0eee2011-05-26 01:26:41 +0100513 bool Init(Deserializer* des);
514
Steve Block44f0eee2011-05-26 01:26:41 +0100515 // True if at least one thread Enter'ed this isolate.
516 bool IsInUse() { return entry_stack_ != NULL; }
517
518 // Destroys the non-default isolates.
519 // Sets default isolate into "has_been_disposed" state rather then destroying,
520 // for legacy API reasons.
521 void TearDown();
522
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525 void ClearSerializerData();
526
Ben Murdoch257744e2011-11-30 15:57:28 +0000527 // Find the PerThread for this particular (isolate, thread) combination
528 // If one does not yet exist, return null.
529 PerIsolateThreadData* FindPerThreadDataForThisThread();
530
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 // Find the PerThread for given (isolate, thread) combination
532 // If one does not yet exist, return null.
533 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100534
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 // Discard the PerThread for this particular (isolate, thread) combination
536 // If one does not yet exist, no-op.
537 void DiscardPerThreadDataForThisThread();
538
Steve Block44f0eee2011-05-26 01:26:41 +0100539 // Returns the key used to store the pointer to the current isolate.
540 // Used internally for V8 threads that do not execute JavaScript but still
541 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100543 return isolate_key_;
544 }
Steve Block44f0eee2011-05-26 01:26:41 +0100545
546 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100548 return thread_id_key_;
549 }
Steve Block44f0eee2011-05-26 01:26:41 +0100550
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100552
Steve Block44f0eee2011-05-26 01:26:41 +0100553 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000555
Steve Block44f0eee2011-05-26 01:26:41 +0100556 Address get_address_from_id(AddressId id);
557
558 // Access to top context (where the current function object was created).
559 Context* context() { return thread_local_top_.context_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000560 inline void set_context(Context* context);
Steve Block44f0eee2011-05-26 01:26:41 +0100561 Context** context_address() { return &thread_local_top_.context_; }
562
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000563 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100564
565 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100567
568 // Interface to pending exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000569 inline Object* pending_exception();
570 inline void set_pending_exception(Object* exception_obj);
571 inline void clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000573 THREAD_LOCAL_TOP_ADDRESS(Object*, pending_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000574
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000575 inline bool has_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000577 THREAD_LOCAL_TOP_ADDRESS(Context*, pending_handler_context)
578 THREAD_LOCAL_TOP_ADDRESS(Code*, pending_handler_code)
579 THREAD_LOCAL_TOP_ADDRESS(intptr_t, pending_handler_offset)
580 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_fp)
581 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_sp)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000582
583 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
584
Steve Block44f0eee2011-05-26 01:26:41 +0100585 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100587 }
Steve Block44f0eee2011-05-26 01:26:41 +0100588 bool* external_caught_exception_address() {
589 return &thread_local_top_.external_caught_exception_;
590 }
591
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 THREAD_LOCAL_TOP_ADDRESS(Object*, scheduled_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000593
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594 inline void clear_pending_message();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 Address pending_message_obj_address() {
596 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
597 }
598
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000599 inline Object* scheduled_exception();
600 inline bool has_scheduled_exception();
601 inline void clear_scheduled_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603 bool IsJavaScriptHandlerOnTop(Object* exception);
604 bool IsExternalHandlerOnTop(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000605
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000606 inline bool is_catchable_by_javascript(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607
Steve Block44f0eee2011-05-26 01:26:41 +0100608 // JS execution stack (see frames.h).
609 static Address c_entry_fp(ThreadLocalTop* thread) {
610 return thread->c_entry_fp_;
611 }
612 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400613 Address c_function() { return thread_local_top_.c_function_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100614
615 inline Address* c_entry_fp_address() {
616 return &thread_local_top_.c_entry_fp_;
617 }
618 inline Address* handler_address() { return &thread_local_top_.handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400619 inline Address* c_function_address() {
620 return &thread_local_top_.c_function_;
621 }
Steve Block44f0eee2011-05-26 01:26:41 +0100622
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623 // Bottom JS entry.
624 Address js_entry_sp() {
625 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100626 }
627 inline Address* js_entry_sp_address() {
628 return &thread_local_top_.js_entry_sp_;
629 }
Steve Block44f0eee2011-05-26 01:26:41 +0100630
Steve Block44f0eee2011-05-26 01:26:41 +0100631 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100632 // a builtin object, or a JS global object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000633 inline Handle<JSGlobalObject> global_object();
Steve Block44f0eee2011-05-26 01:26:41 +0100634
635 // Returns the global proxy object of the current context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 JSObject* global_proxy() {
Steve Block44f0eee2011-05-26 01:26:41 +0100637 return context()->global_proxy();
638 }
639
Steve Block44f0eee2011-05-26 01:26:41 +0100640 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
641 void FreeThreadResources() { thread_local_top_.Free(); }
642
643 // This method is called by the api after operations that may throw
644 // exceptions. If an exception was thrown and not handled by an external
645 // handler the exception is scheduled to be rethrown when we return to running
646 // JavaScript code. If an exception is scheduled true is returned.
647 bool OptionalRescheduleException(bool is_bottom_call);
648
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000649 // Push and pop a promise and the current try-catch handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 void PushPromise(Handle<JSObject> promise, Handle<JSFunction> function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651 void PopPromise();
652 Handle<Object> GetPromiseOnStackOnThrow();
653
Ben Murdoch8b112d22011-06-08 16:22:53 +0100654 class ExceptionScope {
655 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000656 // Scope currently can only be used for regular exceptions,
657 // not termination exception.
658 inline explicit ExceptionScope(Isolate* isolate);
659 inline ~ExceptionScope();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100660
661 private:
662 Isolate* isolate_;
663 Handle<Object> pending_exception_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100664 };
665
Steve Block44f0eee2011-05-26 01:26:41 +0100666 void SetCaptureStackTraceForUncaughtExceptions(
667 bool capture,
668 int frame_limit,
669 StackTrace::StackTraceOptions options);
670
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000671 void SetAbortOnUncaughtExceptionCallback(
672 v8::Isolate::AbortOnUncaughtExceptionCallback callback);
673
674 enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
Steve Block44f0eee2011-05-26 01:26:41 +0100675 void PrintCurrentStackTrace(FILE* out);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 void PrintStack(StringStream* accumulator,
677 PrintStackMode mode = kPrintStackVerbose);
678 void PrintStack(FILE* out, PrintStackMode mode = kPrintStackVerbose);
Steve Block44f0eee2011-05-26 01:26:41 +0100679 Handle<String> StackTraceString();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000680 NO_INLINE(void PushStackTraceAndDie(unsigned int magic, void* ptr1,
681 void* ptr2, unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100682 Handle<JSArray> CaptureCurrentStackTrace(
683 int frame_limit,
684 StackTrace::StackTraceOptions options);
Ben Murdochda12d292016-06-02 14:46:10 +0100685 Handle<Object> CaptureSimpleStackTrace(Handle<JSReceiver> error_object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000686 Handle<Object> caller);
Ben Murdochda12d292016-06-02 14:46:10 +0100687 MaybeHandle<JSReceiver> CaptureAndSetDetailedStackTrace(
688 Handle<JSReceiver> error_object);
689 MaybeHandle<JSReceiver> CaptureAndSetSimpleStackTrace(
690 Handle<JSReceiver> error_object, Handle<Object> caller);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400691 Handle<JSArray> GetDetailedStackTrace(Handle<JSObject> error_object);
692 Handle<JSArray> GetDetailedFromSimpleStackTrace(
693 Handle<JSObject> error_object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100694
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000695 // Returns if the given context may access the given global object. If
Steve Block44f0eee2011-05-26 01:26:41 +0100696 // the result is false, the pending exception is guaranteed to be
697 // set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698 bool MayAccess(Handle<Context> accessing_context, Handle<JSObject> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699
Steve Block44f0eee2011-05-26 01:26:41 +0100700 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000701 void ReportFailedAccessCheck(Handle<JSObject> receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100702
703 // Exception throwing support. The caller should use the result
704 // of Throw() as its return value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000705 Object* Throw(Object* exception, MessageLocation* location = NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706 Object* ThrowIllegalOperation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000707
708 template <typename T>
709 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
710 MessageLocation* location = NULL) {
711 Throw(*exception, location);
712 return MaybeHandle<T>();
713 }
714
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000715 // Re-throw an exception. This involves no error reporting since error
716 // reporting was handled when the exception was thrown originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000717 Object* ReThrow(Object* exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000718
719 // Find the correct handler for the current pending exception. This also
720 // clears and returns the current pending exception.
721 Object* UnwindAndFindHandler();
722
723 // Tries to predict whether an exception will be caught. Note that this can
724 // only produce an estimate, because it is undecidable whether a finally
725 // clause will consume or re-throw an exception. We conservatively assume any
726 // finally clause will behave as if the exception were consumed.
727 enum CatchType { NOT_CAUGHT, CAUGHT_BY_JAVASCRIPT, CAUGHT_BY_EXTERNAL };
728 CatchType PredictExceptionCatcher();
729
Steve Block44f0eee2011-05-26 01:26:41 +0100730 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 // Re-set pending message, script and positions reported to the TryCatch
732 // back to the TLS for re-use when rethrowing.
733 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
734 // Un-schedule an exception that was caught by a TryCatch handler.
735 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100736 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 // Return pending location if any or unfilled structure.
738 MessageLocation GetMessageLocation();
Steve Block44f0eee2011-05-26 01:26:41 +0100739
740 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000741 Object* PromoteScheduledException();
Steve Block44f0eee2011-05-26 01:26:41 +0100742
743 // Attempts to compute the current source location, storing the
744 // result in the target out parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000745 bool ComputeLocation(MessageLocation* target);
746 bool ComputeLocationFromException(MessageLocation* target,
747 Handle<Object> exception);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400748 bool ComputeLocationFromStackTrace(MessageLocation* target,
749 Handle<Object> exception);
750
751 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
752 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100753
Steve Block44f0eee2011-05-26 01:26:41 +0100754 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 Object* StackOverflow();
756 Object* TerminateExecution();
757 void CancelTerminateExecution();
758
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400759 void RequestInterrupt(InterruptCallback callback, void* data);
760 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100761
762 // Administration
763 void Iterate(ObjectVisitor* v);
764 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
765 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100766 void IterateThread(ThreadVisitor* v, char* t);
767
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400768 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100770
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000771 // Returns the native context of the calling JavaScript code. That
772 // is, the native context of the top-most JavaScript frame.
773 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100774
775 void RegisterTryCatchHandler(v8::TryCatch* that);
776 void UnregisterTryCatchHandler(v8::TryCatch* that);
777
778 char* ArchiveThread(char* to);
779 char* RestoreThread(char* from);
780
781 static const char* const kStackOverflowMessage;
782
783 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
784 static const int kBMMaxShift = 250; // See StringSearchBase.
785
786 // Accessors.
787#define GLOBAL_ACCESSOR(type, name, initialvalue) \
788 inline type name() const { \
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##_; \
791 } \
792 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100794 name##_ = value; \
795 }
796 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
797#undef GLOBAL_ACCESSOR
798
799#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
800 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000801 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100802 return &(name##_)[0]; \
803 }
804 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
805#undef GLOBAL_ARRAY_ACCESSOR
806
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000807#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
808 inline Handle<type> name(); \
809 inline bool is_##name(type* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000810 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
811#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100812
813 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000814 Counters* counters() {
815 // Call InitializeLoggingAndCounters() if logging is needed before
816 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000817 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000818 return counters_;
819 }
Steve Block44f0eee2011-05-26 01:26:41 +0100820 CodeRange* code_range() { return code_range_; }
821 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
822 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000823 Logger* logger() {
824 // Call InitializeLoggingAndCounters() if logging is needed before
825 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000827 return logger_;
828 }
Steve Block44f0eee2011-05-26 01:26:41 +0100829 StackGuard* stack_guard() { return &stack_guard_; }
830 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000831 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100832 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100834 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100835 bool deoptimizer_lazy_throw() const { return deoptimizer_lazy_throw_; }
836 void set_deoptimizer_lazy_throw(bool value) {
837 deoptimizer_lazy_throw_ = value;
838 }
Steve Block44f0eee2011-05-26 01:26:41 +0100839 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000840 MaterializedObjectStore* materialized_object_store() {
841 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100842 }
843
844 MemoryAllocator* memory_allocator() {
845 return memory_allocator_;
846 }
847
848 KeyedLookupCache* keyed_lookup_cache() {
849 return keyed_lookup_cache_;
850 }
851
852 ContextSlotCache* context_slot_cache() {
853 return context_slot_cache_;
854 }
855
856 DescriptorLookupCache* descriptor_lookup_cache() {
857 return descriptor_lookup_cache_;
858 }
859
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000860 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
861
Steve Block44f0eee2011-05-26 01:26:41 +0100862 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100864 return handle_scope_implementer_;
865 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000866 Zone* runtime_zone() { return &runtime_zone_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000867 Zone* interface_descriptor_zone() { return &interface_descriptor_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100868
Ben Murdoch8b112d22011-06-08 16:22:53 +0100869 UnicodeCache* unicode_cache() {
870 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100871 }
872
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100873 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
874 return inner_pointer_to_code_cache_;
875 }
Steve Block44f0eee2011-05-26 01:26:41 +0100876
Steve Block44f0eee2011-05-26 01:26:41 +0100877 GlobalHandles* global_handles() { return global_handles_; }
878
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000879 EternalHandles* eternal_handles() { return eternal_handles_; }
880
Steve Block44f0eee2011-05-26 01:26:41 +0100881 ThreadManager* thread_manager() { return thread_manager_; }
882
Steve Block44f0eee2011-05-26 01:26:41 +0100883 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
884 return &jsregexp_uncanonicalize_;
885 }
886
887 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
888 return &jsregexp_canonrange_;
889 }
890
Steve Block44f0eee2011-05-26 01:26:41 +0100891 RuntimeState* runtime_state() { return &runtime_state_; }
892
Steve Block44f0eee2011-05-26 01:26:41 +0100893 Builtins* builtins() { return &builtins_; }
894
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100895 void NotifyExtensionInstalled() {
896 has_installed_extensions_ = true;
897 }
898
899 bool has_installed_extensions() { return has_installed_extensions_; }
900
Steve Block44f0eee2011-05-26 01:26:41 +0100901 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
902 regexp_macro_assembler_canonicalize() {
903 return &regexp_macro_assembler_canonicalize_;
904 }
905
906 RegExpStack* regexp_stack() { return regexp_stack_; }
907
908 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
909 interp_canonicalize_mapping() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100910 return &regexp_macro_assembler_canonicalize_;
Steve Block44f0eee2011-05-26 01:26:41 +0100911 }
912
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000913 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100914
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
916 HeapProfiler* heap_profiler() const { return heap_profiler_; }
917
Steve Block44f0eee2011-05-26 01:26:41 +0100918#ifdef DEBUG
919 HistogramInfo* heap_histograms() { return heap_histograms_; }
920
921 JSObject::SpillInformation* js_spill_information() {
922 return &js_spill_information_;
923 }
Steve Block44f0eee2011-05-26 01:26:41 +0100924#endif
925
926 Factory* factory() { return reinterpret_cast<Factory*>(this); }
927
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000928 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100929
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100931
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000932 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
933
934 void SetData(uint32_t slot, void* data) {
935 DCHECK(slot < Internals::kNumIsolateDataSlots);
936 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100937 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000938 void* GetData(uint32_t slot) {
939 DCHECK(slot < Internals::kNumIsolateDataSlots);
940 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100941 }
Steve Block44f0eee2011-05-26 01:26:41 +0100942
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 bool serializer_enabled() const { return serializer_enabled_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000944 bool snapshot_available() const {
945 return snapshot_blob_ != NULL && snapshot_blob_->raw_size != 0;
946 }
Steve Block44f0eee2011-05-26 01:26:41 +0100947
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 bool IsDead() { return has_fatal_error_; }
949 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000950
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000951 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100952
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100954
955 double time_millis_since_init() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000956 return heap_.MonotonicallyIncreasingTimeInMs() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100957 }
958
959 DateCache* date_cache() {
960 return date_cache_;
961 }
962
963 void set_date_cache(DateCache* date_cache) {
964 if (date_cache != date_cache_) {
965 delete date_cache_;
966 }
967 date_cache_ = date_cache;
968 }
969
Ben Murdochda12d292016-06-02 14:46:10 +0100970 Map* get_initial_js_array_map(ElementsKind kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000971
972 static const int kArrayProtectorValid = 1;
973 static const int kArrayProtectorInvalid = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974
975 bool IsFastArrayConstructorPrototypeChainIntact();
Ben Murdochda12d292016-06-02 14:46:10 +0100976 inline bool IsArraySpeciesLookupChainIntact();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000977
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000978 // On intent to set an element in object, make sure that appropriate
979 // notifications occur if the set is on the elements of the array or
980 // object prototype. Also ensure that changes to prototype chain between
981 // Array and Object fire notifications.
982 void UpdateArrayProtectorOnSetElement(Handle<JSObject> object);
983 void UpdateArrayProtectorOnSetLength(Handle<JSObject> object) {
984 UpdateArrayProtectorOnSetElement(object);
985 }
986 void UpdateArrayProtectorOnSetPrototype(Handle<JSObject> object) {
987 UpdateArrayProtectorOnSetElement(object);
988 }
989 void UpdateArrayProtectorOnNormalizeElements(Handle<JSObject> object) {
990 UpdateArrayProtectorOnSetElement(object);
991 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100992 void InvalidateArraySpeciesProtector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000993
994 // Returns true if array is the initial array prototype in any native context.
995 bool IsAnyInitialArrayPrototype(Handle<JSArray> array);
996
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 CallInterfaceDescriptorData* call_descriptor_data(int index);
998
999 void IterateDeferredHandles(ObjectVisitor* visitor);
1000 void LinkDeferredHandles(DeferredHandles* deferred_handles);
1001 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
1002
1003#ifdef DEBUG
1004 bool IsDeferredHandle(Object** location);
1005#endif // DEBUG
1006
1007 bool concurrent_recompilation_enabled() {
1008 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001009 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001011 return optimizing_compile_dispatcher_ != NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001012 }
1013
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001014 OptimizingCompileDispatcher* optimizing_compile_dispatcher() {
1015 return optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001016 }
1017
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 int id() const { return static_cast<int>(id_); }
1019
1020 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001021 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001022 HTracer* GetHTracer();
1023 CodeTracer* GetCodeTracer();
1024
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001025 void DumpAndResetCompilationStats();
1026
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1028 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1029 function_entry_hook_ = function_entry_hook;
1030 }
1031
1032 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1033
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034 void* virtual_handler_register_address() {
1035 return &virtual_handler_register_;
1036 }
1037
1038 void* virtual_slot_register_address() { return &virtual_slot_register_; }
1039
1040 base::RandomNumberGenerator* random_number_generator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001041
1042 // Given an address occupied by a live code object, return that object.
1043 Object* FindCodeObject(Address a);
1044
1045 int NextOptimizationId() {
1046 int id = next_optimization_id_++;
1047 if (!Smi::IsValid(next_optimization_id_)) {
1048 next_optimization_id_ = 0;
1049 }
1050 return id;
1051 }
1052
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001053 void IncrementJsCallsFromApiCounter() { ++js_calls_from_api_counter_; }
1054
1055 unsigned int js_calls_from_api_counter() {
1056 return js_calls_from_api_counter_;
1057 }
1058
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001059 // Get (and lazily initialize) the registry for per-isolate symbols.
1060 Handle<JSObject> GetSymbolRegistry();
1061
1062 void AddCallCompletedCallback(CallCompletedCallback callback);
1063 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1064 void FireCallCompletedCallback();
1065
Ben Murdoch097c5b22016-05-18 11:27:45 +01001066 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1067 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1068 void FireBeforeCallEnteredCallback();
1069
Ben Murdochda12d292016-06-02 14:46:10 +01001070 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1071 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1072 void FireMicrotasksCompletedCallback();
1073
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001074 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1075 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1076 v8::PromiseRejectEvent event);
1077
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078 void EnqueueMicrotask(Handle<Object> microtask);
1079 void RunMicrotasks();
1080
1081 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1082 void CountUsage(v8::Isolate::UseCounterFeature feature);
1083
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001084 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1085 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1086
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001087 std::string GetTurboCfgFileName();
1088
1089#if TRACE_MAPS
1090 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1091#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001092
Ben Murdochda12d292016-06-02 14:46:10 +01001093 // Support for dynamically disabling tail call elimination.
1094 Address is_tail_call_elimination_enabled_address() {
1095 return reinterpret_cast<Address>(&is_tail_call_elimination_enabled_);
1096 }
1097 bool is_tail_call_elimination_enabled() const {
1098 return is_tail_call_elimination_enabled_;
1099 }
1100 void SetTailCallEliminationEnabled(bool enabled);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001101
1102 void AddDetachedContext(Handle<Context> context);
1103 void CheckDetachedContextsAfterGC();
1104
1105 List<Object*>* partial_snapshot_cache() { return &partial_snapshot_cache_; }
1106
1107 void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
1108 array_buffer_allocator_ = allocator;
1109 }
1110 v8::ArrayBuffer::Allocator* array_buffer_allocator() const {
1111 return array_buffer_allocator_;
1112 }
1113
1114 FutexWaitListNode* futex_wait_list_node() { return &futex_wait_list_node_; }
1115
1116 CancelableTaskManager* cancelable_task_manager() {
1117 return cancelable_task_manager_;
1118 }
1119
1120 interpreter::Interpreter* interpreter() const { return interpreter_; }
1121
Ben Murdochda12d292016-06-02 14:46:10 +01001122 base::AccountingAllocator* allocator() { return &allocator_; }
1123
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001124 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001125 explicit Isolate(bool enable_serializer);
Steve Block44f0eee2011-05-26 01:26:41 +01001126
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001127 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001128 friend struct GlobalState;
1129 friend struct InitializeGlobalState;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001130 Handle<JSObject> SetUpSubregistry(Handle<JSObject> registry, Handle<Map> map,
1131 const char* name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001132
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001133 // These fields are accessed through the API, offsets must be kept in sync
1134 // with v8::internal::Internals (in include/v8.h) constants. This is also
1135 // verified in Isolate::Init() using runtime checks.
1136 void* embedder_data_[Internals::kNumIsolateDataSlots];
1137 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138
Steve Block44f0eee2011-05-26 01:26:41 +01001139 // The per-process lock should be acquired before the ThreadDataTable is
1140 // modified.
1141 class ThreadDataTable {
1142 public:
1143 ThreadDataTable();
1144 ~ThreadDataTable();
1145
1146 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1147 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001148 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001149 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001150
1151 private:
1152 PerIsolateThreadData* list_;
1153 };
1154
1155 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1156 // the Isolate. The top of the stack points to a thread which is currently
1157 // running the Isolate. When the stack is empty, the Isolate is considered
1158 // not entered by any thread and can be Disposed.
1159 // If the same thread enters the Isolate more then once, the entry_count_
1160 // is incremented rather then a new item pushed to the stack.
1161 class EntryStackItem {
1162 public:
1163 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1164 Isolate* previous_isolate,
1165 EntryStackItem* previous_item)
1166 : entry_count(1),
1167 previous_thread_data(previous_thread_data),
1168 previous_isolate(previous_isolate),
1169 previous_item(previous_item) { }
1170
1171 int entry_count;
1172 PerIsolateThreadData* previous_thread_data;
1173 Isolate* previous_isolate;
1174 EntryStackItem* previous_item;
1175
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001176 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001177 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1178 };
1179
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001180 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001181
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001182 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1183 static base::Thread::LocalStorageKey isolate_key_;
1184 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001185 static ThreadDataTable* thread_data_table_;
1186
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001187 // A global counter for all generated Isolates, might overflow.
1188 static base::Atomic32 isolate_counter_;
1189
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001190#if DEBUG
1191 static base::Atomic32 isolate_key_created_;
1192#endif
1193
Steve Block44f0eee2011-05-26 01:26:41 +01001194 void Deinit();
1195
1196 static void SetIsolateThreadLocals(Isolate* isolate,
1197 PerIsolateThreadData* data);
1198
Steve Block44f0eee2011-05-26 01:26:41 +01001199 // Find the PerThread for this particular (isolate, thread) combination.
1200 // If one does not yet exist, allocate a new one.
1201 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1202
Steve Block44f0eee2011-05-26 01:26:41 +01001203 // Initializes the current thread to run this Isolate.
1204 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1205 // at the same time, this should be prevented using external locking.
1206 void Enter();
1207
1208 // Exits the current thread. The previosuly entered Isolate is restored
1209 // for the thread.
1210 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1211 // at the same time, this should be prevented using external locking.
1212 void Exit();
1213
Steve Block44f0eee2011-05-26 01:26:41 +01001214 void InitializeThreadLocal();
1215
Steve Block44f0eee2011-05-26 01:26:41 +01001216 void MarkCompactPrologue(bool is_compacting,
1217 ThreadLocalTop* archived_thread_data);
1218 void MarkCompactEpilogue(bool is_compacting,
1219 ThreadLocalTop* archived_thread_data);
1220
1221 void FillCache();
1222
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223 // Propagate pending exception message to the v8::TryCatch.
1224 // If there is no external try-catch or message was successfully propagated,
1225 // then return true.
1226 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001227
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001228 // Remove per-frame stored materialized objects when we are unwinding
1229 // the frame.
1230 void RemoveMaterializedObjectsOnUnwind(StackFrame* frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001231
Ben Murdochda12d292016-06-02 14:46:10 +01001232 void RunMicrotasksInternal();
1233
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001234 base::Atomic32 id_;
1235 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001236 int stack_trace_nesting_level_;
1237 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001238 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001239 Bootstrapper* bootstrapper_;
1240 RuntimeProfiler* runtime_profiler_;
1241 CompilationCache* compilation_cache_;
1242 Counters* counters_;
Steve Block44f0eee2011-05-26 01:26:41 +01001243 CodeRange* code_range_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001244 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001245 Logger* logger_;
1246 StackGuard stack_guard_;
1247 StatsTable* stats_table_;
1248 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001250 DeoptimizerData* deoptimizer_data_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001251 bool deoptimizer_lazy_throw_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001253 ThreadLocalTop thread_local_top_;
1254 bool capture_stack_trace_for_uncaught_exceptions_;
1255 int stack_trace_for_uncaught_exceptions_frame_limit_;
1256 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001257 MemoryAllocator* memory_allocator_;
1258 KeyedLookupCache* keyed_lookup_cache_;
1259 ContextSlotCache* context_slot_cache_;
1260 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001262 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001263 UnicodeCache* unicode_cache_;
Ben Murdochda12d292016-06-02 14:46:10 +01001264 base::AccountingAllocator allocator_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265 Zone runtime_zone_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001266 Zone interface_descriptor_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001267 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001268 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001269 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001270 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001271 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001272 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001273 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001274 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1275 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001276 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1277 regexp_macro_assembler_canonicalize_;
1278 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001279 DateCache* date_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001280 CallInterfaceDescriptorData* call_descriptor_data_;
1281 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001282
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 // Whether the isolate has been created for snapshotting.
1284 bool serializer_enabled_;
1285
1286 // True if fatal error has been signaled for this isolate.
1287 bool has_fatal_error_;
1288
1289 // True if this isolate was initialized from a snapshot.
1290 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001291
Ben Murdochda12d292016-06-02 14:46:10 +01001292 // True if ES2015 tail call elimination feature is enabled.
1293 bool is_tail_call_elimination_enabled_;
1294
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001295 // Time stamp at initialization.
1296 double time_millis_at_init_;
1297
Steve Block44f0eee2011-05-26 01:26:41 +01001298#ifdef DEBUG
1299 // A static array of histogram info for each type.
1300 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1301 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001302#endif
1303
Steve Block44f0eee2011-05-26 01:26:41 +01001304 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305 CpuProfiler* cpu_profiler_;
1306 HeapProfiler* heap_profiler_;
1307 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001309 interpreter::Interpreter* interpreter_;
1310
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001311 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1312 std::queue<InterruptEntry> api_interrupts_queue_;
1313
Steve Block44f0eee2011-05-26 01:26:41 +01001314#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1315 type name##_;
1316 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1317#undef GLOBAL_BACKING_STORE
1318
1319#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1320 type name##_[length];
1321 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1322#undef GLOBAL_ARRAY_BACKING_STORE
1323
1324#ifdef DEBUG
1325 // This class is huge and has a number of fields controlled by
1326 // preprocessor defines. Make sure the offsets of these fields agree
1327 // between compilation units.
1328#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1329 static const intptr_t name##_debug_offset_;
1330 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1331 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1332#undef ISOLATE_FIELD_OFFSET
1333#endif
1334
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001335 DeferredHandles* deferred_handles_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001336 OptimizingCompileDispatcher* optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337
1338 // Counts deopt points if deopt_every_n_times is enabled.
1339 unsigned int stress_deopt_count_;
1340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001341 Address virtual_handler_register_;
1342 Address virtual_slot_register_;
1343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 int next_optimization_id_;
1345
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001346 // Counts javascript calls from the API. Wraps around on overflow.
1347 unsigned int js_calls_from_api_counter_;
1348
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001349#if TRACE_MAPS
1350 int next_unique_sfi_id_;
1351#endif
1352
Ben Murdoch097c5b22016-05-18 11:27:45 +01001353 // List of callbacks before a Call starts execution.
1354 List<BeforeCallEnteredCallback> before_call_entered_callbacks_;
1355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001356 // List of callbacks when a Call completes.
1357 List<CallCompletedCallback> call_completed_callbacks_;
1358
Ben Murdochda12d292016-06-02 14:46:10 +01001359 // List of callbacks after microtasks were run.
1360 List<MicrotasksCompletedCallback> microtasks_completed_callbacks_;
1361
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001363 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001364
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001365 List<Object*> partial_snapshot_cache_;
1366
1367 v8::ArrayBuffer::Allocator* array_buffer_allocator_;
1368
1369 FutexWaitListNode futex_wait_list_node_;
1370
1371 CancelableTaskManager* cancelable_task_manager_;
1372
1373 v8::Isolate::AbortOnUncaughtExceptionCallback
1374 abort_on_uncaught_exception_callback_;
1375
Steve Block44f0eee2011-05-26 01:26:41 +01001376 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377 friend class HandleScopeImplementer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001378 friend class OptimizingCompileDispatcher;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001379 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001380 friend class ThreadManager;
1381 friend class Simulator;
1382 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001383 friend class ThreadId;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001384 friend class TestMemoryAllocatorScope;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001385 friend class TestCodeRangeScope;
Steve Block44f0eee2011-05-26 01:26:41 +01001386 friend class v8::Isolate;
1387 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001388 friend class v8::Unlocker;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001389 friend v8::StartupData v8::V8::CreateSnapshotDataBlob(const char*);
Ben Murdochda12d292016-06-02 14:46:10 +01001390 friend v8::StartupData v8::V8::WarmUpSnapshotDataBlob(v8::StartupData,
1391 const char*);
Steve Block44f0eee2011-05-26 01:26:41 +01001392
1393 DISALLOW_COPY_AND_ASSIGN(Isolate);
1394};
1395
1396
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397#undef FIELD_ACCESSOR
1398#undef THREAD_LOCAL_TOP_ACCESSOR
1399
1400
1401class PromiseOnStack {
1402 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001403 PromiseOnStack(Handle<JSFunction> function, Handle<JSObject> promise,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404 PromiseOnStack* prev)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001405 : function_(function), promise_(promise), prev_(prev) {}
1406 Handle<JSFunction> function() { return function_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 Handle<JSObject> promise() { return promise_; }
1408 PromiseOnStack* prev() { return prev_; }
1409
1410 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001411 Handle<JSFunction> function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001412 Handle<JSObject> promise_;
1413 PromiseOnStack* prev_;
1414};
1415
1416
Steve Block44f0eee2011-05-26 01:26:41 +01001417// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1418// class as a work around for a bug in the generated code found with these
1419// versions of GCC. See V8 issue 122 for details.
1420class SaveContext BASE_EMBEDDED {
1421 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001422 explicit SaveContext(Isolate* isolate);
1423 ~SaveContext();
Steve Block44f0eee2011-05-26 01:26:41 +01001424
1425 Handle<Context> context() { return context_; }
1426 SaveContext* prev() { return prev_; }
1427
1428 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001429 bool IsBelowFrame(JavaScriptFrame* frame) {
1430 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001431 }
1432
1433 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001434 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001435 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001436 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001437 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001438};
1439
1440
1441class AssertNoContextChange BASE_EMBEDDED {
1442#ifdef DEBUG
1443 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001444 explicit AssertNoContextChange(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001445 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001446 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001447 }
1448
1449 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001451 Handle<Context> context_;
1452#else
1453 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001455#endif
1456};
1457
1458
1459class ExecutionAccess BASE_EMBEDDED {
1460 public:
1461 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1462 Lock(isolate);
1463 }
1464 ~ExecutionAccess() { Unlock(isolate_); }
1465
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001466 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1467 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001468
1469 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001471 }
1472
1473 private:
1474 Isolate* isolate_;
1475};
1476
1477
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001479class StackLimitCheck BASE_EMBEDDED {
1480 public:
1481 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1482
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001483 // Use this to check for stack-overflows in C++ code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001484 bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001485 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001487 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001488
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001489 // Use this to check for interrupt request in C++ code.
1490 bool InterruptRequested() {
1491 StackGuard* stack_guard = isolate_->stack_guard();
1492 return GetCurrentStackPosition() < stack_guard->climit();
1493 }
1494
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001495 // Use this to check for stack-overflow when entering runtime from JS code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001496 bool JsHasOverflowed(uintptr_t gap = 0) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001497
Steve Block44f0eee2011-05-26 01:26:41 +01001498 private:
1499 Isolate* isolate_;
1500};
1501
1502
1503// Support for temporarily postponing interrupts. When the outermost
1504// postpone scope is left the interrupts will be re-enabled and any
1505// interrupts that occurred while in the scope will be taken into
1506// account.
1507class PostponeInterruptsScope BASE_EMBEDDED {
1508 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001509 PostponeInterruptsScope(Isolate* isolate,
1510 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1511 : stack_guard_(isolate->stack_guard()),
1512 intercept_mask_(intercept_mask),
1513 intercepted_flags_(0) {
1514 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001515 }
1516
1517 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001518 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001519 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001520
1521 // Find the bottom-most scope that intercepts this interrupt.
1522 // Return whether the interrupt has been intercepted.
1523 bool Intercept(StackGuard::InterruptFlag flag);
1524
Steve Block44f0eee2011-05-26 01:26:41 +01001525 private:
1526 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001527 int intercept_mask_;
1528 int intercepted_flags_;
1529 PostponeInterruptsScope* prev_;
1530
1531 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001532};
1533
1534
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001535class CodeTracer final : public Malloced {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001536 public:
1537 explicit CodeTracer(int isolate_id)
1538 : file_(NULL),
1539 scope_depth_(0) {
1540 if (!ShouldRedirect()) {
1541 file_ = stdout;
1542 return;
1543 }
Steve Block44f0eee2011-05-26 01:26:41 +01001544
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001545 if (FLAG_redirect_code_traces_to == NULL) {
1546 SNPrintF(filename_,
1547 "code-%d-%d.asm",
1548 base::OS::GetCurrentProcessId(),
1549 isolate_id);
1550 } else {
1551 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1552 }
Steve Block44f0eee2011-05-26 01:26:41 +01001553
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001554 WriteChars(filename_.start(), "", 0, false);
1555 }
Steve Block44f0eee2011-05-26 01:26:41 +01001556
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001557 class Scope {
1558 public:
1559 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1560 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001561
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001562 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001563
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001564 private:
1565 CodeTracer* tracer_;
1566 };
1567
1568 void OpenFile() {
1569 if (!ShouldRedirect()) {
1570 return;
1571 }
1572
1573 if (file_ == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001574 file_ = base::OS::FOpen(filename_.start(), "ab");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001575 }
1576
1577 scope_depth_++;
1578 }
1579
1580 void CloseFile() {
1581 if (!ShouldRedirect()) {
1582 return;
1583 }
1584
1585 if (--scope_depth_ == 0) {
1586 fclose(file_);
1587 file_ = NULL;
1588 }
1589 }
1590
1591 FILE* file() const { return file_; }
1592
1593 private:
1594 static bool ShouldRedirect() {
1595 return FLAG_redirect_code_traces;
1596 }
1597
1598 EmbeddedVector<char, 128> filename_;
1599 FILE* file_;
1600 int scope_depth_;
1601};
Steve Block44f0eee2011-05-26 01:26:41 +01001602
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001603} // namespace internal
1604} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01001605
Steve Block44f0eee2011-05-26 01:26:41 +01001606#endif // V8_ISOLATE_H_