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 v8 {
19
20class Function;
21class MicrotaskQueue;
22class Object;
23class ObjectTemplate;
24class Value;
25class String;
26
31 public:
32 ExtensionConfiguration() : name_count_(0), names_(nullptr) {}
33 ExtensionConfiguration(int name_count, const char* names[])
34 : name_count_(name_count), names_(names) {}
35
36 const char** begin() const { return &names_[0]; }
37 const char** end() const { return &names_[name_count_]; }
38
39 private:
40 const int name_count_;
41 const char** names_;
42};
43
48class V8_EXPORT Context : public Data {
49 public:
63
69
116 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
118 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
119 DeserializeInternalFieldsCallback internal_fields_deserializer =
121 MicrotaskQueue* microtask_queue = nullptr,
122 DeserializeContextDataCallback context_data_deserializer =
124 DeserializeAPIWrapperCallback api_wrapper_deserializer =
126
164 Isolate* isolate, size_t context_snapshot_index,
165 DeserializeInternalFieldsCallback internal_fields_deserializer =
167 ExtensionConfiguration* extensions = nullptr,
168 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
169 MicrotaskQueue* microtask_queue = nullptr,
170 DeserializeContextDataCallback context_data_deserializer =
172 DeserializeAPIWrapperCallback api_wrapper_deserializer =
174
193 Isolate* isolate, Local<ObjectTemplate> global_template,
194 MaybeLocal<Value> global_object = MaybeLocal<Value>());
195
201
204
207
214 void Enter();
215
220 void Exit();
221
227 public:
238 Local<Object> obj, LocalVector<Object>& children_out) = 0;
239 };
240
257
260
263
268 enum EmbedderDataFields { kDebugIdIndex = 0 };
269
274
279 V8_INLINE Local<Data> GetEmbedderDataV2(int index);
280
286 void SetEmbedderDataV2(int index, Local<Data> value);
287
292 V8_DEPRECATE_SOON("Use GetEmbedderDataV2 instead")
293 V8_INLINE Local<Value> GetEmbedderData(int index);
294
301 Local<Object> GetExtrasBindingObject();
302
308 V8_DEPRECATE_SOON("Use SetEmbedderDataV2 instead")
309 void SetEmbedderData(int index, Local<Value> value);
310
317 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
319 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index,
321
323 "Use GetAlignedPointerFromEmbedderData with EmbedderDataTypeTag "
324 "parameter instead.")
325 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate,
326 int index) {
327 return GetAlignedPointerFromEmbedderData(isolate, index,
329 }
330
332 "Use GetAlignedPointerFromEmbedderData with EmbedderDataTypeTag "
333 "parameter instead.")
334 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index) {
335 return GetAlignedPointerFromEmbedderData(index,
337 }
338
339 void SetAlignedPointerInEmbedderData(int index, void* value,
341
348 "Use SetAlignedPointerInEmbedderData with EmbedderDataTypeTag parameter "
349 "instead.")
350 void SetAlignedPointerInEmbedderData(int index, void* value) {
351 SetAlignedPointerInEmbedderData(index, value, kEmbedderDataTypeTagDefault);
352 }
353
368
374
381
387
393 template <class T>
394 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
395
401 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
402 Local<Context> context);
404
409 int64_t (*)(Local<Context> context);
412
421 Local<Function> after_hook,
422 Local<Function> resolve_hook);
423
430 public:
431 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
432 context_->Enter();
433 }
434 V8_INLINE ~Scope() { context_->Exit(); }
435
436 private:
437 Local<Context> context_;
438 };
439
446 public:
451 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
453
454 private:
455 friend class internal::Isolate;
456
457 uintptr_t JSStackComparableAddressPrivate() const {
458 return js_stack_comparable_address_;
459 }
460
461 Local<Context> backup_incumbent_context_;
462 uintptr_t js_stack_comparable_address_ = 0;
463 const BackupIncumbentScope* prev_ = nullptr;
464 };
465
466 V8_INLINE static Context* Cast(Data* data);
467
468 private:
469 friend class Value;
470 friend class Script;
471 friend class Object;
472 friend class Function;
473
474 static void CheckCast(Data* obj);
475
477 size_t index);
478 Local<Value> SlowGetEmbedderData(int index);
479 Local<Data> SlowGetEmbedderDataV2(int index);
480 void* SlowGetAlignedPointerFromEmbedderData(int index,
482};
483
484// --- Implementation ---
485
487#ifndef V8_ENABLE_CHECKS
488 using A = internal::Address;
489 using I = internal::Internals;
491 A embedder_data =
492 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
493 int value_offset =
494 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
495 A value = I::ReadRawField<A>(embedder_data, value_offset);
496#ifdef V8_COMPRESS_POINTERS
497 // We read the full pointer value and then decompress it in order to avoid
498 // dealing with potential endianness issues.
499 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
500#endif
501
502 auto* isolate = I::GetCurrentIsolate();
503 return Local<Value>::New(isolate, value);
504#else
505 return SlowGetEmbedderData(index);
506#endif
507}
508
510#ifndef V8_ENABLE_CHECKS
511 using A = internal::Address;
512 using I = internal::Internals;
514 A embedder_data =
515 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
516 int value_offset =
517 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
518 A value = I::ReadRawField<A>(embedder_data, value_offset);
519#ifdef V8_COMPRESS_POINTERS
520 // We read the full pointer value and then decompress it in order to avoid
521 // dealing with potential endianness issues.
522 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
523#endif
524
525 auto* isolate = I::GetCurrentIsolate();
526 return Local<Data>::New(isolate, value);
527#else
528 return SlowGetEmbedderDataV2(index);
529#endif
530}
531
534#if !defined(V8_ENABLE_CHECKS)
535 using A = internal::Address;
536 using I = internal::Internals;
538 A embedder_data =
539 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
540 int value_offset = I::kEmbedderDataArrayHeaderSize +
541 (I::kEmbedderDataSlotSize * index) +
542 I::kEmbedderDataSlotExternalPointerOffset;
543 return reinterpret_cast<void*>(I::ReadExternalPointerField(
544 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
545#else
546 return SlowGetAlignedPointerFromEmbedderData(index, tag);
547#endif
548}
549
552#if !defined(V8_ENABLE_CHECKS)
553 using A = internal::Address;
554 using I = internal::Internals;
556 A embedder_data =
557 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
558 int value_offset = I::kEmbedderDataArrayHeaderSize +
559 (I::kEmbedderDataSlotSize * index) +
560 I::kEmbedderDataSlotExternalPointerOffset;
561 Isolate* isolate = I::GetCurrentIsolateForSandbox();
562 return reinterpret_cast<void*>(I::ReadExternalPointerField(
563 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
564#else
565 return SlowGetAlignedPointerFromEmbedderData(index, tag);
566#endif
567}
568
569template <class T>
571 if (auto repr = GetDataFromSnapshotOnce(index);
573 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
574 return Local<T>::FromRepr(repr);
575 }
576 return {};
577}
578
580#ifdef V8_ENABLE_CHECKS
581 CheckCast(data);
582#endif
583 return static_cast<Context*>(data);
584}
585
586} // namespace v8
587
588#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:445
BackupIncumbentScope(Local< Context > backup_incumbent_context)
Definition: v8-context.h:226
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
Definition: v8-context.h:429
Scope(Local< Context > context)
Definition: v8-context.h:431
~Scope()
Definition: v8-context.h:434
Definition: v8-context.h:48
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:509
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:579
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:570
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:268
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:486
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:532
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:402
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:409
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:30
ExtensionConfiguration(int name_count, const char *names[])
Definition: v8-context.h:33
ExtensionConfiguration()
Definition: v8-context.h:32
const char ** begin() const
Definition: v8-context.h:36
const char ** end() const
Definition: v8-context.h:37
Definition: v8-function.h:27
Definition: v8-isolate.h:291
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:40
Definition: v8-object.h:266
Definition: v8-script.h:406
Definition: v8-value.h:32
Definition: v8-internal.h:990
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1867
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1821
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1822
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1582
Definition: libplatform.h:15
internal::ExternalPointerTag ToExternalPointerTag(v8::EmbedderDataTypeTag api_tag)
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:856
#define V8_INLINE
Definition: v8config.h:510
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:623
#define V8_DEPRECATED(message)
Definition: v8config.h:615
#define V8_NODISCARD
Definition: v8config.h:702