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
292 void SetEmbedderDataV3(int index, Local<Data> value);
293
298 V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
299 V8_INLINE Local<Value> GetEmbedderData(int index);
300
307 Local<Object> GetExtrasBindingObject();
308
315 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
317 void* GetAlignedPointerFromEmbedderData(int index,
319 template <typename T>
320 requires cppgc::IsGarbageCollectedTypeV<T>
321 V8_INLINE T* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
323
324 void SetAlignedPointerInEmbedderDataV2(int index, void* value,
326
327 template <typename T>
328 requires(!std::is_void_v<T>) && cppgc::IsGarbageCollectedTypeV<T>
329 void SetAlignedPointerInEmbedderDataV2(int index, T* value,
330 CppHeapPointerTag tag) {
331 SetAlignedPointerInEmbedderDataInternalV2(index, static_cast<void*>(value),
332 tag);
333 }
334
349
355
362
368
374 template <class T>
375 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
376
382 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
383 Local<Context> context);
385
390 int64_t (*)(Local<Context> context);
393
402 Local<Function> after_hook,
403 Local<Function> resolve_hook);
404
411 public:
412 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
413 context_->Enter();
414 }
415 V8_INLINE ~Scope() { context_->Exit(); }
416
417 private:
418 Local<Context> context_;
419 };
420
427 public:
432 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
434
435 private:
436 friend class internal::Isolate;
437
438 uintptr_t JSStackComparableAddressPrivate() const {
439 return js_stack_comparable_address_;
440 }
441
442 Local<Context> backup_incumbent_context_;
443 uintptr_t js_stack_comparable_address_ = 0;
444 const BackupIncumbentScope* prev_ = nullptr;
445 };
446
447 V8_INLINE static Context* Cast(Data* data);
448
449 private:
450 friend class Value;
451 friend class Script;
452 friend class Object;
453 friend class Function;
454
455 static void CheckCast(Data* obj);
456
458 size_t index);
459 Local<Value> SlowGetEmbedderData(int index);
460 Local<Data> SlowGetEmbedderDataV2(int index);
461 void* SlowGetAlignedPointerFromEmbedderData(int index, CppHeapPointerTag tag);
462 void SetAlignedPointerInEmbedderDataInternalV2(int index, void* value,
464};
465
466// --- Implementation ---
467
469#ifndef V8_ENABLE_CHECKS
470 using A = internal::Address;
471 using I = internal::Internals;
473 int value_offset =
474 I::kNativeContextEmbedderDataOffset + (I::kEmbedderDataSlotSize * index);
475#ifdef V8_COMPRESS_POINTERS
476 // The tagged payload lives in the low kTaggedSize half of the slot (at
477 // kTaggedPayloadOffset == 0). Read it as a 32-bit field so the correct half
478 // is picked on both little and big endian targets. A full width read plus
479 // truncation would return the CppHeap pointer half on big endian.
480 uint32_t compressed = I::ReadRawField<uint32_t>(ctx, value_offset);
481 A value = I::DecompressTaggedField(ctx, compressed);
482#else
483 A value = I::ReadRawField<A>(ctx, value_offset);
484#endif
485
486 auto* isolate = I::GetCurrentIsolate();
487 return Local<Value>::New(isolate, value);
488#else
489 return SlowGetEmbedderData(index);
490#endif
491}
492
494#ifndef V8_ENABLE_CHECKS
495 using A = internal::Address;
496 using I = internal::Internals;
498 int value_offset =
499 I::kNativeContextEmbedderDataOffset + (I::kEmbedderDataSlotSize * index);
500#ifdef V8_COMPRESS_POINTERS
501 // The tagged payload lives in the low kTaggedSize half of the slot (at
502 // kTaggedPayloadOffset == 0). Read it as a 32-bit field so the correct half
503 // is picked on both little and big endian targets. A full-width read plus
504 // truncation would return the CppHeap pointer half on big endian.
505 uint32_t compressed = I::ReadRawField<uint32_t>(ctx, value_offset);
506 A value = I::DecompressTaggedField(ctx, compressed);
507#else
508 A value = I::ReadRawField<A>(ctx, value_offset);
509#endif
510
511 auto* isolate = I::GetCurrentIsolate();
512 return Local<Data>::New(isolate, value);
513#else
514 return SlowGetEmbedderDataV2(index);
515#endif
516}
517
519 Isolate* isolate, int index, EmbedderDataTypeTag tag) {
520 return GetAlignedPointerFromEmbedderData(index, tag);
521}
522
523template <typename T>
524 requires cppgc::IsGarbageCollectedTypeV<T>
526 CppHeapPointerTag tag) {
527#if !defined(V8_ENABLE_CHECKS)
528 using A = internal::Address;
529 using I = internal::Internals;
531 int value_offset = I::kNativeContextEmbedderDataOffset +
532 (I::kEmbedderDataSlotSize * index) +
533 I::kEmbedderDataSlotCppHeapPointerOffset;
534 return internal::ReadCppHeapPointerField<T>(isolate, ctx, value_offset,
535 CppHeapPointerTagRange(tag, tag));
536#else
537 return static_cast<T*>(SlowGetAlignedPointerFromEmbedderData(index, tag));
538#endif
539}
540
541template <class T>
543 if (auto repr = GetDataFromSnapshotOnce(index);
545 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
546 return Local<T>::FromRepr(repr);
547 }
548 return {};
549}
550
552#ifdef V8_ENABLE_CHECKS
553 CheckCast(data);
554#endif
555 return static_cast<Context*>(data);
556}
557
558} // namespace v8
559
560#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:426
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:410
Scope(Local< Context > context)
Definition: v8-context.h:412
~Scope()
Definition: v8-context.h:415
Definition: v8-context.h:55
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:493
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:551
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:542
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:468
void SetMicrotaskQueue(MicrotaskQueue *queue)
void SetEmbedderDataV3(int index, Local< Data > value)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:518
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:383
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:390
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:299
Definition: v8-local-handle.h:584
Definition: v8-local-handle.h:356
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:438
Definition: v8-local-handle.h:724
Definition: v8-maybe.h:39
Definition: v8-microtask-queue.h:44
Definition: v8-object.h:269
Definition: v8-script.h:432
Definition: v8-value.h:32
Definition: v8-internal.h:965
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1836
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1790
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1791
Definition: allocation.h:39
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1551
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_NODISCARD
Definition: v8config.h:711