Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scopes refactor #406

Merged
merged 5 commits into from
Jun 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ fn build_v8() {
vec!["is_debug=false".to_string()]
};

if !cargo_gn::is_debug() {
gn_args.push("v8_enable_handle_zapping=false".to_string());
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment here? What's this for?


if let Some(clang_base_path) = find_compatible_system_clang() {
println!("clang_base_path {}", clang_base_path.display());
gn_args.push(format!("clang_base_path={:?}", clang_base_path));
Expand Down
46 changes: 27 additions & 19 deletions src/array_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ use crate::support::SharedRef;
use crate::support::UniquePtr;
use crate::support::UniqueRef;
use crate::ArrayBuffer;
use crate::InIsolate;
use crate::HandleScope;
use crate::Isolate;
use crate::Local;
use crate::ToLocal;

extern "C" {
fn v8__ArrayBuffer__Allocator__NewDefaultAllocator() -> *mut Allocator;
Expand Down Expand Up @@ -236,25 +235,34 @@ impl ArrayBuffer {
/// Allocated memory will be owned by a created ArrayBuffer and
/// will be deallocated when it is garbage-collected,
/// unless the object is externalized.
pub fn new<'sc>(
scope: &mut impl ToLocal<'sc>,
pub fn new<'s>(
scope: &mut HandleScope<'s>,
byte_length: usize,
) -> Local<'sc, ArrayBuffer> {
let isolate = scope.isolate();
let ptr =
unsafe { v8__ArrayBuffer__New__with_byte_length(isolate, byte_length) };
unsafe { scope.to_local(ptr) }.unwrap()
) -> Local<'s, ArrayBuffer> {
unsafe {
scope.cast_local(|sd| {
v8__ArrayBuffer__New__with_byte_length(
sd.get_isolate_ptr(),
byte_length,
)
})
}
.unwrap()
}

pub fn with_backing_store<'sc>(
scope: &mut impl ToLocal<'sc>,
pub fn with_backing_store<'s>(
scope: &mut HandleScope<'s>,
backing_store: &SharedRef<BackingStore>,
) -> Local<'sc, ArrayBuffer> {
let isolate = scope.isolate();
let ptr = unsafe {
v8__ArrayBuffer__New__with_backing_store(isolate, backing_store)
};
unsafe { scope.to_local(ptr) }.unwrap()
) -> Local<'s, ArrayBuffer> {
unsafe {
scope.cast_local(|sd| {
v8__ArrayBuffer__New__with_backing_store(
sd.get_isolate_ptr(),
backing_store,
)
})
}
.unwrap()
}

/// Data length in bytes.
Expand All @@ -278,12 +286,12 @@ impl ArrayBuffer {
/// given isolate and re-try the allocation. If GCs do not help, then the
/// function will crash with an out-of-memory error.
pub fn new_backing_store(
scope: &mut impl InIsolate,
scope: &mut Isolate,
byte_length: usize,
) -> UniqueRef<BackingStore> {
unsafe {
UniqueRef::from_raw(v8__ArrayBuffer__NewBackingStore__with_byte_length(
scope.isolate(),
scope,
byte_length,
))
}
Expand Down
10 changes: 5 additions & 5 deletions src/array_buffer_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::ffi::c_void;
use crate::support::int;
use crate::ArrayBuffer;
use crate::ArrayBufferView;
use crate::HandleScope;
use crate::Local;
use crate::ToLocal;

extern "C" {
fn v8__ArrayBufferView__Buffer(
Expand All @@ -22,11 +22,11 @@ extern "C" {

impl ArrayBufferView {
/// Returns underlying ArrayBuffer.
pub fn buffer<'sc>(
pub fn buffer<'s>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Option<Local<'sc, ArrayBuffer>> {
unsafe { scope.to_local(v8__ArrayBufferView__Buffer(self)) }
scope: &mut HandleScope<'s>,
) -> Option<Local<'s, ArrayBuffer>> {
unsafe { scope.cast_local(|_| v8__ArrayBufferView__Buffer(self)) }
}

/// Size of a view in bytes.
Expand Down
27 changes: 4 additions & 23 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,6 @@ void v8__HandleScope__CONSTRUCT(uninit_t<v8::HandleScope>* buf,

void v8__HandleScope__DESTRUCT(v8::HandleScope* self) { self->~HandleScope(); }

v8::Isolate* v8__HandleScope__GetIsolate(const v8::HandleScope& self) {
return self.GetIsolate();
}

void v8__EscapableHandleScope__CONSTRUCT(
uninit_t<v8::EscapableHandleScope>* buf, v8::Isolate* isolate) {
construct_in_place<v8::EscapableHandleScope>(buf, isolate);
}

void v8__EscapableHandleScope__DESTRUCT(v8::EscapableHandleScope* self) {
self->~EscapableHandleScope();
}

const v8::Data* v8__EscapableHandleScope__Escape(v8::EscapableHandleScope* self,
const v8::Data& value) {
return local_to_ptr(self->Escape(ptr_to_local(&value)));
}

v8::Isolate* v8__EscapableHandleScope__GetIsolate(
const v8::EscapableHandleScope& self) {
return self.GetIsolate();
}

const v8::Data* v8__Local__New(v8::Isolate* isolate, const v8::Data& other) {
return local_to_ptr(v8::Local<v8::Data>::New(isolate, ptr_to_local(&other)));
}
Expand Down Expand Up @@ -889,6 +866,10 @@ const v8::Context* v8__Context__New(v8::Isolate* isolate,
DeserializeInternalFields, nullptr)));
}

bool v8__Context__EQ(const v8::Context& self, const v8::Context& other) {
return ptr_to_local(&self) == ptr_to_local(&other);
}

void v8__Context__Enter(const v8::Context& self) {
ptr_to_local(&self)->Enter();
}
Expand Down
51 changes: 20 additions & 31 deletions src/context.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Copyright 2019-2020 the Deno authors. All rights reserved. MIT license.
use crate::isolate::Isolate;
use crate::Context;
use crate::HandleScope;
use crate::Local;
use crate::Object;
use crate::ObjectTemplate;
use crate::ToLocal;
use crate::Value;
use std::ptr::null;

Expand All @@ -14,27 +14,32 @@ extern "C" {
templ: *const ObjectTemplate,
global_object: *const Value,
) -> *const Context;
fn v8__Context__Enter(this: *const Context);
fn v8__Context__Exit(this: *const Context);
fn v8__Context__Global(this: *const Context) -> *const Object;
}

impl Context {
/// Creates a new context.
pub fn new<'sc>(scope: &mut impl ToLocal<'sc>) -> Local<'sc, Context> {
pub fn new<'s>(scope: &mut HandleScope<'s, ()>) -> Local<'s, Context> {
// TODO: optional arguments;
let ptr = unsafe { v8__Context__New(scope.isolate(), null(), null()) };
unsafe { scope.to_local(ptr) }.unwrap()
unsafe {
scope
.cast_local(|sd| v8__Context__New(sd.get_isolate_ptr(), null(), null()))
}
.unwrap()
}

