0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-25 15:29:43 -05:00

Rename CxxIsolate to Isolate and Isolate to OwnedIsolate (#85)

This commit is contained in:
Ry Dahl 2019-12-19 19:15:52 -05:00 committed by GitHub
parent d2d8c64785
commit 11ba352065
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 112 additions and 108 deletions

View file

@ -47,26 +47,26 @@ Isolate* v8__Isolate__New(Isolate::CreateParams& params) {
return isolate;
}
void v8__Isolate__Dispose(Isolate& isolate) {
auto allocator = isolate.GetArrayBufferAllocator();
isolate.Dispose();
void v8__Isolate__Dispose(Isolate* isolate) {
auto allocator = isolate->GetArrayBufferAllocator();
isolate->Dispose();
delete allocator;
}
void v8__Isolate__Enter(Isolate& isolate) { isolate.Enter(); }
void v8__Isolate__Enter(Isolate* isolate) { isolate->Enter(); }
void v8__Isolate__Exit(Isolate& isolate) { isolate.Exit(); }
void v8__Isolate__Exit(Isolate* isolate) { isolate->Exit(); }
void v8__Isolate__SetPromiseRejectCallback(Isolate& isolate,
void v8__Isolate__SetPromiseRejectCallback(Isolate* isolate,
v8::PromiseRejectCallback callback) {
isolate.SetPromiseRejectCallback(callback);
isolate->SetPromiseRejectCallback(callback);
}
void v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(Isolate& isolate,
void v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(Isolate* isolate,
bool capture,
int frame_limit) {
// Note: StackTraceOptions are deprecated so we don't bother to bind to it.
isolate.SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit);
isolate->SetCaptureStackTraceForUncaughtExceptions(capture, frame_limit);
}
Isolate::CreateParams* v8__Isolate__CreateParams__NEW() {
@ -366,21 +366,18 @@ v8::Promise* v8__Promise__Then2(v8::Promise* self,
return maybe_local_to_ptr(self->Then(context, on_fulfilled, on_rejected));
}
v8::PromiseRejectEvent
v8__PromiseRejectMessage__GetEvent(const v8::PromiseRejectMessage &self)
{
v8::PromiseRejectEvent v8__PromiseRejectMessage__GetEvent(
const v8::PromiseRejectMessage& self) {
return self.GetEvent();
}
v8::Promise*
v8__PromiseRejectMessage__GetPromise(const v8::PromiseRejectMessage &self)
{
v8::Promise* v8__PromiseRejectMessage__GetPromise(
const v8::PromiseRejectMessage& self) {
return local_to_ptr(self.GetPromise());
}
v8::Value*
v8__PromiseRejectMessage__GetValue(const v8::PromiseRejectMessage &self)
{
v8::Value* v8__PromiseRejectMessage__GetValue(
const v8::PromiseRejectMessage& self) {
return local_to_ptr(self.GetValue());
}

View file

@ -1,4 +1,4 @@
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
@ -6,10 +6,10 @@ use crate::Local;
use crate::Object;
extern "C" {
fn v8__Context__New(isolate: *mut CxxIsolate) -> *mut Context;
fn v8__Context__New(isolate: *mut Isolate) -> *mut Context;
fn v8__Context__Enter(this: &mut Context);
fn v8__Context__Exit(this: &mut Context);
fn v8__Context__GetIsolate(this: &mut Context) -> *mut CxxIsolate;
fn v8__Context__GetIsolate(this: &mut Context) -> *mut Isolate;
fn v8__Context__Global(this: *mut Context) -> *mut Object;
}

View file

@ -1,6 +1,6 @@
#![allow(non_snake_case)]
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::support::int;
use crate::support::Opaque;
use crate::Local;
@ -18,7 +18,7 @@ extern "C" {
fn v8__Exception__Error(message: *mut String) -> *mut Value;
fn v8__Exception__CreateMessage(
isolate: *mut CxxIsolate,
isolate: *mut Isolate,
exception: *mut Value,
) -> *mut Message;

View file

@ -1,4 +1,4 @@
use crate::isolate::{CxxIsolate, LockedIsolate};
use crate::isolate::{Isolate, LockedIsolate};
use crate::support::{int, Opaque};
use crate::Context;
use crate::HandleScope;
@ -19,7 +19,7 @@ extern "C" {
) -> *mut Value;
fn v8__FunctionTemplate__New(
isolate: *mut CxxIsolate,
isolate: *mut Isolate,
callback: extern "C" fn(&FunctionCallbackInfo),
) -> *mut FunctionTemplate;
fn v8__FunctionTemplate__GetFunction(
@ -29,7 +29,7 @@ extern "C" {
fn v8__FunctionCallbackInfo__GetIsolate(
info: &FunctionCallbackInfo,
) -> &mut CxxIsolate;
) -> &mut Isolate;
fn v8__FunctionCallbackInfo__Length(info: &FunctionCallbackInfo) -> int;
fn v8__FunctionCallbackInfo__GetReturnValue(
info: &FunctionCallbackInfo,
@ -37,7 +37,7 @@ extern "C" {
fn v8__ReturnValue__Set(rv: *mut ReturnValue, value: *mut Value) -> ();
fn v8__ReturnValue__Get(rv: *mut ReturnValue) -> *mut Value;
fn v8__ReturnValue__GetIsolate(rv: *mut ReturnValue) -> *mut CxxIsolate;
fn v8__ReturnValue__GetIsolate(rv: *mut ReturnValue) -> *mut Isolate;
}
#[repr(C)]
@ -55,7 +55,7 @@ impl ReturnValue {
}
/// Convenience getter for Isolate
pub fn get_isolate(&mut self) -> *mut CxxIsolate {
pub fn get_isolate(&mut self) -> *mut Isolate {
unsafe { v8__ReturnValue__GetIsolate(&mut *self) }
}
@ -84,7 +84,7 @@ impl FunctionCallbackInfo {
}
/// The current Isolate.
pub fn get_isolate(&self) -> &mut CxxIsolate {
pub fn get_isolate(&self) -> &mut Isolate {
unsafe { v8__FunctionCallbackInfo__GetIsolate(self) }
}

View file

@ -1,18 +1,18 @@
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
extern "C" {
fn v8__HandleScope__CONSTRUCT(
buf: &mut MaybeUninit<HandleScope>,
isolate: &mut CxxIsolate,
isolate: &mut Isolate,
);
fn v8__HandleScope__DESTRUCT(this: &mut HandleScope);
fn v8__HandleScope__GetIsolate<'sc>(
this: &'sc HandleScope,
) -> &'sc mut CxxIsolate;
) -> &'sc mut Isolate;
}
#[repr(C)]
@ -33,7 +33,7 @@ impl<'sc> HandleScope<'sc> {
}
impl<'sc> LockedIsolate for HandleScope<'sc> {
fn cxx_isolate(&mut self) -> &mut CxxIsolate {
fn cxx_isolate(&mut self) -> &mut Isolate {
unsafe { v8__HandleScope__GetIsolate(self) }
}
}

View file

@ -1,5 +1,6 @@
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr::NonNull;
use crate::array_buffer::Allocator;
use crate::promise::PromiseRejectMessage;
@ -10,19 +11,19 @@ use crate::support::UniqueRef;
type PromiseRejectCallback = extern "C" fn(PromiseRejectMessage);
extern "C" {
fn v8__Isolate__New(params: *mut CreateParams) -> &'static mut CxxIsolate;
fn v8__Isolate__Dispose(this: &mut CxxIsolate) -> ();
fn v8__Isolate__Enter(this: &mut CxxIsolate) -> ();
fn v8__Isolate__Exit(this: &mut CxxIsolate) -> ();
fn v8__Isolate__New(params: *mut CreateParams) -> *mut Isolate;
fn v8__Isolate__Dispose(this: *mut Isolate);
fn v8__Isolate__Enter(this: *mut Isolate);
fn v8__Isolate__Exit(this: *mut Isolate);
fn v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(
this: &mut CxxIsolate,
this: *mut Isolate,
caputre: bool,
frame_limit: i32,
);
fn v8__Isolate__SetPromiseRejectCallback(
isolate: &mut CxxIsolate,
isolate: *mut Isolate,
callback: PromiseRejectCallback,
) -> ();
);
fn v8__Isolate__CreateParams__NEW() -> *mut CreateParams;
fn v8__Isolate__CreateParams__DELETE(this: &mut CreateParams);
@ -33,15 +34,12 @@ extern "C" {
}
#[repr(C)]
pub struct CxxIsolate(Opaque);
pub struct Isolate(Opaque);
pub trait LockedIsolate {
fn cxx_isolate(&mut self) -> &mut CxxIsolate;
fn cxx_isolate(&mut self) -> &mut Isolate;
}
#[repr(transparent)]
pub struct Isolate(&'static mut CxxIsolate);
impl Isolate {
/// Creates a new isolate. Does not change the currently entered
/// isolate.
@ -50,10 +48,12 @@ impl Isolate {
/// by calling V8::dispose(). Using the delete operator is not allowed.
///
/// V8::initialize() must have run prior to this.
pub fn new(params: UniqueRef<CreateParams>) -> Self {
#[allow(clippy::new_ret_no_self)]
pub fn new(params: UniqueRef<CreateParams>) -> OwnedIsolate {
// TODO: support CreateParams.
crate::V8::assert_initialized();
Self(unsafe { v8__Isolate__New(params.into_raw()) })
let isolate_ptr = unsafe { v8__Isolate__New(params.into_raw()) };
OwnedIsolate(NonNull::new(isolate_ptr).unwrap())
}
/// Initial configuration parameters for a new Isolate.
@ -65,7 +65,7 @@ impl Isolate {
/// Saves the previously entered one (if any), so that it can be
/// restored when exiting. Re-entering an isolate is allowed.
pub fn enter(&mut self) {
unsafe { v8__Isolate__Enter(self.0) }
unsafe { v8__Isolate__Enter(self) }
}
/// Exits this isolate by restoring the previously entered one in the
@ -74,7 +74,7 @@ impl Isolate {
///
/// Requires: self == Isolate::GetCurrent().
pub fn exit(&mut self) {
unsafe { v8__Isolate__Exit(self.0) }
unsafe { v8__Isolate__Exit(self) }
}
/// Tells V8 to capture current stack trace when uncaught exception occurs
@ -86,7 +86,7 @@ impl Isolate {
) {
unsafe {
v8__Isolate__SetCaptureStackTraceForUncaughtExceptions(
self.0,
self,
capture,
frame_limit,
)
@ -99,26 +99,35 @@ impl Isolate {
&mut self,
callback: PromiseRejectCallback,
) {
unsafe { v8__Isolate__SetPromiseRejectCallback(self.0, callback) }
unsafe { v8__Isolate__SetPromiseRejectCallback(self, callback) }
}
/// Disposes the isolate. The isolate must not be entered by any
/// thread to be disposable.
pub unsafe fn dispose(&mut self) {
v8__Isolate__Dispose(self)
}
}
impl Drop for Isolate {
/// Same as Isolate but gets disposed when it goes out of scope.
pub struct OwnedIsolate(NonNull<Isolate>);
impl Drop for OwnedIsolate {
fn drop(&mut self) {
unsafe { v8__Isolate__Dispose(self.0) }
unsafe { self.0.as_mut().dispose() }
}
}
impl Deref for Isolate {
type Target = CxxIsolate;
impl Deref for OwnedIsolate {
type Target = Isolate;
fn deref(&self) -> &Self::Target {
self.0
unsafe { self.0.as_ref() }
}
}
impl DerefMut for Isolate {
impl DerefMut for OwnedIsolate {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
unsafe { self.0.as_mut() }
}
}

View file

@ -41,6 +41,7 @@ pub use exception::Exception;
pub use function::{Function, FunctionCallbackInfo, FunctionTemplate};
pub use handle_scope::HandleScope;
pub use isolate::Isolate;
pub use isolate::OwnedIsolate;
pub use json::JSON;
pub use local::Local;
pub use locker::Locker;

View file

@ -1,7 +1,6 @@
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
@ -14,10 +13,7 @@ use crate::isolate::LockedIsolate;
// }
extern "C" {
fn v8__Locker__CONSTRUCT(
buf: &mut MaybeUninit<Locker>,
isolate: &mut CxxIsolate,
);
fn v8__Locker__CONSTRUCT(buf: &mut MaybeUninit<Locker>, isolate: &Isolate);
fn v8__Locker__DESTRUCT(this: &mut Locker);
}
@ -25,12 +21,12 @@ extern "C" {
pub struct Locker<'a> {
has_lock: bool,
top_level: bool,
isolate: &'a mut CxxIsolate,
isolate: &'a mut Isolate,
phantom: PhantomData<&'a Isolate>,
}
impl<'a> Locker<'a> {
pub fn new(isolate: &mut CxxIsolate) -> Self {
pub fn new(isolate: &Isolate) -> Self {
let mut buf = MaybeUninit::<Self>::uninit();
unsafe {
v8__Locker__CONSTRUCT(&mut buf, isolate);
@ -46,7 +42,7 @@ impl<'a> Drop for Locker<'a> {
}
impl<'a> LockedIsolate for Locker<'a> {
fn cxx_isolate(&mut self) -> &mut CxxIsolate {
fn cxx_isolate(&mut self) -> &mut Isolate {
self.isolate
}
}

View file

@ -1,6 +1,6 @@
use std::ops::Deref;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::value::Value;
@ -8,11 +8,11 @@ use crate::HandleScope;
use crate::Local;
extern "C" {
fn v8__Number__New(isolate: &mut CxxIsolate, value: f64) -> *mut Number;
fn v8__Number__New(isolate: &mut Isolate, value: f64) -> *mut Number;
fn v8__Number__Value(this: &Number) -> f64;
fn v8__Integer__New(isolate: &mut CxxIsolate, value: i32) -> *mut Integer;
fn v8__Integer__New(isolate: &mut Isolate, value: i32) -> *mut Integer;
fn v8__Integer__NewFromUnsigned(
isolate: &mut CxxIsolate,
isolate: &mut Isolate,
value: u32,
) -> *mut Integer;
fn v8__Integer__Value(this: &Integer) -> i64;

View file

@ -1,6 +1,6 @@
use std::ops::Deref;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
@ -14,13 +14,13 @@ pub struct Object(Opaque);
extern "C" {
fn v8__Object__New(
isolate: *mut CxxIsolate,
isolate: *mut Isolate,
prototype_or_null: *mut Value,
names: *mut *mut Name,
values: *mut *mut Value,
length: usize,
) -> *mut Object;
fn v8__Object__GetIsolate(object: &Object) -> &mut CxxIsolate;
fn v8__Object__GetIsolate(object: &Object) -> &mut Isolate;
}
impl Object {
@ -61,7 +61,7 @@ impl Object {
}
/// Return the isolate to which the Object belongs to.
pub fn get_isolate(&self) -> &mut CxxIsolate {
pub fn get_isolate(&self) -> &mut Isolate {
unsafe { v8__Object__GetIsolate(self) }
}
}

View file

@ -1,6 +1,6 @@
use std::ops::Deref;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::Opaque;
use crate::HandleScope;
@ -21,13 +21,13 @@ pub struct Boolean(Opaque);
pub struct Name(Opaque);
extern "C" {
fn v8__Null(isolate: *mut CxxIsolate) -> *mut Primitive;
fn v8__Null(isolate: *mut Isolate) -> *mut Primitive;
fn v8__Undefined(isolate: *mut CxxIsolate) -> *mut Primitive;
fn v8__Undefined(isolate: *mut Isolate) -> *mut Primitive;
fn v8__True(isolate: *mut CxxIsolate) -> *mut Boolean;
fn v8__True(isolate: *mut Isolate) -> *mut Boolean;
fn v8__False(isolate: *mut CxxIsolate) -> *mut Boolean;
fn v8__False(isolate: *mut Isolate) -> *mut Boolean;
}
pub fn new_null<'sc>(scope: &mut HandleScope<'sc>) -> Local<'sc, Primitive> {

View file

@ -4,7 +4,7 @@ use std::mem::forget;
use std::ops::Deref;
use std::slice;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::support::char;
use crate::support::int;
@ -15,7 +15,7 @@ use crate::Value;
extern "C" {
fn v8__String__NewFromUtf8(
isolate: *mut CxxIsolate,
isolate: *mut Isolate,
data: *const char,
new_type: NewStringType,
length: int,
@ -23,11 +23,11 @@ extern "C" {
fn v8__String__Length(this: &String) -> int;
fn v8__String__Utf8Length(this: &String, isolate: *mut CxxIsolate) -> int;
fn v8__String__Utf8Length(this: &String, isolate: *mut Isolate) -> int;
fn v8__String__WriteUtf8(
this: &String,
isolate: *mut CxxIsolate,
isolate: *mut Isolate,
buffer: *mut char,
length: int,
nchars_ref: *mut int,

View file

@ -45,8 +45,8 @@ fn handle_scope_nested() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
v8::HandleScope::enter(scope, |_scope| {});
});
@ -61,8 +61,8 @@ fn handle_scope_numbers() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let l1 = v8::Integer::new(scope, -123);
let l2 = v8::Integer::new_from_unsigned(scope, 456);
@ -85,8 +85,8 @@ fn test_string() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let reference = "Hello 🦕 world!";
let local =
@ -116,8 +116,8 @@ fn script_compile_and_run() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| {
let mut context = v8::Context::new(s);
@ -143,8 +143,8 @@ fn script_origin() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| {
let mut context = v8::Context::new(s);
@ -236,8 +236,8 @@ fn test_primitives() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let null = v8::new_null(scope);
assert!(!null.is_undefined());
@ -269,7 +269,7 @@ fn exception() {
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let mut locker = v8::Locker::new(&isolate);
isolate.enter();
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
@ -302,8 +302,8 @@ fn json() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |s| {
let mut context = v8::Context::new(s);
context.enter();
@ -334,8 +334,8 @@ fn object() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
@ -361,8 +361,8 @@ fn promise_resolved() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
@ -400,8 +400,8 @@ fn promise_rejected() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
@ -458,8 +458,8 @@ fn function() {
params.set_array_buffer_allocator(
v8::array_buffer::Allocator::new_default_allocator(),
);
let mut isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&mut isolate);
let isolate = v8::Isolate::new(params);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
@ -510,13 +510,14 @@ fn set_promise_reject_callback() {
let mut isolate = v8::Isolate::new(params);
isolate.set_promise_reject_callback(promise_reject_callback);
isolate.enter();
let mut locker = v8::Locker::new(&mut isolate);
let mut locker = v8::Locker::new(&isolate);
v8::HandleScope::enter(&mut locker, |scope| {
let mut context = v8::Context::new(scope);
context.enter();
let mut resolver = v8::PromiseResolver::new(context).unwrap();
let str_ =
v8::String::new(scope, "promise rejected", v8::NewStringType::Normal).unwrap();
v8::String::new(scope, "promise rejected", v8::NewStringType::Normal)
.unwrap();
let value: Local<v8::Value> = cast(str_);
resolver.reject(context, value);
context.exit();