Skip to content

Commit 6f80709

Browse files
authored
Merge pull request #6312 from cloudflare/yagiz/fix-v8-global-rust
use v8 copy constructor for v8 global
2 parents 15392d4 + 3204893 commit 6f80709

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

src/rust/jsg/ffi.c++

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@ void global_drop(Global value) {
386386
global_from_ffi<v8::Value>(kj::mv(value));
387387
}
388388

389-
Global global_clone(const Global& value) {
390-
return Global{.ptr = value.ptr};
389+
Global global_clone(Isolate* isolate, const Global& value) {
390+
auto& original = global_as_ref_from_ffi<v8::Value>(value);
391+
return to_ffi(v8::Global<v8::Value>(isolate, original));
391392
}
392393

393394
Local global_to_local(Isolate* isolate, const Global& value) {

src/rust/jsg/ffi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ uint64_t local_biguint64_array_get(Isolate* isolate, const Local& array, size_t
103103

104104
// Global<T>
105105
void global_drop(Global value);
106-
Global global_clone(const Global& value);
106+
Global global_clone(Isolate* isolate, const Global& value);
107107
Local global_to_local(Isolate* isolate, const Global& value);
108108
void global_make_weak(
109109
Isolate* isolate, Global* value, size_t /* void* */ data, WeakCallback callback);

src/rust/jsg/v8.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub mod ffi {
239239

240240
// Global<T>
241241
pub unsafe fn global_drop(value: Global);
242-
pub unsafe fn global_clone(value: &Global) -> Global;
242+
pub unsafe fn global_clone(isolate: *mut Isolate, value: &Global) -> Global;
243243
pub unsafe fn global_to_local(isolate: *mut Isolate, value: &Global) -> Local;
244244
pub unsafe fn global_make_weak(
245245
isolate: *mut Isolate,
@@ -1168,9 +1168,18 @@ impl<T> Drop for Global<T> {
11681168
}
11691169
}
11701170

1171-
impl<T> Clone for Global<T> {
1172-
fn clone(&self) -> Self {
1173-
unsafe { ffi::global_clone(&self.handle).into() }
1171+
// Note: Global<T> intentionally does NOT implement the std Clone trait.
1172+
// Cloning a V8 persistent handle requires an isolate pointer to create an
1173+
// independent handle via v8::Global::New(isolate, original). The Clone trait
1174+
// cannot provide this. Use the `clone()` method directly instead.
1175+
impl<T> Global<T> {
1176+
/// Creates an independent copy of this persistent handle.
1177+
///
1178+
/// This properly creates a new V8 persistent handle that references the same
1179+
/// JS object. Both the original and clone can be independently dropped.
1180+
#[must_use]
1181+
pub fn clone(&self, lock: &mut Lock) -> Self {
1182+
unsafe { ffi::global_clone(lock.isolate().as_ffi(), &self.handle).into() }
11741183
}
11751184
}
11761185

0 commit comments

Comments
 (0)