Skip to main content

PlatformImpl

Trait PlatformImpl 

Source
pub trait PlatformImpl: Send + Sync {
    // Provided methods
    fn post_task(&self, isolate_ptr: *mut c_void, task: Task) { ... }
    fn post_non_nestable_task(&self, isolate_ptr: *mut c_void, task: Task) { ... }
    fn post_delayed_task(
        &self,
        isolate_ptr: *mut c_void,
        task: Task,
        delay_in_seconds: f64,
    ) { ... }
    fn post_non_nestable_delayed_task(
        &self,
        isolate_ptr: *mut c_void,
        task: Task,
        delay_in_seconds: f64,
    ) { ... }
    fn post_idle_task(&self, isolate_ptr: *mut c_void, task: IdleTask) { ... }
}
Expand description

Trait for customizing platform behavior, following the same pattern as V8InspectorClientImpl.

Implement this trait to receive V8 foreground tasks and schedule them on your event loop. The C++ CustomPlatform wraps each isolate’s TaskRunner so that every PostTask / PostDelayedTask / etc. call transfers task ownership to Rust through the corresponding trait method.

The embedder is responsible for calling Task::run() on the isolate’s thread. For example, using tokio:

fn post_task(&self, isolate_ptr: *mut c_void, task: Task) {
    tokio::spawn(async move { task.run() });
}

fn post_delayed_task(&self, isolate_ptr: *mut c_void, task: Task, delay: f64) {
    tokio::spawn(async move {
        tokio::time::sleep(Duration::from_secs_f64(delay)).await;
        task.run();
    });
}

All methods have default implementations that run the task immediately (synchronously). Override to integrate with your event loop.

Implementations must be Send + Sync as callbacks may fire from any thread.

Provided Methods§

Source

fn post_task(&self, isolate_ptr: *mut c_void, task: Task)

Called when TaskRunner::PostTask is invoked for the given isolate.

The Task must be run on the isolate’s foreground thread by calling Task::run().

May be called from ANY thread (V8 background threads, etc.).

Source

fn post_non_nestable_task(&self, isolate_ptr: *mut c_void, task: Task)

Called when TaskRunner::PostNonNestableTask is invoked.

Same semantics as post_task, but the task must not be run within a nested PumpMessageLoop.

Source

fn post_delayed_task( &self, isolate_ptr: *mut c_void, task: Task, delay_in_seconds: f64, )

Called when TaskRunner::PostDelayedTask is invoked.

The task should be run after delay_in_seconds has elapsed. For example, using tokio::time::sleep or a timer wheel.

May be called from ANY thread.

Source

fn post_non_nestable_delayed_task( &self, isolate_ptr: *mut c_void, task: Task, delay_in_seconds: f64, )

Called when TaskRunner::PostNonNestableDelayedTask is invoked.

Same semantics as post_delayed_task.

Source

fn post_idle_task(&self, isolate_ptr: *mut c_void, task: IdleTask)

Called when TaskRunner::PostIdleTask is invoked.

The IdleTask should be run when the embedder has idle time, passing the deadline via IdleTask::run(deadline).

Implementors§