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 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate, int index,
322 CppHeapPointerTag tag) {
323 // TODO(ahaas): This is a temporary implementation, the actual
324 // implementation will follow with the refactoring of EmbedderDataSlots.
325 // The refactoring will regress the existing API, as the fast path will move
326 // to this new API.
327 // By using this temporary implementation, blink's ScriptState can already
328 // switch to the new API, and thereby switch from the old fast path to the
329 // new fast path directly.
330 return GetAlignedPointerFromEmbedderData(isolate, index,
332 }
333
334 void SetAlignedPointerInEmbedderData(int index, void* value,
336
337 template <typename T>
340 CppHeapPointerTag tag) {
341 // TODO(ahaas): This is a temporary implementation, the actual
342 // implementation will follow with the refactoring of EmbedderDataSlots.
343 // The refactoring will regress the existing API, as the fast path will move
344 // to this new API.
345 // By using this temporary implementation, blink's ScriptState can already
346 // switch to the new API, and thereby switch from the old fast path to the
347 // new fast path directly.
348 SetAlignedPointerInEmbedderData(index, value, kEmbedderDataTypeTagDefault);
349 }
350
365
371
378
384
390 template <class T>
391 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
392
398 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
399 Local<Context> context);
401
406 int64_t (*)(Local<Context> context);
409
418 Local<Function> after_hook,
419 Local<Function> resolve_hook);
420
427 public:
428 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
429 context_->Enter();
430 }
431 V8_INLINE ~Scope() { context_->Exit(); }
432
433 private:
434 Local<Context> context_;
435 };
436
443 public:
448 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
450
451 private:
452 friend class internal::Isolate;
453
454 uintptr_t JSStackComparableAddressPrivate() const {
455 return js_stack_comparable_address_;
456 }
457
458 Local<Context> backup_incumbent_context_;
459 uintptr_t js_stack_comparable_address_ = 0;
460 const BackupIncumbentScope* prev_ = nullptr;
461 };
462
463 V8_INLINE static Context* Cast(Data* data);
464
465 private:
466 friend class Value;
467 friend class Script;
468 friend class Object;
469 friend class Function;
470
471 static void CheckCast(Data* obj);
472
474 size_t index);
475 Local<Value> SlowGetEmbedderData(int index);
476 Local<Data> SlowGetEmbedderDataV2(int index);
477 void* SlowGetAlignedPointerFromEmbedderData(int index,
479};
480
481// --- Implementation ---
482
484#ifndef V8_ENABLE_CHECKS
485 using A = internal::Address;
486 using I = internal::Internals;
488 A embedder_data =
489 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
490 int value_offset =
491 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
492 A value = I::ReadRawField<A>(embedder_data, value_offset);
493#ifdef V8_COMPRESS_POINTERS
494 // We read the full pointer value and then decompress it in order to avoid
495 // dealing with potential endianness issues.
496 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
497#endif
498
499 auto* isolate = I::GetCurrentIsolate();
500 return Local<Value>::New(isolate, value);
501#else
502 return SlowGetEmbedderData(index);
503#endif
504}
505
507#ifndef V8_ENABLE_CHECKS
508 using A = internal::Address;
509 using I = internal::Internals;
511 A embedder_data =
512 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
513 int value_offset =
514 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
515 A value = I::ReadRawField<A>(embedder_data, value_offset);
516#ifdef V8_COMPRESS_POINTERS
517 // We read the full pointer value and then decompress it in order to avoid
518 // dealing with potential endianness issues.
519 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
520#endif
521
522 auto* isolate = I::GetCurrentIsolate();
523 return Local<Data>::New(isolate, value);
524#else
525 return SlowGetEmbedderDataV2(index);
526#endif
527}
528
531#if !defined(V8_ENABLE_CHECKS)
532 using A = internal::Address;
533 using I = internal::Internals;
535 A embedder_data =
536 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
537 int value_offset = I::kEmbedderDataArrayHeaderSize +
538 (I::kEmbedderDataSlotSize * index) +
539 I::kEmbedderDataSlotExternalPointerOffset;
540 return reinterpret_cast<void*>(I::ReadExternalPointerField(
541 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
542#else
543 return SlowGetAlignedPointerFromEmbedderData(index, tag);
544#endif
545}
546
549#if !defined(V8_ENABLE_CHECKS)
550 using A = internal::Address;
551 using I = internal::Internals;
553 A embedder_data =
554 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
555 int value_offset = I::kEmbedderDataArrayHeaderSize +
556 (I::kEmbedderDataSlotSize * index) +
557 I::kEmbedderDataSlotExternalPointerOffset;
558 Isolate* isolate = I::GetCurrentIsolateForSandbox();
559 return reinterpret_cast<void*>(I::ReadExternalPointerField(
560 isolate, embedder_data, value_offset, ToExternalPointerTag(tag)));
561#else
562 return SlowGetAlignedPointerFromEmbedderData(index, tag);
563#endif
564}
565
566template <class T>
568 if (auto repr = GetDataFromSnapshotOnce(index);
570 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
571 return Local<T>::FromRepr(repr);
572 }
573 return {};
574}
575
577#ifdef V8_ENABLE_CHECKS
578 CheckCast(data);
579#endif
580 return static_cast<Context*>(data);
581}
582
583} // namespace v8
584
585#endif // INCLUDE_V8_CONTEXT_H_
Definition: garbage-collected.h:53
Definition: v8-context.h:442
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:426
Scope(Local< Context > context)
Definition: v8-context.h:428
~Scope()
Definition: v8-context.h:431
Definition: v8-context.h:48
Local< Data > GetEmbedderDataV2(int index)
Definition: v8-context.h:506
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:576
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:567
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:483
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index, EmbedderDataTypeTag tag)
Definition: v8-context.h:529
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:399
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 SetAlignedPointerInEmbedderData(int index, cppgc::GarbageCollected< T > *value, CppHeapPointerTag tag)
Definition: v8-context.h:338
int64_t(*)(Local< Context > context) TemporalHostSystemUTCEpochNanosecondsCallback
Definition: v8-context.h:406
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: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:51
Definition: v8-object.h:266
Definition: v8-script.h:415
Definition: v8-value.h:32
Definition: v8-internal.h:975
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1853
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1807
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1808
uintptr_t Address
Definition: v8-internal.h:38
void PerformCastCheck(T *data)
Definition: v8-internal.h:1568
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