v8 10.2.154 (node 18.16.0)
V8 is Google's open source JavaScript engine
Loading...
Searching...
No Matches
v8-context.h
Go to the documentation of this file.
1// Copyright 2021 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 INCLUDE_V8_CONTEXT_H_
6#define INCLUDE_V8_CONTEXT_H_
7
8#include <stdint.h>
9
10#include "v8-data.h" // NOLINT(build/include_directory)
11#include "v8-local-handle.h" // NOLINT(build/include_directory)
12#include "v8-snapshot.h" // NOLINT(build/include_directory)
13#include "v8config.h" // NOLINT(build/include_directory)
14
15namespace v8 {
16
17class Function;
18class MicrotaskQueue;
19class Object;
20class ObjectTemplate;
21class Value;
22class String;
23
28 public:
29 ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
30 ExtensionConfiguration(int name_count, const char* names[])
31 : name_count_(name_count), names_(names) {}
32
33 const char** begin() const { return &names_[0]; }
34 const char** end() const { return &names_[name_count_]; }
35
36 private:
37 const int name_count_;
38 const char** names_;
39};
40
45class V8_EXPORT Context : public Data {
46 public:
60
66
86 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
88 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
89 DeserializeInternalFieldsCallback internal_fields_deserializer =
91 MicrotaskQueue* microtask_queue = nullptr);
92
112 Isolate* isolate, size_t context_snapshot_index,
113 DeserializeInternalFieldsCallback embedder_fields_deserializer =
115 ExtensionConfiguration* extensions = nullptr,
116 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
117 MicrotaskQueue* microtask_queue = nullptr);
118
137 Isolate* isolate, Local<ObjectTemplate> global_template,
138 MaybeLocal<Value> global_object = MaybeLocal<Value>());
139
145
148
151
158 void Enter();
159
164 void Exit();
165
168
171
176 enum EmbedderDataFields { kDebugIdIndex = 0 };
177
182
187 V8_INLINE Local<Value> GetEmbedderData(int index);
188
196
202 void SetEmbedderData(int index, Local<Value> value);
203
210 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
211
217 void SetAlignedPointerInEmbedderData(int index, void* value);
218
233
239
246
252 template <class T>
254
260 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
261 Local<Context> context);
263
269
275
284 Local<Function> after_hook,
285 Local<Function> resolve_hook);
286
292 public:
293 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
294 context_->Enter();
295 }
296 V8_INLINE ~Scope() { context_->Exit(); }
297
298 private:
299 Local<Context> context_;
300 };
301
308 public:
313 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
315
316 private:
317 friend class internal::Isolate;
318
319 uintptr_t JSStackComparableAddressPrivate() const {
320 return js_stack_comparable_address_;
321 }
322
323 Local<Context> backup_incumbent_context_;
324 uintptr_t js_stack_comparable_address_ = 0;
325 const BackupIncumbentScope* prev_ = nullptr;
326 };
327
328 V8_INLINE static Context* Cast(Data* data);
329
330 private:
331 friend class Value;
332 friend class Script;
333 friend class Object;
334 friend class Function;
335
336 static void CheckCast(Data* obj);
337
338 internal::Address* GetDataFromSnapshotOnce(size_t index);
339 Local<Value> SlowGetEmbedderData(int index);
340 void* SlowGetAlignedPointerFromEmbedderData(int index);
341};
342
343// --- Implementation ---
344
346#ifndef V8_ENABLE_CHECKS
347 using A = internal::Address;
348 using I = internal::Internals;
349 A ctx = *reinterpret_cast<const A*>(this);
350 A embedder_data =
351 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
352 int value_offset =
353 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
354 A value = I::ReadRawField<A>(embedder_data, value_offset);
355#ifdef V8_COMPRESS_POINTERS
356 // We read the full pointer value and then decompress it in order to avoid
357 // dealing with potential endiannes issues.
358 value =
359 I::DecompressTaggedAnyField(embedder_data, static_cast<uint32_t>(value));
360#endif
361 internal::Isolate* isolate = internal::IsolateFromNeverReadOnlySpaceObject(
362 *reinterpret_cast<A*>(this));
363 A* result = HandleScope::CreateHandle(isolate, value);
364 return Local<Value>(reinterpret_cast<Value*>(result));
365#else
366 return SlowGetEmbedderData(index);
367#endif
368}
369
371#if !defined(V8_ENABLE_CHECKS)
372 using A = internal::Address;
373 using I = internal::Internals;
374 A ctx = *reinterpret_cast<const A*>(this);
375 A embedder_data =
376 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
377 int value_offset =
378 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
379#ifdef V8_SANDBOXED_EXTERNAL_POINTERS
380 value_offset += I::kEmbedderDataSlotRawPayloadOffset;
381#endif
382 internal::Isolate* isolate = I::GetIsolateForSandbox(ctx);
383 return reinterpret_cast<void*>(
384 I::ReadExternalPointerField(isolate, embedder_data, value_offset,
386#else
387 return SlowGetAlignedPointerFromEmbedderData(index);
388#endif
389}
390
391template <class T>
393 T* data = reinterpret_cast<T*>(GetDataFromSnapshotOnce(index));
394 if (data) internal::PerformCastCheck(data);
395 return Local<T>(data);
396}
397
399#ifdef V8_ENABLE_CHECKS
400 CheckCast(data);
401#endif
402 return static_cast<Context*>(data);
403}
404
405} // namespace v8
406
407#endif // INCLUDE_V8_CONTEXT_H_
BackupIncumbentScope(Local< Context > backup_incumbent_context)
V8_INLINE ~Scope()
Definition v8-context.h:296
V8_INLINE Scope(Local< Context > context)
Definition v8-context.h:293
void SetEmbedderData(int index, Local< Value > value)
V8_INLINE Local< Value > GetEmbedderData(int index)
Definition v8-context.h:345
Local< Value > GetContinuationPreservedEmbedderData() const
static MaybeLocal< Object > NewRemoteContext(Isolate *isolate, Local< ObjectTemplate > global_template, MaybeLocal< Value > global_object=MaybeLocal< Value >())
Isolate * GetIsolate()
Local< Object > GetExtrasBindingObject()
Local< Object > Global()
bool IsCodeGenerationFromStringsAllowed() const
V8_INLINE void * GetAlignedPointerFromEmbedderData(int index)
Definition v8-context.h:370
Local< Value > GetSecurityToken()
void SetAlignedPointerInEmbedderData(int index, void *value)
void DetachGlobal()
void Enter()
void SetContinuationPreservedEmbedderData(Local< Value > context)
void SetAbortScriptExecution(AbortScriptExecutionCallback callback)
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition v8-context.h:261
V8_INLINE MaybeLocal< T > GetDataFromSnapshotOnce(size_t index)
void AllowCodeGenerationFromStrings(bool allow)
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
void UseDefaultSecurityToken()
MicrotaskQueue * GetMicrotaskQueue()
static V8_INLINE Context * Cast(Data *data)
Definition v8-context.h:398
void SetSecurityToken(Local< Value > token)
static MaybeLocal< Context > FromSnapshot(Isolate *isolate, size_t context_snapshot_index, DeserializeInternalFieldsCallback embedder_fields_deserializer=DeserializeInternalFieldsCallback(), ExtensionConfiguration *extensions=nullptr, MaybeLocal< Value > global_object=MaybeLocal< Value >(), MicrotaskQueue *microtask_queue=nullptr)
static Local< Context > New(Isolate *isolate, ExtensionConfiguration *extensions=nullptr, MaybeLocal< ObjectTemplate > global_template=MaybeLocal< ObjectTemplate >(), MaybeLocal< Value > global_object=MaybeLocal< Value >(), DeserializeInternalFieldsCallback internal_fields_deserializer=DeserializeInternalFieldsCallback(), MicrotaskQueue *microtask_queue=nullptr)
void SetPromiseHooks(Local< Function > init_hook, Local< Function > before_hook, Local< Function > after_hook, Local< Function > resolve_hook)
uint32_t GetNumberOfEmbedderDataFields()
const char ** begin() const
Definition v8-context.h:33
ExtensionConfiguration(int name_count, const char *names[])
Definition v8-context.h:30
const char ** end() const
Definition v8-context.h:34
static internal::Address * CreateHandle(internal::Isolate *isolate, internal::Address value)
V8_EXPORT internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition v8-internal.h:29
@ kEmbedderDataSlotPayloadTag
V8_INLINE void PerformCastCheck(T *data)
#define V8_EXPORT
Definition v8config.h:578
#define V8_INLINE
Definition v8config.h:425
#define V8_NODISCARD
Definition v8config.h:513