v8 12.4.254 (node 22.4.1)
V8 is Google's open source JavaScript engine
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
112 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
114 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
115 DeserializeInternalFieldsCallback internal_fields_deserializer =
117 MicrotaskQueue* microtask_queue = nullptr,
118 DeserializeContextDataCallback context_data_deserializer =
120
154 Isolate* isolate, size_t context_snapshot_index,
155 DeserializeInternalFieldsCallback internal_fields_deserializer =
157 ExtensionConfiguration* extensions = nullptr,
158 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
159 MicrotaskQueue* microtask_queue = nullptr,
160 DeserializeContextDataCallback context_data_deserializer =
162
181 Isolate* isolate, Local<ObjectTemplate> global_template,
182 MaybeLocal<Value> global_object = MaybeLocal<Value>());
183
189
192
195
202 void Enter();
203
208 void Exit();
209
215 public:
226 Local<Object> obj, LocalVector<Object>& children_out) = 0;
227 };
228
245
248
251
254
259 enum EmbedderDataFields { kDebugIdIndex = 0 };
260
265
270 V8_INLINE Local<Value> GetEmbedderData(int index);
271
279
285 void SetEmbedderData(int index, Local<Value> value);
286
293 V8_INLINE void* GetAlignedPointerFromEmbedderData(int index);
294
300 void SetAlignedPointerInEmbedderData(int index, void* value);
301
316
322
329
335
341 template <class T>
343
349 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
350 Local<Context> context);
352
361 Local<Function> after_hook,
362 Local<Function> resolve_hook);
363
370 public:
371 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
372 context_->Enter();
373 }
374 V8_INLINE ~Scope() { context_->Exit(); }
375
376 private:
377 Local<Context> context_;
378 };
379
386 public:
391 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
393
394 private:
395 friend class internal::Isolate;
396
397 uintptr_t JSStackComparableAddressPrivate() const {
398 return js_stack_comparable_address_;
399 }
400
401 Local<Context> backup_incumbent_context_;
402 uintptr_t js_stack_comparable_address_ = 0;
403 const BackupIncumbentScope* prev_ = nullptr;
404 };
405
406 V8_INLINE static Context* Cast(Data* data);
407
408 private:
409 friend class Value;
410 friend class Script;
411 friend class Object;
412 friend class Function;
413
414 static void CheckCast(Data* obj);
415
416 internal::Address* GetDataFromSnapshotOnce(size_t index);
417 Local<Value> SlowGetEmbedderData(int index);
418 void* SlowGetAlignedPointerFromEmbedderData(int index);
419};
420
421// --- Implementation ---
422
424#ifndef V8_ENABLE_CHECKS
425 using A = internal::Address;
426 using I = internal::Internals;
428 A embedder_data =
429 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
430 int value_offset =
431 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
432 A value = I::ReadRawField<A>(embedder_data, value_offset);
433#ifdef V8_COMPRESS_POINTERS
434 // We read the full pointer value and then decompress it in order to avoid
435 // dealing with potential endiannes issues.
436 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
437#endif
438
439 auto isolate = reinterpret_cast<v8::Isolate*>(
441 return Local<Value>::New(isolate, value);
442#else
443 return SlowGetEmbedderData(index);
444#endif
445}
446
448#if !defined(V8_ENABLE_CHECKS)
449 using A = internal::Address;
450 using I = internal::Internals;
452 A embedder_data =
453 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
454 int value_offset = I::kEmbedderDataArrayHeaderSize +
455 (I::kEmbedderDataSlotSize * index) +
456 I::kEmbedderDataSlotExternalPointerOffset;
457 Isolate* isolate = I::GetIsolateForSandbox(ctx);
458 return reinterpret_cast<void*>(
459 I::ReadExternalPointerField<internal::kEmbedderDataSlotPayloadTag>(
460 isolate, embedder_data, value_offset));
461#else
462 return SlowGetAlignedPointerFromEmbedderData(index);
463#endif
464}
465
466template <class T>
468 if (auto slot = GetDataFromSnapshotOnce(index); slot) {
470 internal::ValueHelper::SlotAsValue<T, false>(slot));
471 return Local<T>::FromSlot(slot);
472 }
473 return {};
474}
475
477#ifdef V8_ENABLE_CHECKS
478 CheckCast(data);
479#endif
480 return static_cast<Context*>(data);
481}
482
483} // namespace v8
484
485#endif // INCLUDE_V8_CONTEXT_H_
BackupIncumbentScope(Local< Context > backup_incumbent_context)
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
V8_INLINE ~Scope()
Definition v8-context.h:374
V8_INLINE Scope(Local< Context > context)
Definition v8-context.h:371
void SetEmbedderData(int index, Local< Value > value)
V8_INLINE Local< Value > GetEmbedderData(int index)
Definition v8-context.h:423
static MaybeLocal< Object > NewRemoteContext(Isolate *isolate, Local< ObjectTemplate > global_template, MaybeLocal< Value > global_object=MaybeLocal< Value >())
Isolate * GetIsolate()
Local< Object > GetExtrasBindingObject()
Local< Object > Global()
bool IsCodeGenerationFromStringsAllowed() const
V8_INLINE void * GetAlignedPointerFromEmbedderData(int index)
Definition v8-context.h:447
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())
Local< Value > GetSecurityToken()
void SetAlignedPointerInEmbedderData(int index, void *value)
void DetachGlobal()
void Enter()
void SetAbortScriptExecution(AbortScriptExecutionCallback callback)
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition v8-context.h:350
V8_INLINE MaybeLocal< T > GetDataFromSnapshotOnce(size_t index)
void AllowCodeGenerationFromStrings(bool allow)
void SetErrorMessageForCodeGenerationFromStrings(Local< String > message)
void UseDefaultSecurityToken()
void SetMicrotaskQueue(MicrotaskQueue *queue)
MicrotaskQueue * GetMicrotaskQueue()
Maybe< void > DeepFreeze(DeepFreezeDelegate *delegate=nullptr)
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())
void SetErrorMessageForWasmCodeGeneration(Local< String > message)
static V8_INLINE Context * Cast(Data *data)
Definition v8-context.h:476
void SetSecurityToken(Local< Value > token)
bool HasTemplateLiteralObject(Local< Value > object)
void SetPromiseHooks(Local< Function > init_hook, Local< Function > before_hook, Local< Function > after_hook, Local< Function > resolve_hook)
uint32_t GetNumberOfEmbedderDataFields()
const char ** begin() const
Definition v8-context.h:36
ExtensionConfiguration(int name_count, const char *names[])
Definition v8-context.h:33
const char ** end() const
Definition v8-context.h:37
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
static V8_INLINE Address ValueAsAddress(const T *value)
V8_EXPORT internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
uintptr_t Address
Definition v8-internal.h:31
V8_INLINE void PerformCastCheck(T *data)
#define V8_EXPORT
Definition v8config.h:753
#define V8_INLINE
Definition v8config.h:477
#define V8_NODISCARD
Definition v8config.h:650