pub struct HandleScope<'s, C = Context> { /* private fields */ }
Expand description
A stack-allocated class that governs a number of local handles. After a handle scope has been created, all local handles will be allocated within that handle scope until either the handle scope is deleted or another handle scope is created. If there is already a handle scope and a new one is created, all allocations will take place in the new handle scope until it is deleted. After that, new handles will again be allocated in the original handle scope.
After the handle scope of a local handle has been deleted the garbage collector will no longer track the object stored in the handle and may deallocate it. The behavior of accessing a handle for which the handle scope has been deleted is undefined.
Implementations§
Source§impl<'s> HandleScope<'s>
impl<'s> HandleScope<'s>
pub fn new<P: NewHandleScope<'s>>(param: &'s mut P) -> P::NewScope
Sourcepub fn with_context<P: NewHandleScopeWithContext<'s>, H: Handle<Data = Context>>(
param: &'s mut P,
context: H,
) -> Self
pub fn with_context<P: NewHandleScopeWithContext<'s>, H: Handle<Data = Context>>( param: &'s mut P, context: H, ) -> Self
Opens a new HandleScope
and enters a Context
in one step.
The first argument should be an Isolate
or OwnedIsolate
.
The second argument can be any handle that refers to a Context
object;
usually this will be a Global<Context>
.
Sourcepub fn get_current_context(&self) -> Local<'s, Context>
pub fn get_current_context(&self) -> Local<'s, Context>
Returns the context of the currently running JavaScript, or the context on the top of the stack if no JavaScript is running.
Sourcepub fn get_entered_or_microtask_context(&self) -> Local<'s, Context>
pub fn get_entered_or_microtask_context(&self) -> Local<'s, Context>
Returns either the last context entered through V8’s C++ API, or the context of the currently running microtask while processing microtasks. If a context is entered while executing a microtask, that context is returned.
Source§impl<'s> HandleScope<'s, ()>
impl<'s> HandleScope<'s, ()>
Sourcepub fn throw_exception(
&mut self,
exception: Local<'_, Value>,
) -> Local<'s, Value>
pub fn throw_exception( &mut self, exception: Local<'_, Value>, ) -> Local<'s, Value>
Schedules an exception to be thrown when returning to JavaScript. When an exception has been scheduled it is illegal to invoke any JavaScript operation; the caller must return immediately and only after the exception has been handled does it become legal to invoke JavaScript operations.
This function always returns the undefined
value.
Source§impl<'s> HandleScope<'s>
impl<'s> HandleScope<'s>
Sourcepub fn get_isolate_data_from_snapshot_once<T>(
&mut self,
index: usize,
) -> Result<Local<'s, T>, DataError>
pub fn get_isolate_data_from_snapshot_once<T>( &mut self, index: usize, ) -> Result<Local<'s, T>, DataError>
Return data that was previously attached to the isolate snapshot via
SnapshotCreator, and removes the reference to it. If called again with
same index
argument, this function returns DataError::NoData
.
The value that was stored in the snapshot must either match or be
convertible to type parameter T
, otherwise DataError::BadType
is
returned.
Sourcepub fn get_context_data_from_snapshot_once<T>(
&mut self,
index: usize,
) -> Result<Local<'s, T>, DataError>
pub fn get_context_data_from_snapshot_once<T>( &mut self, index: usize, ) -> Result<Local<'s, T>, DataError>
Return data that was previously attached to the context snapshot via
SnapshotCreator, and removes the reference to it. If called again with
same index
argument, this function returns DataError::NoData
.
The value that was stored in the snapshot must either match or be
convertible to type parameter T
, otherwise DataError::BadType
is
returned.
Methods from Deref<Target = Isolate>§
pub fn thread_safe_handle(&self) -> IsolateHandle
Sourcepub fn terminate_execution(&self) -> bool
pub fn terminate_execution(&self) -> bool
Sourcepub fn cancel_terminate_execution(&self) -> bool
pub fn cancel_terminate_execution(&self) -> bool
Sourcepub fn is_execution_terminating(&self) -> bool
pub fn is_execution_terminating(&self) -> bool
Sourcepub fn get_slot<T: 'static>(&self) -> Option<&T>
pub fn get_slot<T: 'static>(&self) -> Option<&T>
Get a reference to embedder data added with set_slot()
.
Sourcepub fn get_slot_mut<T: 'static>(&mut self) -> Option<&mut T>
pub fn get_slot_mut<T: 'static>(&mut self) -> Option<&mut T>
Get a mutable reference to embedder data added with set_slot()
.
Sourcepub fn set_slot<T: 'static>(&mut self, value: T) -> bool
pub fn set_slot<T: 'static>(&mut self, value: T) -> bool
Use with Isolate::get_slot and Isolate::get_slot_mut to associate state with an Isolate.
This method gives ownership of value to the Isolate. Exactly one object of each type can be associated with an Isolate. If called more than once with an object of the same type, the earlier version will be dropped and replaced.
Returns true if value was set without replacing an existing value.
The value will be dropped when the isolate is dropped.
Sourcepub fn remove_slot<T: 'static>(&mut self) -> Option<T>
pub fn remove_slot<T: 'static>(&mut self) -> Option<T>
Removes the embedder data added with set_slot()
and returns it if it exists.
Sourcepub unsafe fn enter(&mut self)
pub unsafe fn enter(&mut self)
Sets this isolate as the entered one for the current thread. Saves the previously entered one (if any), so that it can be restored when exiting. Re-entering an isolate is allowed.
rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is constructed and exited when dropped.
Sourcepub unsafe fn exit(&mut self)
pub unsafe fn exit(&mut self)
Exits this isolate by restoring the previously entered one in the current thread. The isolate may still stay the same, if it was entered more than once.
Requires: self == Isolate::GetCurrent().
rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is constructed and exited when dropped.
Sourcepub fn clear_kept_objects(&mut self)
pub fn clear_kept_objects(&mut self)
Clears the set of objects held strongly by the heap. This set of objects are originally built when a WeakRef is created or successfully dereferenced.
This is invoked automatically after microtasks are run. See MicrotasksPolicy for when microtasks are run.
This needs to be manually invoked only if the embedder is manually running microtasks via a custom MicrotaskQueue class’s PerformCheckpoint. In that case, it is the embedder’s responsibility to make this call at a time which does not interrupt synchronous ECMAScript code execution.
Sourcepub fn low_memory_notification(&mut self)
pub fn low_memory_notification(&mut self)
Optional notification that the system is running low on memory. V8 uses these notifications to attempt to free memory.
Sourcepub fn get_heap_statistics(&mut self, s: &mut HeapStatistics)
pub fn get_heap_statistics(&mut self, s: &mut HeapStatistics)
Get statistics about the heap memory usage.
Sourcepub fn set_capture_stack_trace_for_uncaught_exceptions(
&mut self,
capture: bool,
frame_limit: i32,
)
pub fn set_capture_stack_trace_for_uncaught_exceptions( &mut self, capture: bool, frame_limit: i32, )
Tells V8 to capture current stack trace when uncaught exception occurs and report it to the message listeners. The option is off by default.
Sourcepub fn add_message_listener(&mut self, callback: MessageCallback) -> bool
pub fn add_message_listener(&mut self, callback: MessageCallback) -> bool
Adds a message listener (errors only).
The same message listener can be added more than once and in that case it will be called more than once for each message.
The exception object will be passed to the callback.
Sourcepub fn set_prepare_stack_trace_callback<'s>(
&mut self,
callback: impl MapFnTo<extern "C" fn(_: Local<'s, Context>, _: Local<'s, Value>, _: Local<'s, Array>) -> *const Value>,
)
pub fn set_prepare_stack_trace_callback<'s>( &mut self, callback: impl MapFnTo<extern "C" fn(_: Local<'s, Context>, _: Local<'s, Value>, _: Local<'s, Array>) -> *const Value>, )
This specifies the callback called when the stack property of Error is accessed.
PrepareStackTraceCallback is called when the stack property of an error is first accessed. The return value will be used as the stack value. If this callback is registed, the |Error.prepareStackTrace| API will be disabled. |sites| is an array of call sites, specified in https://v8.dev/docs/stack-trace-api
Sourcepub fn set_promise_hook(&mut self, hook: PromiseHook)
pub fn set_promise_hook(&mut self, hook: PromiseHook)
Set the PromiseHook callback for various promise lifecycle events.
Sourcepub fn set_promise_reject_callback(&mut self, callback: PromiseRejectCallback)
pub fn set_promise_reject_callback(&mut self, callback: PromiseRejectCallback)
Set callback to notify about promise reject with no handler, or revocation of such a previous notification once the handler is added.
Sourcepub fn set_host_initialize_import_meta_object_callback(
&mut self,
callback: HostInitializeImportMetaObjectCallback,
)
pub fn set_host_initialize_import_meta_object_callback( &mut self, callback: HostInitializeImportMetaObjectCallback, )
This specifies the callback called by the upcoming importa.meta language feature to retrieve host-defined meta data for a module.
Sourcepub fn set_host_import_module_dynamically_callback(
&mut self,
callback: HostImportModuleDynamicallyWithImportAssertionsCallback,
)
pub fn set_host_import_module_dynamically_callback( &mut self, callback: HostImportModuleDynamicallyWithImportAssertionsCallback, )
This specifies the callback called by the upcoming dynamic import() language feature to load modules.
Sourcepub fn add_near_heap_limit_callback(
&mut self,
callback: NearHeapLimitCallback,
data: *mut c_void,
)
pub fn add_near_heap_limit_callback( &mut self, callback: NearHeapLimitCallback, data: *mut c_void, )
Add a callback to invoke in case the heap size is close to the heap limit. If multiple callbacks are added, only the most recently added callback is invoked.
Sourcepub fn remove_near_heap_limit_callback(
&mut self,
callback: NearHeapLimitCallback,
heap_limit: usize,
)
pub fn remove_near_heap_limit_callback( &mut self, callback: NearHeapLimitCallback, heap_limit: usize, )
Remove the given callback and restore the heap limit to the given limit. If the given limit is zero, then it is ignored. If the current heap size is greater than the given limit, then the heap limit is restored to the minimal limit that is possible for the current heap size.
pub fn set_oom_error_handler( &mut self, callback: extern "C" fn(location: *const c_char, is_heap_oom: bool), )
Sourcepub fn get_microtasks_policy(&self) -> MicrotasksPolicy
pub fn get_microtasks_policy(&self) -> MicrotasksPolicy
Returns the policy controlling how Microtasks are invoked.
Sourcepub fn set_microtasks_policy(&mut self, policy: MicrotasksPolicy)
pub fn set_microtasks_policy(&mut self, policy: MicrotasksPolicy)
Returns the policy controlling how Microtasks are invoked.
Sourcepub fn perform_microtask_checkpoint(&mut self)
pub fn perform_microtask_checkpoint(&mut self)
Runs the default MicrotaskQueue until it gets empty and perform other microtask checkpoint steps, such as calling ClearKeptObjects. Asserts that the MicrotasksPolicy is not kScoped. Any exceptions thrown by microtask callbacks are swallowed.
Sourcepub fn run_microtasks(&mut self)
👎Deprecated: Use Isolate::perform_microtask_checkpoint() instead
pub fn run_microtasks(&mut self)
An alias for PerformMicrotaskCheckpoint.
Sourcepub fn enqueue_microtask(&mut self, microtask: Local<'_, Function>)
pub fn enqueue_microtask(&mut self, microtask: Local<'_, Function>)
Enqueues the callback to the default MicrotaskQueue
Sourcepub fn set_allow_atomics_wait(&mut self, allow: bool)
pub fn set_allow_atomics_wait(&mut self, allow: bool)
Set whether calling Atomics.wait (a function that may block) is allowed in this isolate. This can also be configured via CreateParams::allow_atomics_wait.
Sourcepub fn set_wasm_streaming_callback<F>(&mut self, _: F)
pub fn set_wasm_streaming_callback<F>(&mut self, _: F)
Embedder injection point for WebAssembly.compileStreaming(source)
.
The expectation is that the embedder sets it at most once.
The callback receives the source argument (string, Promise, etc.) and an instance of WasmStreaming. The WasmStreaming instance can outlive the callback and is used to feed data chunks to V8 asynchronously.
Sourcepub fn has_pending_background_tasks(&self) -> bool
pub fn has_pending_background_tasks(&self) -> bool
Returns true if there is ongoing background work within V8 that will eventually post a foreground task, like asynchronous WebAssembly compilation.
Sourcepub fn take_heap_snapshot<F>(&mut self, callback: F)
pub fn take_heap_snapshot<F>(&mut self, callback: F)
Take a heap snapshot. The callback is invoked one or more times with byte slices containing the snapshot serialized as JSON. It’s the callback’s responsibility to reassemble them into a single document, e.g., by writing them to a file. Note that Chrome DevTools refuses to load snapshots without a .heapsnapshot suffix.