v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
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
68 void DetachGlobal();
69
115 static Local<Context> New(
116 Isolate* isolate, ExtensionConfiguration* extensions = nullptr,
118 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
119 DeserializeInternalFieldsCallback internal_fields_deserializer =
122 DeserializeContextDataCallback context_data_deserializer =
124 DeserializeAPIWrapperCallback api_wrapper_deserializer =
126
163 static MaybeLocal<Context> FromSnapshot(
164 Isolate* isolate, size_t context_snapshot_index,
165 DeserializeInternalFieldsCallback internal_fields_deserializer =
167 ExtensionConfiguration* extensions = nullptr,
168 MaybeLocal<Value> global_object = MaybeLocal<Value>(),
170 DeserializeContextDataCallback context_data_deserializer =
172 DeserializeAPIWrapperCallback api_wrapper_deserializer =
174
192 static MaybeLocal<Object> NewRemoteContext(
193 Isolate* isolate, Local<ObjectTemplate> global_template,
194 MaybeLocal<Value> global_object = MaybeLocal<Value>());
195
200 void SetSecurityToken(Local<Value> token);
201
203 void UseDefaultSecurityToken();
204
206 Local<Value> GetSecurityToken();
207
214 void Enter();
215
220 void Exit();
221
227 public:
238 Local<Object> obj, LocalVector<Object>& children_out) = 0;
239 };
240
256 Maybe<void> DeepFreeze(DeepFreezeDelegate* delegate = nullptr);
257
259 Isolate* GetIsolate();
260
262 MicrotaskQueue* GetMicrotaskQueue();
263
265 void SetMicrotaskQueue(MicrotaskQueue* queue);
266
271 enum EmbedderDataFields { kDebugIdIndex = 0 };
272
276 uint32_t GetNumberOfEmbedderDataFields();
277
282 V8_INLINE Local<Value> GetEmbedderData(int index);
283
290 Local<Object> GetExtrasBindingObject();
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
329 void AllowCodeGenerationFromStrings(bool allow);
330
335 bool IsCodeGenerationFromStringsAllowed() const;
336
342 void SetErrorMessageForCodeGenerationFromStrings(Local<String> message);
343
348 void SetErrorMessageForWasmCodeGeneration(Local<String> message);
349
355 template <class T>
357
365 void SetAbortScriptExecution(AbortScriptExecutionCallback callback);
366
374 void SetPromiseHooks(Local<Function> init_hook, Local<Function> before_hook,
375 Local<Function> after_hook,
376 Local<Function> resolve_hook);
377
378 bool HasTemplateLiteralObject(Local<Value> object);
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:
392 };
393
400 public:
405 explicit BackupIncumbentScope(Local<Context> backup_incumbent_context);
407
408 private:
409 friend class internal::Isolate;
410
412 return js_stack_comparable_address_;
413 }
414
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
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
496#endif
497}
498
499template <class T>
501 if (auto repr = GetDataFromSnapshotOnce(index);
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_
union v8::internal::@341::BuiltinMetadata::KindSpecificData data
uintptr_t JSStackComparableAddressPrivate() const
Definition v8-context.h:411
Local< Context > backup_incumbent_context_
Definition v8-context.h:415
virtual bool FreezeEmbedderObjectAndGetChildren(Local< Object > obj, LocalVector< Object > &children_out)=0
V8_INLINE Scope(Local< Context > context)
Definition v8-context.h:385
Local< Context > context_
Definition v8-context.h:391
V8_INLINE ~Scope()
Definition v8-context.h:388
static void CheckCast(Data *obj)
Definition api.cc:3945
V8_INLINE void * GetAlignedPointerFromEmbedderData(Isolate *isolate, int index)
Definition v8-context.h:462
static V8_INLINE Context * Cast(Data *data)
Definition v8-context.h:509
void * SlowGetAlignedPointerFromEmbedderData(int index)
Definition api.cc:927
void(*)(Isolate *isolate, Local< Context > context) AbortScriptExecutionCallback
Definition v8-context.h:363
Local< Value > SlowGetEmbedderData(int index)
Definition api.cc:906
V8_INLINE MaybeLocal< T > GetDataFromSnapshotOnce(size_t index)
V8_INLINE Local< Value > GetEmbedderData(int index)
Definition v8-context.h:438
ExtensionConfiguration(int name_count, const char *names[])
Definition v8-context.h:33
const char ** begin() const
Definition v8-context.h:36
const char ** end() const
Definition v8-context.h:37
static V8_INLINE Local< T > New(Isolate *isolate, Local< T > that)
static V8_INLINE Local< T > FromRepr(internal::ValueHelper::InternalRepresentationType repr)
static V8_INLINE Address ValueAsAddress(const T *value)
internal::Address * InternalRepresentationType
static V8_INLINE T * ReprAsValue(InternalRepresentationType repr)
static constexpr InternalRepresentationType kEmpty
Handle< Context > context_
MicrotaskQueue * microtask_queue
Definition execution.cc:77
Isolate * isolate
StringsStorage * names_
OptionalOpIndex index
TNode< Context > context
TNode< Object > callback
V8_EXPORT internal::Isolate * IsolateFromNeverReadOnlySpaceObject(Address obj)
V8_INLINE void PerformCastCheck(T *data)
v8_inspector::String16 String
Definition string-util.h:26
Node * prev_
#define I(name, number_of_args, result_size)
Definition runtime.cc:36
#define V8_EXPORT
Definition v8config.h:800
#define V8_INLINE
Definition v8config.h:500
#define V8_NODISCARD
Definition v8config.h:693