From 83052c45351768c2e8923d1bf557728e03fecbf6 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 19 Feb 2021 17:01:18 -0500 Subject: [PATCH] Enter isolate on construction, exit on drop Fixes #626 --- src/exception.rs | 11 ++++++----- src/isolate.rs | 25 ++++++++++++++++++++----- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/exception.rs b/src/exception.rs index 0712b4aa..9606c2c0 100644 --- a/src/exception.rs +++ b/src/exception.rs @@ -305,11 +305,12 @@ impl Exception { message: Local, contructor: unsafe extern "C" fn(*const String) -> *const Value, ) -> Local<'s, Value> { - scope.enter_isolate(); - let error = - unsafe { scope.cast_local(|_| (contructor)(&*message)) }.unwrap(); - scope.exit_isolate(); - error + unsafe { + scope.enter(); + let error = scope.cast_local(|_| (contructor)(&*message)).unwrap(); + scope.exit(); + error + } } /// Creates an error message for the given exception. diff --git a/src/isolate.rs b/src/isolate.rs index 3f5887bb..bfcb6123 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -282,6 +282,9 @@ extern "C" { /// parallel in multiple threads. An isolate can be entered by at most one /// thread at any given time. The Locker/Unlocker API must be used to /// synchronize. +/// +/// rusty_v8 note: Unlike in the C++ API, the Isolate is entered when it is +/// constructed and exited when dropped. #[repr(C)] #[derive(Debug)] pub struct Isolate(Opaque); @@ -306,6 +309,9 @@ impl Isolate { let mut owned_isolate = OwnedIsolate::new(cxx_isolate); ScopeData::new_root(&mut owned_isolate); owned_isolate.create_annex(create_param_allocations); + unsafe { + owned_isolate.enter(); + } owned_isolate } @@ -442,8 +448,11 @@ impl Isolate { /// Sets this isolate as the entered one for the current thread. /// Saves the previously entered one (if any), so that it can be /// restored when exiting. Re-entering an isolate is allowed. - pub(crate) fn enter_isolate(&mut self) { - unsafe { v8__Isolate__Enter(self) } + /// + /// rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is + /// constructed and exited when dropped. + pub unsafe fn enter(&mut self) { + v8__Isolate__Enter(self) } /// Exits this isolate by restoring the previously entered one in the @@ -451,8 +460,11 @@ impl Isolate { /// entered more than once. /// /// Requires: self == Isolate::GetCurrent(). - pub(crate) fn exit_isolate(&mut self) { - unsafe { v8__Isolate__Exit(self) } + /// + /// rusty_v8 note: Unlike in the C++ API, the isolate is entered when it is + /// constructed and exited when dropped. + pub unsafe fn exit(&mut self) { + v8__Isolate__Exit(self) } /// Clears the set of objects held strongly by the heap. This set of @@ -860,7 +872,10 @@ impl OwnedIsolate { impl Drop for OwnedIsolate { fn drop(&mut self) { - unsafe { self.cxx_isolate.as_mut().dispose() } + unsafe { + self.exit(); + self.cxx_isolate.as_mut().dispose() + } } }