|
|
|||
File indexing completed on 2026-09-25 09:17:57
0001 // Copyright 2021 the V8 project authors. All rights reserved. 0002 // Use of this source code is governed by a BSD-style license that can be 0003 // found in the LICENSE file. 0004 0005 #ifndef INCLUDE_V8_ISOLATE_CALLBACKS_H_ 0006 #define INCLUDE_V8_ISOLATE_CALLBACKS_H_ 0007 0008 #include <stddef.h> 0009 0010 #include <functional> 0011 #include <string> 0012 0013 #include "cppgc/common.h" 0014 #include "v8-data.h" // NOLINT(build/include_directory) 0015 #include "v8-local-handle.h" // NOLINT(build/include_directory) 0016 #include "v8-promise.h" // NOLINT(build/include_directory) 0017 #include "v8config.h" // NOLINT(build/include_directory) 0018 0019 #if defined(V8_OS_WIN) 0020 struct _EXCEPTION_POINTERS; 0021 #endif 0022 0023 namespace v8 { 0024 0025 template <typename T> 0026 class FunctionCallbackInfo; 0027 class Isolate; 0028 class Message; 0029 class Module; 0030 class Object; 0031 class Promise; 0032 class ScriptOrModule; 0033 class String; 0034 class UnboundScript; 0035 class Value; 0036 0037 /** 0038 * A JIT code event is issued each time code is added, moved or removed. 0039 * 0040 * \note removal events are not currently issued. 0041 */ 0042 struct JitCodeEvent { 0043 enum EventType { 0044 CODE_ADDED, 0045 CODE_MOVED, 0046 CODE_REMOVED, 0047 CODE_ADD_LINE_POS_INFO, 0048 CODE_START_LINE_INFO_RECORDING, 0049 CODE_END_LINE_INFO_RECORDING 0050 }; 0051 // Definition of the code position type. The "POSITION" type means the place 0052 // in the source code which are of interest when making stack traces to 0053 // pin-point the source location of a stack frame as close as possible. 0054 // The "STATEMENT_POSITION" means the place at the beginning of each 0055 // statement, and is used to indicate possible break locations. 0056 enum PositionType { POSITION, STATEMENT_POSITION }; 0057 0058 // There are three different kinds of CodeType, one for JIT code generated 0059 // by the optimizing compiler, one for byte code generated for the 0060 // interpreter, and one for code generated from Wasm. For JIT_CODE and 0061 // WASM_CODE, |code_start| points to the beginning of jitted assembly code, 0062 // while for BYTE_CODE events, |code_start| points to the first bytecode of 0063 // the interpreted function. 0064 enum CodeType { BYTE_CODE, JIT_CODE, WASM_CODE }; 0065 0066 // Type of event. 0067 EventType type; 0068 CodeType code_type; 0069 // Start of the instructions. 0070 void* code_start; 0071 // Size of the instructions. 0072 size_t code_len; 0073 // Script info for CODE_ADDED event. 0074 Local<UnboundScript> script; 0075 // User-defined data for *_LINE_INFO_* event. It's used to hold the source 0076 // code line information which is returned from the 0077 // CODE_START_LINE_INFO_RECORDING event. And it's passed to subsequent 0078 // CODE_ADD_LINE_POS_INFO and CODE_END_LINE_INFO_RECORDING events. 0079 void* user_data; 0080 0081 struct name_t { 0082 // Name of the object associated with the code, note that the string is not 0083 // zero-terminated. 0084 const char* str; 0085 // Number of chars in str. 0086 size_t len; 0087 }; 0088 0089 struct line_info_t { 0090 // PC offset 0091 size_t offset; 0092 // Code position 0093 size_t pos; 0094 // The position type. 0095 PositionType position_type; 0096 }; 0097 0098 struct wasm_source_info_t { 0099 // Source file name. 0100 const char* filename; 0101 // Length of filename. 0102 size_t filename_size; 0103 // Line number table, which maps offsets of JITted code to line numbers of 0104 // source file. 0105 const line_info_t* line_number_table; 0106 // Number of entries in the line number table. 0107 size_t line_number_table_size; 0108 }; 0109 0110 wasm_source_info_t* wasm_source_info = nullptr; 0111 0112 union { 0113 // Only valid for CODE_ADDED. 0114 struct name_t name; 0115 0116 // Only valid for CODE_ADD_LINE_POS_INFO 0117 struct line_info_t line_info; 0118 0119 // New location of instructions. Only valid for CODE_MOVED. 0120 void* new_code_start; 0121 }; 0122 0123 Isolate* isolate; 0124 }; 0125 0126 /** 0127 * Option flags passed to the SetJitCodeEventHandler function. 0128 */ 0129 enum JitCodeEventOptions { 0130 kJitCodeEventDefault = 0, 0131 // Generate callbacks for already existent code. 0132 kJitCodeEventEnumExisting = 1, 0133 0134 kLastJitCodeEventOption = kJitCodeEventEnumExisting 0135 }; 0136 0137 /** 0138 * Callback function passed to SetJitCodeEventHandler. 0139 * 0140 * \param event code add, move or removal event. 0141 */ 0142 using JitCodeEventHandler = void (*)(const JitCodeEvent* event); 0143 0144 // --- Garbage Collection Callbacks --- 0145 0146 /** 0147 * Applications can register callback functions which will be called before and 0148 * after certain garbage collection operations. Allocations are not allowed in 0149 * the callback functions, you therefore cannot manipulate objects (set or 0150 * delete properties for example) since it is possible such operations will 0151 * result in the allocation of objects. 0152 * TODO(v8:12612): Deprecate kGCTypeMinorMarkSweep after updating blink. 0153 */ 0154 enum GCType { 0155 kGCTypeScavenge = 1 << 0, 0156 kGCTypeMinorMarkSweep = 1 << 1, 0157 kGCTypeMarkSweepCompact = 1 << 2, 0158 kGCTypeIncrementalMarking = 1 << 3, 0159 kGCTypeProcessWeakCallbacks = 1 << 4, 0160 kGCTypeAll = kGCTypeScavenge | kGCTypeMinorMarkSweep | 0161 kGCTypeMarkSweepCompact | kGCTypeIncrementalMarking | 0162 kGCTypeProcessWeakCallbacks 0163 }; 0164 0165 /** 0166 * GCCallbackFlags is used to notify additional information about the GC 0167 * callback. 0168 * - kGCCallbackFlagConstructRetainedObjectInfos: The GC callback is for 0169 * constructing retained object infos. 0170 * - kGCCallbackFlagForced: The GC callback is for a forced GC for testing. 0171 * - kGCCallbackFlagSynchronousPhantomCallbackProcessing: The GC callback 0172 * is called synchronously without getting posted to an idle task. 0173 * - kGCCallbackFlagCollectAllAvailableGarbage: The GC callback is called 0174 * in a phase where V8 is trying to collect all available garbage 0175 * (e.g., handling a low memory notification). 0176 * - kGCCallbackScheduleIdleGarbageCollection: The GC callback is called to 0177 * trigger an idle garbage collection. 0178 */ 0179 enum GCCallbackFlags { 0180 kNoGCCallbackFlags = 0, 0181 kGCCallbackFlagConstructRetainedObjectInfos = 1 << 1, 0182 kGCCallbackFlagForced = 1 << 2, 0183 kGCCallbackFlagSynchronousPhantomCallbackProcessing = 1 << 3, 0184 kGCCallbackFlagCollectAllAvailableGarbage = 1 << 4, 0185 kGCCallbackFlagCollectAllExternalMemory = 1 << 5, 0186 kGCCallbackScheduleIdleGarbageCollection = 1 << 6, 0187 kGCCallbackFlagLastResort = 1 << 7, 0188 }; 0189 0190 using GCCallback = void (*)(GCType type, GCCallbackFlags flags); 0191 0192 using InterruptCallback = void (*)(Isolate* isolate, void* data); 0193 0194 using PrintCurrentStackTraceFilterCallback = 0195 bool (*)(Isolate* isolate, Local<String> script_name); 0196 0197 /** 0198 * This callback is invoked when the heap size is close to the heap limit and 0199 * V8 is likely to abort with out-of-memory error. 0200 * The callback can extend the heap limit by returning a value that is greater 0201 * than the current_heap_limit. The initial heap limit is the limit that was 0202 * set after heap setup. 0203 */ 0204 using NearHeapLimitCallback = size_t (*)(void* data, size_t current_heap_limit, 0205 size_t initial_heap_limit); 0206 0207 /** 0208 * Callback function passed to SetUnhandledExceptionCallback. 0209 */ 0210 #if defined(V8_OS_WIN) 0211 using UnhandledExceptionCallback = 0212 int (*)(_EXCEPTION_POINTERS* exception_pointers); 0213 #endif 0214 0215 // --- Counters Callbacks --- 0216 0217 using CounterLookupCallback = int* (*)(const char* name); 0218 0219 using CreateHistogramCallback = void* (*)(const char* name, int min, int max, 0220 size_t buckets); 0221 0222 using AddHistogramSampleCallback = void (*)(void* histogram, int sample); 0223 0224 // --- Exceptions --- 0225 0226 using FatalErrorCallback = void (*)(const char* location, const char* message); 0227 0228 struct OOMDetails { 0229 bool is_heap_oom = false; 0230 const char* detail = nullptr; 0231 }; 0232 0233 using OOMErrorCallback = void (*)(const char* location, 0234 const OOMDetails& details); 0235 0236 using OOMErrorCallbackWithData = void (*)(const char* location, 0237 const OOMDetails& details, 0238 void* data); 0239 0240 using MessageCallback = void (*)(Local<Message> message, Local<Value> data); 0241 0242 // --- Tracing --- 0243 0244 enum LogEventStatus : int { kStart = 0, kEnd = 1, kLog = 2 }; 0245 using LogEventCallback = void (*)(const char* name, 0246 int /* LogEventStatus */ status); 0247 0248 // --- Crashkeys Callback --- 0249 enum class CrashKeyId { 0250 kIsolateAddress, 0251 kReadonlySpaceFirstPageAddress, 0252 kMapSpaceFirstPageAddress V8_ENUM_DEPRECATE_SOON("Map space got removed"), 0253 kOldSpaceFirstPageAddress, 0254 kCodeRangeBaseAddress, 0255 kCodeSpaceFirstPageAddress, 0256 kDumpType, 0257 kSnapshotChecksumCalculated, 0258 kSnapshotChecksumExpected, 0259 }; 0260 0261 using AddCrashKeyCallback = void (*)(CrashKeyId id, const std::string& value); 0262 0263 // --- CrashKeyString Callbacks --- 0264 using CrashKey = void*; 0265 enum class CrashKeySize { Size32, Size64, Size256, Size1024 }; 0266 0267 using AllocateCrashKeyStringCallback = 0268 std::function<CrashKey(const char key[], CrashKeySize size)>; 0269 using SetCrashKeyStringCallback = 0270 std::function<void(CrashKey key, const std::string_view value)>; 0271 0272 // --- Enter/Leave Script Callback --- 0273 using BeforeCallEnteredCallback = void (*)(Isolate*); 0274 using CallCompletedCallback = void (*)(Isolate*); 0275 0276 // --- Modify Code Generation From Strings Callback --- 0277 struct ModifyCodeGenerationFromStringsResult { 0278 // If true, proceed with the codegen algorithm. Otherwise, block it. 0279 bool codegen_allowed = false; 0280 // Overwrite the original source with this string, if present. 0281 // Use the original source if empty. 0282 // This field is considered only if codegen_allowed is true. 0283 MaybeLocal<String> modified_source; 0284 }; 0285 0286 /** 0287 * Callback to check if codegen is allowed from a source object, and convert 0288 * the source to string if necessary. See: ModifyCodeGenerationFromStrings. 0289 */ 0290 using ModifyCodeGenerationFromStringsCallback = 0291 ModifyCodeGenerationFromStringsResult (*)(Local<Context> context, 0292 Local<Value> source); 0293 using ModifyCodeGenerationFromStringsCallback2 = 0294 ModifyCodeGenerationFromStringsResult (*)(Local<Context> context, 0295 Local<Value> source, 0296 bool is_code_like); 0297 0298 // --- Failed Access Check Callback --- 0299 0300 /** 0301 * Access type specification. 0302 */ 0303 enum AccessType { 0304 ACCESS_GET, 0305 ACCESS_SET, 0306 ACCESS_HAS, 0307 ACCESS_DELETE, 0308 ACCESS_KEYS 0309 }; 0310 0311 using FailedAccessCheckCallback = void (*)(Local<Object> target, 0312 AccessType type, Local<Value> data); 0313 0314 // --- WebAssembly compilation callbacks --- 0315 using ExtensionCallback = bool (*)(const FunctionCallbackInfo<Value>&); 0316 0317 using AllowWasmCodeGenerationCallback = bool (*)(Local<Context> context, 0318 Local<String> source); 0319 0320 // --- Callback for APIs defined on v8-supported objects, but implemented 0321 // by the embedder. Example: WebAssembly.{compile|instantiate}Streaming --- 0322 using ApiImplementationCallback = void (*)(const FunctionCallbackInfo<Value>&); 0323 0324 // --- Callback for WebAssembly.compileStreaming --- 0325 using WasmStreamingCallback = void (*)(const FunctionCallbackInfo<Value>&); 0326 0327 enum class WasmAsyncSuccess { kSuccess, kFail }; 0328 0329 // --- Callback called when async WebAssembly operations finish --- 0330 using WasmAsyncResolvePromiseCallback = void (*)( 0331 Isolate* isolate, Local<Context> context, Local<Promise::Resolver> resolver, 0332 Local<Value> result, WasmAsyncSuccess success); 0333 0334 // --- Callback for loading source map file for Wasm profiling support 0335 using WasmLoadSourceMapCallback = Local<String> (*)(Isolate* isolate, 0336 const char* name); 0337 0338 // --- Callback for checking if WebAssembly Custom Descriptors are enabled --- 0339 using WasmCustomDescriptorsEnabledCallback = bool (*)(Local<Context> context); 0340 0341 // --- Callback for checking if the SharedArrayBuffer constructor is enabled --- 0342 using SharedArrayBufferConstructorEnabledCallback = 0343 bool (*)(Local<Context> context); 0344 0345 /** 0346 * Import phases in import requests. 0347 */ 0348 enum class ModuleImportPhase { 0349 kSource, 0350 kDefer, 0351 kEvaluation, 0352 }; 0353 0354 /** 0355 * HostImportModuleDynamicallyCallback is called when we 0356 * require the embedder to load a module. This is used as part of the dynamic 0357 * import syntax. 0358 * 0359 * The referrer contains metadata about the script/module that calls 0360 * import. 0361 * 0362 * The specifier is the name of the module that should be imported. 0363 * 0364 * The import_attributes are import attributes for this request in the form: 0365 * [key1, value1, key2, value2, ...] where the keys and values are of type 0366 * v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and 0367 * returned from ModuleRequest::GetImportAttributes(), this array does not 0368 * contain the source Locations of the attributes. 0369 * 0370 * The embedder must compile, instantiate, evaluate the Module, and 0371 * obtain its namespace object. 0372 * 0373 * The Promise returned from this function is forwarded to userland 0374 * JavaScript. The embedder must resolve this promise with the module 0375 * namespace object. In case of an exception, the embedder must reject 0376 * this promise with the exception. If the promise creation itself 0377 * fails (e.g. due to stack overflow), the embedder must propagate 0378 * that exception by returning an empty MaybeLocal. 0379 */ 0380 using HostImportModuleDynamicallyCallback = MaybeLocal<Promise> (*)( 0381 Local<Context> context, Local<Data> host_defined_options, 0382 Local<Value> resource_name, Local<String> specifier, 0383 Local<FixedArray> import_attributes); 0384 0385 /** 0386 * HostImportModuleWithPhaseDynamicallyCallback is called when we 0387 * require the embedder to load a module with a specific phase. This is used 0388 * as part of the dynamic import syntax. 0389 * 0390 * The referrer contains metadata about the script/module that calls 0391 * import. 0392 * 0393 * The specifier is the name of the module that should be imported. 0394 * 0395 * The phase is the phase of the import requested. 0396 * 0397 * The import_attributes are import attributes for this request in the form: 0398 * [key1, value1, key2, value2, ...] where the keys and values are of type 0399 * v8::String. Note, unlike the FixedArray passed to ResolveModuleCallback and 0400 * returned from ModuleRequest::GetImportAttributes(), this array does not 0401 * contain the source Locations of the attributes. 0402 * 0403 * The Promise returned from this function is forwarded to userland 0404 * JavaScript. The embedder must resolve this promise according to the phase 0405 * requested: 0406 * - For ModuleImportPhase::kSource, the promise must be resolved with a 0407 * compiled ModuleSource object, or rejected with a SyntaxError if the 0408 * module does not support source representation. 0409 * - For ModuleImportPhase::kEvaluation, the promise must be resolved with a 0410 * ModuleNamespace object of a module that has been compiled, instantiated, 0411 * and evaluated. 0412 * 0413 * In case of an exception, the embedder must reject this promise with the 0414 * exception. If the promise creation itself fails (e.g. due to stack 0415 * overflow), the embedder must propagate that exception by returning an empty 0416 * MaybeLocal. 0417 * 0418 * This callback is still experimental and is only invoked for source phase 0419 * imports. 0420 */ 0421 using HostImportModuleWithPhaseDynamicallyCallback = MaybeLocal<Promise> (*)( 0422 Local<Context> context, Local<Data> host_defined_options, 0423 Local<Value> resource_name, Local<String> specifier, 0424 ModuleImportPhase phase, Local<FixedArray> import_attributes); 0425 0426 /** 0427 * Callback for requesting a compile hint for a function from the embedder. The 0428 * first parameter is the position of the function in source code and the second 0429 * parameter is embedder data to be passed back. 0430 */ 0431 using CompileHintCallback = bool (*)(int, void*); 0432 0433 /** 0434 * HostInitializeImportMetaObjectCallback is called the first time import.meta 0435 * is accessed for a module. Subsequent access will reuse the same value. 0436 * 0437 * The method combines two implementation-defined abstract operations into one: 0438 * HostGetImportMetaProperties and HostFinalizeImportMeta. 0439 * 0440 * The embedder should use v8::Object::CreateDataProperty to add properties on 0441 * the meta object. 0442 */ 0443 using HostInitializeImportMetaObjectCallback = void (*)(Local<Context> context, 0444 Local<Module> module, 0445 Local<Object> meta); 0446 0447 /** 0448 * HostCreateShadowRealmContextCallback is called each time a ShadowRealm is 0449 * being constructed in the initiator_context. 0450 * 0451 * The method combines Context creation and implementation defined abstract 0452 * operation HostInitializeShadowRealm into one. 0453 * 0454 * The embedder should use v8::Context::New or v8::Context:NewFromSnapshot to 0455 * create a new context. If the creation fails, the embedder must propagate 0456 * that exception by returning an empty MaybeLocal. 0457 */ 0458 using HostCreateShadowRealmContextCallback = 0459 MaybeLocal<Context> (*)(Local<Context> initiator_context); 0460 0461 /** 0462 * IsJSApiWrapperNativeErrorCallback is called on an JSApiWrapper object to 0463 * determine if Error.isError should return true or false. For instance, in an 0464 * HTML embedder, DOMExceptions return true when passed to Error.isError. 0465 */ 0466 using IsJSApiWrapperNativeErrorCallback = bool (*)(Isolate* isolate, 0467 Local<Object> obj); 0468 0469 /** 0470 * PrepareStackTraceCallback is called when the stack property of an error is 0471 * first accessed. The return value will be used as the stack value. If this 0472 * callback is registed, the |Error.prepareStackTrace| API will be disabled. 0473 * |sites| is an array of call sites, specified in 0474 * https://v8.dev/docs/stack-trace-api 0475 */ 0476 using PrepareStackTraceCallback = MaybeLocal<Value> (*)(Local<Context> context, 0477 Local<Value> error, 0478 Local<Array> sites); 0479 0480 #if defined(V8_OS_WIN) 0481 /** 0482 * Callback to selectively enable ETW tracing based on the document URL. 0483 * Implemented by the embedder, it should never call back into V8. 0484 * 0485 * Windows allows passing additional data to the ETW EnableCallback: 0486 * https://learn.microsoft.com/en-us/windows/win32/api/evntprov/nc-evntprov-penablecallback 0487 * 0488 * This data can be configured in a WPR (Windows Performance Recorder) 0489 * profile, adding a CustomFilter to an EventProvider like the following: 0490 * 0491 * <EventProvider Id=".." Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" Level="5"> 0492 * <CustomFilter Type="0x80000000" Value="AQABAAAAAAA..." /> 0493 * </EventProvider> 0494 * 0495 * Where: 0496 * - Name="57277741-3638-4A4B-BDBA-0AC6E45DA56C" is the GUID of the V8 0497 * ETW provider, (see src/libplatform/etw/etw-provider-win.h), 0498 * - Type="0x80000000" is EVENT_FILTER_TYPE_SCHEMATIZED, 0499 * - Value="AQABAAAAAA..." is a base64-encoded byte array that is 0500 * base64-decoded by Windows and passed to the ETW enable callback in 0501 * the 'PEVENT_FILTER_DESCRIPTOR FilterData' argument; see: 0502 * https://learn.microsoft.com/en-us/windows/win32/api/evntprov/ns-evntprov-event_filter_descriptor. 0503 * 0504 * This array contains a struct EVENT_FILTER_HEADER followed by a 0505 * variable length payload, and as payload we pass a string in JSON format, 0506 * with a list of regular expressions that should match the document URL 0507 * in order to enable ETW tracing: 0508 * { 0509 * "version": "2.0", 0510 * "filtered_urls": [ 0511 * "https:\/\/.*\.chromium\.org\/.*", "https://v8.dev/";, "..." 0512 * ], 0513 * "trace_interpreter_frames": true 0514 * } 0515 */ 0516 0517 using FilterETWSessionByURLCallback = 0518 bool (*)(Local<Context> context, const std::string& etw_filter_payload); 0519 0520 struct FilterETWSessionByURLResult { 0521 // If true, enable ETW tracing for the current isolate. 0522 bool enable_etw_tracing; 0523 0524 // If true, also enables ETW tracing for interpreter stack frames. 0525 bool trace_interpreter_frames; 0526 }; 0527 using FilterETWSessionByURL2Callback = FilterETWSessionByURLResult (*)( 0528 Local<Context> context, const std::string& etw_filter_payload); 0529 #endif // V8_OS_WIN 0530 0531 } // namespace v8 0532 0533 #endif // INCLUDE_V8_ISOLATE_CALLBACKS_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|