1 |
|
|
// Copyright Joyent, Inc. and other Node contributors. |
2 |
|
|
// |
3 |
|
|
// Permission is hereby granted, free of charge, to any person obtaining a |
4 |
|
|
// copy of this software and associated documentation files (the |
5 |
|
|
// "Software"), to deal in the Software without restriction, including |
6 |
|
|
// without limitation the rights to use, copy, modify, merge, publish, |
7 |
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit |
8 |
|
|
// persons to whom the Software is furnished to do so, subject to the |
9 |
|
|
// following conditions: |
10 |
|
|
// |
11 |
|
|
// The above copyright notice and this permission notice shall be included |
12 |
|
|
// in all copies or substantial portions of the Software. |
13 |
|
|
// |
14 |
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
15 |
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
16 |
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN |
17 |
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
18 |
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
19 |
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
20 |
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE. |
21 |
|
|
|
22 |
|
|
#ifndef SRC_ENV_H_ |
23 |
|
|
#define SRC_ENV_H_ |
24 |
|
|
|
25 |
|
|
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
26 |
|
|
|
27 |
|
|
#include "aliased_buffer.h" |
28 |
|
|
#if HAVE_INSPECTOR |
29 |
|
|
#include "inspector_agent.h" |
30 |
|
|
#include "inspector_profiler.h" |
31 |
|
|
#endif |
32 |
|
|
#include "callback_queue.h" |
33 |
|
|
#include "cleanup_queue-inl.h" |
34 |
|
|
#include "debug_utils.h" |
35 |
|
|
#include "env_properties.h" |
36 |
|
|
#include "handle_wrap.h" |
37 |
|
|
#include "node.h" |
38 |
|
|
#include "node_binding.h" |
39 |
|
|
#include "node_builtins.h" |
40 |
|
|
#include "node_exit_code.h" |
41 |
|
|
#include "node_main_instance.h" |
42 |
|
|
#include "node_options.h" |
43 |
|
|
#include "node_perf_common.h" |
44 |
|
|
#include "node_realm.h" |
45 |
|
|
#include "node_snapshotable.h" |
46 |
|
|
#include "req_wrap.h" |
47 |
|
|
#include "util.h" |
48 |
|
|
#include "uv.h" |
49 |
|
|
#include "v8.h" |
50 |
|
|
|
51 |
|
|
#include <array> |
52 |
|
|
#include <atomic> |
53 |
|
|
#include <cstdint> |
54 |
|
|
#include <functional> |
55 |
|
|
#include <list> |
56 |
|
|
#include <memory> |
57 |
|
|
#include <ostream> |
58 |
|
|
#include <set> |
59 |
|
|
#include <string> |
60 |
|
|
#include <unordered_map> |
61 |
|
|
#include <unordered_set> |
62 |
|
|
#include <vector> |
63 |
|
|
|
64 |
|
|
namespace node { |
65 |
|
|
|
66 |
|
|
namespace contextify { |
67 |
|
|
class ContextifyScript; |
68 |
|
|
class CompiledFnEntry; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
namespace performance { |
72 |
|
|
class PerformanceState; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
namespace tracing { |
76 |
|
|
class AgentWriterHandle; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
#if HAVE_INSPECTOR |
80 |
|
|
namespace profiler { |
81 |
|
|
class V8CoverageConnection; |
82 |
|
|
class V8CpuProfilerConnection; |
83 |
|
|
class V8HeapProfilerConnection; |
84 |
|
|
} // namespace profiler |
85 |
|
|
|
86 |
|
|
namespace inspector { |
87 |
|
|
class ParentInspectorHandle; |
88 |
|
|
} |
89 |
|
|
#endif // HAVE_INSPECTOR |
90 |
|
|
|
91 |
|
|
namespace worker { |
92 |
|
|
class Worker; |
93 |
|
|
} |
94 |
|
|
|
95 |
|
|
namespace loader { |
96 |
|
|
class ModuleWrap; |
97 |
|
|
|
98 |
|
|
struct PackageConfig { |
99 |
|
|
enum class Exists { Yes, No }; |
100 |
|
|
enum class IsValid { Yes, No }; |
101 |
|
|
enum class HasMain { Yes, No }; |
102 |
|
|
enum class HasName { Yes, No }; |
103 |
|
|
enum PackageType : uint32_t { None = 0, CommonJS, Module }; |
104 |
|
|
|
105 |
|
|
const Exists exists; |
106 |
|
|
const IsValid is_valid; |
107 |
|
|
const HasMain has_main; |
108 |
|
|
const std::string main; |
109 |
|
|
const HasName has_name; |
110 |
|
|
const std::string name; |
111 |
|
|
const PackageType type; |
112 |
|
|
|
113 |
|
|
v8::Global<v8::Value> exports; |
114 |
|
|
}; |
115 |
|
|
} // namespace loader |
116 |
|
|
|
117 |
|
|
class Environment; |
118 |
|
|
class Realm; |
119 |
|
|
|
120 |
|
|
enum class FsStatsOffset { |
121 |
|
|
kDev = 0, |
122 |
|
|
kMode, |
123 |
|
|
kNlink, |
124 |
|
|
kUid, |
125 |
|
|
kGid, |
126 |
|
|
kRdev, |
127 |
|
|
kBlkSize, |
128 |
|
|
kIno, |
129 |
|
|
kSize, |
130 |
|
|
kBlocks, |
131 |
|
|
kATimeSec, |
132 |
|
|
kATimeNsec, |
133 |
|
|
kMTimeSec, |
134 |
|
|
kMTimeNsec, |
135 |
|
|
kCTimeSec, |
136 |
|
|
kCTimeNsec, |
137 |
|
|
kBirthTimeSec, |
138 |
|
|
kBirthTimeNsec, |
139 |
|
|
kFsStatsFieldsNumber |
140 |
|
|
}; |
141 |
|
|
|
142 |
|
|
// Stat fields buffers contain twice the number of entries in an uv_stat_t |
143 |
|
|
// because `fs.StatWatcher` needs room to store 2 `fs.Stats` instances. |
144 |
|
|
constexpr size_t kFsStatsBufferLength = |
145 |
|
|
static_cast<size_t>(FsStatsOffset::kFsStatsFieldsNumber) * 2; |
146 |
|
|
|
147 |
|
|
// Disables zero-filling for ArrayBuffer allocations in this scope. This is |
148 |
|
|
// similar to how we implement Buffer.allocUnsafe() in JS land. |
149 |
|
|
class NoArrayBufferZeroFillScope { |
150 |
|
|
public: |
151 |
|
|
inline explicit NoArrayBufferZeroFillScope(IsolateData* isolate_data); |
152 |
|
|
inline ~NoArrayBufferZeroFillScope(); |
153 |
|
|
|
154 |
|
|
private: |
155 |
|
|
NodeArrayBufferAllocator* node_allocator_; |
156 |
|
|
|
157 |
|
|
friend class Environment; |
158 |
|
|
}; |
159 |
|
|
|
160 |
|
|
struct IsolateDataSerializeInfo { |
161 |
|
|
std::vector<SnapshotIndex> primitive_values; |
162 |
|
|
std::vector<PropInfo> template_values; |
163 |
|
|
|
164 |
|
|
friend std::ostream& operator<<(std::ostream& o, |
165 |
|
|
const IsolateDataSerializeInfo& i); |
166 |
|
|
}; |
167 |
|
|
|
168 |
|
|
class NODE_EXTERN_PRIVATE IsolateData : public MemoryRetainer { |
169 |
|
|
public: |
170 |
|
|
IsolateData(v8::Isolate* isolate, |
171 |
|
|
uv_loop_t* event_loop, |
172 |
|
|
MultiIsolatePlatform* platform = nullptr, |
173 |
|
|
ArrayBufferAllocator* node_allocator = nullptr, |
174 |
|
|
const IsolateDataSerializeInfo* isolate_data_info = nullptr); |
175 |
|
25 |
SET_MEMORY_INFO_NAME(IsolateData) |
176 |
|
25 |
SET_SELF_SIZE(IsolateData) |
177 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
178 |
|
|
IsolateDataSerializeInfo Serialize(v8::SnapshotCreator* creator); |
179 |
|
|
|
180 |
|
|
inline uv_loop_t* event_loop() const; |
181 |
|
|
inline MultiIsolatePlatform* platform() const; |
182 |
|
|
inline std::shared_ptr<PerIsolateOptions> options(); |
183 |
|
|
inline void set_options(std::shared_ptr<PerIsolateOptions> options); |
184 |
|
|
|
185 |
|
|
inline NodeArrayBufferAllocator* node_allocator() const; |
186 |
|
|
|
187 |
|
|
inline worker::Worker* worker_context() const; |
188 |
|
|
inline void set_worker_context(worker::Worker* context); |
189 |
|
|
|
190 |
|
|
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName) |
191 |
|
|
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName) |
192 |
|
|
#define VS(PropertyName, StringValue) V(v8::String, PropertyName) |
193 |
|
|
#define V(TypeName, PropertyName) \ |
194 |
|
|
inline v8::Local<TypeName> PropertyName() const; |
195 |
|
|
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) |
196 |
|
|
PER_ISOLATE_SYMBOL_PROPERTIES(VY) |
197 |
|
|
PER_ISOLATE_STRING_PROPERTIES(VS) |
198 |
|
|
#undef V |
199 |
|
|
#undef VY |
200 |
|
|
#undef VS |
201 |
|
|
#undef VP |
202 |
|
|
|
203 |
|
|
#define V(PropertyName, TypeName) \ |
204 |
|
|
inline v8::Local<TypeName> PropertyName() const; \ |
205 |
|
|
inline void set_##PropertyName(v8::Local<TypeName> value); |
206 |
|
|
PER_ISOLATE_TEMPLATE_PROPERTIES(V) |
207 |
|
|
#undef V |
208 |
|
|
|
209 |
|
|
inline v8::Local<v8::String> async_wrap_provider(int index) const; |
210 |
|
|
|
211 |
|
|
size_t max_young_gen_size = 1; |
212 |
|
|
std::unordered_map<const char*, v8::Eternal<v8::String>> static_str_map; |
213 |
|
|
|
214 |
|
|
inline v8::Isolate* isolate() const; |
215 |
|
|
IsolateData(const IsolateData&) = delete; |
216 |
|
|
IsolateData& operator=(const IsolateData&) = delete; |
217 |
|
|
IsolateData(IsolateData&&) = delete; |
218 |
|
|
IsolateData& operator=(IsolateData&&) = delete; |
219 |
|
|
|
220 |
|
|
private: |
221 |
|
|
void DeserializeProperties(const IsolateDataSerializeInfo* isolate_data_info); |
222 |
|
|
void CreateProperties(); |
223 |
|
|
|
224 |
|
|
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName) |
225 |
|
|
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName) |
226 |
|
|
#define VS(PropertyName, StringValue) V(v8::String, PropertyName) |
227 |
|
|
#define VT(PropertyName, TypeName) V(TypeName, PropertyName) |
228 |
|
|
#define V(TypeName, PropertyName) \ |
229 |
|
|
v8::Eternal<TypeName> PropertyName ## _; |
230 |
|
|
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) |
231 |
|
|
PER_ISOLATE_SYMBOL_PROPERTIES(VY) |
232 |
|
|
PER_ISOLATE_STRING_PROPERTIES(VS) |
233 |
|
|
PER_ISOLATE_TEMPLATE_PROPERTIES(VT) |
234 |
|
|
#undef V |
235 |
|
|
#undef V |
236 |
|
|
#undef VT |
237 |
|
|
#undef VS |
238 |
|
|
#undef VY |
239 |
|
|
#undef VP |
240 |
|
|
// Keep a list of all Persistent strings used for AsyncWrap Provider types. |
241 |
|
|
std::array<v8::Eternal<v8::String>, AsyncWrap::PROVIDERS_LENGTH> |
242 |
|
|
async_wrap_providers_; |
243 |
|
|
|
244 |
|
|
v8::Isolate* const isolate_; |
245 |
|
|
uv_loop_t* const event_loop_; |
246 |
|
|
NodeArrayBufferAllocator* const node_allocator_; |
247 |
|
|
MultiIsolatePlatform* platform_; |
248 |
|
|
std::shared_ptr<PerIsolateOptions> options_; |
249 |
|
|
worker::Worker* worker_context_ = nullptr; |
250 |
|
|
}; |
251 |
|
|
|
252 |
|
|
struct ContextInfo { |
253 |
|
13332 |
explicit ContextInfo(const std::string& name) : name(name) {} |
254 |
|
|
const std::string name; |
255 |
|
|
std::string origin; |
256 |
|
|
bool is_default = false; |
257 |
|
|
}; |
258 |
|
|
|
259 |
|
|
class EnabledDebugList; |
260 |
|
|
|
261 |
|
|
namespace per_process { |
262 |
|
|
extern std::shared_ptr<KVStore> system_environment; |
263 |
|
|
} |
264 |
|
|
|
265 |
|
|
struct EnvSerializeInfo; |
266 |
|
|
|
267 |
|
|
class AsyncHooks : public MemoryRetainer { |
268 |
|
|
public: |
269 |
|
25 |
SET_MEMORY_INFO_NAME(AsyncHooks) |
270 |
|
25 |
SET_SELF_SIZE(AsyncHooks) |
271 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
272 |
|
|
|
273 |
|
|
// Reason for both UidFields and Fields are that one is stored as a double* |
274 |
|
|
// and the other as a uint32_t*. |
275 |
|
|
enum Fields { |
276 |
|
|
kInit, |
277 |
|
|
kBefore, |
278 |
|
|
kAfter, |
279 |
|
|
kDestroy, |
280 |
|
|
kPromiseResolve, |
281 |
|
|
kTotals, |
282 |
|
|
kCheck, |
283 |
|
|
kStackLength, |
284 |
|
|
kUsesExecutionAsyncResource, |
285 |
|
|
kFieldsCount, |
286 |
|
|
}; |
287 |
|
|
|
288 |
|
|
enum UidFields { |
289 |
|
|
kExecutionAsyncId, |
290 |
|
|
kTriggerAsyncId, |
291 |
|
|
kAsyncIdCounter, |
292 |
|
|
kDefaultTriggerAsyncId, |
293 |
|
|
kUidFieldsCount, |
294 |
|
|
}; |
295 |
|
|
|
296 |
|
|
inline AliasedUint32Array& fields(); |
297 |
|
|
inline AliasedFloat64Array& async_id_fields(); |
298 |
|
|
inline AliasedFloat64Array& async_ids_stack(); |
299 |
|
|
inline v8::Local<v8::Array> js_execution_async_resources(); |
300 |
|
|
// Returns the native executionAsyncResource value at stack index `index`. |
301 |
|
|
// Resources provided on the JS side are not stored on the native stack, |
302 |
|
|
// in which case an empty `Local<>` is returned. |
303 |
|
|
// The `js_execution_async_resources` array contains the value in that case. |
304 |
|
|
inline v8::Local<v8::Object> native_execution_async_resource(size_t index); |
305 |
|
|
|
306 |
|
|
void SetJSPromiseHooks(v8::Local<v8::Function> init, |
307 |
|
|
v8::Local<v8::Function> before, |
308 |
|
|
v8::Local<v8::Function> after, |
309 |
|
|
v8::Local<v8::Function> resolve); |
310 |
|
|
|
311 |
|
|
inline v8::Local<v8::String> provider_string(int idx); |
312 |
|
|
|
313 |
|
|
inline void no_force_checks(); |
314 |
|
|
inline Environment* env(); |
315 |
|
|
|
316 |
|
|
// NB: This call does not take (co-)ownership of `execution_async_resource`. |
317 |
|
|
// The lifetime of the `v8::Local<>` pointee must last until |
318 |
|
|
// `pop_async_context()` or `clear_async_id_stack()` are called. |
319 |
|
|
void push_async_context(double async_id, |
320 |
|
|
double trigger_async_id, |
321 |
|
|
v8::Local<v8::Object> execution_async_resource); |
322 |
|
|
bool pop_async_context(double async_id); |
323 |
|
|
void clear_async_id_stack(); // Used in fatal exceptions. |
324 |
|
|
|
325 |
|
|
void AddContext(v8::Local<v8::Context> ctx); |
326 |
|
|
void RemoveContext(v8::Local<v8::Context> ctx); |
327 |
|
|
|
328 |
|
|
AsyncHooks(const AsyncHooks&) = delete; |
329 |
|
|
AsyncHooks& operator=(const AsyncHooks&) = delete; |
330 |
|
|
AsyncHooks(AsyncHooks&&) = delete; |
331 |
|
|
AsyncHooks& operator=(AsyncHooks&&) = delete; |
332 |
|
22656 |
~AsyncHooks() = default; |
333 |
|
|
|
334 |
|
|
// Used to set the kDefaultTriggerAsyncId in a scope. This is instead of |
335 |
|
|
// passing the trigger_async_id along with other constructor arguments. |
336 |
|
|
class DefaultTriggerAsyncIdScope { |
337 |
|
|
public: |
338 |
|
|
DefaultTriggerAsyncIdScope() = delete; |
339 |
|
|
explicit DefaultTriggerAsyncIdScope(Environment* env, |
340 |
|
|
double init_trigger_async_id); |
341 |
|
|
explicit DefaultTriggerAsyncIdScope(AsyncWrap* async_wrap); |
342 |
|
|
~DefaultTriggerAsyncIdScope(); |
343 |
|
|
|
344 |
|
|
DefaultTriggerAsyncIdScope(const DefaultTriggerAsyncIdScope&) = delete; |
345 |
|
|
DefaultTriggerAsyncIdScope& operator=(const DefaultTriggerAsyncIdScope&) = |
346 |
|
|
delete; |
347 |
|
|
DefaultTriggerAsyncIdScope(DefaultTriggerAsyncIdScope&&) = delete; |
348 |
|
|
DefaultTriggerAsyncIdScope& operator=(DefaultTriggerAsyncIdScope&&) = |
349 |
|
|
delete; |
350 |
|
|
|
351 |
|
|
private: |
352 |
|
|
AsyncHooks* async_hooks_; |
353 |
|
|
double old_default_trigger_async_id_; |
354 |
|
|
}; |
355 |
|
|
|
356 |
|
|
struct SerializeInfo { |
357 |
|
|
AliasedBufferIndex async_ids_stack; |
358 |
|
|
AliasedBufferIndex fields; |
359 |
|
|
AliasedBufferIndex async_id_fields; |
360 |
|
|
SnapshotIndex js_execution_async_resources; |
361 |
|
|
std::vector<SnapshotIndex> native_execution_async_resources; |
362 |
|
|
}; |
363 |
|
|
|
364 |
|
|
SerializeInfo Serialize(v8::Local<v8::Context> context, |
365 |
|
|
v8::SnapshotCreator* creator); |
366 |
|
|
void Deserialize(v8::Local<v8::Context> context); |
367 |
|
|
|
368 |
|
|
private: |
369 |
|
|
friend class Environment; // So we can call the constructor. |
370 |
|
|
explicit AsyncHooks(v8::Isolate* isolate, const SerializeInfo* info); |
371 |
|
|
|
372 |
|
|
[[noreturn]] void FailWithCorruptedAsyncStack(double expected_async_id); |
373 |
|
|
|
374 |
|
|
// Stores the ids of the current execution context stack. |
375 |
|
|
AliasedFloat64Array async_ids_stack_; |
376 |
|
|
// Attached to a Uint32Array that tracks the number of active hooks for |
377 |
|
|
// each type. |
378 |
|
|
AliasedUint32Array fields_; |
379 |
|
|
// Attached to a Float64Array that tracks the state of async resources. |
380 |
|
|
AliasedFloat64Array async_id_fields_; |
381 |
|
|
|
382 |
|
|
void grow_async_ids_stack(); |
383 |
|
|
|
384 |
|
|
v8::Global<v8::Array> js_execution_async_resources_; |
385 |
|
|
std::vector<v8::Local<v8::Object>> native_execution_async_resources_; |
386 |
|
|
|
387 |
|
|
// Non-empty during deserialization |
388 |
|
|
const SerializeInfo* info_ = nullptr; |
389 |
|
|
|
390 |
|
|
std::vector<v8::Global<v8::Context>> contexts_; |
391 |
|
|
|
392 |
|
|
std::array<v8::Global<v8::Function>, 4> js_promise_hooks_; |
393 |
|
|
}; |
394 |
|
|
|
395 |
|
|
class ImmediateInfo : public MemoryRetainer { |
396 |
|
|
public: |
397 |
|
|
inline AliasedUint32Array& fields(); |
398 |
|
|
inline uint32_t count() const; |
399 |
|
|
inline uint32_t ref_count() const; |
400 |
|
|
inline bool has_outstanding() const; |
401 |
|
|
inline void ref_count_inc(uint32_t increment); |
402 |
|
|
inline void ref_count_dec(uint32_t decrement); |
403 |
|
|
|
404 |
|
|
ImmediateInfo(const ImmediateInfo&) = delete; |
405 |
|
|
ImmediateInfo& operator=(const ImmediateInfo&) = delete; |
406 |
|
|
ImmediateInfo(ImmediateInfo&&) = delete; |
407 |
|
|
ImmediateInfo& operator=(ImmediateInfo&&) = delete; |
408 |
|
11328 |
~ImmediateInfo() = default; |
409 |
|
|
|
410 |
|
25 |
SET_MEMORY_INFO_NAME(ImmediateInfo) |
411 |
|
25 |
SET_SELF_SIZE(ImmediateInfo) |
412 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
413 |
|
|
|
414 |
|
|
struct SerializeInfo { |
415 |
|
|
AliasedBufferIndex fields; |
416 |
|
|
}; |
417 |
|
|
SerializeInfo Serialize(v8::Local<v8::Context> context, |
418 |
|
|
v8::SnapshotCreator* creator); |
419 |
|
|
void Deserialize(v8::Local<v8::Context> context); |
420 |
|
|
|
421 |
|
|
private: |
422 |
|
|
friend class Environment; // So we can call the constructor. |
423 |
|
|
explicit ImmediateInfo(v8::Isolate* isolate, const SerializeInfo* info); |
424 |
|
|
|
425 |
|
|
enum Fields { kCount, kRefCount, kHasOutstanding, kFieldsCount }; |
426 |
|
|
|
427 |
|
|
AliasedUint32Array fields_; |
428 |
|
|
}; |
429 |
|
|
|
430 |
|
|
class TickInfo : public MemoryRetainer { |
431 |
|
|
public: |
432 |
|
|
inline AliasedUint8Array& fields(); |
433 |
|
|
inline bool has_tick_scheduled() const; |
434 |
|
|
inline bool has_rejection_to_warn() const; |
435 |
|
|
|
436 |
|
25 |
SET_MEMORY_INFO_NAME(TickInfo) |
437 |
|
25 |
SET_SELF_SIZE(TickInfo) |
438 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
439 |
|
|
|
440 |
|
|
TickInfo(const TickInfo&) = delete; |
441 |
|
|
TickInfo& operator=(const TickInfo&) = delete; |
442 |
|
|
TickInfo(TickInfo&&) = delete; |
443 |
|
|
TickInfo& operator=(TickInfo&&) = delete; |
444 |
|
11328 |
~TickInfo() = default; |
445 |
|
|
|
446 |
|
|
struct SerializeInfo { |
447 |
|
|
AliasedBufferIndex fields; |
448 |
|
|
}; |
449 |
|
|
SerializeInfo Serialize(v8::Local<v8::Context> context, |
450 |
|
|
v8::SnapshotCreator* creator); |
451 |
|
|
void Deserialize(v8::Local<v8::Context> context); |
452 |
|
|
|
453 |
|
|
private: |
454 |
|
|
friend class Environment; // So we can call the constructor. |
455 |
|
|
explicit TickInfo(v8::Isolate* isolate, const SerializeInfo* info); |
456 |
|
|
|
457 |
|
|
enum Fields { kHasTickScheduled = 0, kHasRejectionToWarn, kFieldsCount }; |
458 |
|
|
|
459 |
|
|
AliasedUint8Array fields_; |
460 |
|
|
}; |
461 |
|
|
|
462 |
|
|
class TrackingTraceStateObserver : |
463 |
|
|
public v8::TracingController::TraceStateObserver { |
464 |
|
|
public: |
465 |
|
6354 |
explicit TrackingTraceStateObserver(Environment* env) : env_(env) {} |
466 |
|
|
|
467 |
|
120 |
void OnTraceEnabled() override { |
468 |
|
120 |
UpdateTraceCategoryState(); |
469 |
|
120 |
} |
470 |
|
|
|
471 |
|
34 |
void OnTraceDisabled() override { |
472 |
|
34 |
UpdateTraceCategoryState(); |
473 |
|
34 |
} |
474 |
|
|
|
475 |
|
|
private: |
476 |
|
|
void UpdateTraceCategoryState(); |
477 |
|
|
|
478 |
|
|
Environment* env_; |
479 |
|
|
}; |
480 |
|
|
|
481 |
|
|
class ShouldNotAbortOnUncaughtScope { |
482 |
|
|
public: |
483 |
|
|
explicit inline ShouldNotAbortOnUncaughtScope(Environment* env); |
484 |
|
|
inline void Close(); |
485 |
|
|
inline ~ShouldNotAbortOnUncaughtScope(); |
486 |
|
|
ShouldNotAbortOnUncaughtScope(const ShouldNotAbortOnUncaughtScope&) = delete; |
487 |
|
|
ShouldNotAbortOnUncaughtScope& operator=( |
488 |
|
|
const ShouldNotAbortOnUncaughtScope&) = delete; |
489 |
|
|
ShouldNotAbortOnUncaughtScope(ShouldNotAbortOnUncaughtScope&&) = delete; |
490 |
|
|
ShouldNotAbortOnUncaughtScope& operator=(ShouldNotAbortOnUncaughtScope&&) = |
491 |
|
|
delete; |
492 |
|
|
|
493 |
|
|
private: |
494 |
|
|
Environment* env_; |
495 |
|
|
}; |
496 |
|
|
|
497 |
|
|
typedef void (*DeserializeRequestCallback)(v8::Local<v8::Context> context, |
498 |
|
|
v8::Local<v8::Object> holder, |
499 |
|
|
int index, |
500 |
|
|
InternalFieldInfoBase* info); |
501 |
|
|
struct DeserializeRequest { |
502 |
|
|
DeserializeRequestCallback cb; |
503 |
|
|
v8::Global<v8::Object> holder; |
504 |
|
|
int index; |
505 |
|
|
InternalFieldInfoBase* info = nullptr; // Owned by the request |
506 |
|
|
|
507 |
|
|
// Move constructor |
508 |
✓✗ |
44528 |
DeserializeRequest(DeserializeRequest&& other) = default; |
509 |
|
|
}; |
510 |
|
|
|
511 |
|
|
struct EnvSerializeInfo { |
512 |
|
|
std::vector<std::string> builtins; |
513 |
|
|
AsyncHooks::SerializeInfo async_hooks; |
514 |
|
|
TickInfo::SerializeInfo tick_info; |
515 |
|
|
ImmediateInfo::SerializeInfo immediate_info; |
516 |
|
|
performance::PerformanceState::SerializeInfo performance_state; |
517 |
|
|
AliasedBufferIndex exiting; |
518 |
|
|
AliasedBufferIndex stream_base_state; |
519 |
|
|
AliasedBufferIndex should_abort_on_uncaught_toggle; |
520 |
|
|
|
521 |
|
|
RealmSerializeInfo principal_realm; |
522 |
|
|
friend std::ostream& operator<<(std::ostream& o, const EnvSerializeInfo& i); |
523 |
|
|
}; |
524 |
|
|
|
525 |
|
|
struct SnapshotMetadata { |
526 |
|
|
// For now kFullyCustomized is only built with the --build-snapshot CLI flag. |
527 |
|
|
// We might want to add more types of snapshots in the future. |
528 |
|
|
enum class Type : uint8_t { kDefault, kFullyCustomized }; |
529 |
|
|
|
530 |
|
|
Type type; |
531 |
|
|
std::string node_version; |
532 |
|
|
std::string node_arch; |
533 |
|
|
std::string node_platform; |
534 |
|
|
// Result of v8::ScriptCompiler::CachedDataVersionTag(). |
535 |
|
|
uint32_t v8_cache_version_tag; |
536 |
|
|
}; |
537 |
|
|
|
538 |
|
|
struct SnapshotData { |
539 |
|
|
enum class DataOwnership { kOwned, kNotOwned }; |
540 |
|
|
|
541 |
|
|
static const uint32_t kMagic = 0x143da19; |
542 |
|
|
static const SnapshotIndex kNodeVMContextIndex = 0; |
543 |
|
|
static const SnapshotIndex kNodeBaseContextIndex = kNodeVMContextIndex + 1; |
544 |
|
|
static const SnapshotIndex kNodeMainContextIndex = kNodeBaseContextIndex + 1; |
545 |
|
|
|
546 |
|
|
DataOwnership data_ownership = DataOwnership::kOwned; |
547 |
|
|
|
548 |
|
|
SnapshotMetadata metadata; |
549 |
|
|
|
550 |
|
|
// The result of v8::SnapshotCreator::CreateBlob() during the snapshot |
551 |
|
|
// building process. |
552 |
|
|
v8::StartupData v8_snapshot_blob_data{nullptr, 0}; |
553 |
|
|
|
554 |
|
|
IsolateDataSerializeInfo isolate_data_info; |
555 |
|
|
// TODO(joyeecheung): there should be a vector of env_info once we snapshot |
556 |
|
|
// the worker environments. |
557 |
|
|
EnvSerializeInfo env_info; |
558 |
|
|
|
559 |
|
|
// A vector of built-in ids and v8::ScriptCompiler::CachedData, this can be |
560 |
|
|
// shared across Node.js instances because they are supposed to share the |
561 |
|
|
// read only space. We use builtins::CodeCacheInfo because |
562 |
|
|
// v8::ScriptCompiler::CachedData is not copyable. |
563 |
|
|
std::vector<builtins::CodeCacheInfo> code_cache; |
564 |
|
|
|
565 |
|
|
void ToBlob(FILE* out) const; |
566 |
|
|
// If returns false, the metadata doesn't match the current Node.js binary, |
567 |
|
|
// and the caller should not consume the snapshot data. |
568 |
|
|
bool Check() const; |
569 |
|
|
static bool FromBlob(SnapshotData* out, FILE* in); |
570 |
|
|
|
571 |
|
|
~SnapshotData(); |
572 |
|
|
|
573 |
|
|
SnapshotData(const SnapshotData&) = delete; |
574 |
|
|
SnapshotData& operator=(const SnapshotData&) = delete; |
575 |
|
|
SnapshotData(SnapshotData&&) = delete; |
576 |
|
|
SnapshotData& operator=(SnapshotData&&) = delete; |
577 |
|
|
|
578 |
|
10 |
SnapshotData() = default; |
579 |
|
|
}; |
580 |
|
|
|
581 |
|
|
void DefaultProcessExitHandlerInternal(Environment* env, ExitCode exit_code); |
582 |
|
|
v8::Maybe<ExitCode> SpinEventLoopInternal(Environment* env); |
583 |
|
|
v8::Maybe<ExitCode> EmitProcessExitInternal(Environment* env); |
584 |
|
|
|
585 |
|
|
/** |
586 |
|
|
* Environment is a per-isolate data structure that represents an execution |
587 |
|
|
* environment. Each environment has a principal realm. An environment can |
588 |
|
|
* create multiple subsidiary synthetic realms. |
589 |
|
|
*/ |
590 |
|
|
class Environment : public MemoryRetainer { |
591 |
|
|
public: |
592 |
|
|
Environment(const Environment&) = delete; |
593 |
|
|
Environment& operator=(const Environment&) = delete; |
594 |
|
|
Environment(Environment&&) = delete; |
595 |
|
|
Environment& operator=(Environment&&) = delete; |
596 |
|
|
|
597 |
|
25 |
SET_MEMORY_INFO_NAME(Environment) |
598 |
|
|
|
599 |
|
|
inline size_t SelfSize() const override; |
600 |
|
50 |
bool IsRootNode() const override { return true; } |
601 |
|
|
void MemoryInfo(MemoryTracker* tracker) const override; |
602 |
|
|
|
603 |
|
|
EnvSerializeInfo Serialize(v8::SnapshotCreator* creator); |
604 |
|
|
void DeserializeProperties(const EnvSerializeInfo* info); |
605 |
|
|
|
606 |
|
|
void PrintInfoForSnapshotIfDebug(); |
607 |
|
|
void EnqueueDeserializeRequest(DeserializeRequestCallback cb, |
608 |
|
|
v8::Local<v8::Object> holder, |
609 |
|
|
int index, |
610 |
|
|
InternalFieldInfoBase* info); |
611 |
|
|
void RunDeserializeRequests(); |
612 |
|
|
// Should be called before InitializeInspector() |
613 |
|
|
void InitializeDiagnostics(); |
614 |
|
|
|
615 |
|
|
std::string GetCwd(); |
616 |
|
|
|
617 |
|
|
#if HAVE_INSPECTOR |
618 |
|
|
// If the environment is created for a worker, pass parent_handle and |
619 |
|
|
// the ownership if transferred into the Environment. |
620 |
|
|
void InitializeInspector( |
621 |
|
|
std::unique_ptr<inspector::ParentInspectorHandle> parent_handle); |
622 |
|
|
#endif |
623 |
|
|
|
624 |
|
|
inline size_t async_callback_scope_depth() const; |
625 |
|
|
inline void PushAsyncCallbackScope(); |
626 |
|
|
inline void PopAsyncCallbackScope(); |
627 |
|
|
|
628 |
|
|
static inline Environment* GetCurrent(v8::Isolate* isolate); |
629 |
|
|
static inline Environment* GetCurrent(v8::Local<v8::Context> context); |
630 |
|
|
static inline Environment* GetCurrent( |
631 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& info); |
632 |
|
|
|
633 |
|
|
template <typename T> |
634 |
|
|
static inline Environment* GetCurrent( |
635 |
|
|
const v8::PropertyCallbackInfo<T>& info); |
636 |
|
|
|
637 |
|
|
// Methods created using SetMethod(), SetPrototypeMethod(), etc. inside |
638 |
|
|
// this scope can access the created T* object using |
639 |
|
|
// GetBindingData<T>(args) later. |
640 |
|
|
template <typename T> |
641 |
|
|
T* AddBindingData(v8::Local<v8::Context> context, |
642 |
|
|
v8::Local<v8::Object> target); |
643 |
|
|
template <typename T, typename U> |
644 |
|
|
static inline T* GetBindingData(const v8::PropertyCallbackInfo<U>& info); |
645 |
|
|
template <typename T> |
646 |
|
|
static inline T* GetBindingData( |
647 |
|
|
const v8::FunctionCallbackInfo<v8::Value>& info); |
648 |
|
|
template <typename T> |
649 |
|
|
static inline T* GetBindingData(v8::Local<v8::Context> context); |
650 |
|
|
|
651 |
|
|
typedef std::unordered_map< |
652 |
|
|
FastStringKey, |
653 |
|
|
BaseObjectPtr<BaseObject>, |
654 |
|
|
FastStringKey::Hash> BindingDataStore; |
655 |
|
|
|
656 |
|
|
// Create an Environment without initializing a main Context. Use |
657 |
|
|
// InitializeMainContext() to initialize a main context for it. |
658 |
|
|
Environment(IsolateData* isolate_data, |
659 |
|
|
v8::Isolate* isolate, |
660 |
|
|
const std::vector<std::string>& args, |
661 |
|
|
const std::vector<std::string>& exec_args, |
662 |
|
|
const EnvSerializeInfo* env_info, |
663 |
|
|
EnvironmentFlags::Flags flags, |
664 |
|
|
ThreadId thread_id); |
665 |
|
|
void InitializeMainContext(v8::Local<v8::Context> context, |
666 |
|
|
const EnvSerializeInfo* env_info); |
667 |
|
|
// Create an Environment and initialize the provided principal context for it. |
668 |
|
|
Environment(IsolateData* isolate_data, |
669 |
|
|
v8::Local<v8::Context> context, |
670 |
|
|
const std::vector<std::string>& args, |
671 |
|
|
const std::vector<std::string>& exec_args, |
672 |
|
|
const EnvSerializeInfo* env_info, |
673 |
|
|
EnvironmentFlags::Flags flags, |
674 |
|
|
ThreadId thread_id); |
675 |
|
|
~Environment() override; |
676 |
|
|
|
677 |
|
|
void InitializeLibuv(); |
678 |
|
|
inline const std::vector<std::string>& exec_argv(); |
679 |
|
|
inline const std::vector<std::string>& argv(); |
680 |
|
|
const std::string& exec_path() const; |
681 |
|
|
|
682 |
|
|
typedef void (*HandleCleanupCb)(Environment* env, |
683 |
|
|
uv_handle_t* handle, |
684 |
|
|
void* arg); |
685 |
|
|
struct HandleCleanup { |
686 |
|
|
uv_handle_t* handle_; |
687 |
|
|
HandleCleanupCb cb_; |
688 |
|
|
void* arg_; |
689 |
|
|
}; |
690 |
|
|
|
691 |
|
|
void RegisterHandleCleanups(); |
692 |
|
|
void CleanupHandles(); |
693 |
|
|
void Exit(ExitCode code); |
694 |
|
|
void ExitEnv(); |
695 |
|
|
|
696 |
|
|
// Register clean-up cb to be called on environment destruction. |
697 |
|
|
inline void RegisterHandleCleanup(uv_handle_t* handle, |
698 |
|
|
HandleCleanupCb cb, |
699 |
|
|
void* arg); |
700 |
|
|
|
701 |
|
|
template <typename T, typename OnCloseCallback> |
702 |
|
|
inline void CloseHandle(T* handle, OnCloseCallback callback); |
703 |
|
|
|
704 |
|
|
void AssignToContext(v8::Local<v8::Context> context, |
705 |
|
|
Realm* realm, |
706 |
|
|
const ContextInfo& info); |
707 |
|
|
|
708 |
|
|
void StartProfilerIdleNotifier(); |
709 |
|
|
|
710 |
|
|
inline v8::Isolate* isolate() const; |
711 |
|
|
inline uv_loop_t* event_loop() const; |
712 |
|
|
void TryLoadAddon(const char* filename, |
713 |
|
|
int flags, |
714 |
|
|
const std::function<bool(binding::DLib*)>& was_loaded); |
715 |
|
|
|
716 |
|
|
static inline Environment* from_timer_handle(uv_timer_t* handle); |
717 |
|
|
inline uv_timer_t* timer_handle(); |
718 |
|
|
|
719 |
|
|
static inline Environment* from_immediate_check_handle(uv_check_t* handle); |
720 |
|
|
inline uv_check_t* immediate_check_handle(); |
721 |
|
|
inline uv_idle_t* immediate_idle_handle(); |
722 |
|
|
|
723 |
|
|
inline void IncreaseWaitingRequestCounter(); |
724 |
|
|
inline void DecreaseWaitingRequestCounter(); |
725 |
|
|
|
726 |
|
|
inline AsyncHooks* async_hooks(); |
727 |
|
|
inline ImmediateInfo* immediate_info(); |
728 |
|
|
inline TickInfo* tick_info(); |
729 |
|
|
inline uint64_t timer_base() const; |
730 |
|
|
inline std::shared_ptr<KVStore> env_vars(); |
731 |
|
|
inline void set_env_vars(std::shared_ptr<KVStore> env_vars); |
732 |
|
|
|
733 |
|
|
inline IsolateData* isolate_data() const; |
734 |
|
|
|
735 |
|
|
inline bool printed_error() const; |
736 |
|
|
inline void set_printed_error(bool value); |
737 |
|
|
|
738 |
|
|
void PrintSyncTrace() const; |
739 |
|
|
inline void set_trace_sync_io(bool value); |
740 |
|
|
|
741 |
|
|
inline void set_force_context_aware(bool value); |
742 |
|
|
inline bool force_context_aware() const; |
743 |
|
|
|
744 |
|
|
// This is a pseudo-boolean that keeps track of whether the process is |
745 |
|
|
// exiting. |
746 |
|
|
inline void set_exiting(bool value); |
747 |
|
|
inline AliasedUint32Array& exiting(); |
748 |
|
|
|
749 |
|
|
// This stores whether the --abort-on-uncaught-exception flag was passed |
750 |
|
|
// to Node. |
751 |
|
|
inline bool abort_on_uncaught_exception() const; |
752 |
|
|
inline void set_abort_on_uncaught_exception(bool value); |
753 |
|
|
// This is a pseudo-boolean that keeps track of whether an uncaught exception |
754 |
|
|
// should abort the process or not if --abort-on-uncaught-exception was |
755 |
|
|
// passed to Node. If the flag was not passed, it is ignored. |
756 |
|
|
inline AliasedUint32Array& should_abort_on_uncaught_toggle(); |
757 |
|
|
|
758 |
|
|
inline AliasedInt32Array& stream_base_state(); |
759 |
|
|
|
760 |
|
|
// The necessary API for async_hooks. |
761 |
|
|
inline double new_async_id(); |
762 |
|
|
inline double execution_async_id(); |
763 |
|
|
inline double trigger_async_id(); |
764 |
|
|
inline double get_default_trigger_async_id(); |
765 |
|
|
|
766 |
|
|
// List of id's that have been destroyed and need the destroy() cb called. |
767 |
|
|
inline std::vector<double>* destroy_async_id_list(); |
768 |
|
|
|
769 |
|
|
std::set<struct node_module*> internal_bindings; |
770 |
|
|
std::set<std::string> builtins_with_cache; |
771 |
|
|
std::set<std::string> builtins_without_cache; |
772 |
|
|
// This is only filled during deserialization. We use a vector since |
773 |
|
|
// it's only used for tests. |
774 |
|
|
std::vector<std::string> builtins_in_snapshot; |
775 |
|
|
|
776 |
|
|
std::unordered_multimap<int, loader::ModuleWrap*> hash_to_module_map; |
777 |
|
|
std::unordered_map<uint32_t, loader::ModuleWrap*> id_to_module_map; |
778 |
|
|
std::unordered_map<uint32_t, contextify::ContextifyScript*> |
779 |
|
|
id_to_script_map; |
780 |
|
|
std::unordered_map<uint32_t, contextify::CompiledFnEntry*> id_to_function_map; |
781 |
|
|
|
782 |
|
|
inline uint32_t get_next_module_id(); |
783 |
|
|
inline uint32_t get_next_script_id(); |
784 |
|
|
inline uint32_t get_next_function_id(); |
785 |
|
|
|
786 |
|
1386581 |
EnabledDebugList* enabled_debug_list() { return &enabled_debug_list_; } |
787 |
|
|
|
788 |
|
|
inline performance::PerformanceState* performance_state(); |
789 |
|
|
|
790 |
|
|
void CollectUVExceptionInfo(v8::Local<v8::Value> context, |
791 |
|
|
int errorno, |
792 |
|
|
const char* syscall = nullptr, |
793 |
|
|
const char* message = nullptr, |
794 |
|
|
const char* path = nullptr, |
795 |
|
|
const char* dest = nullptr); |
796 |
|
|
|
797 |
|
|
// If this flag is set, calls into JS (if they would be observable |
798 |
|
|
// from userland) must be avoided. This flag does not indicate whether |
799 |
|
|
// calling into JS is allowed from a VM perspective at this point. |
800 |
|
|
inline bool can_call_into_js() const; |
801 |
|
|
inline void set_can_call_into_js(bool can_call_into_js); |
802 |
|
|
|
803 |
|
|
// Increase or decrease a counter that manages whether this Environment |
804 |
|
|
// keeps the event loop alive on its own or not. The counter starts out at 0, |
805 |
|
|
// meaning it does not, and any positive value will make it keep the event |
806 |
|
|
// loop alive. |
807 |
|
|
// This is used by Workers to manage their own .ref()/.unref() implementation, |
808 |
|
|
// as Workers aren't directly associated with their own libuv handles. |
809 |
|
|
void add_refs(int64_t diff); |
810 |
|
|
|
811 |
|
|
// Convenient getter of the principal realm's has_run_bootstrapping_code(). |
812 |
|
|
inline bool has_run_bootstrapping_code() const; |
813 |
|
|
|
814 |
|
|
inline bool has_serialized_options() const; |
815 |
|
|
inline void set_has_serialized_options(bool has_serialized_options); |
816 |
|
|
|
817 |
|
|
inline bool is_main_thread() const; |
818 |
|
|
inline bool no_native_addons() const; |
819 |
|
|
inline bool should_not_register_esm_loader() const; |
820 |
|
|
inline bool should_create_inspector() const; |
821 |
|
|
inline bool owns_process_state() const; |
822 |
|
|
inline bool owns_inspector() const; |
823 |
|
|
inline bool tracks_unmanaged_fds() const; |
824 |
|
|
inline bool hide_console_windows() const; |
825 |
|
|
inline bool no_global_search_paths() const; |
826 |
|
|
inline bool no_browser_globals() const; |
827 |
|
|
inline uint64_t thread_id() const; |
828 |
|
|
inline worker::Worker* worker_context() const; |
829 |
|
|
Environment* worker_parent_env() const; |
830 |
|
|
inline void add_sub_worker_context(worker::Worker* context); |
831 |
|
|
inline void remove_sub_worker_context(worker::Worker* context); |
832 |
|
|
void stop_sub_worker_contexts(); |
833 |
|
|
template <typename Fn> |
834 |
|
|
inline void ForEachWorker(Fn&& iterator); |
835 |
|
|
inline bool is_stopping() const; |
836 |
|
|
inline void set_stopping(bool value); |
837 |
|
|
inline std::list<node_module>* extra_linked_bindings(); |
838 |
|
|
inline node_module* extra_linked_bindings_head(); |
839 |
|
|
inline node_module* extra_linked_bindings_tail(); |
840 |
|
|
inline const Mutex& extra_linked_bindings_mutex() const; |
841 |
|
|
|
842 |
|
|
inline bool filehandle_close_warning() const; |
843 |
|
|
inline void set_filehandle_close_warning(bool on); |
844 |
|
|
|
845 |
|
|
inline void set_source_maps_enabled(bool on); |
846 |
|
|
inline bool source_maps_enabled() const; |
847 |
|
|
|
848 |
|
|
inline void ThrowError(const char* errmsg); |
849 |
|
|
inline void ThrowTypeError(const char* errmsg); |
850 |
|
|
inline void ThrowRangeError(const char* errmsg); |
851 |
|
|
inline void ThrowErrnoException(int errorno, |
852 |
|
|
const char* syscall = nullptr, |
853 |
|
|
const char* message = nullptr, |
854 |
|
|
const char* path = nullptr); |
855 |
|
|
inline void ThrowUVException(int errorno, |
856 |
|
|
const char* syscall = nullptr, |
857 |
|
|
const char* message = nullptr, |
858 |
|
|
const char* path = nullptr, |
859 |
|
|
const char* dest = nullptr); |
860 |
|
|
|
861 |
|
|
void AtExit(void (*cb)(void* arg), void* arg); |
862 |
|
|
void RunAtExitCallbacks(); |
863 |
|
|
|
864 |
|
|
void RunWeakRefCleanup(); |
865 |
|
|
|
866 |
|
|
v8::MaybeLocal<v8::Value> RunSnapshotSerializeCallback() const; |
867 |
|
|
v8::MaybeLocal<v8::Value> RunSnapshotDeserializeCallback() const; |
868 |
|
|
v8::MaybeLocal<v8::Value> RunSnapshotDeserializeMain() const; |
869 |
|
|
|
870 |
|
|
// Primitive values are shared across realms. |
871 |
|
|
// The getters simply proxy to the per-isolate primitive. |
872 |
|
|
#define VP(PropertyName, StringValue) V(v8::Private, PropertyName) |
873 |
|
|
#define VY(PropertyName, StringValue) V(v8::Symbol, PropertyName) |
874 |
|
|
#define VS(PropertyName, StringValue) V(v8::String, PropertyName) |
875 |
|
|
#define V(TypeName, PropertyName) \ |
876 |
|
|
inline v8::Local<TypeName> PropertyName() const; |
877 |
|
|
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP) |
878 |
|
|
PER_ISOLATE_SYMBOL_PROPERTIES(VY) |
879 |
|
|
PER_ISOLATE_STRING_PROPERTIES(VS) |
880 |
|
|
#undef V |
881 |
|
|
#undef VS |
882 |
|
|
#undef VY |
883 |
|
|
#undef VP |
884 |
|
|
|
885 |
|
|
#define V(PropertyName, TypeName) \ |
886 |
|
|
inline v8::Local<TypeName> PropertyName() const; \ |
887 |
|
|
inline void set_ ## PropertyName(v8::Local<TypeName> value); |
888 |
|
|
PER_ISOLATE_TEMPLATE_PROPERTIES(V) |
889 |
|
|
// Per-realm strong persistent values of the principal realm. |
890 |
|
|
// Get/set the value with an explicit realm instead when possible. |
891 |
|
|
// Deprecate soon. |
892 |
|
|
PER_REALM_STRONG_PERSISTENT_VALUES(V) |
893 |
|
|
#undef V |
894 |
|
|
|
895 |
|
|
// Return the context of the principal realm. |
896 |
|
|
// Get the context with an explicit realm instead when possible. |
897 |
|
|
// Deprecate soon. |
898 |
|
|
inline v8::Local<v8::Context> context() const; |
899 |
|
|
inline Realm* principal_realm() const; |
900 |
|
|
|
901 |
|
|
#if HAVE_INSPECTOR |
902 |
|
106464 |
inline inspector::Agent* inspector_agent() const { |
903 |
|
106464 |
return inspector_agent_.get(); |
904 |
|
|
} |
905 |
|
|
|
906 |
|
|
inline bool is_in_inspector_console_call() const; |
907 |
|
|
inline void set_is_in_inspector_console_call(bool value); |
908 |
|
|
#endif |
909 |
|
|
|
910 |
|
|
typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue; |
911 |
|
|
typedef ListHead<ReqWrapBase, &ReqWrapBase::req_wrap_queue_> ReqWrapQueue; |
912 |
|
|
|
913 |
|
65615 |
inline HandleWrapQueue* handle_wrap_queue() { return &handle_wrap_queue_; } |
914 |
|
84646 |
inline ReqWrapQueue* req_wrap_queue() { return &req_wrap_queue_; } |
915 |
|
|
|
916 |
|
7122 |
inline uint64_t time_origin() { |
917 |
|
7122 |
return time_origin_; |
918 |
|
|
} |
919 |
|
8 |
inline double time_origin_timestamp() { |
920 |
|
8 |
return time_origin_timestamp_; |
921 |
|
|
} |
922 |
|
|
|
923 |
|
1 |
inline bool EmitProcessEnvWarning() { |
924 |
|
1 |
bool current_value = emit_env_nonstring_warning_; |
925 |
|
1 |
emit_env_nonstring_warning_ = false; |
926 |
|
1 |
return current_value; |
927 |
|
|
} |
928 |
|
|
|
929 |
|
1 |
inline bool EmitErrNameWarning() { |
930 |
|
1 |
bool current_value = emit_err_name_warning_; |
931 |
|
1 |
emit_err_name_warning_ = false; |
932 |
|
1 |
return current_value; |
933 |
|
|
} |
934 |
|
|
|
935 |
|
|
// cb will be called as cb(env) on the next event loop iteration. |
936 |
|
|
// Unlike the JS setImmediate() function, nested SetImmediate() calls will |
937 |
|
|
// be run without returning control to the event loop, similar to nextTick(). |
938 |
|
|
template <typename Fn> |
939 |
|
|
inline void SetImmediate( |
940 |
|
|
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed); |
941 |
|
|
template <typename Fn> |
942 |
|
|
// This behaves like SetImmediate() but can be called from any thread. |
943 |
|
|
inline void SetImmediateThreadsafe( |
944 |
|
|
Fn&& cb, CallbackFlags::Flags flags = CallbackFlags::kRefed); |
945 |
|
|
// This behaves like V8's Isolate::RequestInterrupt(), but also accounts for |
946 |
|
|
// the event loop (i.e. combines the V8 function with SetImmediate()). |
947 |
|
|
// The passed callback may not throw exceptions. |
948 |
|
|
// This function can be called from any thread. |
949 |
|
|
template <typename Fn> |
950 |
|
|
inline void RequestInterrupt(Fn&& cb); |
951 |
|
|
// This needs to be available for the JS-land setImmediate(). |
952 |
|
|
void ToggleImmediateRef(bool ref); |
953 |
|
|
|
954 |
|
|
inline void PushShouldNotAbortOnUncaughtScope(); |
955 |
|
|
inline void PopShouldNotAbortOnUncaughtScope(); |
956 |
|
|
inline bool inside_should_not_abort_on_uncaught_scope() const; |
957 |
|
|
|
958 |
|
|
static inline Environment* ForAsyncHooks(AsyncHooks* hooks); |
959 |
|
|
|
960 |
|
|
v8::Local<v8::Value> GetNow(); |
961 |
|
|
void ScheduleTimer(int64_t duration); |
962 |
|
|
void ToggleTimerRef(bool ref); |
963 |
|
|
|
964 |
|
|
inline void AddCleanupHook(CleanupQueue::Callback cb, void* arg); |
965 |
|
|
inline void RemoveCleanupHook(CleanupQueue::Callback cb, void* arg); |
966 |
|
|
void RunCleanup(); |
967 |
|
|
|
968 |
|
|
static size_t NearHeapLimitCallback(void* data, |
969 |
|
|
size_t current_heap_limit, |
970 |
|
|
size_t initial_heap_limit); |
971 |
|
|
static void BuildEmbedderGraph(v8::Isolate* isolate, |
972 |
|
|
v8::EmbedderGraph* graph, |
973 |
|
|
void* data); |
974 |
|
|
|
975 |
|
|
inline std::shared_ptr<EnvironmentOptions> options(); |
976 |
|
|
inline std::shared_ptr<ExclusiveAccess<HostPort>> inspector_host_port(); |
977 |
|
|
|
978 |
|
3 |
inline int32_t stack_trace_limit() const { return 10; } |
979 |
|
|
|
980 |
|
|
#if HAVE_INSPECTOR |
981 |
|
|
void set_coverage_connection( |
982 |
|
|
std::unique_ptr<profiler::V8CoverageConnection> connection); |
983 |
|
|
profiler::V8CoverageConnection* coverage_connection(); |
984 |
|
|
|
985 |
|
|
inline void set_coverage_directory(const char* directory); |
986 |
|
|
inline const std::string& coverage_directory() const; |
987 |
|
|
|
988 |
|
|
void set_cpu_profiler_connection( |
989 |
|
|
std::unique_ptr<profiler::V8CpuProfilerConnection> connection); |
990 |
|
|
profiler::V8CpuProfilerConnection* cpu_profiler_connection(); |
991 |
|
|
|
992 |
|
|
inline void set_cpu_prof_name(const std::string& name); |
993 |
|
|
inline const std::string& cpu_prof_name() const; |
994 |
|
|
|
995 |
|
|
inline void set_cpu_prof_interval(uint64_t interval); |
996 |
|
|
inline uint64_t cpu_prof_interval() const; |
997 |
|
|
|
998 |
|
|
inline void set_cpu_prof_dir(const std::string& dir); |
999 |
|
|
inline const std::string& cpu_prof_dir() const; |
1000 |
|
|
|
1001 |
|
|
void set_heap_profiler_connection( |
1002 |
|
|
std::unique_ptr<profiler::V8HeapProfilerConnection> connection); |
1003 |
|
|
profiler::V8HeapProfilerConnection* heap_profiler_connection(); |
1004 |
|
|
|
1005 |
|
|
inline void set_heap_prof_name(const std::string& name); |
1006 |
|
|
inline const std::string& heap_prof_name() const; |
1007 |
|
|
|
1008 |
|
|
inline void set_heap_prof_dir(const std::string& dir); |
1009 |
|
|
inline const std::string& heap_prof_dir() const; |
1010 |
|
|
|
1011 |
|
|
inline void set_heap_prof_interval(uint64_t interval); |
1012 |
|
|
inline uint64_t heap_prof_interval() const; |
1013 |
|
|
|
1014 |
|
|
#endif // HAVE_INSPECTOR |
1015 |
|
|
|
1016 |
|
|
inline void set_main_utf16(std::unique_ptr<v8::String::Value>); |
1017 |
|
|
inline void set_process_exit_handler( |
1018 |
|
|
std::function<void(Environment*, ExitCode)>&& handler); |
1019 |
|
|
|
1020 |
|
|
void RunAndClearNativeImmediates(bool only_refed = false); |
1021 |
|
|
void RunAndClearInterrupts(); |
1022 |
|
|
|
1023 |
|
|
uv_buf_t allocate_managed_buffer(const size_t suggested_size); |
1024 |
|
|
std::unique_ptr<v8::BackingStore> release_managed_buffer(const uv_buf_t& buf); |
1025 |
|
|
|
1026 |
|
|
void AddUnmanagedFd(int fd); |
1027 |
|
|
void RemoveUnmanagedFd(int fd); |
1028 |
|
|
|
1029 |
|
|
template <typename T> |
1030 |
|
|
void ForEachRealm(T&& iterator) const; |
1031 |
|
|
|
1032 |
|
|
inline void set_heap_snapshot_near_heap_limit(uint32_t limit); |
1033 |
|
|
inline bool is_in_heapsnapshot_heap_limit_callback() const; |
1034 |
|
|
|
1035 |
|
|
inline void AddHeapSnapshotNearHeapLimitCallback(); |
1036 |
|
|
|
1037 |
|
|
inline void RemoveHeapSnapshotNearHeapLimitCallback(size_t heap_limit); |
1038 |
|
|
|
1039 |
|
|
private: |
1040 |
|
|
inline void ThrowError(v8::Local<v8::Value> (*fun)(v8::Local<v8::String>), |
1041 |
|
|
const char* errmsg); |
1042 |
|
|
|
1043 |
|
|
std::list<binding::DLib> loaded_addons_; |
1044 |
|
|
v8::Isolate* const isolate_; |
1045 |
|
|
IsolateData* const isolate_data_; |
1046 |
|
|
uv_timer_t timer_handle_; |
1047 |
|
|
uv_check_t immediate_check_handle_; |
1048 |
|
|
uv_idle_t immediate_idle_handle_; |
1049 |
|
|
uv_prepare_t idle_prepare_handle_; |
1050 |
|
|
uv_check_t idle_check_handle_; |
1051 |
|
|
uv_async_t task_queues_async_; |
1052 |
|
|
int64_t task_queues_async_refs_ = 0; |
1053 |
|
|
|
1054 |
|
|
AsyncHooks async_hooks_; |
1055 |
|
|
ImmediateInfo immediate_info_; |
1056 |
|
|
TickInfo tick_info_; |
1057 |
|
|
const uint64_t timer_base_; |
1058 |
|
|
std::shared_ptr<KVStore> env_vars_; |
1059 |
|
|
bool printed_error_ = false; |
1060 |
|
|
bool trace_sync_io_ = false; |
1061 |
|
|
bool emit_env_nonstring_warning_ = true; |
1062 |
|
|
bool emit_err_name_warning_ = true; |
1063 |
|
|
bool emit_filehandle_warning_ = true; |
1064 |
|
|
bool source_maps_enabled_ = false; |
1065 |
|
|
|
1066 |
|
|
size_t async_callback_scope_depth_ = 0; |
1067 |
|
|
std::vector<double> destroy_async_id_list_; |
1068 |
|
|
|
1069 |
|
|
#if HAVE_INSPECTOR |
1070 |
|
|
std::unique_ptr<profiler::V8CoverageConnection> coverage_connection_; |
1071 |
|
|
std::unique_ptr<profiler::V8CpuProfilerConnection> cpu_profiler_connection_; |
1072 |
|
|
std::string coverage_directory_; |
1073 |
|
|
std::string cpu_prof_dir_; |
1074 |
|
|
std::string cpu_prof_name_; |
1075 |
|
|
uint64_t cpu_prof_interval_; |
1076 |
|
|
std::unique_ptr<profiler::V8HeapProfilerConnection> heap_profiler_connection_; |
1077 |
|
|
std::string heap_prof_dir_; |
1078 |
|
|
std::string heap_prof_name_; |
1079 |
|
|
uint64_t heap_prof_interval_; |
1080 |
|
|
#endif // HAVE_INSPECTOR |
1081 |
|
|
|
1082 |
|
|
std::shared_ptr<EnvironmentOptions> options_; |
1083 |
|
|
// options_ contains debug options parsed from CLI arguments, |
1084 |
|
|
// while inspector_host_port_ stores the actual inspector host |
1085 |
|
|
// and port being used. For example the port is -1 by default |
1086 |
|
|
// and can be specified as 0 (meaning any port allocated when the |
1087 |
|
|
// server starts listening), but when the inspector server starts |
1088 |
|
|
// the inspector_host_port_->port() will be the actual port being |
1089 |
|
|
// used. |
1090 |
|
|
std::shared_ptr<ExclusiveAccess<HostPort>> inspector_host_port_; |
1091 |
|
|
std::vector<std::string> exec_argv_; |
1092 |
|
|
std::vector<std::string> argv_; |
1093 |
|
|
std::string exec_path_; |
1094 |
|
|
|
1095 |
|
|
bool is_in_heapsnapshot_heap_limit_callback_ = false; |
1096 |
|
|
uint32_t heap_limit_snapshot_taken_ = 0; |
1097 |
|
|
uint32_t heap_snapshot_near_heap_limit_ = 0; |
1098 |
|
|
bool heapsnapshot_near_heap_limit_callback_added_ = false; |
1099 |
|
|
|
1100 |
|
|
uint32_t module_id_counter_ = 0; |
1101 |
|
|
uint32_t script_id_counter_ = 0; |
1102 |
|
|
uint32_t function_id_counter_ = 0; |
1103 |
|
|
|
1104 |
|
|
AliasedUint32Array exiting_; |
1105 |
|
|
|
1106 |
|
|
AliasedUint32Array should_abort_on_uncaught_toggle_; |
1107 |
|
|
int should_not_abort_scope_counter_ = 0; |
1108 |
|
|
|
1109 |
|
|
std::unique_ptr<TrackingTraceStateObserver> trace_state_observer_; |
1110 |
|
|
|
1111 |
|
|
AliasedInt32Array stream_base_state_; |
1112 |
|
|
|
1113 |
|
|
// https://w3c.github.io/hr-time/#dfn-time-origin |
1114 |
|
|
uint64_t time_origin_; |
1115 |
|
|
// https://w3c.github.io/hr-time/#dfn-get-time-origin-timestamp |
1116 |
|
|
double time_origin_timestamp_; |
1117 |
|
|
std::unique_ptr<performance::PerformanceState> performance_state_; |
1118 |
|
|
|
1119 |
|
|
bool has_serialized_options_ = false; |
1120 |
|
|
|
1121 |
|
|
std::atomic_bool can_call_into_js_ { true }; |
1122 |
|
|
uint64_t flags_; |
1123 |
|
|
uint64_t thread_id_; |
1124 |
|
|
std::unordered_set<worker::Worker*> sub_worker_contexts_; |
1125 |
|
|
|
1126 |
|
|
#if HAVE_INSPECTOR |
1127 |
|
|
std::unique_ptr<inspector::Agent> inspector_agent_; |
1128 |
|
|
bool is_in_inspector_console_call_ = false; |
1129 |
|
|
#endif |
1130 |
|
|
|
1131 |
|
|
std::list<DeserializeRequest> deserialize_requests_; |
1132 |
|
|
|
1133 |
|
|
// handle_wrap_queue_ and req_wrap_queue_ needs to be at a fixed offset from |
1134 |
|
|
// the start of the class because it is used by |
1135 |
|
|
// src/node_postmortem_metadata.cc to calculate offsets and generate debug |
1136 |
|
|
// symbols for Environment, which assumes that the position of members in |
1137 |
|
|
// memory are predictable. For more information please refer to |
1138 |
|
|
// `doc/contributing/node-postmortem-support.md` |
1139 |
|
|
friend int GenDebugSymbols(); |
1140 |
|
|
HandleWrapQueue handle_wrap_queue_; |
1141 |
|
|
ReqWrapQueue req_wrap_queue_; |
1142 |
|
|
std::list<HandleCleanup> handle_cleanup_queue_; |
1143 |
|
|
int handle_cleanup_waiting_ = 0; |
1144 |
|
|
int request_waiting_ = 0; |
1145 |
|
|
|
1146 |
|
|
EnabledDebugList enabled_debug_list_; |
1147 |
|
|
|
1148 |
|
|
std::list<node_module> extra_linked_bindings_; |
1149 |
|
|
Mutex extra_linked_bindings_mutex_; |
1150 |
|
|
|
1151 |
|
|
static void RunTimers(uv_timer_t* handle); |
1152 |
|
|
|
1153 |
|
|
struct ExitCallback { |
1154 |
|
|
void (*cb_)(void* arg); |
1155 |
|
|
void* arg_; |
1156 |
|
|
}; |
1157 |
|
|
|
1158 |
|
|
std::list<ExitCallback> at_exit_functions_; |
1159 |
|
|
|
1160 |
|
|
typedef CallbackQueue<void, Environment*> NativeImmediateQueue; |
1161 |
|
|
NativeImmediateQueue native_immediates_; |
1162 |
|
|
Mutex native_immediates_threadsafe_mutex_; |
1163 |
|
|
NativeImmediateQueue native_immediates_threadsafe_; |
1164 |
|
|
NativeImmediateQueue native_immediates_interrupts_; |
1165 |
|
|
// Also guarded by native_immediates_threadsafe_mutex_. This can be used when |
1166 |
|
|
// trying to post tasks from other threads to an Environment, as the libuv |
1167 |
|
|
// handle for the immediate queues (task_queues_async_) may not be initialized |
1168 |
|
|
// yet or already have been destroyed. |
1169 |
|
|
bool task_queues_async_initialized_ = false; |
1170 |
|
|
|
1171 |
|
|
std::atomic<Environment**> interrupt_data_ {nullptr}; |
1172 |
|
|
void RequestInterruptFromV8(); |
1173 |
|
|
static void CheckImmediate(uv_check_t* handle); |
1174 |
|
|
|
1175 |
|
|
BindingDataStore bindings_; |
1176 |
|
|
|
1177 |
|
|
CleanupQueue cleanup_queue_; |
1178 |
|
|
bool started_cleanup_ = false; |
1179 |
|
|
|
1180 |
|
|
std::atomic_bool is_stopping_ { false }; |
1181 |
|
|
|
1182 |
|
|
std::unordered_set<int> unmanaged_fds_; |
1183 |
|
|
|
1184 |
|
|
std::function<void(Environment*, ExitCode)> process_exit_handler_{ |
1185 |
|
|
DefaultProcessExitHandlerInternal}; |
1186 |
|
|
|
1187 |
|
|
std::unique_ptr<Realm> principal_realm_ = nullptr; |
1188 |
|
|
|
1189 |
|
|
// Keeps the main script source alive is one was passed to LoadEnvironment(). |
1190 |
|
|
// We should probably find a way to just use plain `v8::String`s created from |
1191 |
|
|
// the source passed to LoadEnvironment() directly instead. |
1192 |
|
|
std::unique_ptr<v8::String::Value> main_utf16_; |
1193 |
|
|
|
1194 |
|
|
// Used by allocate_managed_buffer() and release_managed_buffer() to keep |
1195 |
|
|
// track of the BackingStore for a given pointer. |
1196 |
|
|
std::unordered_map<char*, std::unique_ptr<v8::BackingStore>> |
1197 |
|
|
released_allocated_buffers_; |
1198 |
|
|
}; |
1199 |
|
|
|
1200 |
|
|
} // namespace node |
1201 |
|
|
|
1202 |
|
|
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS |
1203 |
|
|
|
1204 |
|
|
#endif // SRC_ENV_H_ |