Skip to content

Commit 1b1694c

Browse files
committed
magic?
1 parent a56cc25 commit 1b1694c

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

src/binding.cc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3096,12 +3096,30 @@ class CustomTaskRunner final : public v8::TaskRunner {
30963096
class CustomPlatform : public v8::platform::DefaultPlatform {
30973097
using IdleTaskSupport = v8::platform::IdleTaskSupport;
30983098

3099+
// Magic value used to identify CustomPlatform instances at runtime.
3100+
// v8::platform::NotifyIsolateShutdown does static_cast<DefaultPlatform*>
3101+
// which bypasses our non-virtual NotifyIsolateShutdown, so the FFI
3102+
// wrapper needs to detect CustomPlatform and dispatch correctly.
3103+
static constexpr uint64_t kMagic = 0x4375'7374'506C'6174; // "CustPlat"
3104+
30993105
public:
31003106
CustomPlatform(int thread_pool_size, IdleTaskSupport idle_task_support,
31013107
void* context)
31023108
: DefaultPlatform(thread_pool_size, idle_task_support),
3109+
magic_(kMagic),
31033110
context_(context) {}
31043111

3112+
static bool IsCustomPlatform(v8::Platform* platform) {
3113+
auto* dp = static_cast<DefaultPlatform*>(platform);
3114+
auto* maybe_custom = static_cast<CustomPlatform*>(dp);
3115+
return maybe_custom->magic_ == kMagic;
3116+
}
3117+
3118+
static CustomPlatform* Cast(v8::Platform* platform) {
3119+
return static_cast<CustomPlatform*>(
3120+
static_cast<DefaultPlatform*>(platform));
3121+
}
3122+
31053123
// SAFETY: The platform is single-owner (via unique_ptr). The destructor
31063124
// runs after all isolates have been disposed and no more task runner
31073125
// callbacks can fire, so DROP does not race with other callbacks.
@@ -3152,6 +3170,7 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31523170
}
31533171

31543172
private:
3173+
uint64_t magic_;
31553174
void* context_;
31563175
std::mutex mutex_;
31573176
// weak_ptr so runners are kept alive only while V8 holds a reference.
@@ -3246,7 +3265,15 @@ void v8__Platform__RunIdleTasks(v8::Platform* platform, v8::Isolate* isolate,
32463265

32473266
void v8__Platform__NotifyIsolateShutdown(v8::Platform* platform,
32483267
v8::Isolate* isolate) {
3249-
v8::platform::NotifyIsolateShutdown(platform, isolate);
3268+
// v8::platform::NotifyIsolateShutdown does static_cast<DefaultPlatform*>
3269+
// and calls the non-virtual NotifyIsolateShutdown, which would bypass
3270+
// CustomPlatform's override. Dispatch to CustomPlatform directly when
3271+
// applicable so the Rust callback fires.
3272+
if (CustomPlatform::IsCustomPlatform(platform)) {
3273+
CustomPlatform::Cast(platform)->NotifyIsolateShutdown(isolate);
3274+
} else {
3275+
v8::platform::NotifyIsolateShutdown(platform, isolate);
3276+
}
32503277
}
32513278

32523279
void v8__Platform__DELETE(v8::Platform* self) { delete self; }

0 commit comments

Comments
 (0)