@@ -3012,20 +3012,21 @@ v8::StartupData v8__SnapshotCreator__CreateBlob(
30123012 return self->CreateBlob (function_code_handling);
30133013}
30143014
3015- // Callback type: called from any thread when a foreground task is posted
3016- // for a given isolate. The void* is the raw isolate pointer. delay_in_seconds
3017- // is 0.0 for immediate tasks, or the delay before the task should be executed.
3018- using ForegroundTaskPostedCallback = void (*)(void * isolate_ptr,
3019- double delay_in_seconds);
3015+ // Rust-side callbacks for the trait-based NotifyingPlatform.
3016+ // `context` is a pointer to a Rust Box<dyn ForegroundTaskCallback>.
3017+ extern " C" {
3018+ void v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3019+ void * context, void * isolate, double delay_in_seconds);
3020+ void v8__Platform__NotifyingPlatform__dropContext (void * context);
3021+ }
30203022
30213023// TaskRunner wrapper that intercepts all PostTask* calls and notifies
3022- // the embedder before delegating to the real runner.
3024+ // the embedder (via a Rust trait) before delegating to the real runner.
30233025class NotifyingTaskRunner final : public v8::TaskRunner {
30243026 public:
3025- NotifyingTaskRunner (std::shared_ptr<v8::TaskRunner> wrapped,
3026- ForegroundTaskPostedCallback callback,
3027+ NotifyingTaskRunner (std::shared_ptr<v8::TaskRunner> wrapped, void * context,
30273028 v8::Isolate* isolate)
3028- : wrapped_(std::move(wrapped)), callback_(callback ), isolate_(isolate) {}
3029+ : wrapped_(std::move(wrapped)), context_(context ), isolate_(isolate) {}
30293030
30303031 bool IdleTasksEnabled () override { return wrapped_->IdleTasksEnabled (); }
30313032 bool NonNestableTasksEnabled () const override {
@@ -3039,50 +3040,59 @@ class NotifyingTaskRunner final : public v8::TaskRunner {
30393040 void PostTaskImpl (std::unique_ptr<v8::Task> task,
30403041 const v8::SourceLocation& location) override {
30413042 wrapped_->PostTask (std::move (task), location);
3042- callback_ (static_cast <void *>(isolate_), 0.0 );
3043+ v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3044+ context_, static_cast <void *>(isolate_), 0.0 );
30433045 }
30443046 void PostNonNestableTaskImpl (std::unique_ptr<v8::Task> task,
30453047 const v8::SourceLocation& location) override {
30463048 wrapped_->PostNonNestableTask (std::move (task), location);
3047- callback_ (static_cast <void *>(isolate_), 0.0 );
3049+ v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3050+ context_, static_cast <void *>(isolate_), 0.0 );
30483051 }
30493052 void PostDelayedTaskImpl (std::unique_ptr<v8::Task> task,
30503053 double delay_in_seconds,
30513054 const v8::SourceLocation& location) override {
30523055 wrapped_->PostDelayedTask (std::move (task), delay_in_seconds, location);
3053- callback_ (static_cast <void *>(isolate_),
3054- delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
3056+ v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3057+ context_, static_cast <void *>(isolate_),
3058+ delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
30553059 }
30563060 void PostNonNestableDelayedTaskImpl (
30573061 std::unique_ptr<v8::Task> task, double delay_in_seconds,
30583062 const v8::SourceLocation& location) override {
30593063 wrapped_->PostNonNestableDelayedTask (std::move (task), delay_in_seconds,
30603064 location);
3061- callback_ (static_cast <void *>(isolate_),
3062- delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
3065+ v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3066+ context_, static_cast <void *>(isolate_),
3067+ delay_in_seconds > 0 ? delay_in_seconds : 0.0 );
30633068 }
30643069 void PostIdleTaskImpl (std::unique_ptr<v8::IdleTask> task,
30653070 const v8::SourceLocation& location) override {
30663071 wrapped_->PostIdleTask (std::move (task), location);
3067- callback_ (static_cast <void *>(isolate_), 0.0 );
3072+ v8__Platform__NotifyingPlatform__onForegroundTaskPosted (
3073+ context_, static_cast <void *>(isolate_), 0.0 );
30683074 }
30693075
30703076 private:
30713077 std::shared_ptr<v8::TaskRunner> wrapped_;
3072- ForegroundTaskPostedCallback callback_ ;
3078+ void * context_ ;
30733079 v8::Isolate* isolate_;
30743080};
30753081
30763082// Platform wrapper that intercepts GetForegroundTaskRunner to return
3077- // NotifyingTaskRunner instances, notifying the embedder of foreground tasks .
3083+ // NotifyingTaskRunner instances, dispatching to a Rust trait object .
30783084class NotifyingPlatform : public v8 ::platform::DefaultPlatform {
30793085 using IdleTaskSupport = v8::platform::IdleTaskSupport;
30803086
30813087 public:
30823088 NotifyingPlatform (int thread_pool_size, IdleTaskSupport idle_task_support,
3083- ForegroundTaskPostedCallback callback )
3089+ void * context )
30843090 : DefaultPlatform(thread_pool_size, idle_task_support),
3085- callback_ (callback) {}
3091+ context_ (context) {}
3092+
3093+ ~NotifyingPlatform () override {
3094+ v8__Platform__NotifyingPlatform__dropContext (context_);
3095+ }
30863096
30873097 std::shared_ptr<v8::TaskRunner> GetForegroundTaskRunner (
30883098 v8::Isolate* isolate, v8::TaskPriority priority) override {
@@ -3095,12 +3105,12 @@ class NotifyingPlatform : public v8::platform::DefaultPlatform {
30953105 if (runner) return runner;
30963106 }
30973107 auto notifying =
3098- std::make_shared<NotifyingTaskRunner>(original, callback_ , isolate);
3108+ std::make_shared<NotifyingTaskRunner>(original, context_ , isolate);
30993109 runners_[key] = notifying;
31003110 return notifying;
31013111 }
31023112
3103- void NotifyIsolateShutdown (v8::Isolate* isolate) override {
3113+ void NotifyIsolateShutdown (v8::Isolate* isolate) {
31043114 {
31053115 std::lock_guard<std::mutex> lock (mutex_);
31063116 for (auto it = runners_.begin (); it != runners_.end ();) {
@@ -3119,7 +3129,7 @@ class NotifyingPlatform : public v8::platform::DefaultPlatform {
31193129 }
31203130
31213131 private:
3122- ForegroundTaskPostedCallback callback_ ;
3132+ void * context_ ;
31233133 std::mutex mutex_;
31243134 std::map<std::pair<v8::Isolate*, v8::TaskPriority>,
31253135 std::weak_ptr<NotifyingTaskRunner>>
@@ -3181,8 +3191,7 @@ v8::Platform* v8__Platform__NewSingleThreadedDefaultPlatform(
31813191
31823192v8::Platform* v8__Platform__NewNotifyingPlatform (int thread_pool_size,
31833193 bool idle_task_support,
3184- void (*callback)(void *,
3185- double )) {
3194+ void * context) {
31863195 if (thread_pool_size < 1 ) {
31873196 thread_pool_size = std::thread::hardware_concurrency ();
31883197 }
@@ -3191,7 +3200,7 @@ v8::Platform* v8__Platform__NewNotifyingPlatform(int thread_pool_size,
31913200 thread_pool_size,
31923201 idle_task_support ? v8::platform::IdleTaskSupport::kEnabled
31933202 : v8::platform::IdleTaskSupport::kDisabled ,
3194- callback )
3203+ context )
31953204 .release ();
31963205}
31973206
0 commit comments