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
266
271 enum EmbedderDataFields { kDebugIdIndex = 0 };
272
277
282 V8_INLINE Local<Value> GetEmbedderData(int index);
283
291
297 void SetEmbedderData(int index, Local<Value> value);
298
305 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
306
312 void SetAlignedPointerInEmbedderData(int index, void* value);
313
328
334
341
347
353 template <class T>
354 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
355
361 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
362 Local<Context> context);
364
373 Local<Function> after_hook,
374 Local<Function> resolve_hook);
375
382 public:
383 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
384 context_->Enter();
385 }
386 V8_INLINE ~Scope() { context_->Exit(); }
387
388 private:
389 Local<Context> context_;
390 };
391
398 public:
403 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
405
406 private:
407 friend class internal::Isolate;
408
409 uintptr_t JSStackComparableAddressPrivate() const {
410 return js_stack_comparable_address_;
411 }
412
413 Local<Context> backup_incumbent_context_;
414 uintptr_t js_stack_comparable_address_ = 0;
415 const BackupIncumbentScope* prev_ = nullptr;
416 };
417
418 V8_INLINE static Context* Cast(Data* data);
419
420 private:
421 friend class Value;
422 friend class Script;
423 friend class Object;
424 friend class Function;
425
426 static void CheckCast(Data* obj);
427
428 internal::Address* GetDataFromSnapshotOnce(size_t index);
429 Local<Value> SlowGetEmbedderData(int index);
430 void* SlowGetAlignedPointerFromEmbedderData(int index);
431};
432
433// --- Implementation ---
434
436#ifndef V8_ENABLE_CHECKS
437 using A = internal::Address;
438 using I = internal::Internals;
440 A embedder_data =
441 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
442 int value_offset =
443 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
444 A value = I::ReadRawField<A>(embedder_data, value_offset);
445#ifdef V8_COMPRESS_POINTERS
446 // We read the full pointer value and then decompress it in order to avoid
447 // dealing with potential endiannes issues.
448 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
449#endif
450
451 auto isolate = reinterpret_cast<v8::Isolate*>(
453 return Local<Value>::New(isolate, value);
454#else
455 return SlowGetEmbedderData(index);
456#endif
457}
458
460#if !defined(V8_ENABLE_CHECKS)
461 using A = internal::Address;
462 using I = internal::Internals;
464 A embedder_data =
465 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
466 int value_offset = I::kEmbedderDataArrayHeaderSize +
467 (I::kEmbedderDataSlotSize * index) +
468 I::kEmbedderDataSlotExternalPointerOffset;
469 Isolate* isolate = I::GetIsolateForSandbox(ctx);
470 return reinterpret_cast<void*>(
471 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
472 isolate, embedder_data, value_offset));
473#else
474 return SlowGetAlignedPointerFromEmbedderData(index);
475#endif
476}
477
478template <class T>
480 if (auto slot = GetDataFromSnapshotOnce(index); slot) {
482 internal::ValueHelper::SlotAsValue<T, false>(slot));
483 return Local<T>::FromSlot(slot);
484 }
485 return {};
486}
487
489#ifdef V8_ENABLE_CHECKS
490 CheckCast(data);
491#endif
492 return static_cast<Context*>(data);
493}
494
495} // namespace v8
496
497#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:397
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:381
Scope(Local< Context > context)
Definition: v8-context.h:383
~Scope()
Definition: v8-context.h:386
Definition: v8-context.h:48
Isolate * GetIsolate()
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
static Context * Cast(Data *data)
Definition: v8-context.h:488
void SetAlignedPointerInEmbedderData(int index, void *value)
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:479
void DetachGlobal()
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
EmbedderDataFields
Definition: v8-context.h:271
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:435
void SetMicrotaskQueue(MicrotaskQueue *queue)
void * GetAlignedPointerFromEmbedderData(int index)
Definition: v8-context.h:459
void UseDefaultSecurityToken()
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:362
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:26
Definition: v8-isolate.h:210
Definition: v8-local-handle.h:483
Definition: v8-local-handle.h:258
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:338
Definition: v8-local-handle.h:619
Definition: v8-maybe.h:32
Definition: v8-microtask-queue.h:40
Definition: v8-object.h:238
Definition: v8-script.h:329
Definition: v8-value.h:32
Definition: v8-internal.h:729
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1457
internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition: v8-internal.h:31
void PerformCastCheck(T *data)
Definition: v8-internal.h:1197
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:762
#define V8_INLINE
Definition: v8config.h:477
#define V8_NODISCARD
Definition: v8config.h:659