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 <vector>
11
12#include "v8-data.h" // NOLINT(build/include_directory)
13#include "v8-local-handle.h" // NOLINT(build/include_directory)
14#include "v8-maybe.h" // NOLINT(build/include_directory)
15#include "v8-snapshot.h" // NOLINT(build/include_directory)
16#include "v8config.h" // NOLINT(build/include_directory)
17
18namespace cppgc {
19template <typename T>
20class GarbageCollected;
21} // namespace cppgc
22
23namespace v8 {
24
25class Function;
26class MicrotaskQueue;
27class Object;
28class ObjectTemplate;
29class Value;
30class String;
31
36 public:
37 ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
38 ExtensionConfiguration(int name_count, const char* names[])
39 : name_count_(name_count), names_(names) {}
40
41 const char** begin() const { return &names_[0]; }
42 const char** end() const { return &names_[name_count_]; }
43
44 private:
45 const int name_count_;
46 const char** names_;
47};
48
53class V8_EXPORT Context : public Data {
54 public:
68
74
121 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
123 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
124 DeserializeInternalFieldsCallback internal_fields_deserializer =
126 MicrotaskQueue* microtask_queue = nullptr,
127 DeserializeContextDataCallback context_data_deserializer =
129 DeserializeAPIWrapperCallback api_wrapper_deserializer =
131
169 Isolate* isolate, size_t context_snapshot_index,
170 DeserializeInternalFieldsCallback internal_fields_deserializer =
172 ExtensionConfiguration* extensions = nullptr,
173 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
174 MicrotaskQueue* microtask_queue = nullptr,
175 DeserializeContextDataCallback context_data_deserializer =
177 DeserializeAPIWrapperCallback api_wrapper_deserializer =
179
198 Isolate* isolate, Local<ObjectTemplate> global_template,
199 MaybeLocal<Value> global_object = MaybeLocal<Value>());
200
206
209
212
219 void Enter();
220
225 void Exit();
226
232 public:
243 Local<Object> obj, LocalVector<Object>& children_out) = 0;
244 };
245
262
265
268
273 enum EmbedderDataFields { kDebugIdIndex = 0 };
274
279
284 V8_INLINE Local<Data> GetEmbedderDataV2(int index);
285
291 void SetEmbedderDataV2(int index, Local<Data> value);
292
297 V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
298 V8_INLINE Local<Value> GetEmbedderData(int index);
299
306 Local<Object> GetExtrasBindingObject();
307
313 V8_DEPRECATE_SOON("Use SetEmbedderDataV2 instead")
314 void SetEmbedderData(int index, Local<Value> value);
315
322 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
324 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index,
326 template <typename T>
327 requires std::is_base_of_v<cppgc::GarbageCollected<T>, T>
328 V8_INLINE T* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
329 CppHeapPointerTag tag) {
330 // TODO(ahaas): This is a temporary implementation, the actual
331 // implementation will follow with the refactoring of EmbedderDataSlots.
332 // The refactoring will regress the existing API, as the fast path will move
333 // to this new API.
334 // By using this temporary implementation, blink's ScriptState can already
335 // switch to the new API, and thereby switch from the old fast path to the
336 // new fast path directly.
337 return static_cast<T*>(GetAlignedPointerFromEmbedderData(
338 isolate, index, kEmbedderDataTypeTagDefault));
339 }
340
341 void SetAlignedPointerInEmbedderData(int index, void* value,
343
344 template <typename T>
345 requires std::is_base_of_v<cppgc::GarbageCollected<T>, T>
346 void SetAlignedPointerInEmbedderData(int index, T* value,
347 CppHeapPointerTag tag) {
348 // TODO(ahaas): This is a temporary implementation, the actual
349 // implementation will follow with the refactoring of EmbedderDataSlots.
350 // The refactoring will regress the existing API, as the fast path will move
351 // to this new API.
352 // By using this temporary implementation, blink's ScriptState can already
353 // switch to the new API, and thereby switch from the old fast path to the
354 // new fast path directly.
355 SetAlignedPointerInEmbedderData(index, value, kEmbedderDataTypeTagDefault);
356 }
357
372
378
385
391
397 template <class T>
398 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
399
405 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
406 Local<Context> context);
408
413 int64_t (*)(Local<Context> context);
416
425 Local<Function> after_hook,
426 Local<Function> resolve_hook);
427
434 public:
435 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
436 context_->Enter();
437 }
438 V8_INLINE ~Scope() { context_->Exit(); }
439
440 private:
441 Local<Context> context_;
442 };
443
450 public:
455 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
457
458 private:
459 friend class internal::Isolate;
460
461 uintptr_t JSStackComparableAddressPrivate() const {
462 return js_stack_comparable_address_;
463 }
464
465 Local<Context> backup_incumbent_context_;
466 uintptr_t js_stack_comparable_address_ = 0;
467 const BackupIncumbentScope* prev_ = nullptr;
468 };
469
470 V8_INLINE static Context* Cast(Data* data);
471
472 private:
473 friend class Value;
474 friend class Script;
475 friend class Object;
476 friend class Function;
477
478 static void CheckCast(Data* obj);
479
481 size_t index);
482 Local<Value> SlowGetEmbedderData(int index);
483 Local<Data> SlowGetEmbedderDataV2(int index);
484 void* SlowGetAlignedPointerFromEmbedderData(int index,
486};
487
488// --- Implementation ---
489
491#ifndef V8_ENABLE_CHECKS
492 using A = internal::Address;
493 using I = internal::Internals;
495 A embedder_data =
496 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
497 int value_offset =
498 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
499 A value = I::ReadRawField<A>(embedder_data, value_offset);
500#ifdef V8_COMPRESS_POINTERS
501 // We read the full pointer value and then decompress it in order to avoid
502 // dealing with potential endianness issues.
503 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
504#endif
505
506 auto* isolate = I::GetCurrentIsolate();
507 return Local<Value>::New(isolate, value);
508#else
509 return SlowGetEmbedderData(index);
510#endif
511}
512
514#ifndef V8_ENABLE_CHECKS
515 using A = internal::Address;
516 using I = internal::Internals;
518 A embedder_data =
519 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
520 int value_offset =
521 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
522 A value = I::ReadRawField<A>(embedder_data, value_offset);
523#ifdef V8_COMPRESS_POINTERS
524 // We read the full pointer value and then decompress it in order to avoid
525 // dealing with potential endianness issues.
526 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
527#endif
528
529 auto* isolate = I::GetCurrentIsolate();
530 return Local<Data>::New(isolate, value);
531#else
532 return SlowGetEmbedderDataV2(index);
533#endif
534}
535
538#if !defined(V8_ENABLE_CHECKS)
539 using A = internal::Address;
540 using I = internal::Internals;
542 A embedder_data =
543 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
544 int value_offset = I::kEmbedderDataArrayHeaderSize +
545 (I::kEmbedderDataSlotSize * index) +
546 I::kEmbedderDataSlotExternalPointerOffset;
547 return reinterpret_cast<void*>(I::ReadExternalPointerField(
548 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
549#else
550 return SlowGetAlignedPointerFromEmbedderData(index, tag);
551#endif
552}
553
556#if !defined(V8_ENABLE_CHECKS)
557 using A = internal::Address;
558 using I = internal::Internals;
560 A embedder_data =
561 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
562 int value_offset = I::kEmbedderDataArrayHeaderSize +
563 (I::kEmbedderDataSlotSize * index) +
564 I::kEmbedderDataSlotExternalPointerOffset;
565 Isolate* isolate = I::GetCurrentIsolateForSandbox();
566 return reinterpret_cast<void*>(I::ReadExternalPointerField(
567 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
568#else
569 return SlowGetAlignedPointerFromEmbedderData(index, tag);
570#endif
571}
572
573template <class T>
575 if (auto repr = GetDataFromSnapshotOnce(index);
577 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
578 return Local<T>::FromRepr(repr);
579 }
580 return {};
581}
582
584#ifdef V8_ENABLE_CHECKS
585 CheckCast(data);
586#endif
587 return static_cast<Context*>(data);
588}
589
590} // namespace v8
591
592#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:449
BackupIncumbentScope(Local< Context > backup_incumbent_context)
Definition: v8-context.h:231
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
Definition: v8-context.h:433
Scope(Local< Context > context)
Definition: v8-context.h:435
~Scope()
Definition: v8-context.h:438
Definition: v8-context.h:53
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:513
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
void SetAlignedPointerInEmbedderData(int index, T *value, CppHeapPointerTag tag)
Definition: v8-context.h:346
static Context * Cast(Data *data)
Definition: v8-context.h:583
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:574
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:273
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:490
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:536
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:406
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:413
void SetEmbedderDataV2(int index, Local< Data > value)
Local< Object > Global()
void SetAlignedPointerInEmbedderData(int index, void *value, EmbedderDataTypeTag tag)
Definition: v8-data.h:18
Definition: v8-context.h:35
ExtensionConfiguration(int name_count, const char *names[])
Definition: v8-context.h:38
ExtensionConfiguration()
Definition: v8-context.h:37
const char ** begin() const
Definition: v8-context.h:41
const char ** end() const
Definition: v8-context.h:42
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:53
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:1850
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1804
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1805
Definition: allocation.h:38
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1565
Definition: libplatform.h:15
internal::ExternalPointerTag ToExternalPointerTag(v8::EmbedderDataTypeTag api_tag)
CppHeapPointerTag
Definition: v8-sandbox.h:28
constexpr EmbedderDataTypeTag kEmbedderDataTypeTagDefault
Definition: v8-object.h:36
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