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
413 Local<Function> after_hook,
414 Local<Function> resolve_hook);
415
422 public:
423 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
424 context_->Enter();
425 }
426 V8_INLINE ~Scope() { context_->Exit(); }
427
428 private:
429 Local<Context> context_;
430 };
431
438 public:
443 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
445
446 private:
447 friend class internal::Isolate;
448
449 uintptr_t JSStackComparableAddressPrivate() const {
450 return js_stack_comparable_address_;
451 }
452
453 Local<Context> backup_incumbent_context_;
454 uintptr_t js_stack_comparable_address_ = 0;
455 const BackupIncumbentScope* prev_ = nullptr;
456 };
457
458 V8_INLINE static Context* Cast(Data* data);
459
460 private:
461 friend class Value;
462 friend class Script;
463 friend class Object;
464 friend class Function;
465
466 static void CheckCast(Data* obj);
467
469 size_t index);
470 Local<Value> SlowGetEmbedderData(int index);
471 Local<Data> SlowGetEmbedderDataV2(int index);
472 void* SlowGetAlignedPointerFromEmbedderData(int index,
474};
475
476// --- Implementation ---
477
479#ifndef V8_ENABLE_CHECKS
480 using A = internal::Address;
481 using I = internal::Internals;
483 A embedder_data =
484 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
485 int value_offset =
486 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
487 A value = I::ReadRawField<A>(embedder_data, value_offset);
488#ifdef V8_COMPRESS_POINTERS
489 // We read the full pointer value and then decompress it in order to avoid
490 // dealing with potential endianness issues.
491 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
492#endif
493
494 auto* isolate = I::GetCurrentIsolate();
495 return Local<Value>::New(isolate, value);
496#else
497 return SlowGetEmbedderData(index);
498#endif
499}
500
502#ifndef V8_ENABLE_CHECKS
503 using A = internal::Address;
504 using I = internal::Internals;
506 A embedder_data =
507 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
508 int value_offset =
509 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
510 A value = I::ReadRawField<A>(embedder_data, value_offset);
511#ifdef V8_COMPRESS_POINTERS
512 // We read the full pointer value and then decompress it in order to avoid
513 // dealing with potential endianness issues.
514 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
515#endif
516
517 auto* isolate = I::GetCurrentIsolate();
518 return Local<Data>::New(isolate, value);
519#else
520 return SlowGetEmbedderDataV2(index);
521#endif
522}
523
526#if !defined(V8_ENABLE_CHECKS)
527 using A = internal::Address;
528 using I = internal::Internals;
530 A embedder_data =
531 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
532 int value_offset = I::kEmbedderDataArrayHeaderSize +
533 (I::kEmbedderDataSlotSize * index) +
534 I::kEmbedderDataSlotExternalPointerOffset;
535 return reinterpret_cast<void*>(I::ReadExternalPointerField(
536 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
537#else
538 return SlowGetAlignedPointerFromEmbedderData(index, tag);
539#endif
540}
541
544#if !defined(V8_ENABLE_CHECKS)
545 using A = internal::Address;
546 using I = internal::Internals;
548 A embedder_data =
549 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
550 int value_offset = I::kEmbedderDataArrayHeaderSize +
551 (I::kEmbedderDataSlotSize * index) +
552 I::kEmbedderDataSlotExternalPointerOffset;
553 Isolate* isolate = I::GetCurrentIsolateForSandbox();
554 return reinterpret_cast<void*>(I::ReadExternalPointerField(
555 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
556#else
557 return SlowGetAlignedPointerFromEmbedderData(index, tag);
558#endif
559}
560
561template <class T>
563 if (auto repr = GetDataFromSnapshotOnce(index);
565 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
566 return Local<T>::FromRepr(repr);
567 }
568 return {};
569}
570
572#ifdef V8_ENABLE_CHECKS
573 CheckCast(data);
574#endif
575 return static_cast<Context*>(data);
576}
577
578} // namespace v8
579
580#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:437
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:421
Scope(Local< Context > context)
Definition: v8-context.h:423
~Scope()
Definition: v8-context.h:426
Definition: v8-context.h:48
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:501
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())
bool IsCodeGenerationFromStringsAllowed() const
static Context * Cast(Data *data)
Definition: v8-context.h:571
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:562
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:478
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:524
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())
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:234
Definition: v8-script.h:377
Definition: v8-value.h:32
Definition: v8-internal.h:878
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1723
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1677
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1678
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1443
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:855
#define V8_INLINE
Definition: v8config.h:508
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:622
#define V8_DEPRECATED(message)
Definition: v8config.h:614
#define V8_NODISCARD
Definition: v8config.h:701