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 <type_traits>
11#include <vector>
12
13#include "cppgc/type-traits.h" // NOLINT(build/include_directory)
14#include "v8-data.h" // NOLINT(build/include_directory)
15#include "v8-local-handle.h" // NOLINT(build/include_directory)
16#include "v8-maybe.h" // NOLINT(build/include_directory)
17#include "v8-snapshot.h" // NOLINT(build/include_directory)
18#include "v8config.h" // NOLINT(build/include_directory)
19
20namespace v8 {
21
22class Function;
23class MicrotaskQueue;
24class Object;
25class ObjectTemplate;
26class Value;
27class String;
28
33 public:
34 ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
35 ExtensionConfiguration(int name_count, const char* names[])
36 : name_count_(name_count), names_(names) {}
37
38 const char** begin() const { return &names_[0]; }
39 const char** end() const { return &names_[name_count_]; }
40
41 private:
42 const int name_count_;
43 const char** names_;
44};
45
50class V8_EXPORT Context : public Data {
51 public:
65
71
118 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
120 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
121 DeserializeInternalFieldsCallback internal_fields_deserializer =
123 MicrotaskQueue* microtask_queue = nullptr,
124 DeserializeContextDataCallback context_data_deserializer =
126 DeserializeAPIWrapperCallback api_wrapper_deserializer =
128
166 Isolate* isolate, size_t context_snapshot_index,
167 DeserializeInternalFieldsCallback internal_fields_deserializer =
169 ExtensionConfiguration* extensions = nullptr,
170 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
171 MicrotaskQueue* microtask_queue = nullptr,
172 DeserializeContextDataCallback context_data_deserializer =
174 DeserializeAPIWrapperCallback api_wrapper_deserializer =
176
195 Isolate* isolate, Local<ObjectTemplate> global_template,
196 MaybeLocal<Value> global_object = MaybeLocal<Value>());
197
203
206
209
216 void Enter();
217
222 void Exit();
223
229 public:
240 Local<Object> obj, LocalVector<Object>& children_out) = 0;
241 };
242
259
262
265
270 enum EmbedderDataFields { kDebugIdIndex = 0 };
271
276
281 V8_INLINE Local<Data> GetEmbedderDataV2(int index);
282
288 void SetEmbedderDataV2(int index, Local<Data> value);
289
294 V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
295 V8_INLINE Local<Value> GetEmbedderData(int index);
296
303 Local<Object> GetExtrasBindingObject();
304
310 V8_DEPRECATE_SOON("Use SetEmbedderDataV2 instead")
311 void SetEmbedderData(int index, Local<Value> value);
312
319 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
321 void* GetAlignedPointerFromEmbedderData(int index,
323 template <typename T>
324 requires cppgc::IsGarbageCollectedTypeV<T>
325 V8_INLINE T* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
327
328 void SetAlignedPointerInEmbedderData(int index, void* value,
330
331 template <typename T>
332 requires(!std::is_void_v<T>) && cppgc::IsGarbageCollectedTypeV<T>
333 void SetAlignedPointerInEmbedderData(int index, T* value,
334 CppHeapPointerTag tag) {
335 SetAlignedPointerInEmbedderDataInternal(index, static_cast<void*>(value),
336 tag);
337 }
338
353
359
366
372
378 template <class T>
379 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
380
386 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
387 Local<Context> context);
389
394 int64_t (*)(Local<Context> context);
397
406 Local<Function> after_hook,
407 Local<Function> resolve_hook);
408
415 public:
416 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
417 context_->Enter();
418 }
419 V8_INLINE ~Scope() { context_->Exit(); }
420
421 private:
422 Local<Context> context_;
423 };
424
431 public:
436 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
438
439 private:
440 friend class internal::Isolate;
441
442 uintptr_t JSStackComparableAddressPrivate() const {
443 return js_stack_comparable_address_;
444 }
445
446 Local<Context> backup_incumbent_context_;
447 uintptr_t js_stack_comparable_address_ = 0;
448 const BackupIncumbentScope* prev_ = nullptr;
449 };
450
451 V8_INLINE static Context* Cast(Data* data);
452
453 private:
454 friend class Value;
455 friend class Script;
456 friend class Object;
457 friend class Function;
458
459 static void CheckCast(Data* obj);
460
462 size_t index);
463 Local<Value> SlowGetEmbedderData(int index);
464 Local<Data> SlowGetEmbedderDataV2(int index);
465 void* SlowGetAlignedPointerFromEmbedderData(int index, CppHeapPointerTag tag);
466 void SetAlignedPointerInEmbedderDataInternal(int index, void* value,
468};
469
470// --- Implementation ---
471
473#ifndef V8_ENABLE_CHECKS
474 using A = internal::Address;
475 using I = internal::Internals;
477 A embedder_data =
478 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
479 int value_offset =
480 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
481#ifdef V8_COMPRESS_POINTERS
482 // The tagged payload lives in the low kTaggedSize half of the slot (at
483 // kTaggedPayloadOffset == 0). Read it as a 32-bit field so the correct half
484 // is picked on both little and big endian targets. A full width read plus
485 // truncation would return the CppHeap pointer half on big endian.
486 uint32_t compressed = I::ReadRawField<uint32_t>(embedder_data, value_offset);
487 A value = I::DecompressTaggedField(embedder_data, compressed);
488#else
489 A value = I::ReadRawField<A>(embedder_data, value_offset);
490#endif
491
492 auto* isolate = I::GetCurrentIsolate();
493 return Local<Value>::New(isolate, value);
494#else
495 return SlowGetEmbedderData(index);
496#endif
497}
498
500#ifndef V8_ENABLE_CHECKS
501 using A = internal::Address;
502 using I = internal::Internals;
504 A embedder_data =
505 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
506 int value_offset =
507 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
508#ifdef V8_COMPRESS_POINTERS
509 // The tagged payload lives in the low kTaggedSize half of the slot (at
510 // kTaggedPayloadOffset == 0). Read it as a 32-bit field so the correct half
511 // is picked on both little and big endian targets. A full-width read plus
512 // truncation would return the CppHeap pointer half on big endian.
513 uint32_t compressed = I::ReadRawField<uint32_t>(embedder_data, value_offset);
514 A value = I::DecompressTaggedField(embedder_data, compressed);
515#else
516 A value = I::ReadRawField<A>(embedder_data, value_offset);
517#endif
518
519 auto* isolate = I::GetCurrentIsolate();
520 return Local<Data>::New(isolate, value);
521#else
522 return SlowGetEmbedderDataV2(index);
523#endif
524}
525
527 Isolate* isolate, int index, EmbedderDataTypeTag tag) {
528 return GetAlignedPointerFromEmbedderData(index, tag);
529}
530
531template <typename T>
532 requires cppgc::IsGarbageCollectedTypeV<T>
534 CppHeapPointerTag tag) {
535#if !defined(V8_ENABLE_CHECKS)
536 using A = internal::Address;
537 using I = internal::Internals;
539 A embedder_data =
540 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
541 int value_offset = I::kEmbedderDataArrayHeaderSize +
542 (I::kEmbedderDataSlotSize * index) +
543 I::kEmbedderDataSlotCppHeapPointerOffset;
544 return internal::ReadCppHeapPointerField<T>(
545 isolate, embedder_data, value_offset, CppHeapPointerTagRange(tag, tag));
546#else
547 return static_cast<T*>(SlowGetAlignedPointerFromEmbedderData(index, tag));
548#endif
549}
550
551template <class T>
553 if (auto repr = GetDataFromSnapshotOnce(index);
555 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
556 return Local<T>::FromRepr(repr);
557 }
558 return {};
559}
560
562#ifdef V8_ENABLE_CHECKS
563 CheckCast(data);
564#endif
565 return static_cast<Context*>(data);
566}
567
568} // namespace v8
569
570#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:430
BackupIncumbentScope(Local< Context > backup_incumbent_context)
Definition: v8-context.h:228
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
Definition: v8-context.h:414
Scope(Local< Context > context)
Definition: v8-context.h:416
~Scope()
Definition: v8-context.h:419
Definition: v8-context.h:50
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:499
static MaybeLocal< Object > NewRemoteContext(Isolate *isolate, Local< ObjectTemplate > global_template, MaybeLocal< Value > global_object=MaybeLocal< Value >())
void SetErrorMessageForWasmCodeGeneration(Local< String > message)
void SetPromiseHooks(Local< Function > init_hook, Local< Function > before_hook, Local< Function > after_hook, Local< Function > resolve_hook)
void SetSecurityToken(Local< Value > token)
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, DeserializeContextDataCallback context_data_deserializer=DeserializeContextDataCallback(), DeserializeAPIWrapperCallback api_wrapper_deserializer=DeserializeAPIWrapperCallback())
void SetTemporalHostSystemUTCEpochNanosecondsCallback(TemporalHostSystemUTCEpochNanosecondsCallback callback)
bool IsCodeGenerationFromStringsAllowed() const
static Context * Cast(Data *data)
Definition: v8-context.h:561
MicrotaskQueue * GetMicrotaskQueue()
void Enter()
void SetAbortScriptExecution(AbortScriptExecutionCallback callback)
Maybe< void > DeepFreeze(DeepFreezeDelegate *delegate=nullptr)
bool HasTemplateLiteralObject(Local< Value > object)
void AllowCodeGenerationFromStrings(bool allow)
Local< Value > GetSecurityToken()
MaybeLocal< T > GetDataFromSnapshotOnce(size_t index)
Definition: v8-context.h:552
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:270
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:472
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:526
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:387
static MaybeLocal< Context > FromSnapshot(Isolate *isolate, size_t context_snapshot_index, DeserializeInternalFieldsCallback internal_fields_deserializer=DeserializeInternalFieldsCallback(), ExtensionConfiguration *extensions=nullptr, MaybeLocal< Value > global_object=MaybeLocal< Value >(), MicrotaskQueue *microtask_queue=nullptr, DeserializeContextDataCallback context_data_deserializer=DeserializeContextDataCallback(), DeserializeAPIWrapperCallback api_wrapper_deserializer=DeserializeAPIWrapperCallback())
int64_t(*)(Local< Context > context) TemporalHostSystemUTCEpochNanosecondsCallback
Definition: v8-context.h:394
void SetEmbedderDataV2(int index, Local< Data > value)
Local< Object > Global()
Definition: v8-data.h:18
Definition: v8-context.h:32
ExtensionConfiguration(int name_count, const char *names[])
Definition: v8-context.h:35
ExtensionConfiguration()
Definition: v8-context.h:34
const char ** begin() const
Definition: v8-context.h:38
const char ** end() const
Definition: v8-context.h:39
Definition: v8-function.h:27
Definition: v8-isolate.h:292
Definition: v8-local-handle.h:594
Definition: v8-local-handle.h:366
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:448
Definition: v8-local-handle.h:734
Definition: v8-maybe.h:39
Definition: v8-microtask-queue.h:44
Definition: v8-object.h:267
Definition: v8-script.h:433
Definition: v8-value.h:32
Definition: v8-internal.h:975
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1846
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1800
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1801
Definition: allocation.h:38
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1561
Definition: libplatform.h:15
internal::TagRange< CppHeapPointerTag > CppHeapPointerTagRange
Definition: v8-sandbox.h:85
CppHeapPointerTag
Definition: v8-sandbox.h:28
uint16_t EmbedderDataTypeTag
Definition: v8-object.h:34
Definition: v8-snapshot.h:116
Definition: v8-snapshot.h:106
Definition: v8-snapshot.h:91
#define V8_EXPORT
Definition: v8config.h:867
#define V8_INLINE
Definition: v8config.h:511
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:624
#define V8_NODISCARD
Definition: v8config.h:703