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 "Use Isolate::GetCurrent() instead, which is guaranteed to return the "
261 "same isolate since https://crrev.com/c/6458560.")
262 Isolate* GetIsolate();
263
265 MicrotaskQueue* GetMicrotaskQueue();
266
268 void SetMicrotaskQueue(MicrotaskQueue* queue);
269
274 enum EmbedderDataFields { kDebugIdIndex = 0 };
275
280
285 V8_INLINE Local<Value> GetEmbedderData(int index);
286
294
300 void SetEmbedderData(int index, Local<Value> value);
301
308 V8_INLINE void* GetAlignedPointerFromEmbedderData(Isolate* isolate,
309 int index);
310 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
311
317 void SetAlignedPointerInEmbedderData(int index, void* value);
318
333
339
346
352
358 template <class T>
359 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
360
366 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
367 Local<Context> context);
369
378 Local<Function> after_hook,
379 Local<Function> resolve_hook);
380
387 public:
388 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
389 context_->Enter();
390 }
391 V8_INLINE ~Scope() { context_->Exit(); }
392
393 private:
394 Local<Context> context_;
395 };
396
403 public:
408 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
410
411 private:
412 friend class internal::Isolate;
413
414 uintptr_t JSStackComparableAddressPrivate() const {
415 return js_stack_comparable_address_;
416 }
417
418 Local<Context> backup_incumbent_context_;
419 uintptr_t js_stack_comparable_address_ = 0;
420 const BackupIncumbentScope* prev_ = nullptr;
421 };
422
423 V8_INLINE static Context* Cast(Data* data);
424
425 private:
426 friend class Value;
427 friend class Script;
428 friend class Object;
429 friend class Function;
430
431 static void CheckCast(Data* obj);
432
434 size_t index);
435 Local<Value> SlowGetEmbedderData(int index);
436 void* SlowGetAlignedPointerFromEmbedderData(int index);
437};
438
439// --- Implementation ---
440
442#ifndef V8_ENABLE_CHECKS
443 using A = internal::Address;
444 using I = internal::Internals;
446 A embedder_data =
447 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
448 int value_offset =
449 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
450 A value = I::ReadRawField<A>(embedder_data, value_offset);
451#ifdef V8_COMPRESS_POINTERS
452 // We read the full pointer value and then decompress it in order to avoid
453 // dealing with potential endiannes issues.
454 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
455#endif
456
457 auto* isolate = I::GetCurrentIsolate();
458 return Local<Value>::New(isolate, value);
459#else
460 return SlowGetEmbedderData(index);
461#endif
462}
463
465#if !defined(V8_ENABLE_CHECKS)
466 using A = internal::Address;
467 using I = internal::Internals;
469 A embedder_data =
470 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
471 int value_offset = I::kEmbedderDataArrayHeaderSize +
472 (I::kEmbedderDataSlotSize * index) +
473 I::kEmbedderDataSlotExternalPointerOffset;
474 return reinterpret_cast<void*>(
475 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
476 isolate, embedder_data, value_offset));
477#else
478 return SlowGetAlignedPointerFromEmbedderData(index);
479#endif
480}
481
483#if !defined(V8_ENABLE_CHECKS)
484 using A = internal::Address;
485 using I = internal::Internals;
487 A embedder_data =
488 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
489 int value_offset = I::kEmbedderDataArrayHeaderSize +
490 (I::kEmbedderDataSlotSize * index) +
491 I::kEmbedderDataSlotExternalPointerOffset;
492 Isolate* isolate = I::GetCurrentIsolateForSandbox();
493 return reinterpret_cast<void*>(
494 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
495 isolate, embedder_data, value_offset));
496#else
497 return SlowGetAlignedPointerFromEmbedderData(index);
498#endif
499}
500
501template <class T>
503 if (auto repr = GetDataFromSnapshotOnce(index);
505 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
506 return Local<T>::FromRepr(repr);
507 }
508 return {};
509}
510
512#ifdef V8_ENABLE_CHECKS
513 CheckCast(data);
514#endif
515 return static_cast<Context*>(data);
516}
517
518} // namespace v8
519
520#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:402
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:386
Scope(Local< Context > context)
Definition: v8-context.h:388
~Scope()
Definition: v8-context.h:391
Definition: v8-context.h:48
static MaybeLocal< Object > NewRemoteContext(Isolate *isolate, Local< ObjectTemplate > global_template, MaybeLocal< Value > global_object=MaybeLocal< Value >())
Local< Object > GetExtrasBindingObject()
void SetErrorMessageForWasmCodeGeneration(Local< String > message)
void SetEmbedderData(int index, Local< Value > value)
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
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index)
Definition: v8-context.h:464
static Context * Cast(Data *data)
Definition: v8-context.h:511
void SetAlignedPointerInEmbedderData(int index, void *value)
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:502
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:274
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:441
void UseDefaultSecurityToken()
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:367
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())
Local< Object > Global()
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:285
Definition: v8-local-handle.h:525
Definition: v8-local-handle.h:297
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:379
Definition: v8-local-handle.h:665
Definition: v8-maybe.h:33
Definition: v8-microtask-queue.h:40
Definition: v8-object.h:233
Definition: v8-script.h:353
Definition: v8-value.h:32
Definition: v8-internal.h:849
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1717
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1671
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1672
uintptr_t Address
Definition: v8-internal.h:52
void PerformCastCheck(T *data)
Definition: v8-internal.h:1378
Definition: libplatform.h:15
Definition: v8-snapshot.h:116
Definition: v8-snapshot.h:106
Definition: v8-snapshot.h:91
#define V8_EXPORT
Definition: v8config.h:800
#define V8_INLINE
Definition: v8config.h:500
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:614
#define V8_NODISCARD
Definition: v8config.h:693