@@ -3012,91 +3012,78 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
30123012 return self->CreateBlob (function_code_handling);
30133013}
30143014
3015- // Rust-side callbacks for TaskRunnerImpl trait.
3016- // `context ` is a double-boxed pointer to a Box<dyn TaskRunnerImpl>.
3017- // Task/IdleTask pointers are released from unique_ptr; Rust takes ownership .
3015+ // 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 .
30183018extern " C" {
3019- void v8__Platform__CustomTaskRunner__PostTask (void * context, v8::Task* task);
3020- void v8__Platform__CustomTaskRunner__PostNonNestableTask (void * context,
3021- v8::Task* task);
3022- void v8__Platform__CustomTaskRunner__PostDelayedTask (void * context,
3023- v8::Task* task,
3024- double delay_in_seconds);
3025- void v8__Platform__CustomTaskRunner__PostNonNestableDelayedTask (
3026- void * context, v8::Task* task, double delay_in_seconds);
3027- void v8__Platform__CustomTaskRunner__PostIdleTask (void * context,
3028- v8::IdleTask* task);
3029- bool v8__Platform__CustomTaskRunner__IdleTasksEnabled (void * context);
3030- bool v8__Platform__CustomTaskRunner__NonNestableTasksEnabled (void * context);
3031- bool v8__Platform__CustomTaskRunner__NonNestableDelayedTasksEnabled (
3032- void * context);
3033- void v8__Platform__CustomTaskRunner__DROP (void * context);
3034- // PlatformImpl trait callbacks.
3035- // `context` is a double-boxed pointer to a Box<dyn PlatformImpl>.
3036- // Returns a double-boxed pointer to a Box<dyn TaskRunnerImpl>, or nullptr
3037- // to fall back to the default task runner.
3038- void * v8__Platform__CustomPlatform__GetForegroundTaskRunner (void * context,
3039- void * isolate);
3040- void v8__Platform__CustomPlatform__DROP (void * context);
3041- }
3042-
3043- void v8__Task__Run (v8::Task* task) { task->Run (); }
3044- void v8__Task__DELETE (v8::Task* task) { delete task; }
3045- void v8__IdleTask__Run (v8::IdleTask* task, double deadline_in_seconds) {
3046- task->Run (deadline_in_seconds);
3047- }
3048- void v8__IdleTask__DELETE (v8::IdleTask* task) { delete task; }
3049-
3050- // TaskRunner that delegates all virtual methods to a Rust TaskRunnerImpl trait.
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.
30513028class CustomTaskRunner final : public v8::TaskRunner {
30523029 public:
3053- explicit CustomTaskRunner (void * context) : context_(context) {}
3054- ~CustomTaskRunner () { v8__Platform__CustomTaskRunner__DROP (context_); }
3030+ CustomTaskRunner (std::shared_ptr<v8::TaskRunner> wrapped, void * context,
3031+ v8::Isolate* isolate)
3032+ : wrapped_(std::move(wrapped)), context_(context), isolate_(isolate) {}
30553033
3056- bool IdleTasksEnabled () override {
3057- return v8__Platform__CustomTaskRunner__IdleTasksEnabled (context_);
3058- }
3034+ bool IdleTasksEnabled () override { return wrapped_->IdleTasksEnabled (); }
30593035 bool NonNestableTasksEnabled () const override {
3060- return v8__Platform__CustomTaskRunner__NonNestableTasksEnabled (context_ );
3036+ return wrapped_-> NonNestableTasksEnabled ( );
30613037 }
30623038 bool NonNestableDelayedTasksEnabled () const override {
3063- return v8__Platform__CustomTaskRunner__NonNestableDelayedTasksEnabled (
3064- context_);
3039+ return wrapped_->NonNestableDelayedTasksEnabled ();
30653040 }
30663041
30673042 protected:
30683043 void PostTaskImpl (std::unique_ptr<v8::Task> task,
3069- const v8::SourceLocation&) override {
3070- v8__Platform__CustomTaskRunner__PostTask (context_, task.release ());
3044+ const v8::SourceLocation& location) override {
3045+ wrapped_->PostTask (std::move (task), location);
3046+ v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
3047+ context_, static_cast <void *>(isolate_), 0.0 );
30713048 }
30723049 void PostNonNestableTaskImpl (std::unique_ptr<v8::Task> task,
3073- const v8::SourceLocation&) override {
3074- v8__Platform__CustomTaskRunner__PostNonNestableTask (context_,
3075- task.release ());
3050+ const v8::SourceLocation& location) override {
3051+ wrapped_->PostNonNestableTask (std::move (task), location);
3052+ v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
3053+ context_, static_cast <void *>(isolate_), 0.0 );
30763054 }
30773055 void PostDelayedTaskImpl (std::unique_ptr<v8::Task> task,
30783056 double delay_in_seconds,
3079- const v8::SourceLocation&) override {
3080- v8__Platform__CustomTaskRunner__PostDelayedTask (context_, task.release (),
3081- delay_in_seconds);
3057+ const v8::SourceLocation& location) override {
3058+ wrapped_->PostDelayedTask (std::move (task), delay_in_seconds, location);
3059+ v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
3060+ context_, static_cast <void *>(isolate_),
3061+ delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
30823062 }
3083- void PostNonNestableDelayedTaskImpl (std::unique_ptr<v8::Task> task,
3084- double delay_in_seconds,
3085- const v8::SourceLocation&) override {
3086- v8__Platform__CustomTaskRunner__PostNonNestableDelayedTask (
3087- context_, task.release (), delay_in_seconds);
3063+ void PostNonNestableDelayedTaskImpl (
3064+ std::unique_ptr<v8::Task> task, double delay_in_seconds,
3065+ const v8::SourceLocation& location) override {
3066+ wrapped_->PostNonNestableDelayedTask (std::move (task), delay_in_seconds,
3067+ location);
3068+ v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
3069+ context_, static_cast <void *>(isolate_),
3070+ delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
30883071 }
30893072 void PostIdleTaskImpl (std::unique_ptr<v8::IdleTask> task,
3090- const v8::SourceLocation&) override {
3091- v8__Platform__CustomTaskRunner__PostIdleTask (context_, task.release ());
3073+ const v8::SourceLocation& location) override {
3074+ wrapped_->PostIdleTask (std::move (task), location);
3075+ v8__Platform__CustomPlatform__BASE__onForegroundTaskPosted (
3076+ context_, static_cast <void *>(isolate_), 0.0 );
30923077 }
30933078
30943079 private:
3080+ std::shared_ptr<v8::TaskRunner> wrapped_;
30953081 void * context_;
3082+ v8::Isolate* isolate_;
30963083};
30973084
3098- // Platform subclass that delegates GetForegroundTaskRunner to a Rust
3099- // PlatformImpl trait object, following the inspector API pattern.
3085+ // Generic Platform subclass that delegates virtual method overrides to a
3086+ // Rust PlatformImpl trait object, following the inspector API pattern.
31003087class CustomPlatform : public v8 ::platform::DefaultPlatform {
31013088 using IdleTaskSupport = v8::platform::IdleTaskSupport;
31023089
@@ -3106,30 +3093,26 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31063093 : DefaultPlatform(thread_pool_size, idle_task_support),
31073094 context_ (context) {}
31083095
3109- ~CustomPlatform () override { v8__Platform__CustomPlatform__DROP (context_); }
3096+ ~CustomPlatform () override {
3097+ v8__Platform__CustomPlatform__BASE__DROP (context_);
3098+ }
31103099
31113100 std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner (
31123101 v8::Isolate* isolate, v8::TaskPriority priority) override {
3102+ auto original = DefaultPlatform::GetForegroundTaskRunner (isolate, priority);
31133103 std::lock_guard<std::mutex> lock (mutex_);
31143104 auto key = std::make_pair (isolate, priority);
31153105 auto it = runners_.find (key);
31163106 if (it != runners_.end ()) {
31173107 auto runner = it->second .lock ();
31183108 if (runner) return runner;
31193109 }
3120- void * runner_context =
3121- v8__Platform__CustomPlatform__GetForegroundTaskRunner (
3122- context_, static_cast <void *>(isolate));
3123- if (!runner_context) {
3124- return DefaultPlatform::GetForegroundTaskRunner (isolate, priority);
3125- }
3126- auto custom = std::make_shared<CustomTaskRunner>(runner_context);
3110+ auto custom =
3111+ std::make_shared<CustomTaskRunner>(original, context_, isolate);
31273112 runners_[key] = custom;
31283113 return custom;
31293114 }
31303115
3131- // Intentionally hides DefaultPlatform::NotifyIsolateShutdown (not virtual)
3132- // to clean up the runner cache for the isolate.
31333116 void NotifyIsolateShutdown (v8::Isolate* isolate) {
31343117 {
31353118 std::lock_guard<std::mutex> lock (mutex_);
@@ -3141,6 +3124,8 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31413124 }
31423125 }
31433126 }
3127+ v8__Platform__CustomPlatform__BASE__onIsolateShutdown (
3128+ context_, static_cast <void *>(isolate));
31443129 DefaultPlatform::NotifyIsolateShutdown (isolate);
31453130 }
31463131
0 commit comments