/// Creates a new context using the object template as the template for
/// the global object.
pub fn new_from_template<'sc>(
scope: &mut impl ToLocal<'sc>,
pub fn new_from_template<'s>(
scope: &mut HandleScope<'s, ()>,
templ: Local<ObjectTemplate>,
) -> Local<'sc, Context> {
let ptr = unsafe { v8__Context__New(scope.isolate(), &*templ, null()) };
unsafe { scope.to_local(ptr) }.unwrap()
) -> Local<'s, Context> {
unsafe {
scope.cast_local(|sd| {
v8__Context__New(sd.get_isolate_ptr(), &*templ, null())
})
}
.unwrap()
}

/// Returns the global proxy object.
Expand All @@ -47,26 +52,10 @@ impl Context {
/// Please note that changes to global proxy object prototype most probably
/// would break VM---v8 expects only global object as a prototype of global
/// proxy object.
pub fn global<'sc>(
pub fn global<'s>(
&self,
scope: &mut impl ToLocal<'sc>,
) -> Local<'sc, Object> {
unsafe { scope.to_local(v8__Context__Global(self)) }.unwrap()
}

/// Enter this context. After entering a context, all code compiled
/// and run is compiled and run in this context. If another context
/// is already entered, this old context is saved so it can be
/// restored when the new context is exited.
pub(crate) fn enter(&self) {
// TODO: enter/exit should be controlled by a scope.
unsafe { v8__Context__Enter(self) };
}

/// Exit this context. Exiting the current context restores the
/// context that was in place when entering the current context.
pub(crate) fn exit(&self) {
// TODO: enter/exit should be controlled by a scope.
unsafe { v8__Context__Exit(self) };
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, Object> {
piscisaureus marked this conversation as resolved.
Show resolved Hide resolved
unsafe { scope.cast_local(|_| v8__Context__Global(self)) }.unwrap()
}
}
18 changes: 9 additions & 9 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ macro_rules! impl_deref {

macro_rules! impl_from {
{ $source:ident for $type:ident } => {
impl<'sc> From<Local<'sc, $source>> for Local<'sc, $type> {
fn from(l: Local<'sc, $source>) -> Self {
impl<'s> From<Local<'s, $source>> for Local<'s, $type> {
fn from(l: Local<'s, $source>) -> Self {
unsafe { transmute(l) }
}
}
Expand All @@ -35,9 +35,9 @@ macro_rules! impl_from {

macro_rules! impl_try_from {
{ $source:ident for $target:ident if $value:pat => $check:expr } => {
impl<'sc> TryFrom<Local<'sc, $source>> for Local<'sc, $target> {
impl<'s> TryFrom<Local<'s, $source>> for Local<'s, $target> {
type Error = TryFromTypeError;
fn try_from(l: Local<'sc, $source>) -> Result<Self, Self::Error> {
fn try_from(l: Local<'s, $source>) -> Result<Self, Self::Error> {
match l {
$value if $check => Ok(unsafe { transmute(l) }),
_ => Err(TryFromTypeError::new(stringify!($target)))
Expand All @@ -49,21 +49,21 @@ macro_rules! impl_try_from {

macro_rules! impl_eq {
{ for $type:ident } => {
impl<'sc> Eq for Local<'sc, $type> {}
impl<'s> Eq for Local<'s, $type> {}
};
}

macro_rules! impl_partial_eq {
{ $rhs:ident for $type:ident use identity } => {
impl<'sc> PartialEq<Local<'sc, $rhs>> for Local<'sc, $type> {
fn eq(&self, other: &Local<'sc, $rhs>) -> bool {
impl<'s> PartialEq<Local<'s, $rhs>> for Local<'s, $type> {
fn eq(&self, other: &Local<'s, $rhs>) -> bool {
self.eq_identity((*other).into())
}
}
};
{ $rhs:ident for $type:ident use strict_equals } => {
impl<'sc> PartialEq<Local<'sc, $rhs>> for Local<'sc, $type> {
fn eq(&self, other: &Local<'sc, $rhs>) -> bool {
impl<'s> PartialEq<Local<'s, $rhs>> for Local<'s, $type> {
fn eq(&self, other: &Local<'s, $rhs>) -> bool {
self.strict_equals((*other).into())
}
}
Expand Down
Loading