From db5bbf6e43235bbe09a2ed9474ae3d6e2aa0b213 Mon Sep 17 00:00:00 2001 From: Bert Belder Date: Thu, 23 Apr 2020 20:14:53 +0200 Subject: [PATCH] Move 'create_param_allocations' from OwnedIsolate to IsolateAnnex (#365) --- src/isolate.rs | 30 ++++++++++++++---------------- src/snapshot.rs | 2 +- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/src/isolate.rs b/src/isolate.rs index c26a9ea4..3fb73c6e 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -145,9 +145,8 @@ impl Isolate { crate::V8::assert_initialized(); let (raw_create_params, create_param_allocations) = params.finalize(); let cxx_isolate = unsafe { v8__Isolate__New(&raw_create_params) }; - let mut owned_isolate = - OwnedIsolate::new(cxx_isolate, create_param_allocations); - owned_isolate.create_annex(); + let mut owned_isolate = OwnedIsolate::new(cxx_isolate); + owned_isolate.create_annex(create_param_allocations); owned_isolate } @@ -160,8 +159,8 @@ impl Isolate { IsolateHandle::new(self) } - fn create_annex(&mut self) { - let annex_arc = Arc::new(IsolateAnnex::new(self)); + fn create_annex(&mut self, create_param_allocations: Box) { + let annex_arc = Arc::new(IsolateAnnex::new(self, create_param_allocations)); let annex_ptr = Arc::into_raw(annex_arc); unsafe { v8__Isolate__SetData(self, 0, annex_ptr as *mut c_void) } } @@ -362,7 +361,8 @@ impl Isolate { annex.isolate = null_mut(); } - // Clear slots. + // Clear slots and drop owned objects that were taken out of `CreateParams`. + annex.create_param_allocations = Box::new(()); annex.slots.clear(); // Subtract one from the Arc reference count. @@ -404,6 +404,7 @@ impl Isolate { } pub(crate) struct IsolateAnnex { + create_param_allocations: Box, slots: HashMap>>, // The `isolate` and `isolate_mutex` fields are there so an `IsolateHandle` // (which may outlive the isolate itself) can determine whether the isolate is @@ -417,8 +418,12 @@ pub(crate) struct IsolateAnnex { } impl IsolateAnnex { - fn new(isolate: &mut Isolate) -> Self { + fn new( + isolate: &mut Isolate, + create_param_allocations: Box, + ) -> Self { Self { + create_param_allocations, slots: HashMap::new(), isolate, isolate_mutex: Mutex::new(()), @@ -531,19 +536,12 @@ impl IsolateHandle { /// Same as Isolate but gets disposed when it goes out of scope. pub struct OwnedIsolate { cxx_isolate: NonNull, - create_param_allocations: Box, } impl OwnedIsolate { - pub(crate) fn new( - cxx_isolate: *mut Isolate, - create_param_allocations: Box, - ) -> Self { + pub(crate) fn new(cxx_isolate: *mut Isolate) -> Self { let cxx_isolate = NonNull::new(cxx_isolate).unwrap(); - Self { - cxx_isolate, - create_param_allocations, - } + Self { cxx_isolate } } } diff --git a/src/snapshot.rs b/src/snapshot.rs index 4c0967ba..06693862 100644 --- a/src/snapshot.rs +++ b/src/snapshot.rs @@ -133,6 +133,6 @@ impl SnapshotCreator { // revisited after the libdeno integration is complete. pub unsafe fn get_owned_isolate(&mut self) -> OwnedIsolate { let isolate_ptr = v8__SnapshotCreator__GetIsolate(self); - OwnedIsolate::new(isolate_ptr, Box::new(())) + OwnedIsolate::new(isolate_ptr) } }