Skip to content

Commit 0693f56

Browse files
committed
wip
1 parent 73dbcfc commit 0693f56

File tree

2 files changed

+127
-43
lines changed

2 files changed

+127
-43
lines changed

src/binding.cc

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3013,18 +3013,27 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
30133013
}
30143014

30153015
// Rust-side callbacks for trait-based CustomPlatform (PlatformImpl trait).
3016-
// `this` is a pointer to the CustomPlatform instance, used by Rust to
3017-
// recover the Box<dyn PlatformImpl> stored at the same offset.
3016+
// Each callback corresponds to a C++ virtual method on TaskRunner or Platform.
3017+
// `context` is a pointer to the Rust Box<dyn PlatformImpl>.
30183018
extern "C" {
3019-
void v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3020-
void* this_, void* isolate, double delay_in_seconds);
3021-
void v8__Platform__CustomPlatform__BASE__onIsolateShutdown(void* this_,
3022-
void* isolate);
3023-
void v8__Platform__CustomPlatform__BASE__DROP(void* this_);
3024-
}
3025-
3026-
// TaskRunner wrapper that intercepts all PostTask* calls and dispatches
3027-
// to the Rust PlatformImpl trait via the CustomPlatform context.
3019+
void v8__Platform__CustomPlatform__BASE__PostTask(void* context,
3020+
void* isolate);
3021+
void v8__Platform__CustomPlatform__BASE__PostNonNestableTask(void* context,
3022+
void* isolate);
3023+
void v8__Platform__CustomPlatform__BASE__PostDelayedTask(
3024+
void* context, void* isolate, double delay_in_seconds);
3025+
void v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask(
3026+
void* context, void* isolate, double delay_in_seconds);
3027+
void v8__Platform__CustomPlatform__BASE__PostIdleTask(void* context,
3028+
void* isolate);
3029+
void v8__Platform__CustomPlatform__BASE__NotifyIsolateShutdown(void* context,
3030+
void* isolate);
3031+
void v8__Platform__CustomPlatform__BASE__DROP(void* context);
3032+
}
3033+
3034+
// TaskRunner wrapper that intercepts all PostTask* virtual methods, forwards
3035+
// tasks to the default platform's queue, and notifies Rust via the
3036+
// corresponding PlatformImpl trait method.
30283037
class CustomTaskRunner final : public v8::TaskRunner {
30293038
public:
30303039
CustomTaskRunner(std::shared_ptr<v8::TaskRunner> wrapped, void* context,
@@ -3043,20 +3052,20 @@ class CustomTaskRunner final : public v8::TaskRunner {
30433052
void PostTaskImpl(std::unique_ptr<v8::Task> task,
30443053
const v8::SourceLocation& location) override {
30453054
wrapped_->PostTask(std::move(task), location);
3046-
v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3047-
context_, static_cast<void*>(isolate_), 0.0);
3055+
v8__Platform__CustomPlatform__BASE__PostTask(
3056+
context_, static_cast<void*>(isolate_));
30483057
}
30493058
void PostNonNestableTaskImpl(std::unique_ptr<v8::Task> task,
30503059
const v8::SourceLocation& location) override {
30513060
wrapped_->PostNonNestableTask(std::move(task), location);
3052-
v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3053-
context_, static_cast<void*>(isolate_), 0.0);
3061+
v8__Platform__CustomPlatform__BASE__PostNonNestableTask(
3062+
context_, static_cast<void*>(isolate_));
30543063
}
30553064
void PostDelayedTaskImpl(std::unique_ptr<v8::Task> task,
30563065
double delay_in_seconds,
30573066
const v8::SourceLocation& location) override {
30583067
wrapped_->PostDelayedTask(std::move(task), delay_in_seconds, location);
3059-
v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3068+
v8__Platform__CustomPlatform__BASE__PostDelayedTask(
30603069
context_, static_cast<void*>(isolate_),
30613070
delay_in_seconds > 0 ? delay_in_seconds : 0.0);
30623071
}
@@ -3065,15 +3074,15 @@ class CustomTaskRunner final : public v8::TaskRunner {
30653074
const v8::SourceLocation& location) override {
30663075
wrapped_->PostNonNestableDelayedTask(std::move(task), delay_in_seconds,
30673076
location);
3068-
v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3077+
v8__Platform__CustomPlatform__BASE__PostNonNestableDelayedTask(
30693078
context_, static_cast<void*>(isolate_),
30703079
delay_in_seconds > 0 ? delay_in_seconds : 0.0);
30713080
}
30723081
void PostIdleTaskImpl(std::unique_ptr<v8::IdleTask> task,
30733082
const v8::SourceLocation& location) override {
30743083
wrapped_->PostIdleTask(std::move(task), location);
3075-
v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted(
3076-
context_, static_cast<void*>(isolate_), 0.0);
3084+
v8__Platform__CustomPlatform__BASE__PostIdleTask(
3085+
context_, static_cast<void*>(isolate_));
30773086
}
30783087

30793088
private:
@@ -3082,8 +3091,9 @@ class CustomTaskRunner final : public v8::TaskRunner {
30823091
v8::Isolate* isolate_;
30833092
};
30843093

3085-
// Generic Platform subclass that delegates virtual method overrides to a
3086-
// Rust PlatformImpl trait object, following the inspector API pattern.
3094+
// Platform subclass that overrides GetForegroundTaskRunner to wrap each
3095+
// isolate's runner with a CustomTaskRunner, and intercepts
3096+
// NotifyIsolateShutdown. Follows the inspector API pattern.
30873097
class CustomPlatform : public v8::platform::DefaultPlatform {
30883098
using IdleTaskSupport = v8::platform::IdleTaskSupport;
30893099

@@ -3124,7 +3134,7 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31243134
}
31253135
}
31263136
}
3127-
v8__Platform__CustomPlatform__BASE__onIsolateShutdown(
3137+
v8__Platform__CustomPlatform__BASE__NotifyIsolateShutdown(
31283138
context_, static_cast<void*>(isolate));
31293139
DefaultPlatform::NotifyIsolateShutdown(isolate);
31303140
}

src/platform.rs

Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
7785
pub 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

Comments
 (0)