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(Isolate* isolate,
306 int index);
307 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
308
314 void SetAlignedPointerInEmbedderData(int index, void* value);
315
330
336
343
349
355 template <class T>
356 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
357
363 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
364 Local<Context> context);
366
375 Local<Function> after_hook,
376 Local<Function> resolve_hook);
377
384 public:
385 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
386 context_->Enter();
387 }
388 V8_INLINE ~Scope() { context_->Exit(); }
389
390 private:
391 Local<Context> context_;
392 };
393
400 public:
405 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
407
408 private:
409 friend class internal::Isolate;
410
411 uintptr_t JSStackComparableAddressPrivate() const {
412 return js_stack_comparable_address_;
413 }
414
415 Local<Context> backup_incumbent_context_;
416 uintptr_t js_stack_comparable_address_ = 0;
417 const BackupIncumbentScope* prev_ = nullptr;
418 };
419
420 V8_INLINE static Context* Cast(Data* data);
421
422 private:
423 friend class Value;
424 friend class Script;
425 friend class Object;
426 friend class Function;
427
428 static void CheckCast(Data* obj);
429
431 size_t index);
432 Local<Value> SlowGetEmbedderData(int index);
433 void* SlowGetAlignedPointerFromEmbedderData(int index);
434};
435
436// --- Implementation ---
437
439#ifndef V8_ENABLE_CHECKS
440 using A = internal::Address;
441 using I = internal::Internals;
443 A embedder_data =
444 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
445 int value_offset =
446 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
447 A value = I::ReadRawField<A>(embedder_data, value_offset);
448#ifdef V8_COMPRESS_POINTERS
449 // We read the full pointer value and then decompress it in order to avoid
450 // dealing with potential endiannes issues.
451 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
452#endif
453
454 auto isolate = reinterpret_cast<v8::Isolate*>(
456 return Local<Value>::New(isolate, value);
457#else
458 return SlowGetEmbedderData(index);
459#endif
460}
461
463#if !defined(V8_ENABLE_CHECKS)
464 using A = internal::Address;
465 using I = internal::Internals;
467 A embedder_data =
468 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
469 int value_offset = I::kEmbedderDataArrayHeaderSize +
470 (I::kEmbedderDataSlotSize * index) +
471 I::kEmbedderDataSlotExternalPointerOffset;
472 return reinterpret_cast<void*>(
473 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
474 isolate, embedder_data, value_offset));
475#else
476 return SlowGetAlignedPointerFromEmbedderData(index);
477#endif
478}
479
481#if !defined(V8_ENABLE_CHECKS)
482 using A = internal::Address;
483 using I = internal::Internals;
485 A embedder_data =
486 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
487 int value_offset = I::kEmbedderDataArrayHeaderSize +
488 (I::kEmbedderDataSlotSize * index) +
489 I::kEmbedderDataSlotExternalPointerOffset;
490 Isolate* isolate = I::GetIsolateForSandbox(ctx);
491 return reinterpret_cast<void*>(
492 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
493 isolate, embedder_data, value_offset));
494#else
495 return SlowGetAlignedPointerFromEmbedderData(index);
496#endif
497}
498
499template <class T>
501 if (auto repr = GetDataFromSnapshotOnce(index);
503 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
504 return Local<T>::FromRepr(repr);
505 }
506 return {};
507}
508
510#ifdef V8_ENABLE_CHECKS
511 CheckCast(data);
512#endif
513 return static_cast<Context*>(data);
514}
515
516} // namespace v8
517
518#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:399
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:383
Scope(Local< Context > context)
Definition: v8-context.h:385
~Scope()
Definition: v8-context.h:388
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
void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index)
Definition: v8-context.h:462
static Context * Cast(Data *data)
Definition: v8-context.h:509
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:500
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:438
void SetMicrotaskQueue(MicrotaskQueue *queue)
void UseDefaultSecurityToken()
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:364
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:261
Definition: v8-local-handle.h:492
Definition: v8-local-handle.h:266
static Local< T > New(Isolate *isolate, Local< T > that)
Definition: v8-local-handle.h:346
Definition: v8-local-handle.h:632
Definition: v8-maybe.h:32
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:794
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1625
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1579
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1580
internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition: v8-internal.h:52
void PerformCastCheck(T *data)
Definition: v8-internal.h:1290
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:793
#define V8_INLINE
Definition: v8config.h:499
#define V8_NODISCARD
Definition: v8config.h:682