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
318 "Use SetAlignedPointerInEmbedderData with EmbedderDataTypeTag parameter "
319 "instead.")
320 void SetAlignedPointerInEmbedderData(int index, void* value);
321
322 void SetAlignedPointerInEmbedderData(int index, void* value,
324
338 void AllowCodeGenerationFromStrings(bool allow);
339
344 bool IsCodeGenerationFromStringsAllowed() const;
345
351 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
352
357 void SetErrorMessageForWasmCodeGeneration(Local<String> message);
358
364 template <class T>
365 V8_INLINE MaybeLocal<T> GetDataFromSnapshotOnce(size_t index);
366
372 using AbortScriptExecutionCallback = void (*)(Isolate* isolate,
373 Local<Context> context);
374 void SetAbortScriptExecution(AbortScriptExecutionCallback callback);
375
383 void SetPromiseHooks(Local<Function> init_hook, Local<Function> before_hook,
384 Local<Function> after_hook,
385 Local<Function> resolve_hook);
386
387 bool HasTemplateLiteralObject(Local<Value> object);
393 public:
394 explicit V8_INLINE Scope(Local<Context> context) : context_(context) {
395 context_->Enter();
396 }
397 V8_INLINE ~Scope() { context_->Exit(); }
398
399 private:
400 Local<Context> context_;
401 };
402
409 public:
414 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
416
417 private:
418 friend class internal::Isolate;
419
420 uintptr_t JSStackComparableAddressPrivate() const {
421 return js_stack_comparable_address_;
422 }
423
424 Local<Context> backup_incumbent_context_;
425 uintptr_t js_stack_comparable_address_ = 0;
426 const BackupIncumbentScope* prev_ = nullptr;
427 };
428
429 V8_INLINE static Context* Cast(Data* data);
430
431 private:
432 friend class Value;
433 friend class Script;
434 friend class Object;
435 friend class Function;
436
437 static void CheckCast(Data* obj);
438
440 size_t index);
441 Local<Value> SlowGetEmbedderData(int index);
442 void* SlowGetAlignedPointerFromEmbedderData(int index);
443};
444
445// --- Implementation ---
446
448#ifndef 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 =
455 I::kEmbedderDataArrayHeaderSize + (I::kEmbedderDataSlotSize * index);
456 A value = I::ReadRawField<A>(embedder_data, value_offset);
457#ifdef V8_COMPRESS_POINTERS
458 // We read the full pointer value and then decompress it in order to avoid
459 // dealing with potential endiannes issues.
460 value = I::DecompressTaggedField(embedder_data, static_cast<uint32_t>(value));
461#endif
462
463 auto* isolate = I::GetCurrentIsolate();
464 return Local<Value>::New(isolate, value);
465#else
466 return SlowGetEmbedderData(index);
467#endif
468}
469
471#if !defined(V8_ENABLE_CHECKS)
472 using A = internal::Address;
473 using I = internal::Internals;
475 A embedder_data =
476 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
477 int value_offset = I::kEmbedderDataArrayHeaderSize +
478 (I::kEmbedderDataSlotSize * index) +
479 I::kEmbedderDataSlotExternalPointerOffset;
480 return reinterpret_cast<void*>(
481 I::ReadExternalPointerField<{internal::kFirstEmbedderDataTag,
483 isolate, embedder_data, value_offset));
484#else
485 return SlowGetAlignedPointerFromEmbedderData(index);
486#endif
487}
488
490#if !defined(V8_ENABLE_CHECKS)
491 using A = internal::Address;
492 using I = internal::Internals;
494 A embedder_data =
495 I::ReadTaggedPointerField(ctx, I::kNativeContextEmbedderDataOffset);
496 int value_offset = I::kEmbedderDataArrayHeaderSize +
497 (I::kEmbedderDataSlotSize * index) +
498 I::kEmbedderDataSlotExternalPointerOffset;
499 Isolate* isolate = I::GetCurrentIsolateForSandbox();
500 return reinterpret_cast<void*>(
501 I::ReadExternalPointerField<{internal::kFirstEmbedderDataTag,
503 isolate, embedder_data, value_offset));
504#else
505 return SlowGetAlignedPointerFromEmbedderData(index);
506#endif
507}
508
509template <class T>
511 if (auto repr = GetDataFromSnapshotOnce(index);
513 internal::PerformCastCheck(internal::ValueHelper::ReprAsValue<T>(repr));
514 return Local<T>::FromRepr(repr);
515 }
516 return {};
517}
518
520#ifdef V8_ENABLE_CHECKS
521 CheckCast(data);
522#endif
523 return static_cast<Context*>(data);
524}
525
526} // namespace v8
527
528#endif // INCLUDE_V8_CONTEXT_H_
Definition: v8-context.h:408
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:392
Scope(Local< Context > context)
Definition: v8-context.h:394
~Scope()
Definition: v8-context.h:397
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 SetEmbedderData(int index, Local< Value > value)
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 * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index)
Definition: v8-context.h:470
static Context * Cast(Data *data)
Definition: v8-context.h:519
void Enter()
Maybe< void > DeepFreeze(DeepFreezeDelegate *delegate=nullptr)
Local< Value > GetSecurityToken()
MaybeLocal< T > GetDataFromSnapshotOnce(size_t index)
Definition: v8-context.h:510
void DetachGlobal()
EmbedderDataFields
Definition: v8-context.h:274
uint32_t GetNumberOfEmbedderDataFields()
Local< Value > GetEmbedderData(int index)
Definition: v8-context.h:447
void UseDefaultSecurityToken()
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition: v8-context.h:373
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:290
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:40
Definition: v8-object.h:243
Definition: v8-script.h:353
Definition: v8-primitive.h:124
Definition: v8-value.h:32
Definition: v8-internal.h:883
static Address ValueAsAddress(const T *value)
Definition: v8-internal.h:1757
internal::Address * InternalRepresentationType
Definition: v8-internal.h:1711
static constexpr InternalRepresentationType kEmpty
Definition: v8-internal.h:1712
@ kFirstEmbedderDataTag
Definition: v8-internal.h:580
@ kLastEmbedderDataTag
Definition: v8-internal.h:581
uintptr_t Address
Definition: v8-internal.h:52
void PerformCastCheck(T *data)
Definition: v8-internal.h:1418
Definition: libplatform.h:15
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:860
#define V8_INLINE
Definition: v8config.h:513
#define V8_DEPRECATE_SOON(message)
Definition: v8config.h:627
#define V8_NODISCARD
Definition: v8config.h:706