@@ -68,58 +68,132 @@ pub struct Platform(Opaque);
6868/// Trait for customizing platform behavior, following the same pattern as
6969/// [`V8InspectorClientImpl`](crate::inspector::V8InspectorClientImpl).
7070///
71- /// Implement this trait to receive callbacks for platform virtual method
72- /// overrides. The C++ `CustomPlatform` base class delegates to these methods.
73- /// All methods have default no-op implementations; override only what you need.
71+ /// Implement this trait to receive callbacks for overridden C++ virtual
72+ /// methods on the `DefaultPlatform` and its per-isolate `TaskRunner`.
7473///
75- /// Implementations must be `Send + Sync` as callbacks may fire from any thread.
74+ /// The C++ `CustomPlatform` wraps each isolate's `TaskRunner` so that
75+ /// every `PostTask` / `PostDelayedTask` / etc. call is forwarded to the
76+ /// default implementation *and* notifies Rust through the corresponding
77+ /// trait method.
78+ ///
79+ /// All methods have default no-op implementations; override only what
80+ /// you need.
81+ ///
82+ /// Implementations must be `Send + Sync` as callbacks may fire from any
83+ /// thread.
7684#[ allow( unused_variables) ]
7785pub trait PlatformImpl : Send + Sync {
78- /// Called when a foreground task has been posted for the given isolate.
86+ // ---- TaskRunner virtual methods ----
87+
88+ /// Called when `TaskRunner::PostTask` is invoked for the given isolate.
89+ ///
90+ /// The task itself has already been forwarded to the default platform's
91+ /// queue and will be executed by `PumpMessageLoop`. This callback is a
92+ /// notification that a new task is available.
93+ ///
94+ /// May be called from ANY thread (V8 background threads, etc.).
95+ fn post_task ( & self , isolate_ptr : * mut std:: ffi:: c_void ) { }
96+
97+ /// Called when `TaskRunner::PostNonNestableTask` is invoked.
7998 ///
80- /// This corresponds to intercepted calls on the isolate's `TaskRunner`
81- /// (`PostTask`, `PostDelayedTask`, `PostIdleTask`, etc.).
99+ /// Same semantics as [`post_task`](Self::post_task).
100+ fn post_non_nestable_task ( & self , isolate_ptr : * mut std:: ffi:: c_void ) { }
101+
102+ /// Called when `TaskRunner::PostDelayedTask` is invoked.
82103 ///
83- /// `isolate_ptr` is the raw `v8::Isolate*` pointer of the target isolate.
84- /// `delay_in_seconds` is 0.0 for immediate tasks, or the delay before the
85- /// task should be executed. For delayed tasks, the embedder should schedule
86- /// a wake-up after the given delay (e.g. via a timer in tokio).
104+ /// The task has been forwarded to the default runner's delayed queue.
105+ /// `delay_in_seconds` is the delay before the task should execute.
106+ /// Embedders should schedule a wake-up after this delay.
87107 ///
88- /// This may be called from ANY thread (V8 background threads, etc.) .
89- fn on_foreground_task_posted (
108+ /// May be called from ANY thread.
109+ fn post_delayed_task (
90110 & self ,
91111 isolate_ptr : * mut std:: ffi:: c_void ,
92112 delay_in_seconds : f64 ,
93113 ) {
94114 }
95115
96- /// Called when an isolate is about to be shut down .
116+ /// Called when `TaskRunner::PostNonNestableDelayedTask` is invoked .
97117 ///
98- /// This corresponds to the `NotifyIsolateShutdown` virtual method.
99- /// The default `DefaultPlatform` cleanup runs after this callback returns.
100- fn on_isolate_shutdown ( & self , isolate_ptr : * mut std:: ffi:: c_void ) { }
118+ /// Same semantics as [`post_delayed_task`](Self::post_delayed_task).
119+ fn post_non_nestable_delayed_task (
120+ & self ,
121+ isolate_ptr : * mut std:: ffi:: c_void ,
122+ delay_in_seconds : f64 ,
123+ ) {
124+ }
125+
126+ /// Called when `TaskRunner::PostIdleTask` is invoked.
127+ ///
128+ /// Same semantics as [`post_task`](Self::post_task).
129+ fn post_idle_task ( & self , isolate_ptr : * mut std:: ffi:: c_void ) { }
130+
131+ // ---- Platform virtual methods ----
132+
133+ /// Called when `Platform::NotifyIsolateShutdown` is invoked.
134+ ///
135+ /// The default `DefaultPlatform` cleanup runs after this callback
136+ /// returns.
137+ fn notify_isolate_shutdown ( & self , isolate_ptr : * mut std:: ffi:: c_void ) { }
101138}
102139
103140// FFI callbacks called from C++ CustomPlatform/CustomTaskRunner.
104141// `context` is a raw pointer to a `Box<dyn PlatformImpl>`.
105142
106143#[ unsafe( no_mangle) ]
107- unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
144+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostTask (
145+ context : * mut std:: ffi:: c_void ,
146+ isolate : * mut std:: ffi:: c_void ,
147+ ) {
148+ let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
149+ imp. post_task ( isolate) ;
150+ }
151+
152+ #[ unsafe( no_mangle) ]
153+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableTask (
154+ context : * mut std:: ffi:: c_void ,
155+ isolate : * mut std:: ffi:: c_void ,
156+ ) {
157+ let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
158+ imp. post_non_nestable_task ( isolate) ;
159+ }
160+
161+ #[ unsafe( no_mangle) ]
162+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostDelayedTask (
108163 context : * mut std:: ffi:: c_void ,
109164 isolate : * mut std:: ffi:: c_void ,
110165 delay_in_seconds : f64 ,
111166) {
112167 let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
113- imp. on_foreground_task_posted ( isolate, delay_in_seconds) ;
168+ imp. post_delayed_task ( isolate, delay_in_seconds) ;
169+ }
170+
171+ #[ unsafe( no_mangle) ]
172+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask (
173+ context : * mut std:: ffi:: c_void ,
174+ isolate : * mut std:: ffi:: c_void ,
175+ delay_in_seconds : f64 ,
176+ ) {
177+ let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
178+ imp. post_non_nestable_delayed_task ( isolate, delay_in_seconds) ;
179+ }
180+
181+ #[ unsafe( no_mangle) ]
182+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__PostIdleTask (
183+ context : * mut std:: ffi:: c_void ,
184+ isolate : * mut std:: ffi:: c_void ,
185+ ) {
186+ let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
187+ imp. post_idle_task ( isolate) ;
114188}
115189
116190#[ unsafe( no_mangle) ]
117- unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__onIsolateShutdown (
191+ unsafe extern "C" fn v8__Platform__CustomPlatform__BASE__NotifyIsolateShutdown (
118192 context : * mut std:: ffi:: c_void ,
119193 isolate : * mut std:: ffi:: c_void ,
120194) {
121195 let imp = unsafe { & * ( context as * const Box < dyn PlatformImpl > ) } ;
122- imp. on_isolate_shutdown ( isolate) ;
196+ imp. notify_isolate_shutdown ( isolate) ;
123197}
124198
125199#[ unsafe( no_mangle) ]
0 commit comments