v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
local-isolate.h
Go to the documentation of this file.
1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_EXECUTION_LOCAL_ISOLATE_H_
6#define V8_EXECUTION_LOCAL_ISOLATE_H_
7
8#include <optional>
9
10#include "src/base/macros.h"
13#include "src/handles/handles.h"
17#include "src/heap/local-heap.h"
19
20namespace v8 {
21
22namespace bigint {
23class Processor;
24}
25
26namespace internal {
27
28class Isolate;
29class LocalLogger;
30class RuntimeCallStats;
31
32// HiddenLocalFactory parallels Isolate's HiddenFactory
34 public:
35 // Forward constructors.
36 using LocalFactory::LocalFactory;
37};
38
39// And Isolate-like class that can be passed in to templated methods that need
40// an isolate syntactically, but are usable off-thread.
41//
42// This class holds an LocalFactory, but is otherwise effectively a stub
43// implementation of an Isolate. In particular, it doesn't allow throwing
44// exceptions, and hard crashes if you try.
46 public:
48
49 explicit LocalIsolate(Isolate* isolate, ThreadKind kind);
51
52 // Kinda sketchy.
54 return reinterpret_cast<LocalIsolate*>(reinterpret_cast<Address>(heap) -
56 }
57
58 bool is_main_thread() const { return heap()->is_main_thread(); }
59
60 LocalHeap* heap() { return &heap_; }
61 const LocalHeap* heap() const { return &heap_; }
62
63 inline Address cage_base() const;
64 inline Address code_cage_base() const;
65 inline ReadOnlyHeap* read_only_heap() const;
66 inline RootsTable& roots_table();
67 inline const RootsTable& roots_table() const;
68 inline Tagged<Object> root(RootIndex index) const;
69 inline Handle<Object> root_handle(RootIndex index) const;
70
72 return isolate_->fuzzer_rng();
73 }
74
75 StringTable* string_table() const { return isolate_->string_table(); }
77 return isolate_->internalized_string_access();
78 }
80 return isolate_->shared_function_info_access();
81 }
83 return isolate_->ast_string_constants();
84 }
86 return isolate_->lazy_compile_dispatcher();
87 }
89 // TODO(leszeks): This is needed for logging in ParseInfo. Figure out a way
90 // to use the LocalLogger for this instead.
91 return isolate_->v8_file_logger();
92 }
93
95 return isolate_->is_precise_binary_code_coverage();
96 }
97
99 // Upcast to the privately inherited base-class using c-style casts to avoid
100 // undefined behavior (as static_cast cannot cast across private bases).
101 return (v8::internal::LocalFactory*)this;
102 }
103
104 IsolateGroup* isolate_group() const { return isolate_->isolate_group(); }
105
106 AccountingAllocator* allocator() { return isolate_->allocator(); }
107
108 bool has_exception() const { return false; }
109 bool serializer_enabled() const { return isolate_->serializer_enabled(); }
110
111 void RegisterDeserializerStarted();
112 void RegisterDeserializerFinished();
113 bool has_active_deserializer() const;
114
115 void Throw(Tagged<Object> exception) { UNREACHABLE(); }
116 [[noreturn]] void FatalProcessOutOfHeapMemory(const char* location) {
117 UNREACHABLE();
118 }
119
120 int GetNextScriptId();
122 return isolate_->GetAndIncNextUniqueSfiId();
123 }
124
125 // TODO(cbruni): rename this back to logger() once the V8FileLogger
126 // refactoring is completed.
127 LocalLogger* v8_file_logger() const { return logger_.get(); }
128 ThreadId thread_id() const { return thread_id_; }
129 Address stack_limit() const { return stack_limit_; }
130#ifdef V8_RUNTIME_CALL_STATS
131 RuntimeCallStats* runtime_call_stats() const { return runtime_call_stats_; }
132#else
133 RuntimeCallStats* runtime_call_stats() const { return nullptr; }
134#endif
136 if (!bigint_processor_) InitializeBigIntProcessor();
137 return bigint_processor_;
138 }
139
140#ifdef V8_ENABLE_LEAPTIERING
141 JSDispatchTable::Space* GetJSDispatchTableSpaceFor(Address owning_slot) {
142 return isolate_->GetJSDispatchTableSpaceFor(owning_slot);
143 }
144#endif // V8_ENABLE_LEAPTIERING
145
146 // AsIsolate is only allowed on the main-thread.
148 DCHECK(is_main_thread());
149 DCHECK_EQ(ThreadId::Current(), isolate_->thread_id());
150 return isolate_;
151 }
152 LocalIsolate* AsLocalIsolate() { return this; }
153
155 return isolate_->shared_space_isolate()->main_thread_local_isolate();
156 }
157
158 // TODO(victorgomes): Remove this when/if MacroAssembler supports LocalIsolate
159 // only constructor.
161
163 return isolate_->snapshot_blob();
164 }
166 return isolate_->pending_message_address();
167 }
168
169 int NextOptimizationId() { return isolate_->NextOptimizationId(); }
170
171 template <typename Callback>
172 V8_INLINE void ExecuteMainThreadWhileParked(Callback callback);
173
174 template <typename Callback>
175 V8_INLINE void ParkIfOnBackgroundAndExecute(Callback callback);
176
177#ifdef V8_INTL_SUPPORT
178 // WARNING: This might be out-of-sync with the main-thread.
179 const std::string& DefaultLocale();
180#endif
181
182 private:
184 friend class LocalIsolateFactory;
186 friend class IsolateForSandbox;
187
188 // See IsolateForSandbox.
190
191 void InitializeBigIntProcessor();
192
194
195 // TODO(leszeks): Extract out the fields of the Isolate we want and store
196 // those instead of the whole thing.
198
199 std::unique_ptr<LocalLogger> logger_;
201 Address const stack_limit_;
202
203 bigint::Processor* bigint_processor_{nullptr};
204
205#ifdef V8_RUNTIME_CALL_STATS
206 std::optional<WorkerThreadRuntimeCallStatsScope> rcs_scope_;
207 RuntimeCallStats* runtime_call_stats_;
208#endif
209#ifdef V8_INTL_SUPPORT
210 std::string default_locale_;
211#endif
212};
213
214template <>
216 public:
219 DCHECK_NOT_NULL(isolate);
220 if (!isolate->is_main_thread()) mutex_guard_.emplace(mutex);
221 }
222
225
226 private:
227 std::optional<base::MutexGuard> mutex_guard_;
228};
229
230} // namespace internal
231} // namespace v8
232
233#endif // V8_EXECUTION_LOCAL_ISOLATE_H_
Isolate * isolate_
Builtins::Kind kind
Definition builtins.cc:40
base::RandomNumberGenerator * fuzzer_rng() const
LazyCompileDispatcher * lazy_compile_dispatcher()
V8FileLogger * main_thread_logger()
v8::internal::LocalFactory * factory()
std::unique_ptr< LocalLogger > logger_
LocalIsolate * AsLocalIsolate()
Isolate * GetMainThreadIsolateUnsafe() const
static LocalIsolate * FromHeap(LocalHeap *heap)
Tagged< Object > * pending_message_address()
base::Mutex * internalized_string_access()
RuntimeCallStats * runtime_call_stats() const
IsolateGroup * isolate_group() const
StringTable * string_table() const
LocalLogger * v8_file_logger() const
LocalIsolate * shared_space_isolate() const
base::Mutex * shared_function_info_access()
const AstStringConstants * ast_string_constants()
bool is_precise_binary_code_coverage() const
void Throw(Tagged< Object > exception)
const LocalHeap * heap() const
bigint::Processor * bigint_processor()
AccountingAllocator * allocator()
const v8::StartupData * snapshot_blob() const
void FatalProcessOutOfHeapMemory(const char *location)
std::optional< base::MutexGuard > mutex_guard_
MutexGuardIfOffThread(base::Mutex *mutex, LocalIsolate *isolate)
MutexGuardIfOffThread & operator=(const MutexGuardIfOffThread &)=delete
MutexGuardIfOffThread(const MutexGuardIfOffThread &)=delete
TNode< Object > callback
base::Mutex mutex
const uintptr_t stack_limit_
#define UNREACHABLE()
Definition logging.h:67
#define DCHECK_NOT_NULL(val)
Definition logging.h:492
#define DCHECK(condition)
Definition logging.h:482
#define DCHECK_EQ(v1, v2)
Definition logging.h:485
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define OFFSET_OF(type, field)
Definition macros.h:57
Heap * heap_
#define V8_INLINE
Definition v8config.h:500
#define V8_NODISCARD
Definition v8config.h:693