0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-26 09:13:46 -05:00

Change HandleScope::enter to take &Isolate, make v8::Locker into opaque blob

This commit is contained in:
Ryan Dahl 2019-12-19 16:58:39 -05:00 committed by Ry Dahl
parent 11ba352065
commit 71140b4cc4
6 changed files with 61 additions and 56 deletions

View file

@ -21,6 +21,9 @@ static_assert(sizeof(HandleScope) == sizeof(size_t) * 3,
static_assert(sizeof(v8::PromiseRejectMessage) == sizeof(size_t) * 3, static_assert(sizeof(v8::PromiseRejectMessage) == sizeof(size_t) * 3,
"PromiseRejectMessage size mismatch"); "PromiseRejectMessage size mismatch");
static_assert(sizeof(v8::Locker) == sizeof(size_t) * 2,
"Locker size mismatch");
extern "C" { extern "C" {
void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv) { void v8__V8__SetFlagsFromCommandLine(int* argc, char** argv) {

View file

@ -37,7 +37,7 @@ extern "C" {
fn v8__ReturnValue__Set(rv: *mut ReturnValue, value: *mut Value) -> (); fn v8__ReturnValue__Set(rv: *mut ReturnValue, value: *mut Value) -> ();
fn v8__ReturnValue__Get(rv: *mut ReturnValue) -> *mut Value; fn v8__ReturnValue__Get(rv: *mut ReturnValue) -> *mut Value;
fn v8__ReturnValue__GetIsolate(rv: *mut ReturnValue) -> *mut Isolate; fn v8__ReturnValue__GetIsolate(rv: &ReturnValue) -> *mut Isolate;
} }
#[repr(C)] #[repr(C)]
@ -55,8 +55,8 @@ impl ReturnValue {
} }
/// Convenience getter for Isolate /// Convenience getter for Isolate
pub fn get_isolate(&mut self) -> *mut Isolate { pub fn get_isolate(&self) -> &Isolate {
unsafe { v8__ReturnValue__GetIsolate(&mut *self) } unsafe { v8__ReturnValue__GetIsolate(self).as_ref().unwrap() }
} }
/// Getter. Creates a new Local<> so it comes with a certain performance /// Getter. Creates a new Local<> so it comes with a certain performance
@ -84,7 +84,7 @@ impl FunctionCallbackInfo {
} }
/// The current Isolate. /// The current Isolate.
pub fn get_isolate(&self) -> &mut Isolate { pub fn get_isolate(&self) -> &Isolate {
unsafe { v8__FunctionCallbackInfo__GetIsolate(self) } unsafe { v8__FunctionCallbackInfo__GetIsolate(self) }
} }

View file

@ -7,7 +7,7 @@ use crate::isolate::LockedIsolate;
extern "C" { extern "C" {
fn v8__HandleScope__CONSTRUCT( fn v8__HandleScope__CONSTRUCT(
buf: &mut MaybeUninit<HandleScope>, buf: &mut MaybeUninit<HandleScope>,
isolate: &mut Isolate, isolate: &Isolate,
); );
fn v8__HandleScope__DESTRUCT(this: &mut HandleScope); fn v8__HandleScope__DESTRUCT(this: &mut HandleScope);
fn v8__HandleScope__GetIsolate<'sc>( fn v8__HandleScope__GetIsolate<'sc>(
@ -19,12 +19,12 @@ extern "C" {
pub struct HandleScope<'sc>([usize; 3], PhantomData<&'sc mut ()>); pub struct HandleScope<'sc>([usize; 3], PhantomData<&'sc mut ()>);
impl<'sc> HandleScope<'sc> { impl<'sc> HandleScope<'sc> {
pub fn enter<P>(parent: &mut P, mut f: impl FnMut(&mut HandleScope<'_>) -> ()) pub fn enter(
where isolate: &Isolate,
P: LockedIsolate, mut f: impl FnMut(&mut HandleScope<'_>) -> (),
{ ) {
let mut scope: MaybeUninit<Self> = MaybeUninit::uninit(); let mut scope: MaybeUninit<Self> = MaybeUninit::uninit();
unsafe { v8__HandleScope__CONSTRUCT(&mut scope, parent.cxx_isolate()) }; unsafe { v8__HandleScope__CONSTRUCT(&mut scope, isolate) };
let scope = unsafe { &mut *(scope.as_mut_ptr()) }; let scope = unsafe { &mut *(scope.as_mut_ptr()) };
f(scope); f(scope);

View file

@ -2,7 +2,6 @@ use std::marker::PhantomData;
use std::mem::MaybeUninit; use std::mem::MaybeUninit;
use crate::isolate::Isolate; use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
// class Locker { // class Locker {
// public: // public:
@ -18,12 +17,7 @@ extern "C" {
} }
#[repr(C)] #[repr(C)]
pub struct Locker<'a> { pub struct Locker<'sc>([usize; 2], PhantomData<&'sc mut ()>);
has_lock: bool,
top_level: bool,
isolate: &'a mut Isolate,
phantom: PhantomData<&'a Isolate>,
}
impl<'a> Locker<'a> { impl<'a> Locker<'a> {
pub fn new(isolate: &Isolate) -> Self { pub fn new(isolate: &Isolate) -> Self {
@ -40,9 +34,3 @@ impl<'a> Drop for Locker<'a> {
unsafe { v8__Locker__DESTRUCT(self) } unsafe { v8__Locker__DESTRUCT(self) }
} }
} }
impl<'a> LockedIsolate for Locker<'a> {
fn cxx_isolate(&mut self) -> &mut Isolate {
self.isolate
}
}

View file

@ -61,7 +61,7 @@ impl Object {
} }
/// Return the isolate to which the Object belongs to. /// Return the isolate to which the Object belongs to.
pub fn get_isolate(&self) -> &mut Isolate { pub fn get_isolate(&self) -> &Isolate {
unsafe { v8__Object__GetIsolate(self) } unsafe { v8__Object__GetIsolate(self) }
} }
} }

View file

@ -46,10 +46,11 @@ fn handle_scope_nested() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |_scope1| {
v8::HandleScope::enter(scope, |_scope| {}); v8::HandleScope::enter(&isolate, |_scope2| {});
}); });
drop(locker);
drop(g); drop(g);
} }
@ -62,11 +63,11 @@ fn handle_scope_numbers() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let l1 = v8::Integer::new(scope, -123); let l1 = v8::Integer::new(scope, -123);
let l2 = v8::Integer::new_from_unsigned(scope, 456); let l2 = v8::Integer::new_from_unsigned(scope, 456);
v8::HandleScope::enter(scope, |scope2| { v8::HandleScope::enter(&isolate, |scope2| {
let l3 = v8::Number::new(scope2, 78.9); let l3 = v8::Number::new(scope2, 78.9);
assert_eq!(l1.value(), -123); assert_eq!(l1.value(), -123);
assert_eq!(l2.value(), 456); assert_eq!(l2.value(), 456);
@ -75,6 +76,7 @@ fn handle_scope_numbers() {
assert_eq!(v8::Number::value(&l2), 456f64); assert_eq!(v8::Number::value(&l2), 456f64);
}); });
}); });
drop(locker);
drop(g); drop(g);
} }
@ -86,8 +88,8 @@ fn test_string() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let reference = "Hello 🦕 world!"; let reference = "Hello 🦕 world!";
let local = let local =
v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap(); v8::String::new(scope, reference, v8::NewStringType::Normal).unwrap();
@ -95,6 +97,7 @@ fn test_string() {
assert_eq!(17, local.utf8_length(scope)); assert_eq!(17, local.utf8_length(scope));
assert_eq!(reference, local.to_rust_string_lossy(scope)); assert_eq!(reference, local.to_rust_string_lossy(scope));
}); });
drop(locker);
} }
#[test] #[test]
@ -117,9 +120,9 @@ fn script_compile_and_run() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| { v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s); let mut context = v8::Context::new(s);
context.enter(); context.enter();
let source = let source =
@ -134,6 +137,7 @@ fn script_compile_and_run() {
assert_eq!(result.to_rust_string_lossy(s), "Hello 13th planet"); assert_eq!(result.to_rust_string_lossy(s), "Hello 13th planet");
context.exit(); context.exit();
}); });
drop(locker);
} }
#[test] #[test]
@ -144,9 +148,9 @@ fn script_origin() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| { v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s); let mut context = v8::Context::new(s);
context.enter(); context.enter();
@ -181,6 +185,7 @@ fn script_origin() {
let _result = script.run(s, context).unwrap(); let _result = script.run(s, context).unwrap();
context.exit(); context.exit();
}); });
drop(locker);
} }
#[test] #[test]
@ -237,8 +242,8 @@ fn test_primitives() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let null = v8::new_null(scope); let null = v8::new_null(scope);
assert!(!null.is_undefined()); assert!(!null.is_undefined());
assert!(null.is_null()); assert!(null.is_null());
@ -259,6 +264,7 @@ fn test_primitives() {
assert!(!false_.is_null()); assert!(!false_.is_null());
assert!(!false_.is_null_or_undefined()); assert!(!false_.is_null_or_undefined());
}); });
drop(locker);
} }
#[test] #[test]
@ -269,9 +275,9 @@ fn exception() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let mut isolate = v8::Isolate::new(params); let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
isolate.enter(); isolate.enter();
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let reference = "This is a test error"; let reference = "This is a test error";
@ -292,6 +298,7 @@ fn exception() {
assert!(v8::Exception::GetStackTrace(exception).is_none()); assert!(v8::Exception::GetStackTrace(exception).is_none());
context.exit(); context.exit();
}); });
drop(locker);
isolate.exit(); isolate.exit();
} }
@ -303,8 +310,8 @@ fn json() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| { v8::HandleScope::enter(&isolate, |s| {
let mut context = v8::Context::new(s); let mut context = v8::Context::new(s);
context.enter(); context.enter();
let json_string = let json_string =
@ -319,6 +326,7 @@ fn json() {
assert_eq!("{\"a\":1,\"b\":2}".to_string(), rust_str); assert_eq!("{\"a\":1,\"b\":2}".to_string(), rust_str);
context.exit(); context.exit();
}); });
drop(locker);
} }
// TODO Safer casts https://github.com/denoland/rusty_v8/issues/51 // TODO Safer casts https://github.com/denoland/rusty_v8/issues/51
@ -335,8 +343,8 @@ fn object() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let null: v8::Local<v8::Value> = new_null(scope).into(); let null: v8::Local<v8::Value> = new_null(scope).into();
@ -352,6 +360,7 @@ fn object() {
assert!(!object.is_null_or_undefined()); assert!(!object.is_null_or_undefined());
context.exit(); context.exit();
}); });
drop(locker);
} }
#[test] #[test]
@ -362,8 +371,8 @@ fn promise_resolved() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let maybe_resolver = v8::PromiseResolver::new(context); let maybe_resolver = v8::PromiseResolver::new(context);
@ -391,6 +400,7 @@ fn promise_resolved() {
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string()); assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
context.exit(); context.exit();
}); });
drop(locker);
} }
#[test] #[test]
@ -401,8 +411,8 @@ fn promise_rejected() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let maybe_resolver = v8::PromiseResolver::new(context); let maybe_resolver = v8::PromiseResolver::new(context);
@ -431,12 +441,13 @@ fn promise_rejected() {
assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string()); assert_eq!(result_str.to_rust_string_lossy(scope), "test".to_string());
context.exit(); context.exit();
}); });
drop(locker);
} }
extern "C" fn fn_callback(info: &FunctionCallbackInfo) { extern "C" fn fn_callback(info: &FunctionCallbackInfo) {
assert_eq!(info.length(), 0); assert_eq!(info.length(), 0);
let mut locker = v8::Locker::new(info.get_isolate()); let isolate = info.get_isolate();
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let s = let s =
@ -459,8 +470,8 @@ fn function() {
v8::array_buffer::Allocator::new_default_allocator(), v8::array_buffer::Allocator::new_default_allocator(),
); );
let isolate = v8::Isolate::new(params); let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let global = context.global(); let global = context.global();
@ -482,6 +493,7 @@ fn function() {
assert_eq!(rust_str, "Hello callback!".to_string()); assert_eq!(rust_str, "Hello callback!".to_string());
context.exit(); context.exit();
}); });
drop(locker);
} }
extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) { extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) {
@ -492,12 +504,13 @@ extern "C" fn promise_reject_callback(msg: v8::PromiseRejectMessage) {
let promise_obj: v8::Local<v8::Object> = cast(promise); let promise_obj: v8::Local<v8::Object> = cast(promise);
let isolate = promise_obj.get_isolate(); let isolate = promise_obj.get_isolate();
let value = msg.get_value(); let value = msg.get_value();
let mut locker = v8::Locker::new(isolate); let locker = v8::Locker::new(isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let value_str: v8::Local<v8::String> = cast(value); let value_str: v8::Local<v8::String> = cast(value);
let rust_str = value_str.to_rust_string_lossy(scope); let rust_str = value_str.to_rust_string_lossy(scope);
assert_eq!(rust_str, "promise rejected".to_string()); assert_eq!(rust_str, "promise rejected".to_string());
}); });
drop(locker);
} }
#[test] #[test]
@ -510,8 +523,8 @@ fn set_promise_reject_callback() {
let mut isolate = v8::Isolate::new(params); let mut isolate = v8::Isolate::new(params);
isolate.set_promise_reject_callback(promise_reject_callback); isolate.set_promise_reject_callback(promise_reject_callback);
isolate.enter(); isolate.enter();
let mut locker = v8::Locker::new(&isolate); let locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| { v8::HandleScope::enter(&isolate, |scope| {
let mut context = v8::Context::new(scope); let mut context = v8::Context::new(scope);
context.enter(); context.enter();
let mut resolver = v8::PromiseResolver::new(context).unwrap(); let mut resolver = v8::PromiseResolver::new(context).unwrap();
@ -522,5 +535,6 @@ fn set_promise_reject_callback() {
resolver.reject(context, value); resolver.reject(context, value);
context.exit(); context.exit();
}); });
drop(locker);
isolate.exit(); isolate.exit();
} }