Skip to content

Commit aa5d6ee

Browse files
committed
protect opt
1 parent 1062e03 commit aa5d6ee

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/binding.cc

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,8 +3101,9 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31013101

31023102
public:
31033103
CustomPlatform(int thread_pool_size, IdleTaskSupport idle_task_support,
3104-
void* context)
3104+
bool unprotected, void* context)
31053105
: DefaultPlatform(thread_pool_size, idle_task_support),
3106+
unprotected_(unprotected),
31063107
context_(context) {}
31073108

31083109
// SAFETY: The platform is single-owner (via unique_ptr). The destructor
@@ -3128,14 +3129,17 @@ class CustomPlatform : public v8::platform::DefaultPlatform {
31283129
return custom;
31293130
}
31303131

3131-
// Disable thread-isolated allocations (same as UnprotectedDefaultPlatform).
3132-
// Required when isolates may be created on threads other than the one that
3133-
// called v8::V8::Initialize (e.g. worker threads in Deno).
3132+
// When unprotected, disable thread-isolated allocations (same as
3133+
// UnprotectedDefaultPlatform). Required when isolates may be created on
3134+
// threads other than the one that called v8::V8::Initialize (e.g. worker
3135+
// threads in Deno).
31343136
v8::ThreadIsolatedAllocator* GetThreadIsolatedAllocator() override {
3135-
return nullptr;
3137+
if (unprotected_) return nullptr;
3138+
return DefaultPlatform::GetThreadIsolatedAllocator();
31363139
}
31373140

31383141
private:
3142+
bool unprotected_;
31393143
void* context_;
31403144
std::mutex mutex_;
31413145
// weak_ptr so runners are kept alive only while V8 holds a reference.
@@ -3202,7 +3206,7 @@ v8::Platform* v8__Platform__NewSingleThreadedDefaultPlatform(
32023206

32033207
v8::Platform* v8__Platform__NewCustomPlatform(int thread_pool_size,
32043208
bool idle_task_support,
3205-
void* context) {
3209+
bool unprotected, void* context) {
32063210
if (thread_pool_size < 1) {
32073211
thread_pool_size = std::thread::hardware_concurrency();
32083212
}
@@ -3211,7 +3215,7 @@ v8::Platform* v8__Platform__NewCustomPlatform(int thread_pool_size,
32113215
thread_pool_size,
32123216
idle_task_support ? v8::platform::IdleTaskSupport::kEnabled
32133217
: v8::platform::IdleTaskSupport::kDisabled,
3214-
context)
3218+
unprotected, context)
32153219
.release();
32163220
}
32173221

src/platform.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ unsafe extern "C" {
2525
fn v8__Platform__NewCustomPlatform(
2626
thread_pool_size: int,
2727
idle_task_support: bool,
28+
unprotected: bool,
2829
context: *mut std::ffi::c_void,
2930
) -> *mut Platform;
3031
fn v8__Platform__DELETE(this: *mut Platform);
@@ -245,15 +246,18 @@ pub fn new_single_threaded_default_platform(
245246
/// This follows the same pattern as
246247
/// [`V8InspectorClient::new`](crate::inspector::V8InspectorClient::new).
247248
///
248-
/// Thread-isolated allocations are disabled (same as
249-
/// `new_unprotected_default_platform`).
249+
/// When `unprotected` is true, thread-isolated allocations are disabled
250+
/// (same as `new_unprotected_default_platform`). This is required when
251+
/// isolates may be created on threads other than the one that called
252+
/// `V8::initialize`.
250253
#[inline(always)]
251254
pub fn new_custom_platform(
252255
thread_pool_size: u32,
253256
idle_task_support: bool,
257+
unprotected: bool,
254258
platform_impl: impl PlatformImpl + 'static,
255259
) -> UniqueRef<Platform> {
256-
Platform::new_custom(thread_pool_size, idle_task_support, platform_impl)
260+
Platform::new_custom(thread_pool_size, idle_task_support, unprotected, platform_impl)
257261
}
258262

259263
impl Platform {
@@ -330,6 +334,7 @@ impl Platform {
330334
pub fn new_custom(
331335
thread_pool_size: u32,
332336
idle_task_support: bool,
337+
unprotected: bool,
333338
platform_impl: impl PlatformImpl + 'static,
334339
) -> UniqueRef<Self> {
335340
// Double-box: inner Box<dyn> is a fat pointer, outer Box gives us a
@@ -342,6 +347,7 @@ impl Platform {
342347
UniqueRef::from_raw(v8__Platform__NewCustomPlatform(
343348
thread_pool_size as i32,
344349
idle_task_support,
350+
unprotected,
345351
context,
346352
))
347353
}

0 commit comments

Comments
 (0)