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
29#ifndef V8_EMBEDDER_DATA_ARRAY_SIZE
30// Defined using gn arg `v8_embedder_data_array_size`.
31#define V8_EMBEDDER_DATA_ARRAY_SIZE 5
32#endif
33
38 public:
39 ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
40 ExtensionConfiguration(int name_count, const char* names[])
41 : name_count_(name_count), names_(names) {}
42
43 const char** begin() const { return &names_[0]; }
44 const char** end() const { return &names_[name_count_]; }
45
46 private:
47 const int name_count_;
48 const char** names_;
49};
50
55class V8_EXPORT Context : public Data {
56 public:
70
76
123 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
125 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
126 DeserializeInternalFieldsCallback internal_fields_deserializer =
128 MicrotaskQueue* microtask_queue = nullptr,
129 DeserializeContextDataCallback context_data_deserializer =
131 DeserializeAPIWrapperCallback api_wrapper_deserializer =
133
171 Isolate* isolate, size_t context_snapshot_index,
172 DeserializeInternalFieldsCallback internal_fields_deserializer =
174 ExtensionConfiguration* extensions = nullptr,
175 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
176 MicrotaskQueue* microtask_queue = nullptr,
177 DeserializeContextDataCallback context_data_deserializer =
179 DeserializeAPIWrapperCallback api_wrapper_deserializer =
181
200 Isolate* isolate, Local<ObjectTemplate> global_template,
201 MaybeLocal<Value> global_object = MaybeLocal<Value>());
202
208
211
214
221 void Enter();
222
227 void Exit();
228
234 public:
245 Local<Object> obj, LocalVector<Object>& children_out) = 0;
246 };
247
264
267
270
275 enum EmbedderDataFields { kDebugIdIndex = 0 };
276
281
286 V8_INLINE Local<Data> GetEmbedderDataV2(int index);
287
293 V8_DEPRECATED("Use SetEmbedderDataV3 instead")
294 void SetEmbedderDataV2(int index, Local<Data> value);
295
300 void SetEmbedderDataV3(int index, Local<Data> value);
301
306 V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
307 V8_INLINE Local<Value> GetEmbedderData(int index);
308
315 Local<Object> GetExtrasBindingObject();
316
322 V8_DEPRECATED("Use SetEmbedderDataV3 instead")
323 void SetEmbedderData(int index, Local<Value> value);
324
331 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
333 void* GetAlignedPointerFromEmbedderData(int index,
335 template <typename T>
336 requires cppgc::IsGarbageCollectedTypeV<T>
337 V8_INLINE T* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
339
340 V8_DEPRECATED("Use SetAlignedPointerInEmbedderDataV2 instead")
341 void SetAlignedPointerInEmbedderData(int index, void* value,
343
344 template <typename T>
345 requires(!std::is_void_v<T>) && cppgc::IsGarbageCollectedTypeV<T>
346 V8_DEPRECATED("Use SetAlignedPointerInEmbedderDataV2 instead")
347 void SetAlignedPointerInEmbedderData(int index, T* value,
348 CppHeapPointerTag tag) {
349 SetAlignedPointerInEmbedderDataInternal(index, static_cast<void*>(value),
350 tag);
351 }
352
353 void SetAlignedPointerInEmbedderDataV2(int index, void* value,
355
356 template <typename T>
357 requires(!std::is_void_v<T>) && cppgc::IsGarbageCollectedTypeV<T>
358 void SetAlignedPointerInEmbedderDataV2(int index, T* value,
359 CppHeapPointerTag tag) {
360 SetAlignedPointerInEmbedderDataInternalV2(index, static_cast<void*>(value),
361 tag);
362 }
363
378
384
391
397
403 template <class T>
404 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
405
411 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
412 Local<Context> context);
414
419 int64_t (*)(Local<Context> context);
422
431 Local<Function> after_hook,
432 Local<Function> resolve_hook);
433
440 public:
441 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
442 context_->Enter();
443 }
444 V8_INLINE ~Scope() { context_->Exit(); }
445
446 private:
447 Local<Context> context_;
448 };
449
456 public:
461 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
463
464 private:
465 friend class internal::Isolate;
466
467 uintptr_t JSStackComparableAddressPrivate() const {
468 return js_stack_comparable_address_;
469 }
470
471 Local<Context> backup_incumbent_context_;
472 uintptr_t js_stack_comparable_address_ = 0;
473 const BackupIncumbentScope* prev_ = nullptr;
474 };
475
476 V8_INLINE static Context* Cast(Data* data);
477
478 private:
479 friend class Value;
480 friend class Script;
481 friend class Object;
482 friend class Function;
483
484 static void CheckCast(Data* obj);
485
487 size_t index);
488 Local<Value> SlowGetEmbedderData(int index);
489 Local<Data> SlowGetEmbedderDataV2(int index);
490 void* SlowGetAlignedPointerFromEmbedderData(int index, CppHeapPointerTag tag);
491 void SetAlignedPointerInEmbedderDataInternal(int index, void* value,
493 void SetAlignedPointerInEmbedderDataInternalV2(int index, void* value,
495};
496
497// --- Implementation ---
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<Value>::New(isolate, value);
521#else
522 return SlowGetEmbedderData(index);
523#endif
524}
525
527#ifndef V8_ENABLE_CHECKS
528 using A = internal::Address;
529 using I = internal::Internals;
531 A embedder_data =
532 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
533 int value_offset =
534 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
535#ifdef V8_COMPRESS_POINTERS
536 // The tagged payload lives in the low kTaggedSize half of the slot (at
537 // kTaggedPayloadOffset == 0). Read it as a 32-bit field so the correct half
538 // is picked on both little and big endian targets. A full-width read plus
539 // truncation would return the CppHeap pointer half on big endian.
540 uint32_t compressed = I::ReadRawField<uint32_t>(embedder_data, value_offset);
541 A value = I::DecompressTaggedField(embedder_data, compressed);
542#else
543 A value = I::ReadRawField<A>(embedder_data, value_offset);
544#endif
545
546 auto* isolate = I::GetCurrentIsolate();
547 return Local<Data>::New(isolate, value);
548#else
549 return SlowGetEmbedderDataV2(index);
550#endif
551}
552
554 Isolate* isolate, int index, EmbedderDataTypeTag tag) {
555 return GetAlignedPointerFromEmbedderData(index, tag);
556}
557
558template <typename T>
559 requires cppgc::IsGarbageCollectedTypeV<T>
561 CppHeapPointerTag tag) {
562#if !defined(V8_ENABLE_CHECKS)
563 using A = internal::Address;
564 using I = internal::Internals;
566 A embedder_data =
567 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
568 int value_offset = I::kEmbedderDataArrayHeaderSize +
569 (I::kEmbedderDataSlotSize * index) +
570 I::kEmbedderDataSlotCppHeapPointerOffset;
571 return internal::ReadCppHeapPointerField<T>(
572 isolate, embedder_data, value_offset, CppHeapPointerTagRange(tag, tag));
573#else
574 return static_cast<T*>(SlowGetAlignedPointerFromEmbedderData(index, tag));
575#endif
576}
577
578template <class T>
580 if (auto repr = GetDataFromSnapshotOnce(index);
582 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
583 return Local<T>::FromRepr(repr);
584 }
585 return {};
586}
587
589#ifdef V8_ENABLE_CHECKS
590 CheckCast(data);
591#endif
592 return static_cast<Context*>(data);
593}
594
595} // namespace v8
596
597#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:455
BackupIncumbentScope(Local< Context > backup_incumbent_context)
Definition: v8-context.h:233
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
Definition: v8-context.h:439
Scope(Local< Context > context)
Definition: v8-context.h:441
~Scope()
Definition: v8-context.h:444
Definition: v8-context.h:55
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:526
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:588
void SetAlignedPointerInEmbedderDataV2(int index, T *value, CppHeapPointerTag tag)
Definition: v8-context.h:358
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:579
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:275
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:499
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:553
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:412
void SetAlignedPointerInEmbedderDataV2(int index, void *value, EmbedderDataTypeTag tag)
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:419
Local< Object > Global()
Definition: v8-data.h:18
Definition: v8-context.h:37
ExtensionConfiguration(int name_count, const char *names[])
Definition: v8-context.h:40
ExtensionConfiguration()
Definition: v8-context.h:39
const char ** begin() const
Definition: v8-context.h:43
const char ** end() const
Definition: v8-context.h:44
Definition: v8-function.h:27
Definition: v8-isolate.h:305
Definition: v8-local-handle.h:590
Definition: v8-local-handle.h:359
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:441
Definition: v8-local-handle.h:730
Definition: v8-maybe.h:39
Definition: v8-microtask-queue.h:44
Definition: v8-object.h:265
Definition: v8-script.h:433
Definition: v8-value.h:32
Definition: v8-internal.h:969
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1838
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1792
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1793
Definition: allocation.h:39
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1553
Definition: libplatform.h:15
internal::TagRange< CppHeapPointerTag > CppHeapPointerTagRange
Definition: v8-sandbox.h:97
CppHeapPointerTag
Definition: v8-sandbox.h:28
uint16_t EmbedderDataTypeTag
Definition: v8-object.h:37
Definition: v8-snapshot.h:116
Definition: v8-snapshot.h:106
Definition: v8-snapshot.h:91
#define V8_EXPORT
Definition: v8config.h:875
#define V8_INLINE
Definition: v8config.h:519
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:632
#define V8_DEPRECATED(message)
Definition: v8config.h:624
#define V8_NODISCARD
Definition: v8config.h:711