0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-24 08:09:16 -05:00

Implement Clone for SharedRef<T> (#241)

This commit is contained in:
Bert Belder 2020-01-22 21:53:19 +01:00
parent ab3a086132
commit bf128554fc
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
4 changed files with 32 additions and 2 deletions

View file

@ -49,6 +49,9 @@ extern "C" {
fn v8__BackingStore__IsShared(this: *const BackingStore) -> bool; fn v8__BackingStore__IsShared(this: *const BackingStore) -> bool;
fn v8__BackingStore__DELETE(this: &mut BackingStore); fn v8__BackingStore__DELETE(this: &mut BackingStore);
fn std__shared_ptr__v8__BackingStore__COPY(
ptr: *const SharedRef<BackingStore>,
) -> SharedRef<BackingStore>;
fn std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr( fn std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr(
unique: UniqueRef<BackingStore>, unique: UniqueRef<BackingStore>,
) -> SharedRef<BackingStore>; ) -> SharedRef<BackingStore>;
@ -180,6 +183,9 @@ impl Delete for BackingStore {
} }
impl Shared for BackingStore { impl Shared for BackingStore {
fn clone(ptr: *const SharedRef<Self>) -> SharedRef<Self> {
unsafe { std__shared_ptr__v8__BackingStore__COPY(ptr) }
}
fn from_unique(unique: UniqueRef<Self>) -> SharedRef<Self> { fn from_unique(unique: UniqueRef<Self>) -> SharedRef<Self> {
unsafe { unsafe {
std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr(unique) std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr(unique)

View file

@ -592,6 +592,11 @@ bool v8__BackingStore__IsShared(const v8::BackingStore& self) {
void v8__BackingStore__DELETE(v8::BackingStore& self) { delete &self; } void v8__BackingStore__DELETE(v8::BackingStore& self) { delete &self; }
two_pointers_t std__shared_ptr__v8__BackingStore__COPY(
const std::shared_ptr<v8::BackingStore>& ptr) {
return make_pod<two_pointers_t>(ptr);
}
two_pointers_t std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr( two_pointers_t std__shared_ptr__v8__BackingStore__CONVERT__std__unique_ptr(
v8::BackingStore* ptr) { v8::BackingStore* ptr) {
return make_pod<two_pointers_t>(std::shared_ptr<v8::BackingStore>(ptr)); return make_pod<two_pointers_t>(std::shared_ptr<v8::BackingStore>(ptr));

View file

@ -161,6 +161,7 @@ pub trait Shared
where where
Self: Delete + 'static, Self: Delete + 'static,
{ {
fn clone(shared_ptr: *const SharedRef<Self>) -> SharedRef<Self>;
fn from_unique(unique: UniqueRef<Self>) -> SharedRef<Self>; fn from_unique(unique: UniqueRef<Self>) -> SharedRef<Self>;
fn deref(shared_ptr: *const SharedRef<Self>) -> *mut Self; fn deref(shared_ptr: *const SharedRef<Self>) -> *mut Self;
fn reset(shared_ptr: *mut SharedRef<Self>); fn reset(shared_ptr: *mut SharedRef<Self>);
@ -185,6 +186,15 @@ where
} }
} }
impl<T> Clone for SharedRef<T>
where
T: Shared,
{
fn clone(&self) -> Self {
<T as Shared>::clone(self)
}
}
impl<T> From<UniqueRef<T>> for SharedRef<T> impl<T> From<UniqueRef<T>> for SharedRef<T>
where where
T: Delete + Shared, T: Delete + Shared,

View file

@ -342,11 +342,20 @@ fn array_buffer_with_shared_backing_store() {
let bs4 = ab2.get_backing_store(); let bs4 = ab2.get_backing_store();
assert_eq!(ab2.byte_length(), bs4.byte_length()); assert_eq!(ab2.byte_length(), bs4.byte_length());
assert_eq!(4, v8::SharedRef::use_count(&bs4));
assert_eq!(4, v8::SharedRef::use_count(&bs3)); assert_eq!(4, v8::SharedRef::use_count(&bs3));
assert_eq!(4, v8::SharedRef::use_count(&bs4));
let bs5 = bs4.clone();
assert_eq!(5, v8::SharedRef::use_count(&bs3));
assert_eq!(5, v8::SharedRef::use_count(&bs4));
assert_eq!(5, v8::SharedRef::use_count(&bs5));
drop(bs3); drop(bs3);
assert_eq!(3, v8::SharedRef::use_count(&bs4)); assert_eq!(4, v8::SharedRef::use_count(&bs4));
assert_eq!(4, v8::SharedRef::use_count(&bs4));
drop(bs4);
assert_eq!(3, v8::SharedRef::use_count(&bs5));
} }
} }