Skip to content

Commit

Permalink
Rewrite the scope system from scratch (denoland#406)
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Jun 14, 2020
1 parent 7a98f4e commit 154170e
Show file tree
Hide file tree
Showing 58 changed files with 2,076 additions and 1,820 deletions.
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ fn platform() -> &'static str {
fn download_ninja_gn_binaries() {
let root = env::current_dir().unwrap();
let out_dir = env::var_os("OUT_DIR").expect(
"The 'OUT_DIR' environment is not set (it should be something like 'target/debug/rusty_v8-{hash}').",
"The 'OUT_DIR' environment is not set (it should be something like \
'target/debug/rusty_v8-{hash}').",
);
let out_dir_abs = root.join(out_dir);
// This would be target/debug or target/release
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.to_local(|scope| {
v8__ArrayBuffer__New__with_byte_length(scope.isolate(), byte_length)
scope.cast_local(|scope| {
v8__ArrayBuffer__New__with_byte_length(
scope.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.to_local(|scope| {
v8__ArrayBuffer__New__with_backing_store(scope.isolate(), backing_store)
scope.cast_local(|scope| {
v8__ArrayBuffer__New__with_backing_store(
scope.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,
isolate: &mut Isolate,
byte_length: usize,
) -> UniqueRef<BackingStore> {
unsafe {
UniqueRef::from_raw(v8__ArrayBuffer__NewBackingStore__with_byte_length(
scope.isolate(),
isolate,
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
29 changes: 0 additions & 29 deletions src/binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,35 +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();
}

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

void v8__Locker__DESTRUCT(v8::Locker* self) { self->~Locker(); }

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
26 changes: 15 additions & 11 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 @@ -21,22 +21,26 @@ extern "C" {

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.to_local(|scope| v8__Context__New(scope.isolate(), null(), null()))
scope.cast_local(|scope| {
v8__Context__New(scope.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.to_local(|scope| v8__Context__New(scope.isolate(), &*templ, null()))
scope.cast_local(|scope| {
v8__Context__New(scope.get_isolate_ptr(), &*templ, null())
})
}
.unwrap()
}
Expand All @@ -51,11 +55,11 @@ 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()
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
Expand Down
22 changes: 11 additions & 11 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 Expand Up @@ -355,8 +355,8 @@ impl_partial_eq! { ObjectTemplate for Template use identity }
/// temporary functions that can be collected using Scripts is
/// preferred.
///
/// Any modification of a FunctionTemplate after first instantiation will trigger
/// a crash.
/// Any modification of a FunctionTemplate after first instantiation will
/// trigger a crash.
///
/// A FunctionTemplate can have properties, these properties are added to the
/// function object when it is created.
Expand Down
Loading

0 comments on commit 154170e

Please sign in to comment.