Skip to content

Commit

Permalink
Rewrite the scope system from scratch (#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jun 20, 2020
1 parent 8a4dc30 commit 2ca26e2
Show file tree
Hide file tree
Showing 56 changed files with 2,264 additions and 1,846 deletions.
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());
}

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
33 changes: 19 additions & 14 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,31 @@ 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> {
) -> Local<'s, ArrayBuffer> {
unsafe {
scope.cast_local(|scope| {
v8__ArrayBuffer__New__with_byte_length(scope.isolate(), byte_length)
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> {
) -> Local<'s, ArrayBuffer> {
unsafe {
scope.cast_local(|scope| {
v8__ArrayBuffer__New__with_backing_store(scope.isolate(), backing_store)
scope.cast_local(|sd| {
v8__ArrayBuffer__New__with_backing_store(
sd.get_isolate_ptr(),
backing_store,
)
})
}
.unwrap()
Expand All @@ -281,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
8 changes: 4 additions & 4 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,10 +22,10 @@ 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>> {
scope: &mut HandleScope<'s>,
) -> Option<Local<'s, ArrayBuffer>> {
unsafe { scope.cast_local(|_| v8__ArrayBufferView__Buffer(self)) }
}

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
41 changes: 12 additions & 29 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,31 +14,30 @@ 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;
unsafe {
scope
.cast_local(|scope| v8__Context__New(scope.isolate(), null(), null()))
.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> {
) -> Local<'s, Context> {
unsafe {
scope
.cast_local(|scope| v8__Context__New(scope.isolate(), &*templ, null()))
scope.cast_local(|sd| {
v8__Context__New(sd.get_isolate_ptr(), &*templ, null())
})
}
.unwrap()
}
Expand All @@ -53,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> {
scope: &mut HandleScope<'s, ()>,
) -> Local<'s, Object> {
unsafe { scope.cast_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) };
}
}
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

0 comments on commit 2ca26e2

Please sign in to comment.