blob: 5895ebbc9d7c33b1ba1208a08e220e178c7aca7d [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;
Steve Block44f0eee2011-05-26 01:26:41 +010061class HandleScopeImplementer;
62class HeapProfiler;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063class HStatistics;
64class HTracer;
Steve Block44f0eee2011-05-26 01:26:41 +010065class InlineRuntimeFunctionsTable;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066class InnerPointerToCodeCache;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067class Logger;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068class MaterializedObjectStore;
69class CodeAgingHelper;
Steve Block44f0eee2011-05-26 01:26:41 +010070class RegExpStack;
71class SaveContext;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072class StatsTable;
Steve Block44f0eee2011-05-26 01:26:41 +010073class StringTracker;
74class StubCache;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075class SweeperThread;
Steve Block44f0eee2011-05-26 01:26:41 +010076class ThreadManager;
77class ThreadState;
78class ThreadVisitor; // Defined in v8threads.h
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079class UnicodeCache;
80template <StateTag Tag> class VMState;
Steve Block44f0eee2011-05-26 01:26:41 +010081
82// 'void function pointer', used to roundtrip the
83// ExternalReference::ExternalReferenceRedirector since we can not include
84// assembler.h, where it is defined, here.
85typedef void* ExternalReferenceRedirectorPointer();
86
87
Steve Block44f0eee2011-05-26 01:26:41 +010088class Debug;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000089class PromiseOnStack;
Steve Block44f0eee2011-05-26 01:26:41 +010090class Redirection;
91class Simulator;
Steve Block44f0eee2011-05-26 01:26:41 +010092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093namespace interpreter {
94class Interpreter;
95}
Steve Block44f0eee2011-05-26 01:26:41 +010096
97// Static indirection table for handles to constants. If a frame
98// element represents a constant, the data contains an index into
99// this table of handles to the actual constants.
100// Static indirection table for handles to constants. If a Result
101// represents a constant, the data contains an index into this table
102// of handles to the actual constants.
103typedef ZoneList<Handle<Object> > ZoneObjectList;
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105#define RETURN_FAILURE_IF_SCHEDULED_EXCEPTION(isolate) \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100106 do { \
107 Isolate* __isolate__ = (isolate); \
108 if (__isolate__->has_scheduled_exception()) { \
109 return __isolate__->PromoteScheduledException(); \
110 } \
111 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100112
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113// Macros for MaybeHandle.
114
115#define RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, value) \
116 do { \
117 Isolate* __isolate__ = (isolate); \
118 if (__isolate__->has_scheduled_exception()) { \
119 __isolate__->PromoteScheduledException(); \
120 return value; \
121 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100122 } while (false)
123
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124#define RETURN_EXCEPTION_IF_SCHEDULED_EXCEPTION(isolate, T) \
125 RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, MaybeHandle<T>())
126
127#define ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, value) \
128 do { \
129 if (!(call).ToHandle(&dst)) { \
130 DCHECK((isolate)->has_pending_exception()); \
131 return value; \
132 } \
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100133 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100134
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call) \
136 ASSIGN_RETURN_ON_EXCEPTION_VALUE( \
137 isolate, dst, call, isolate->heap()->exception())
138
139#define ASSIGN_RETURN_ON_EXCEPTION(isolate, dst, call, T) \
140 ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate, dst, call, MaybeHandle<T>())
141
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000142#define THROW_NEW_ERROR(isolate, call, T) \
143 do { \
144 return isolate->Throw<T>(isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000145 } while (false)
146
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call) \
148 do { \
149 return isolate->Throw(*isolate->factory()->call); \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 } while (false)
151
152#define RETURN_ON_EXCEPTION_VALUE(isolate, call, value) \
153 do { \
154 if ((call).is_null()) { \
155 DCHECK((isolate)->has_pending_exception()); \
156 return value; \
157 } \
158 } while (false)
159
160#define RETURN_FAILURE_ON_EXCEPTION(isolate, call) \
161 RETURN_ON_EXCEPTION_VALUE(isolate, call, isolate->heap()->exception())
162
163#define RETURN_ON_EXCEPTION(isolate, call, T) \
164 RETURN_ON_EXCEPTION_VALUE(isolate, call, MaybeHandle<T>())
165
Steve Block44f0eee2011-05-26 01:26:41 +0100166
Ben Murdoch589d6972011-11-30 16:04:58 +0000167#define FOR_EACH_ISOLATE_ADDRESS_NAME(C) \
168 C(Handler, handler) \
169 C(CEntryFP, c_entry_fp) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400170 C(CFunction, c_function) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000171 C(Context, context) \
172 C(PendingException, pending_exception) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000173 C(PendingHandlerContext, pending_handler_context) \
174 C(PendingHandlerCode, pending_handler_code) \
175 C(PendingHandlerOffset, pending_handler_offset) \
176 C(PendingHandlerFP, pending_handler_fp) \
177 C(PendingHandlerSP, pending_handler_sp) \
Ben Murdoch589d6972011-11-30 16:04:58 +0000178 C(ExternalCaughtException, external_caught_exception) \
179 C(JSEntrySP, js_entry_sp)
Steve Block44f0eee2011-05-26 01:26:41 +0100180
Ben Murdochda12d292016-06-02 14:46:10 +0100181#define FOR_WITH_HANDLE_SCOPE(isolate, loop_var_type, init, loop_var, \
182 limit_check, increment, body) \
183 do { \
184 loop_var_type init; \
185 loop_var_type for_with_handle_limit = loop_var; \
186 Isolate* for_with_handle_isolate = isolate; \
187 while (limit_check) { \
188 for_with_handle_limit += 1024; \
189 HandleScope loop_scope(for_with_handle_isolate); \
190 for (; limit_check && loop_var < for_with_handle_limit; increment) { \
191 body \
192 } \
193 } \
194 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +0100195
Ben Murdoch8b112d22011-06-08 16:22:53 +0100196// Platform-independent, reliable thread identifier.
197class ThreadId {
198 public:
199 // Creates an invalid ThreadId.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400200 ThreadId() { base::NoBarrier_Store(&id_, kInvalidId); }
201
202 ThreadId& operator=(const ThreadId& other) {
203 base::NoBarrier_Store(&id_, base::NoBarrier_Load(&other.id_));
204 return *this;
205 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100206
207 // Returns ThreadId for current thread.
208 static ThreadId Current() { return ThreadId(GetCurrentThreadId()); }
209
210 // Returns invalid ThreadId (guaranteed not to be equal to any thread).
211 static ThreadId Invalid() { return ThreadId(kInvalidId); }
212
213 // Compares ThreadIds for equality.
214 INLINE(bool Equals(const ThreadId& other) const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400215 return base::NoBarrier_Load(&id_) == base::NoBarrier_Load(&other.id_);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100216 }
217
218 // Checks whether this ThreadId refers to any thread.
219 INLINE(bool IsValid() const) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400220 return base::NoBarrier_Load(&id_) != kInvalidId;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100221 }
222
223 // Converts ThreadId to an integer representation
224 // (required for public API: V8::V8::GetCurrentThreadId).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400225 int ToInteger() const { return static_cast<int>(base::NoBarrier_Load(&id_)); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100226
227 // Converts ThreadId to an integer representation
228 // (required for public API: V8::V8::TerminateExecution).
229 static ThreadId FromInteger(int id) { return ThreadId(id); }
230
231 private:
232 static const int kInvalidId = -1;
233
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234 explicit ThreadId(int id) { base::NoBarrier_Store(&id_, id); }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100235
236 static int AllocateThreadId();
237
238 static int GetCurrentThreadId();
239
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400240 base::Atomic32 id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100241
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000242 static base::Atomic32 highest_thread_id_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100243
244 friend class Isolate;
245};
246
247
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248#define FIELD_ACCESSOR(type, name) \
249 inline void set_##name(type v) { name##_ = v; } \
250 inline type name() const { return name##_; }
251
252
Steve Block44f0eee2011-05-26 01:26:41 +0100253class ThreadLocalTop BASE_EMBEDDED {
254 public:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100255 // Does early low-level initialization that does not depend on the
256 // isolate being present.
257 ThreadLocalTop();
258
Steve Block44f0eee2011-05-26 01:26:41 +0100259 // Initialize the thread data.
260 void Initialize();
261
262 // Get the top C++ try catch handler or NULL if none are registered.
263 //
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264 // This method is not guaranteed to return an address that can be
Steve Block44f0eee2011-05-26 01:26:41 +0100265 // used for comparison with addresses into the JS stack. If such an
266 // address is needed, use try_catch_handler_address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 FIELD_ACCESSOR(v8::TryCatch*, try_catch_handler)
Steve Block44f0eee2011-05-26 01:26:41 +0100268
269 // Get the address of the top C++ try catch handler or NULL if
270 // none are registered.
271 //
272 // This method always returns an address that can be compared to
273 // pointers into the JavaScript stack. When running on actual
274 // hardware, try_catch_handler_address and TryCatchHandler return
275 // the same pointer. When running on a simulator with a separate JS
276 // stack, try_catch_handler_address returns a JS stack address that
277 // corresponds to the place on the JS stack where the C++ handler
278 // would have been if the stack were not separate.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 Address try_catch_handler_address() {
280 return reinterpret_cast<Address>(
281 v8::TryCatch::JSStackComparableAddress(try_catch_handler()));
Steve Block44f0eee2011-05-26 01:26:41 +0100282 }
283
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 void Free();
Steve Block44f0eee2011-05-26 01:26:41 +0100285
Ben Murdoch257744e2011-11-30 15:57:28 +0000286 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +0100287 // The context where the current execution method is created and for variable
288 // lookups.
289 Context* context_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100290 ThreadId thread_id_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 Object* pending_exception_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000292
293 // Communication channel between Isolate::FindHandler and the CEntryStub.
294 Context* pending_handler_context_;
295 Code* pending_handler_code_;
296 intptr_t pending_handler_offset_;
297 Address pending_handler_fp_;
298 Address pending_handler_sp_;
299
300 // Communication channel between Isolate::Throw and message consumers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 bool rethrowing_message_;
Steve Block44f0eee2011-05-26 01:26:41 +0100302 Object* pending_message_obj_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303
Steve Block44f0eee2011-05-26 01:26:41 +0100304 // Use a separate value for scheduled exceptions to preserve the
305 // invariants that hold about pending_exception. We may want to
306 // unify them later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000307 Object* scheduled_exception_;
Steve Block44f0eee2011-05-26 01:26:41 +0100308 bool external_caught_exception_;
309 SaveContext* save_context_;
Steve Block44f0eee2011-05-26 01:26:41 +0100310
311 // Stack.
312 Address c_entry_fp_; // the frame pointer of the top c entry frame
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313 Address handler_; // try-blocks are chained through the stack
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400314 Address c_function_; // C function that was called at c entry.
Steve Block44f0eee2011-05-26 01:26:41 +0100315
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 // Throwing an exception may cause a Promise rejection. For this purpose
317 // we keep track of a stack of nested promises and the corresponding
318 // try-catch handlers.
319 PromiseOnStack* promise_on_stack_;
320
Steve Block44f0eee2011-05-26 01:26:41 +0100321#ifdef USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100322 Simulator* simulator_;
323#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100324
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100325 Address js_entry_sp_; // the stack pointer of the bottom JS entry frame
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 // the external callback we're currently in
327 ExternalCallbackScope* external_callback_scope_;
Steve Block44f0eee2011-05-26 01:26:41 +0100328 StateTag current_vm_state_;
Steve Block44f0eee2011-05-26 01:26:41 +0100329
Steve Block44f0eee2011-05-26 01:26:41 +0100330 // Call back function to report unsafe JS accesses.
331 v8::FailedAccessCheckCallback failed_access_check_callback_;
332
333 private:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100334 void InitializeInternal();
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 v8::TryCatch* try_catch_handler_;
Steve Block44f0eee2011-05-26 01:26:41 +0100337};
338
Steve Block44f0eee2011-05-26 01:26:41 +0100339
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100341
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342#define ISOLATE_INIT_SIMULATOR_LIST(V) \
343 V(bool, simulator_initialized, false) \
344 V(HashMap*, simulator_i_cache, NULL) \
345 V(Redirection*, simulator_redirection, NULL)
Steve Block44f0eee2011-05-26 01:26:41 +0100346#else
347
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348#define ISOLATE_INIT_SIMULATOR_LIST(V)
Steve Block44f0eee2011-05-26 01:26:41 +0100349
350#endif
351
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000352
Steve Block44f0eee2011-05-26 01:26:41 +0100353#ifdef DEBUG
354
355#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V) \
356 V(CommentStatistic, paged_space_comments_statistics, \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 CommentStatistic::kMaxComments + 1) \
358 V(int, code_kind_statistics, Code::NUMBER_OF_KINDS)
Steve Block44f0eee2011-05-26 01:26:41 +0100359#else
360
361#define ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
362
363#endif
364
Steve Block44f0eee2011-05-26 01:26:41 +0100365#define ISOLATE_INIT_ARRAY_LIST(V) \
366 /* SerializerDeserializer state. */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 V(int32_t, jsregexp_static_offsets_vector, kJSRegexpStaticOffsetsVectorSize) \
Steve Block44f0eee2011-05-26 01:26:41 +0100368 V(int, bad_char_shift_table, kUC16AlphabetSize) \
369 V(int, good_suffix_shift_table, (kBMMaxShift + 1)) \
370 V(int, suffix_table, (kBMMaxShift + 1)) \
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000371 V(uint32_t, private_random_seed, 2) \
Steve Block44f0eee2011-05-26 01:26:41 +0100372 ISOLATE_INIT_DEBUG_ARRAY_LIST(V)
373
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000374typedef List<HeapObject*> DebugObjectCache;
Steve Block44f0eee2011-05-26 01:26:41 +0100375
376#define ISOLATE_INIT_LIST(V) \
Steve Block44f0eee2011-05-26 01:26:41 +0100377 /* Assembler state. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100378 V(FatalErrorCallback, exception_behavior, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000379 V(LogEventCallback, event_logger, NULL) \
Ben Murdoch257744e2011-11-30 15:57:28 +0000380 V(AllowCodeGenerationFromStringsCallback, allow_code_gen_callback, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100381 /* To distinguish the function templates, so that we can find them in the */ \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 /* function cache of the native context. */ \
Steve Block44f0eee2011-05-26 01:26:41 +0100383 V(int, next_serial_number, 0) \
384 V(ExternalReferenceRedirectorPointer*, external_reference_redirector, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100385 /* State for Relocatable. */ \
386 V(Relocatable*, relocatable_top, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100387 V(DebugObjectCache*, string_stream_debug_object_cache, NULL) \
388 V(Object*, string_stream_current_security_token, NULL) \
Steve Block44f0eee2011-05-26 01:26:41 +0100389 V(ExternalReferenceTable*, external_reference_table, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 V(HashMap*, external_reference_map, NULL) \
391 V(HashMap*, root_index_map, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 V(int, pending_microtask_count, 0) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393 V(HStatistics*, hstatistics, NULL) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400394 V(CompilationStatistics*, turbo_statistics, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000395 V(HTracer*, htracer, NULL) \
396 V(CodeTracer*, code_tracer, NULL) \
397 V(bool, fp_stubs_generated, false) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398 V(uint32_t, per_isolate_assert_data, 0xFFFFFFFFu) \
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400399 V(PromiseRejectCallback, promise_reject_callback, NULL) \
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 V(const v8::StartupData*, snapshot_blob, NULL) \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000401 ISOLATE_INIT_SIMULATOR_LIST(V)
402
403#define THREAD_LOCAL_TOP_ACCESSOR(type, name) \
404 inline void set_##name(type v) { thread_local_top_.name##_ = v; } \
405 inline type name() const { return thread_local_top_.name##_; }
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407#define THREAD_LOCAL_TOP_ADDRESS(type, name) \
408 type* name##_address() { return &thread_local_top_.name##_; }
409
Steve Block44f0eee2011-05-26 01:26:41 +0100410
411class Isolate {
412 // These forward declarations are required to make the friend declarations in
413 // PerIsolateThreadData work on some older versions of gcc.
414 class ThreadDataTable;
415 class EntryStackItem;
416 public:
417 ~Isolate();
418
Steve Block44f0eee2011-05-26 01:26:41 +0100419 // A thread has a PerIsolateThreadData instance for each isolate that it has
420 // entered. That instance is allocated when the isolate is initially entered
421 // and reused on subsequent entries.
422 class PerIsolateThreadData {
423 public:
424 PerIsolateThreadData(Isolate* isolate, ThreadId thread_id)
425 : isolate_(isolate),
426 thread_id_(thread_id),
427 stack_limit_(0),
428 thread_state_(NULL),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000429#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100430 simulator_(NULL),
431#endif
432 next_(NULL),
433 prev_(NULL) { }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 ~PerIsolateThreadData();
Steve Block44f0eee2011-05-26 01:26:41 +0100435 Isolate* isolate() const { return isolate_; }
436 ThreadId thread_id() const { return thread_id_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100437
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438 FIELD_ACCESSOR(uintptr_t, stack_limit)
439 FIELD_ACCESSOR(ThreadState*, thread_state)
440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441#if USE_SIMULATOR
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 FIELD_ACCESSOR(Simulator*, simulator)
Steve Block44f0eee2011-05-26 01:26:41 +0100443#endif
444
445 bool Matches(Isolate* isolate, ThreadId thread_id) const {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100446 return isolate_ == isolate && thread_id_.Equals(thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100447 }
448
449 private:
450 Isolate* isolate_;
451 ThreadId thread_id_;
452 uintptr_t stack_limit_;
453 ThreadState* thread_state_;
454
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000455#if USE_SIMULATOR
Steve Block44f0eee2011-05-26 01:26:41 +0100456 Simulator* simulator_;
457#endif
458
459 PerIsolateThreadData* next_;
460 PerIsolateThreadData* prev_;
461
462 friend class Isolate;
463 friend class ThreadDataTable;
464 friend class EntryStackItem;
465
466 DISALLOW_COPY_AND_ASSIGN(PerIsolateThreadData);
467 };
468
469
470 enum AddressId {
Ben Murdoch589d6972011-11-30 16:04:58 +0000471#define DECLARE_ENUM(CamelName, hacker_name) k##CamelName##Address,
472 FOR_EACH_ISOLATE_ADDRESS_NAME(DECLARE_ENUM)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473#undef DECLARE_ENUM
Ben Murdoch589d6972011-11-30 16:04:58 +0000474 kIsolateAddressCount
Steve Block44f0eee2011-05-26 01:26:41 +0100475 };
476
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477 static void InitializeOncePerProcess();
478
Steve Block44f0eee2011-05-26 01:26:41 +0100479 // Returns the PerIsolateThreadData for the current thread (or NULL if one is
480 // not currently set).
481 static PerIsolateThreadData* CurrentPerIsolateThreadData() {
482 return reinterpret_cast<PerIsolateThreadData*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 base::Thread::GetThreadLocal(per_isolate_thread_data_key_));
Steve Block44f0eee2011-05-26 01:26:41 +0100484 }
485
486 // Returns the isolate inside which the current thread is running.
487 INLINE(static Isolate* Current()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400488 DCHECK(base::NoBarrier_Load(&isolate_key_created_) == 1);
Steve Block44f0eee2011-05-26 01:26:41 +0100489 Isolate* isolate = reinterpret_cast<Isolate*>(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 base::Thread::GetExistingThreadLocal(isolate_key_));
491 DCHECK(isolate != NULL);
Steve Block44f0eee2011-05-26 01:26:41 +0100492 return isolate;
493 }
494
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000495 // Usually called by Init(), but can be called early e.g. to allow
496 // testing components that require logging but not the whole
497 // isolate.
498 //
499 // Safe to call more than once.
500 void InitializeLoggingAndCounters();
501
Steve Block44f0eee2011-05-26 01:26:41 +0100502 bool Init(Deserializer* des);
503
Steve Block44f0eee2011-05-26 01:26:41 +0100504 // True if at least one thread Enter'ed this isolate.
505 bool IsInUse() { return entry_stack_ != NULL; }
506
507 // Destroys the non-default isolates.
508 // Sets default isolate into "has_been_disposed" state rather then destroying,
509 // for legacy API reasons.
510 void TearDown();
511
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 static void GlobalTearDown();
Steve Block44f0eee2011-05-26 01:26:41 +0100513
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000514 void ClearSerializerData();
515
Ben Murdoch257744e2011-11-30 15:57:28 +0000516 // Find the PerThread for this particular (isolate, thread) combination
517 // If one does not yet exist, return null.
518 PerIsolateThreadData* FindPerThreadDataForThisThread();
519
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 // Find the PerThread for given (isolate, thread) combination
521 // If one does not yet exist, return null.
522 PerIsolateThreadData* FindPerThreadDataForThread(ThreadId thread_id);
Steve Block44f0eee2011-05-26 01:26:41 +0100523
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000524 // Discard the PerThread for this particular (isolate, thread) combination
525 // If one does not yet exist, no-op.
526 void DiscardPerThreadDataForThisThread();
527
Steve Block44f0eee2011-05-26 01:26:41 +0100528 // Returns the key used to store the pointer to the current isolate.
529 // Used internally for V8 threads that do not execute JavaScript but still
530 // are part of the domain of an isolate (like the context switcher).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000531 static base::Thread::LocalStorageKey isolate_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100532 return isolate_key_;
533 }
Steve Block44f0eee2011-05-26 01:26:41 +0100534
535 // Returns the key used to store process-wide thread IDs.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 static base::Thread::LocalStorageKey thread_id_key() {
Ben Murdoch85b71792012-04-11 18:30:58 +0100537 return thread_id_key_;
538 }
Steve Block44f0eee2011-05-26 01:26:41 +0100539
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000540 static base::Thread::LocalStorageKey per_isolate_thread_data_key();
Steve Block44f0eee2011-05-26 01:26:41 +0100541
Steve Block44f0eee2011-05-26 01:26:41 +0100542 // Mutex for serializing access to break control structures.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 base::RecursiveMutex* break_access() { return &break_access_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000544
Steve Block44f0eee2011-05-26 01:26:41 +0100545 Address get_address_from_id(AddressId id);
546
547 // Access to top context (where the current function object was created).
548 Context* context() { return thread_local_top_.context_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000549 inline void set_context(Context* context);
Steve Block44f0eee2011-05-26 01:26:41 +0100550 Context** context_address() { return &thread_local_top_.context_; }
551
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552 THREAD_LOCAL_TOP_ACCESSOR(SaveContext*, save_context)
Steve Block44f0eee2011-05-26 01:26:41 +0100553
554 // Access to current thread id.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000555 THREAD_LOCAL_TOP_ACCESSOR(ThreadId, thread_id)
Steve Block44f0eee2011-05-26 01:26:41 +0100556
557 // Interface to pending exception.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000558 inline Object* pending_exception();
559 inline void set_pending_exception(Object* exception_obj);
560 inline void clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000561
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000562 THREAD_LOCAL_TOP_ADDRESS(Object*, pending_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000563
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000564 inline bool has_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000566 THREAD_LOCAL_TOP_ADDRESS(Context*, pending_handler_context)
567 THREAD_LOCAL_TOP_ADDRESS(Code*, pending_handler_code)
568 THREAD_LOCAL_TOP_ADDRESS(intptr_t, pending_handler_offset)
569 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_fp)
570 THREAD_LOCAL_TOP_ADDRESS(Address, pending_handler_sp)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571
572 THREAD_LOCAL_TOP_ACCESSOR(bool, external_caught_exception)
573
Steve Block44f0eee2011-05-26 01:26:41 +0100574 v8::TryCatch* try_catch_handler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000575 return thread_local_top_.try_catch_handler();
Steve Block44f0eee2011-05-26 01:26:41 +0100576 }
Steve Block44f0eee2011-05-26 01:26:41 +0100577 bool* external_caught_exception_address() {
578 return &thread_local_top_.external_caught_exception_;
579 }
580
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000581 THREAD_LOCAL_TOP_ADDRESS(Object*, scheduled_exception)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000582
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583 inline void clear_pending_message();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000584 Address pending_message_obj_address() {
585 return reinterpret_cast<Address>(&thread_local_top_.pending_message_obj_);
586 }
587
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000588 inline Object* scheduled_exception();
589 inline bool has_scheduled_exception();
590 inline void clear_scheduled_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 bool IsJavaScriptHandlerOnTop(Object* exception);
593 bool IsExternalHandlerOnTop(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000595 inline bool is_catchable_by_javascript(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596
Steve Block44f0eee2011-05-26 01:26:41 +0100597 // JS execution stack (see frames.h).
598 static Address c_entry_fp(ThreadLocalTop* thread) {
599 return thread->c_entry_fp_;
600 }
601 static Address handler(ThreadLocalTop* thread) { return thread->handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400602 Address c_function() { return thread_local_top_.c_function_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100603
604 inline Address* c_entry_fp_address() {
605 return &thread_local_top_.c_entry_fp_;
606 }
607 inline Address* handler_address() { return &thread_local_top_.handler_; }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400608 inline Address* c_function_address() {
609 return &thread_local_top_.c_function_;
610 }
Steve Block44f0eee2011-05-26 01:26:41 +0100611
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612 // Bottom JS entry.
613 Address js_entry_sp() {
614 return thread_local_top_.js_entry_sp_;
Steve Block44f0eee2011-05-26 01:26:41 +0100615 }
616 inline Address* js_entry_sp_address() {
617 return &thread_local_top_.js_entry_sp_;
618 }
Steve Block44f0eee2011-05-26 01:26:41 +0100619
Steve Block44f0eee2011-05-26 01:26:41 +0100620 // Returns the global object of the current context. It could be
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100621 // a builtin object, or a JS global object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000622 inline Handle<JSGlobalObject> global_object();
Steve Block44f0eee2011-05-26 01:26:41 +0100623
624 // Returns the global proxy object of the current context.
Ben Murdochc5610432016-08-08 18:44:38 +0100625 inline Handle<JSObject> global_proxy();
Steve Block44f0eee2011-05-26 01:26:41 +0100626
Steve Block44f0eee2011-05-26 01:26:41 +0100627 static int ArchiveSpacePerThread() { return sizeof(ThreadLocalTop); }
628 void FreeThreadResources() { thread_local_top_.Free(); }
629
630 // This method is called by the api after operations that may throw
631 // exceptions. If an exception was thrown and not handled by an external
632 // handler the exception is scheduled to be rethrown when we return to running
633 // JavaScript code. If an exception is scheduled true is returned.
634 bool OptionalRescheduleException(bool is_bottom_call);
635
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 // Push and pop a promise and the current try-catch handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000637 void PushPromise(Handle<JSObject> promise, Handle<JSFunction> function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 void PopPromise();
639 Handle<Object> GetPromiseOnStackOnThrow();
640
Ben Murdoch8b112d22011-06-08 16:22:53 +0100641 class ExceptionScope {
642 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000643 // Scope currently can only be used for regular exceptions,
644 // not termination exception.
645 inline explicit ExceptionScope(Isolate* isolate);
646 inline ~ExceptionScope();
Ben Murdoch8b112d22011-06-08 16:22:53 +0100647
648 private:
649 Isolate* isolate_;
650 Handle<Object> pending_exception_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100651 };
652
Steve Block44f0eee2011-05-26 01:26:41 +0100653 void SetCaptureStackTraceForUncaughtExceptions(
654 bool capture,
655 int frame_limit,
656 StackTrace::StackTraceOptions options);
657
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000658 void SetAbortOnUncaughtExceptionCallback(
659 v8::Isolate::AbortOnUncaughtExceptionCallback callback);
660
661 enum PrintStackMode { kPrintStackConcise, kPrintStackVerbose };
Steve Block44f0eee2011-05-26 01:26:41 +0100662 void PrintCurrentStackTrace(FILE* out);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000663 void PrintStack(StringStream* accumulator,
664 PrintStackMode mode = kPrintStackVerbose);
665 void PrintStack(FILE* out, PrintStackMode mode = kPrintStackVerbose);
Steve Block44f0eee2011-05-26 01:26:41 +0100666 Handle<String> StackTraceString();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000667 NO_INLINE(void PushStackTraceAndDie(unsigned int magic, void* ptr1,
668 void* ptr2, unsigned int magic2));
Steve Block44f0eee2011-05-26 01:26:41 +0100669 Handle<JSArray> CaptureCurrentStackTrace(
670 int frame_limit,
671 StackTrace::StackTraceOptions options);
Ben Murdochda12d292016-06-02 14:46:10 +0100672 Handle<Object> CaptureSimpleStackTrace(Handle<JSReceiver> error_object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 Handle<Object> caller);
Ben Murdochda12d292016-06-02 14:46:10 +0100674 MaybeHandle<JSReceiver> CaptureAndSetDetailedStackTrace(
675 Handle<JSReceiver> error_object);
676 MaybeHandle<JSReceiver> CaptureAndSetSimpleStackTrace(
677 Handle<JSReceiver> error_object, Handle<Object> caller);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400678 Handle<JSArray> GetDetailedStackTrace(Handle<JSObject> error_object);
679 Handle<JSArray> GetDetailedFromSimpleStackTrace(
680 Handle<JSObject> error_object);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100681
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000682 // Returns if the given context may access the given global object. If
Steve Block44f0eee2011-05-26 01:26:41 +0100683 // the result is false, the pending exception is guaranteed to be
684 // set.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000685 bool MayAccess(Handle<Context> accessing_context, Handle<JSObject> receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000686
Steve Block44f0eee2011-05-26 01:26:41 +0100687 void SetFailedAccessCheckCallback(v8::FailedAccessCheckCallback callback);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000688 void ReportFailedAccessCheck(Handle<JSObject> receiver);
Steve Block44f0eee2011-05-26 01:26:41 +0100689
690 // Exception throwing support. The caller should use the result
Ben Murdochc5610432016-08-08 18:44:38 +0100691 // of Throw() as its return vaue.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 Object* Throw(Object* exception, MessageLocation* location = NULL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000693 Object* ThrowIllegalOperation();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694
695 template <typename T>
696 MUST_USE_RESULT MaybeHandle<T> Throw(Handle<Object> exception,
697 MessageLocation* location = NULL) {
698 Throw(*exception, location);
699 return MaybeHandle<T>();
700 }
701
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000702 // Re-throw an exception. This involves no error reporting since error
703 // reporting was handled when the exception was thrown originally.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 Object* ReThrow(Object* exception);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000705
706 // Find the correct handler for the current pending exception. This also
707 // clears and returns the current pending exception.
708 Object* UnwindAndFindHandler();
709
710 // Tries to predict whether an exception will be caught. Note that this can
711 // only produce an estimate, because it is undecidable whether a finally
712 // clause will consume or re-throw an exception. We conservatively assume any
713 // finally clause will behave as if the exception were consumed.
714 enum CatchType { NOT_CAUGHT, CAUGHT_BY_JAVASCRIPT, CAUGHT_BY_EXTERNAL };
715 CatchType PredictExceptionCatcher();
716
Steve Block44f0eee2011-05-26 01:26:41 +0100717 void ScheduleThrow(Object* exception);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718 // Re-set pending message, script and positions reported to the TryCatch
719 // back to the TLS for re-use when rethrowing.
720 void RestorePendingMessageFromTryCatch(v8::TryCatch* handler);
721 // Un-schedule an exception that was caught by a TryCatch handler.
722 void CancelScheduledExceptionFromTryCatch(v8::TryCatch* handler);
Steve Block44f0eee2011-05-26 01:26:41 +0100723 void ReportPendingMessages();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000724 // Return pending location if any or unfilled structure.
725 MessageLocation GetMessageLocation();
Steve Block44f0eee2011-05-26 01:26:41 +0100726
727 // Promote a scheduled exception to pending. Asserts has_scheduled_exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 Object* PromoteScheduledException();
Steve Block44f0eee2011-05-26 01:26:41 +0100729
730 // Attempts to compute the current source location, storing the
731 // result in the target out parameter.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000732 bool ComputeLocation(MessageLocation* target);
733 bool ComputeLocationFromException(MessageLocation* target,
734 Handle<Object> exception);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400735 bool ComputeLocationFromStackTrace(MessageLocation* target,
736 Handle<Object> exception);
737
738 Handle<JSMessageObject> CreateMessage(Handle<Object> exception,
739 MessageLocation* location);
Steve Block44f0eee2011-05-26 01:26:41 +0100740
Steve Block44f0eee2011-05-26 01:26:41 +0100741 // Out of resource exception helpers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 Object* StackOverflow();
743 Object* TerminateExecution();
744 void CancelTerminateExecution();
745
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400746 void RequestInterrupt(InterruptCallback callback, void* data);
747 void InvokeApiInterruptCallbacks();
Steve Block44f0eee2011-05-26 01:26:41 +0100748
749 // Administration
750 void Iterate(ObjectVisitor* v);
751 void Iterate(ObjectVisitor* v, ThreadLocalTop* t);
752 char* Iterate(ObjectVisitor* v, char* t);
Steve Block44f0eee2011-05-26 01:26:41 +0100753 void IterateThread(ThreadVisitor* v, char* t);
754
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400755 // Returns the current native context.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 Handle<Context> native_context();
Steve Block44f0eee2011-05-26 01:26:41 +0100757
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000758 // Returns the native context of the calling JavaScript code. That
759 // is, the native context of the top-most JavaScript frame.
760 Handle<Context> GetCallingNativeContext();
Steve Block44f0eee2011-05-26 01:26:41 +0100761
762 void RegisterTryCatchHandler(v8::TryCatch* that);
763 void UnregisterTryCatchHandler(v8::TryCatch* that);
764
765 char* ArchiveThread(char* to);
766 char* RestoreThread(char* from);
767
768 static const char* const kStackOverflowMessage;
769
770 static const int kUC16AlphabetSize = 256; // See StringSearchBase.
771 static const int kBMMaxShift = 250; // See StringSearchBase.
772
773 // Accessors.
774#define GLOBAL_ACCESSOR(type, name, initialvalue) \
775 inline type name() const { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100777 return name##_; \
778 } \
779 inline void set_##name(type value) { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100781 name##_ = value; \
782 }
783 ISOLATE_INIT_LIST(GLOBAL_ACCESSOR)
784#undef GLOBAL_ACCESSOR
785
786#define GLOBAL_ARRAY_ACCESSOR(type, name, length) \
787 inline type* name() { \
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 DCHECK(OFFSET_OF(Isolate, name##_) == name##_debug_offset_); \
Steve Block44f0eee2011-05-26 01:26:41 +0100789 return &(name##_)[0]; \
790 }
791 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_ACCESSOR)
792#undef GLOBAL_ARRAY_ACCESSOR
793
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794#define NATIVE_CONTEXT_FIELD_ACCESSOR(index, type, name) \
795 inline Handle<type> name(); \
796 inline bool is_##name(type* value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797 NATIVE_CONTEXT_FIELDS(NATIVE_CONTEXT_FIELD_ACCESSOR)
798#undef NATIVE_CONTEXT_FIELD_ACCESSOR
Steve Block44f0eee2011-05-26 01:26:41 +0100799
800 Bootstrapper* bootstrapper() { return bootstrapper_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000801 Counters* counters() {
802 // Call InitializeLoggingAndCounters() if logging is needed before
803 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 DCHECK(counters_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000805 return counters_;
806 }
Steve Block44f0eee2011-05-26 01:26:41 +0100807 RuntimeProfiler* runtime_profiler() { return runtime_profiler_; }
808 CompilationCache* compilation_cache() { return compilation_cache_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000809 Logger* logger() {
810 // Call InitializeLoggingAndCounters() if logging is needed before
811 // the isolate is fully initialized.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812 DCHECK(logger_ != NULL);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000813 return logger_;
814 }
Steve Block44f0eee2011-05-26 01:26:41 +0100815 StackGuard* stack_guard() { return &stack_guard_; }
816 Heap* heap() { return &heap_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000817 StatsTable* stats_table();
Steve Block44f0eee2011-05-26 01:26:41 +0100818 StubCache* stub_cache() { return stub_cache_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000819 CodeAgingHelper* code_aging_helper() { return code_aging_helper_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100820 DeoptimizerData* deoptimizer_data() { return deoptimizer_data_; }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100821 bool deoptimizer_lazy_throw() const { return deoptimizer_lazy_throw_; }
822 void set_deoptimizer_lazy_throw(bool value) {
823 deoptimizer_lazy_throw_ = value;
824 }
Steve Block44f0eee2011-05-26 01:26:41 +0100825 ThreadLocalTop* thread_local_top() { return &thread_local_top_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826 MaterializedObjectStore* materialized_object_store() {
827 return materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +0100828 }
829
Steve Block44f0eee2011-05-26 01:26:41 +0100830 KeyedLookupCache* keyed_lookup_cache() {
831 return keyed_lookup_cache_;
832 }
833
834 ContextSlotCache* context_slot_cache() {
835 return context_slot_cache_;
836 }
837
838 DescriptorLookupCache* descriptor_lookup_cache() {
839 return descriptor_lookup_cache_;
840 }
841
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 HandleScopeData* handle_scope_data() { return &handle_scope_data_; }
843
Steve Block44f0eee2011-05-26 01:26:41 +0100844 HandleScopeImplementer* handle_scope_implementer() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000845 DCHECK(handle_scope_implementer_);
Steve Block44f0eee2011-05-26 01:26:41 +0100846 return handle_scope_implementer_;
847 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 Zone* runtime_zone() { return &runtime_zone_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000849 Zone* interface_descriptor_zone() { return &interface_descriptor_zone_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100850
Ben Murdoch8b112d22011-06-08 16:22:53 +0100851 UnicodeCache* unicode_cache() {
852 return unicode_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +0100853 }
854
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100855 InnerPointerToCodeCache* inner_pointer_to_code_cache() {
856 return inner_pointer_to_code_cache_;
857 }
Steve Block44f0eee2011-05-26 01:26:41 +0100858
Steve Block44f0eee2011-05-26 01:26:41 +0100859 GlobalHandles* global_handles() { return global_handles_; }
860
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000861 EternalHandles* eternal_handles() { return eternal_handles_; }
862
Steve Block44f0eee2011-05-26 01:26:41 +0100863 ThreadManager* thread_manager() { return thread_manager_; }
864
Steve Block44f0eee2011-05-26 01:26:41 +0100865 unibrow::Mapping<unibrow::Ecma262UnCanonicalize>* jsregexp_uncanonicalize() {
866 return &jsregexp_uncanonicalize_;
867 }
868
869 unibrow::Mapping<unibrow::CanonicalizationRange>* jsregexp_canonrange() {
870 return &jsregexp_canonrange_;
871 }
872
Steve Block44f0eee2011-05-26 01:26:41 +0100873 RuntimeState* runtime_state() { return &runtime_state_; }
874
Steve Block44f0eee2011-05-26 01:26:41 +0100875 Builtins* builtins() { return &builtins_; }
876
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100877 void NotifyExtensionInstalled() {
878 has_installed_extensions_ = true;
879 }
880
881 bool has_installed_extensions() { return has_installed_extensions_; }
882
Steve Block44f0eee2011-05-26 01:26:41 +0100883 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
884 regexp_macro_assembler_canonicalize() {
885 return &regexp_macro_assembler_canonicalize_;
886 }
887
888 RegExpStack* regexp_stack() { return regexp_stack_; }
889
890 unibrow::Mapping<unibrow::Ecma262Canonicalize>*
891 interp_canonicalize_mapping() {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100892 return &regexp_macro_assembler_canonicalize_;
Steve Block44f0eee2011-05-26 01:26:41 +0100893 }
894
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895 Debug* debug() { return debug_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100896
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000897 CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
898 HeapProfiler* heap_profiler() const { return heap_profiler_; }
899
Steve Block44f0eee2011-05-26 01:26:41 +0100900#ifdef DEBUG
901 HistogramInfo* heap_histograms() { return heap_histograms_; }
902
903 JSObject::SpillInformation* js_spill_information() {
904 return &js_spill_information_;
905 }
Steve Block44f0eee2011-05-26 01:26:41 +0100906#endif
907
908 Factory* factory() { return reinterpret_cast<Factory*>(this); }
909
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 static const int kJSRegexpStaticOffsetsVectorSize = 128;
Steve Block44f0eee2011-05-26 01:26:41 +0100911
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 THREAD_LOCAL_TOP_ACCESSOR(ExternalCallbackScope*, external_callback_scope)
Steve Block44f0eee2011-05-26 01:26:41 +0100913
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 THREAD_LOCAL_TOP_ACCESSOR(StateTag, current_vm_state)
915
916 void SetData(uint32_t slot, void* data) {
917 DCHECK(slot < Internals::kNumIsolateDataSlots);
918 embedder_data_[slot] = data;
Steve Block44f0eee2011-05-26 01:26:41 +0100919 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000920 void* GetData(uint32_t slot) {
921 DCHECK(slot < Internals::kNumIsolateDataSlots);
922 return embedder_data_[slot];
Steve Block44f0eee2011-05-26 01:26:41 +0100923 }
Steve Block44f0eee2011-05-26 01:26:41 +0100924
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 bool serializer_enabled() const { return serializer_enabled_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 bool snapshot_available() const {
927 return snapshot_blob_ != NULL && snapshot_blob_->raw_size != 0;
928 }
Steve Block44f0eee2011-05-26 01:26:41 +0100929
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930 bool IsDead() { return has_fatal_error_; }
931 void SignalFatalError() { has_fatal_error_ = true; }
Ben Murdoch257744e2011-11-30 15:57:28 +0000932
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 bool use_crankshaft() const;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100934
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 bool initialized_from_snapshot() { return initialized_from_snapshot_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100936
937 double time_millis_since_init() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000938 return heap_.MonotonicallyIncreasingTimeInMs() - time_millis_at_init_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100939 }
940
941 DateCache* date_cache() {
942 return date_cache_;
943 }
944
945 void set_date_cache(DateCache* date_cache) {
946 if (date_cache != date_cache_) {
947 delete date_cache_;
948 }
949 date_cache_ = date_cache;
950 }
951
Ben Murdochda12d292016-06-02 14:46:10 +0100952 Map* get_initial_js_array_map(ElementsKind kind);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000953
954 static const int kArrayProtectorValid = 1;
955 static const int kArrayProtectorInvalid = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000956
957 bool IsFastArrayConstructorPrototypeChainIntact();
Ben Murdochda12d292016-06-02 14:46:10 +0100958 inline bool IsArraySpeciesLookupChainIntact();
Ben Murdochc5610432016-08-08 18:44:38 +0100959 inline bool IsHasInstanceLookupChainIntact();
960 bool IsIsConcatSpreadableLookupChainIntact();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000962 // On intent to set an element in object, make sure that appropriate
963 // notifications occur if the set is on the elements of the array or
964 // object prototype. Also ensure that changes to prototype chain between
965 // Array and Object fire notifications.
966 void UpdateArrayProtectorOnSetElement(Handle<JSObject> object);
967 void UpdateArrayProtectorOnSetLength(Handle<JSObject> object) {
968 UpdateArrayProtectorOnSetElement(object);
969 }
970 void UpdateArrayProtectorOnSetPrototype(Handle<JSObject> object) {
971 UpdateArrayProtectorOnSetElement(object);
972 }
973 void UpdateArrayProtectorOnNormalizeElements(Handle<JSObject> object) {
974 UpdateArrayProtectorOnSetElement(object);
975 }
Ben Murdoch097c5b22016-05-18 11:27:45 +0100976 void InvalidateArraySpeciesProtector();
Ben Murdochc5610432016-08-08 18:44:38 +0100977 void InvalidateHasInstanceProtector();
978 void InvalidateIsConcatSpreadableProtector();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979
980 // Returns true if array is the initial array prototype in any native context.
981 bool IsAnyInitialArrayPrototype(Handle<JSArray> array);
982
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000983 CallInterfaceDescriptorData* call_descriptor_data(int index);
984
985 void IterateDeferredHandles(ObjectVisitor* visitor);
986 void LinkDeferredHandles(DeferredHandles* deferred_handles);
987 void UnlinkDeferredHandles(DeferredHandles* deferred_handles);
988
989#ifdef DEBUG
990 bool IsDeferredHandle(Object** location);
991#endif // DEBUG
992
993 bool concurrent_recompilation_enabled() {
994 // Thread is only available with flag enabled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000995 DCHECK(optimizing_compile_dispatcher_ == NULL ||
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 FLAG_concurrent_recompilation);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000997 return optimizing_compile_dispatcher_ != NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998 }
999
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001000 OptimizingCompileDispatcher* optimizing_compile_dispatcher() {
1001 return optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 }
1003
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001004 int id() const { return static_cast<int>(id_); }
1005
1006 HStatistics* GetHStatistics();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001007 CompilationStatistics* GetTurboStatistics();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001008 HTracer* GetHTracer();
1009 CodeTracer* GetCodeTracer();
1010
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001011 void DumpAndResetCompilationStats();
1012
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001013 FunctionEntryHook function_entry_hook() { return function_entry_hook_; }
1014 void set_function_entry_hook(FunctionEntryHook function_entry_hook) {
1015 function_entry_hook_ = function_entry_hook;
1016 }
1017
1018 void* stress_deopt_count_address() { return &stress_deopt_count_; }
1019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001020 void* virtual_handler_register_address() {
1021 return &virtual_handler_register_;
1022 }
1023
1024 void* virtual_slot_register_address() { return &virtual_slot_register_; }
1025
1026 base::RandomNumberGenerator* random_number_generator();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027
1028 // Given an address occupied by a live code object, return that object.
1029 Object* FindCodeObject(Address a);
1030
1031 int NextOptimizationId() {
1032 int id = next_optimization_id_++;
1033 if (!Smi::IsValid(next_optimization_id_)) {
1034 next_optimization_id_ = 0;
1035 }
1036 return id;
1037 }
1038
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001039 void IncrementJsCallsFromApiCounter() { ++js_calls_from_api_counter_; }
1040
1041 unsigned int js_calls_from_api_counter() {
1042 return js_calls_from_api_counter_;
1043 }
1044
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001045 // Get (and lazily initialize) the registry for per-isolate symbols.
1046 Handle<JSObject> GetSymbolRegistry();
1047
1048 void AddCallCompletedCallback(CallCompletedCallback callback);
1049 void RemoveCallCompletedCallback(CallCompletedCallback callback);
1050 void FireCallCompletedCallback();
1051
Ben Murdoch097c5b22016-05-18 11:27:45 +01001052 void AddBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1053 void RemoveBeforeCallEnteredCallback(BeforeCallEnteredCallback callback);
1054 void FireBeforeCallEnteredCallback();
1055
Ben Murdochda12d292016-06-02 14:46:10 +01001056 void AddMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1057 void RemoveMicrotasksCompletedCallback(MicrotasksCompletedCallback callback);
1058 void FireMicrotasksCompletedCallback();
1059
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001060 void SetPromiseRejectCallback(PromiseRejectCallback callback);
1061 void ReportPromiseReject(Handle<JSObject> promise, Handle<Object> value,
1062 v8::PromiseRejectEvent event);
1063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064 void EnqueueMicrotask(Handle<Object> microtask);
1065 void RunMicrotasks();
Ben Murdochc5610432016-08-08 18:44:38 +01001066 bool IsRunningMicrotasks() const { return is_running_microtasks_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001067
1068 void SetUseCounterCallback(v8::Isolate::UseCounterCallback callback);
1069 void CountUsage(v8::Isolate::UseCounterFeature feature);
1070
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001071 BasicBlockProfiler* GetOrCreateBasicBlockProfiler();
1072 BasicBlockProfiler* basic_block_profiler() { return basic_block_profiler_; }
1073
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001074 std::string GetTurboCfgFileName();
1075
1076#if TRACE_MAPS
1077 int GetNextUniqueSharedFunctionInfoId() { return next_unique_sfi_id_++; }
1078#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001079
Ben Murdochda12d292016-06-02 14:46:10 +01001080 // Support for dynamically disabling tail call elimination.
1081 Address is_tail_call_elimination_enabled_address() {
1082 return reinterpret_cast<Address>(&is_tail_call_elimination_enabled_);
1083 }
1084 bool is_tail_call_elimination_enabled() const {
1085 return is_tail_call_elimination_enabled_;
1086 }
1087 void SetTailCallEliminationEnabled(bool enabled);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001088
1089 void AddDetachedContext(Handle<Context> context);
1090 void CheckDetachedContextsAfterGC();
1091
1092 List<Object*>* partial_snapshot_cache() { return &partial_snapshot_cache_; }
1093
1094 void set_array_buffer_allocator(v8::ArrayBuffer::Allocator* allocator) {
1095 array_buffer_allocator_ = allocator;
1096 }
1097 v8::ArrayBuffer::Allocator* array_buffer_allocator() const {
1098 return array_buffer_allocator_;
1099 }
1100
1101 FutexWaitListNode* futex_wait_list_node() { return &futex_wait_list_node_; }
1102
1103 CancelableTaskManager* cancelable_task_manager() {
1104 return cancelable_task_manager_;
1105 }
1106
1107 interpreter::Interpreter* interpreter() const { return interpreter_; }
1108
Ben Murdochda12d292016-06-02 14:46:10 +01001109 base::AccountingAllocator* allocator() { return &allocator_; }
1110
Ben Murdochc5610432016-08-08 18:44:38 +01001111 bool IsInAnyContext(Object* object, uint32_t index);
1112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001113 protected:
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001114 explicit Isolate(bool enable_serializer);
Ben Murdochc5610432016-08-08 18:44:38 +01001115 bool IsArrayOrObjectPrototype(Object* object);
Steve Block44f0eee2011-05-26 01:26:41 +01001116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001117 private:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001118 friend struct GlobalState;
1119 friend struct InitializeGlobalState;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001120 Handle<JSObject> SetUpSubregistry(Handle<JSObject> registry, Handle<Map> map,
1121 const char* name);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001122
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123 // These fields are accessed through the API, offsets must be kept in sync
1124 // with v8::internal::Internals (in include/v8.h) constants. This is also
1125 // verified in Isolate::Init() using runtime checks.
1126 void* embedder_data_[Internals::kNumIsolateDataSlots];
1127 Heap heap_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001128
Steve Block44f0eee2011-05-26 01:26:41 +01001129 // The per-process lock should be acquired before the ThreadDataTable is
1130 // modified.
1131 class ThreadDataTable {
1132 public:
1133 ThreadDataTable();
1134 ~ThreadDataTable();
1135
1136 PerIsolateThreadData* Lookup(Isolate* isolate, ThreadId thread_id);
1137 void Insert(PerIsolateThreadData* data);
Steve Block44f0eee2011-05-26 01:26:41 +01001138 void Remove(PerIsolateThreadData* data);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001139 void RemoveAllThreads(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001140
1141 private:
1142 PerIsolateThreadData* list_;
1143 };
1144
1145 // These items form a stack synchronously with threads Enter'ing and Exit'ing
1146 // the Isolate. The top of the stack points to a thread which is currently
1147 // running the Isolate. When the stack is empty, the Isolate is considered
1148 // not entered by any thread and can be Disposed.
1149 // If the same thread enters the Isolate more then once, the entry_count_
1150 // is incremented rather then a new item pushed to the stack.
1151 class EntryStackItem {
1152 public:
1153 EntryStackItem(PerIsolateThreadData* previous_thread_data,
1154 Isolate* previous_isolate,
1155 EntryStackItem* previous_item)
1156 : entry_count(1),
1157 previous_thread_data(previous_thread_data),
1158 previous_isolate(previous_isolate),
1159 previous_item(previous_item) { }
1160
1161 int entry_count;
1162 PerIsolateThreadData* previous_thread_data;
1163 Isolate* previous_isolate;
1164 EntryStackItem* previous_item;
1165
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001166 private:
Steve Block44f0eee2011-05-26 01:26:41 +01001167 DISALLOW_COPY_AND_ASSIGN(EntryStackItem);
1168 };
1169
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 static base::LazyMutex thread_data_table_mutex_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001171
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001172 static base::Thread::LocalStorageKey per_isolate_thread_data_key_;
1173 static base::Thread::LocalStorageKey isolate_key_;
1174 static base::Thread::LocalStorageKey thread_id_key_;
Ben Murdoch85b71792012-04-11 18:30:58 +01001175 static ThreadDataTable* thread_data_table_;
1176
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001177 // A global counter for all generated Isolates, might overflow.
1178 static base::Atomic32 isolate_counter_;
1179
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001180#if DEBUG
1181 static base::Atomic32 isolate_key_created_;
1182#endif
1183
Steve Block44f0eee2011-05-26 01:26:41 +01001184 void Deinit();
1185
1186 static void SetIsolateThreadLocals(Isolate* isolate,
1187 PerIsolateThreadData* data);
1188
Steve Block44f0eee2011-05-26 01:26:41 +01001189 // Find the PerThread for this particular (isolate, thread) combination.
1190 // If one does not yet exist, allocate a new one.
1191 PerIsolateThreadData* FindOrAllocatePerThreadDataForThisThread();
1192
Steve Block44f0eee2011-05-26 01:26:41 +01001193 // Initializes the current thread to run this Isolate.
1194 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1195 // at the same time, this should be prevented using external locking.
1196 void Enter();
1197
1198 // Exits the current thread. The previosuly entered Isolate is restored
1199 // for the thread.
1200 // Not thread-safe. Multiple threads should not Enter/Exit the same isolate
1201 // at the same time, this should be prevented using external locking.
1202 void Exit();
1203
Steve Block44f0eee2011-05-26 01:26:41 +01001204 void InitializeThreadLocal();
1205
Steve Block44f0eee2011-05-26 01:26:41 +01001206 void MarkCompactPrologue(bool is_compacting,
1207 ThreadLocalTop* archived_thread_data);
1208 void MarkCompactEpilogue(bool is_compacting,
1209 ThreadLocalTop* archived_thread_data);
1210
1211 void FillCache();
1212
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213 // Propagate pending exception message to the v8::TryCatch.
1214 // If there is no external try-catch or message was successfully propagated,
1215 // then return true.
1216 bool PropagatePendingExceptionToExternalTryCatch();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001217
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001218 // Remove per-frame stored materialized objects when we are unwinding
1219 // the frame.
1220 void RemoveMaterializedObjectsOnUnwind(StackFrame* frame);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001221
Ben Murdochda12d292016-06-02 14:46:10 +01001222 void RunMicrotasksInternal();
1223
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001224 base::Atomic32 id_;
1225 EntryStackItem* entry_stack_;
Steve Block44f0eee2011-05-26 01:26:41 +01001226 int stack_trace_nesting_level_;
1227 StringStream* incomplete_message_;
Ben Murdoch589d6972011-11-30 16:04:58 +00001228 Address isolate_addresses_[kIsolateAddressCount + 1]; // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +01001229 Bootstrapper* bootstrapper_;
1230 RuntimeProfiler* runtime_profiler_;
1231 CompilationCache* compilation_cache_;
1232 Counters* counters_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001233 base::RecursiveMutex break_access_;
Steve Block44f0eee2011-05-26 01:26:41 +01001234 Logger* logger_;
1235 StackGuard stack_guard_;
1236 StatsTable* stats_table_;
1237 StubCache* stub_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 CodeAgingHelper* code_aging_helper_;
Steve Block44f0eee2011-05-26 01:26:41 +01001239 DeoptimizerData* deoptimizer_data_;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001240 bool deoptimizer_lazy_throw_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241 MaterializedObjectStore* materialized_object_store_;
Steve Block44f0eee2011-05-26 01:26:41 +01001242 ThreadLocalTop thread_local_top_;
1243 bool capture_stack_trace_for_uncaught_exceptions_;
1244 int stack_trace_for_uncaught_exceptions_frame_limit_;
1245 StackTrace::StackTraceOptions stack_trace_for_uncaught_exceptions_options_;
Steve Block44f0eee2011-05-26 01:26:41 +01001246 KeyedLookupCache* keyed_lookup_cache_;
1247 ContextSlotCache* context_slot_cache_;
1248 DescriptorLookupCache* descriptor_lookup_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001249 HandleScopeData handle_scope_data_;
Steve Block44f0eee2011-05-26 01:26:41 +01001250 HandleScopeImplementer* handle_scope_implementer_;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001251 UnicodeCache* unicode_cache_;
Ben Murdochda12d292016-06-02 14:46:10 +01001252 base::AccountingAllocator allocator_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001253 Zone runtime_zone_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001254 Zone interface_descriptor_zone_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001255 InnerPointerToCodeCache* inner_pointer_to_code_cache_;
Steve Block44f0eee2011-05-26 01:26:41 +01001256 GlobalHandles* global_handles_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257 EternalHandles* eternal_handles_;
Steve Block44f0eee2011-05-26 01:26:41 +01001258 ThreadManager* thread_manager_;
Steve Block44f0eee2011-05-26 01:26:41 +01001259 RuntimeState runtime_state_;
Steve Block44f0eee2011-05-26 01:26:41 +01001260 Builtins builtins_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001261 bool has_installed_extensions_;
Steve Block44f0eee2011-05-26 01:26:41 +01001262 unibrow::Mapping<unibrow::Ecma262UnCanonicalize> jsregexp_uncanonicalize_;
1263 unibrow::Mapping<unibrow::CanonicalizationRange> jsregexp_canonrange_;
Steve Block44f0eee2011-05-26 01:26:41 +01001264 unibrow::Mapping<unibrow::Ecma262Canonicalize>
1265 regexp_macro_assembler_canonicalize_;
1266 RegExpStack* regexp_stack_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001267 DateCache* date_cache_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268 CallInterfaceDescriptorData* call_descriptor_data_;
1269 base::RandomNumberGenerator* random_number_generator_;
Steve Block44f0eee2011-05-26 01:26:41 +01001270
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 // Whether the isolate has been created for snapshotting.
1272 bool serializer_enabled_;
1273
1274 // True if fatal error has been signaled for this isolate.
1275 bool has_fatal_error_;
1276
1277 // True if this isolate was initialized from a snapshot.
1278 bool initialized_from_snapshot_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001279
Ben Murdochda12d292016-06-02 14:46:10 +01001280 // True if ES2015 tail call elimination feature is enabled.
1281 bool is_tail_call_elimination_enabled_;
1282
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001283 // Time stamp at initialization.
1284 double time_millis_at_init_;
1285
Steve Block44f0eee2011-05-26 01:26:41 +01001286#ifdef DEBUG
1287 // A static array of histogram info for each type.
1288 HistogramInfo heap_histograms_[LAST_TYPE + 1];
1289 JSObject::SpillInformation js_spill_information_;
Steve Block44f0eee2011-05-26 01:26:41 +01001290#endif
1291
Steve Block44f0eee2011-05-26 01:26:41 +01001292 Debug* debug_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001293 CpuProfiler* cpu_profiler_;
1294 HeapProfiler* heap_profiler_;
1295 FunctionEntryHook function_entry_hook_;
Steve Block44f0eee2011-05-26 01:26:41 +01001296
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001297 interpreter::Interpreter* interpreter_;
1298
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001299 typedef std::pair<InterruptCallback, void*> InterruptEntry;
1300 std::queue<InterruptEntry> api_interrupts_queue_;
1301
Steve Block44f0eee2011-05-26 01:26:41 +01001302#define GLOBAL_BACKING_STORE(type, name, initialvalue) \
1303 type name##_;
1304 ISOLATE_INIT_LIST(GLOBAL_BACKING_STORE)
1305#undef GLOBAL_BACKING_STORE
1306
1307#define GLOBAL_ARRAY_BACKING_STORE(type, name, length) \
1308 type name##_[length];
1309 ISOLATE_INIT_ARRAY_LIST(GLOBAL_ARRAY_BACKING_STORE)
1310#undef GLOBAL_ARRAY_BACKING_STORE
1311
1312#ifdef DEBUG
1313 // This class is huge and has a number of fields controlled by
1314 // preprocessor defines. Make sure the offsets of these fields agree
1315 // between compilation units.
1316#define ISOLATE_FIELD_OFFSET(type, name, ignored) \
1317 static const intptr_t name##_debug_offset_;
1318 ISOLATE_INIT_LIST(ISOLATE_FIELD_OFFSET)
1319 ISOLATE_INIT_ARRAY_LIST(ISOLATE_FIELD_OFFSET)
1320#undef ISOLATE_FIELD_OFFSET
1321#endif
1322
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 DeferredHandles* deferred_handles_head_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001324 OptimizingCompileDispatcher* optimizing_compile_dispatcher_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001325
1326 // Counts deopt points if deopt_every_n_times is enabled.
1327 unsigned int stress_deopt_count_;
1328
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001329 Address virtual_handler_register_;
1330 Address virtual_slot_register_;
1331
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 int next_optimization_id_;
1333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001334 // Counts javascript calls from the API. Wraps around on overflow.
1335 unsigned int js_calls_from_api_counter_;
1336
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001337#if TRACE_MAPS
1338 int next_unique_sfi_id_;
1339#endif
1340
Ben Murdoch097c5b22016-05-18 11:27:45 +01001341 // List of callbacks before a Call starts execution.
1342 List<BeforeCallEnteredCallback> before_call_entered_callbacks_;
1343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 // List of callbacks when a Call completes.
1345 List<CallCompletedCallback> call_completed_callbacks_;
1346
Ben Murdochda12d292016-06-02 14:46:10 +01001347 // List of callbacks after microtasks were run.
1348 List<MicrotasksCompletedCallback> microtasks_completed_callbacks_;
Ben Murdochc5610432016-08-08 18:44:38 +01001349 bool is_running_microtasks_;
Ben Murdochda12d292016-06-02 14:46:10 +01001350
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 v8::Isolate::UseCounterCallback use_counter_callback_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001352 BasicBlockProfiler* basic_block_profiler_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001354 List<Object*> partial_snapshot_cache_;
1355
1356 v8::ArrayBuffer::Allocator* array_buffer_allocator_;
1357
1358 FutexWaitListNode futex_wait_list_node_;
1359
1360 CancelableTaskManager* cancelable_task_manager_;
1361
1362 v8::Isolate::AbortOnUncaughtExceptionCallback
1363 abort_on_uncaught_exception_callback_;
1364
Steve Block44f0eee2011-05-26 01:26:41 +01001365 friend class ExecutionAccess;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001366 friend class HandleScopeImplementer;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001367 friend class OptimizingCompileDispatcher;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001368 friend class SweeperThread;
Ben Murdoch257744e2011-11-30 15:57:28 +00001369 friend class ThreadManager;
1370 friend class Simulator;
1371 friend class StackGuard;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001372 friend class ThreadId;
Steve Block44f0eee2011-05-26 01:26:41 +01001373 friend class v8::Isolate;
1374 friend class v8::Locker;
Ben Murdoch257744e2011-11-30 15:57:28 +00001375 friend class v8::Unlocker;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001376 friend v8::StartupData v8::V8::CreateSnapshotDataBlob(const char*);
Ben Murdochda12d292016-06-02 14:46:10 +01001377 friend v8::StartupData v8::V8::WarmUpSnapshotDataBlob(v8::StartupData,
1378 const char*);
Steve Block44f0eee2011-05-26 01:26:41 +01001379
1380 DISALLOW_COPY_AND_ASSIGN(Isolate);
1381};
1382
1383
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384#undef FIELD_ACCESSOR
1385#undef THREAD_LOCAL_TOP_ACCESSOR
1386
1387
1388class PromiseOnStack {
1389 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001390 PromiseOnStack(Handle<JSFunction> function, Handle<JSObject> promise,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001391 PromiseOnStack* prev)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001392 : function_(function), promise_(promise), prev_(prev) {}
1393 Handle<JSFunction> function() { return function_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 Handle<JSObject> promise() { return promise_; }
1395 PromiseOnStack* prev() { return prev_; }
1396
1397 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001398 Handle<JSFunction> function_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001399 Handle<JSObject> promise_;
1400 PromiseOnStack* prev_;
1401};
1402
1403
Steve Block44f0eee2011-05-26 01:26:41 +01001404// If the GCC version is 4.1.x or 4.2.x an additional field is added to the
1405// class as a work around for a bug in the generated code found with these
1406// versions of GCC. See V8 issue 122 for details.
1407class SaveContext BASE_EMBEDDED {
1408 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001409 explicit SaveContext(Isolate* isolate);
1410 ~SaveContext();
Steve Block44f0eee2011-05-26 01:26:41 +01001411
1412 Handle<Context> context() { return context_; }
1413 SaveContext* prev() { return prev_; }
1414
1415 // Returns true if this save context is below a given JavaScript frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001416 bool IsBelowFrame(JavaScriptFrame* frame) {
1417 return (c_entry_fp_ == 0) || (c_entry_fp_ > frame->sp());
Steve Block44f0eee2011-05-26 01:26:41 +01001418 }
1419
1420 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001421 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001422 Handle<Context> context_;
Steve Block44f0eee2011-05-26 01:26:41 +01001423 SaveContext* prev_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001424 Address c_entry_fp_;
Steve Block44f0eee2011-05-26 01:26:41 +01001425};
1426
1427
1428class AssertNoContextChange BASE_EMBEDDED {
1429#ifdef DEBUG
1430 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001431 explicit AssertNoContextChange(Isolate* isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001432 ~AssertNoContextChange() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001433 DCHECK(isolate_->context() == *context_);
Steve Block44f0eee2011-05-26 01:26:41 +01001434 }
1435
1436 private:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 Isolate* isolate_;
Steve Block44f0eee2011-05-26 01:26:41 +01001438 Handle<Context> context_;
1439#else
1440 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001441 explicit AssertNoContextChange(Isolate* isolate) { }
Steve Block44f0eee2011-05-26 01:26:41 +01001442#endif
1443};
1444
1445
1446class ExecutionAccess BASE_EMBEDDED {
1447 public:
1448 explicit ExecutionAccess(Isolate* isolate) : isolate_(isolate) {
1449 Lock(isolate);
1450 }
1451 ~ExecutionAccess() { Unlock(isolate_); }
1452
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001453 static void Lock(Isolate* isolate) { isolate->break_access()->Lock(); }
1454 static void Unlock(Isolate* isolate) { isolate->break_access()->Unlock(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001455
1456 static bool TryLock(Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001457 return isolate->break_access()->TryLock();
Steve Block44f0eee2011-05-26 01:26:41 +01001458 }
1459
1460 private:
1461 Isolate* isolate_;
1462};
1463
1464
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001465// Support for checking for stack-overflows.
Steve Block44f0eee2011-05-26 01:26:41 +01001466class StackLimitCheck BASE_EMBEDDED {
1467 public:
1468 explicit StackLimitCheck(Isolate* isolate) : isolate_(isolate) { }
1469
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470 // Use this to check for stack-overflows in C++ code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001471 bool HasOverflowed() const {
Steve Block44f0eee2011-05-26 01:26:41 +01001472 StackGuard* stack_guard = isolate_->stack_guard();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001473 return GetCurrentStackPosition() < stack_guard->real_climit();
Steve Block44f0eee2011-05-26 01:26:41 +01001474 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001475
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001476 // Use this to check for interrupt request in C++ code.
1477 bool InterruptRequested() {
1478 StackGuard* stack_guard = isolate_->stack_guard();
1479 return GetCurrentStackPosition() < stack_guard->climit();
1480 }
1481
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001482 // Use this to check for stack-overflow when entering runtime from JS code.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001483 bool JsHasOverflowed(uintptr_t gap = 0) const;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484
Steve Block44f0eee2011-05-26 01:26:41 +01001485 private:
1486 Isolate* isolate_;
1487};
1488
Ben Murdochc5610432016-08-08 18:44:38 +01001489#define STACK_CHECK(isolate, result_value) \
1490 do { \
1491 StackLimitCheck stack_check(isolate); \
1492 if (stack_check.HasOverflowed()) { \
1493 isolate->Throw(*isolate->factory()->NewRangeError( \
1494 MessageTemplate::kStackOverflow)); \
1495 return result_value; \
1496 } \
1497 } while (false)
Steve Block44f0eee2011-05-26 01:26:41 +01001498
1499// Support for temporarily postponing interrupts. When the outermost
1500// postpone scope is left the interrupts will be re-enabled and any
1501// interrupts that occurred while in the scope will be taken into
1502// account.
1503class PostponeInterruptsScope BASE_EMBEDDED {
1504 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001505 PostponeInterruptsScope(Isolate* isolate,
1506 int intercept_mask = StackGuard::ALL_INTERRUPTS)
1507 : stack_guard_(isolate->stack_guard()),
1508 intercept_mask_(intercept_mask),
1509 intercepted_flags_(0) {
1510 stack_guard_->PushPostponeInterruptsScope(this);
Steve Block44f0eee2011-05-26 01:26:41 +01001511 }
1512
1513 ~PostponeInterruptsScope() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001514 stack_guard_->PopPostponeInterruptsScope();
Steve Block44f0eee2011-05-26 01:26:41 +01001515 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001516
1517 // Find the bottom-most scope that intercepts this interrupt.
1518 // Return whether the interrupt has been intercepted.
1519 bool Intercept(StackGuard::InterruptFlag flag);
1520
Steve Block44f0eee2011-05-26 01:26:41 +01001521 private:
1522 StackGuard* stack_guard_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 int intercept_mask_;
1524 int intercepted_flags_;
1525 PostponeInterruptsScope* prev_;
1526
1527 friend class StackGuard;
Steve Block44f0eee2011-05-26 01:26:41 +01001528};
1529
1530
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001531class CodeTracer final : public Malloced {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001532 public:
1533 explicit CodeTracer(int isolate_id)
1534 : file_(NULL),
1535 scope_depth_(0) {
1536 if (!ShouldRedirect()) {
1537 file_ = stdout;
1538 return;
1539 }
Steve Block44f0eee2011-05-26 01:26:41 +01001540
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001541 if (FLAG_redirect_code_traces_to == NULL) {
1542 SNPrintF(filename_,
1543 "code-%d-%d.asm",
1544 base::OS::GetCurrentProcessId(),
1545 isolate_id);
1546 } else {
1547 StrNCpy(filename_, FLAG_redirect_code_traces_to, filename_.length());
1548 }
Steve Block44f0eee2011-05-26 01:26:41 +01001549
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001550 WriteChars(filename_.start(), "", 0, false);
1551 }
Steve Block44f0eee2011-05-26 01:26:41 +01001552
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001553 class Scope {
1554 public:
1555 explicit Scope(CodeTracer* tracer) : tracer_(tracer) { tracer->OpenFile(); }
1556 ~Scope() { tracer_->CloseFile(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001557
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001558 FILE* file() const { return tracer_->file(); }
Steve Block44f0eee2011-05-26 01:26:41 +01001559
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001560 private:
1561 CodeTracer* tracer_;
1562 };
1563
1564 void OpenFile() {
1565 if (!ShouldRedirect()) {
1566 return;
1567 }
1568
1569 if (file_ == NULL) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001570 file_ = base::OS::FOpen(filename_.start(), "ab");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001571 }
1572
1573 scope_depth_++;
1574 }
1575
1576 void CloseFile() {
1577 if (!ShouldRedirect()) {
1578 return;
1579 }
1580
1581 if (--scope_depth_ == 0) {
1582 fclose(file_);
1583 file_ = NULL;
1584 }
1585 }
1586
1587 FILE* file() const { return file_; }
1588
1589 private:
1590 static bool ShouldRedirect() {
1591 return FLAG_redirect_code_traces;
1592 }
1593
1594 EmbeddedVector<char, 128> filename_;
1595 FILE* file_;
1596 int scope_depth_;
1597};
Steve Block44f0eee2011-05-26 01:26:41 +01001598
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599} // namespace internal
1600} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01001601
Steve Block44f0eee2011-05-26 01:26:41 +01001602#endif // V8_ISOLATE_H_