@@ -3012,78 +3012,91 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
30123012 return self->CreateBlob (function_code_handling);
30133013}
30143014
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 .
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 .
30183018extern " 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__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.
30283051class CustomTaskRunner final : public v8::TaskRunner {
30293052 public:
3030- CustomTaskRunner (std::shared_ptr<v8::TaskRunner> wrapped, void * context,
3031- v8::Isolate* isolate)
3032- : wrapped_(std::move(wrapped)), context_(context), isolate_(isolate) {}
3053+ explicit CustomTaskRunner (void * context) : context_(context) {}
3054+ ~CustomTaskRunner () { v8__Platform__CustomTaskRunner__DROP (context_); }
30333055
3034- bool IdleTasksEnabled () override { return wrapped_->IdleTasksEnabled (); }
3056+ bool IdleTasksEnabled () override {
3057+ return v8__Platform__CustomTaskRunner__IdleTasksEnabled (context_);
3058+ }
30353059 bool NonNestableTasksEnabled () const override {
3036- return wrapped_-> NonNestableTasksEnabled ( );
3060+ return v8__Platform__CustomTaskRunner__NonNestableTasksEnabled (context_ );
30373061 }
30383062 bool NonNestableDelayedTasksEnabled () const override {
3039- return wrapped_->NonNestableDelayedTasksEnabled ();
3063+ return v8__Platform__CustomTaskRunner__NonNestableDelayedTasksEnabled (
3064+ context_);
30403065 }
30413066
30423067 protected:
30433068 void PostTaskImpl (std::unique_ptr<v8::Task> task,
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 );
3069+ const v8::SourceLocation&) override {
3070+ v8__Platform__CustomTaskRunner__PostTask (context_, task.release ());
30483071 }
30493072 void PostNonNestableTaskImpl (std::unique_ptr<v8::Task> task,
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 );
3073+ const v8::SourceLocation&) override {
3074+ v8__Platform__CustomTaskRunner__PostNonNestableTask (context_,
3075+ task.release ());
30543076 }
30553077 void PostDelayedTaskImpl (std::unique_ptr<v8::Task> task,
30563078 double 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 );
3079+ const v8::SourceLocation&) override {
3080+ v8__Platform__CustomTaskRunner__PostDelayedTask (context_, task.release (),
3081+ delay_in_seconds);
30623082 }
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 );
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);
30713088 }
30723089 void PostIdleTaskImpl (std::unique_ptr<v8::IdleTask> task,
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 );
3090+ const v8::SourceLocation&) override {
3091+ v8__Platform__CustomTaskRunner__PostIdleTask (context_, task.release ());
30773092 }
30783093
30793094 private:
3080- std::shared_ptr<v8::TaskRunner> wrapped_;
30813095 void * context_;
3082- v8::Isolate* isolate_;
30833096};
30843097
3085- // Generic Platform subclass that delegates virtual method overrides to a
3086- // Rust PlatformImpl trait object, following the inspector API pattern.
3098+ // Platform subclass that delegates GetForegroundTaskRunner to a Rust
3099+ // PlatformImpl trait object, following the inspector API pattern.
30873100class CustomPlatform : public v8 ::platform::DefaultPlatform {
30883101 using IdleTaskSupport = v8::platform::IdleTaskSupport;
30893102
@@ -3093,26 +3106,30 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
30933106 : DefaultPlatform(thread_pool_size, idle_task_support),
30943107 context_ (context) {}
30953108
3096- ~CustomPlatform () override {
3097- v8__Platform__CustomPlatform__BASE__DROP (context_);
3098- }
3109+ ~CustomPlatform () override { v8__Platform__CustomPlatform__DROP (context_); }
30993110
31003111 std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner (
31013112 v8::Isolate* isolate, v8::TaskPriority priority) override {
3102- auto original = DefaultPlatform::GetForegroundTaskRunner (isolate, priority);
31033113 std::lock_guard<std::mutex> lock (mutex_);
31043114 auto key = std::make_pair (isolate, priority);
31053115 auto it = runners_.find (key);
31063116 if (it != runners_.end ()) {
31073117 auto runner = it->second .lock ();
31083118 if (runner) return runner;
31093119 }
3110- auto custom =
3111- std::make_shared<CustomTaskRunner>(original, context_, isolate);
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);
31123127 runners_[key] = custom;
31133128 return custom;
31143129 }
31153130
3131+ // Intentionally hides DefaultPlatform::NotifyIsolateShutdown (not virtual)
3132+ // to clean up the runner cache for the isolate.
31163133 void NotifyIsolateShutdown (v8::Isolate* isolate) {
31173134 {
31183135 std::lock_guard<std::mutex> lock (mutex_);
@@ -3124,8 +3141,6 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31243141 }
31253142 }
31263143 }
3127- v8__Platform__CustomPlatform__BASE__onIsolateShutdown (
3128- context_, static_cast <void *>(isolate));
31293144 DefaultPlatform::NotifyIsolateShutdown (isolate);
31303145 }
31313146
0 commit comments