0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-21 15:04:33 -05:00

First pass at HandleScope (#18)

This commit is contained in:
Bert Belder 2019-11-27 07:14:39 -08:00 committed by Ry Dahl
parent 0339c5f3b3
commit 011b9f31a7
9 changed files with 148 additions and 77 deletions

View file

@ -4,6 +4,7 @@ import("//v8/gni/v8.gni")
v8_static_library("rusty_v8") {
sources = [
"src/array_buffer.cc",
"src/handle_scope.cc",
"src/inspector/channel.cc",
"src/inspector/client.cc",
"src/isolate.cc",

View file

@ -1,4 +1,4 @@
#include "v8.h"
#include "v8/include/v8.h"
using namespace v8;

21
src/handle_scope.cc Normal file
View file

@ -0,0 +1,21 @@
#include <cassert>
#include <cstdint>
#include "support.h"
#include "v8/include/v8.h"
using namespace v8;
using namespace support;
static_assert(sizeof(HandleScope) == sizeof(size_t) * 3,
"HandleScope size mismatch");
extern "C" {
void v8__HandleScope__CONSTRUCT(uninit_t<HandleScope>& buf, Isolate* isolate) {
construct_in_place<HandleScope>(buf, isolate);
}
void v8__HandleScope__DESTRUCT(HandleScope& self) {
self.~HandleScope();
}
}

85
src/handle_scope.rs Normal file
View file

@ -0,0 +1,85 @@
use std::mem::drop;
use std::mem::MaybeUninit;
use crate::isolate::CxxIsolate;
use crate::isolate::LockedIsolate;
use crate::support::Scope;
extern "C" {
fn v8__HandleScope__CONSTRUCT(
buf: &mut MaybeUninit<CxxHandleScope>,
isolate: &mut CxxIsolate,
);
fn v8__HandleScope__DESTRUCT(this: &mut CxxHandleScope);
}
#[repr(C)]
pub struct CxxHandleScope([usize; 3]);
pub struct HandleScope<'a, P> {
parent: &'a mut P,
cxx_handle_scope: MaybeUninit<CxxHandleScope>,
}
impl<'a, 'b, P> LockedIsolate for Scope<'a, HandleScope<'b, P>>
where
P: LockedIsolate,
{
fn cxx_isolate(&mut self) -> &mut CxxIsolate {
self.0.parent.cxx_isolate()
}
}
impl<'a, P> HandleScope<'a, P>
where
P: LockedIsolate,
{
pub fn new(parent: &'a mut P) -> Self {
Self {
parent,
cxx_handle_scope: MaybeUninit::uninit(),
}
}
pub fn enter(&mut self, mut f: impl FnMut(&mut Scope<Self>) -> ()) {
unsafe {
v8__HandleScope__CONSTRUCT(
&mut self.cxx_handle_scope,
self.parent.cxx_isolate(),
)
};
let mut scope = Scope::new(self);
f(&mut scope);
drop(scope);
unsafe {
v8__HandleScope__DESTRUCT(
&mut *(&mut self.cxx_handle_scope as *mut _ as *mut CxxHandleScope),
)
};
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::array_buffer::Allocator;
use crate::isolate::*;
use crate::platform::*;
use crate::v8::*;
use crate::Locker;
#[test]
fn test_handle_scope() {
initialize_platform(new_default_platform());
initialize();
let mut params = CreateParams::new();
params.set_array_buffer_allocator(Allocator::new_default_allocator());
let isolate = Isolate::new(params);
let mut locker = Locker::new(&isolate);
HandleScope::new(&mut locker).enter(|scope| {
HandleScope::new(scope).enter(|_scope| {});
});
}
}

View file

@ -7,10 +7,8 @@ use crate::support::UniqueRef;
use crate::v8::assert_initialized;
extern "C" {
fn v8__Isolate__New(
params: *mut CreateParams,
) -> &'static mut UnlockedIsolate;
fn v8__Isolate__Dispose(this: &mut UnlockedIsolate) -> ();
fn v8__Isolate__New(params: *mut CreateParams) -> &'static mut CxxIsolate;
fn v8__Isolate__Dispose(this: &mut CxxIsolate) -> ();
fn v8__Isolate__CreateParams__NEW() -> *mut CreateParams;
fn v8__Isolate__CreateParams__DELETE(this: &mut CreateParams);
@ -21,12 +19,14 @@ extern "C" {
}
#[repr(C)]
pub struct UnlockedIsolate(Opaque);
#[repr(C)]
pub struct LockedIsolate(Opaque);
pub struct CxxIsolate(Opaque);
pub trait LockedIsolate {
fn cxx_isolate(&mut self) -> &mut CxxIsolate;
}
#[repr(transparent)]
pub struct Isolate(&'static mut UnlockedIsolate);
pub struct Isolate(&'static mut CxxIsolate);
impl Isolate {
pub fn new(params: UniqueRef<CreateParams>) -> Self {
@ -43,8 +43,8 @@ impl Drop for Isolate {
}
impl Deref for Isolate {
type Target = UnlockedIsolate;
fn deref(&self) -> &UnlockedIsolate {
type Target = CxxIsolate;
fn deref(&self) -> &CxxIsolate {
self.0
}
}
@ -73,7 +73,7 @@ impl Delete for CreateParams {
}
}
#[cfg(test)]
#[cfg(disabled_test)]
mod tests {
use super::*;
use crate::platform::*;

View file

@ -10,6 +10,7 @@ extern crate lazy_static;
extern crate libc;
pub mod array_buffer;
pub mod handle_scope;
pub mod inspector;
pub mod isolate;
pub mod locker;

View file

@ -1,11 +1,9 @@
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::DerefMut;
use crate::isolate::CxxIsolate;
use crate::isolate::Isolate;
use crate::isolate::LockedIsolate;
use crate::isolate::UnlockedIsolate;
// class Locker {
// public:
@ -16,10 +14,7 @@ use crate::isolate::UnlockedIsolate;
// }
extern "C" {
fn v8__Locker__CONSTRUCT(
buf: &mut MaybeUninit<Locker>,
isolate: &UnlockedIsolate,
);
fn v8__Locker__CONSTRUCT(buf: &mut MaybeUninit<Locker>, isolate: &CxxIsolate);
fn v8__Locker__DESTRUCT(this: &mut Locker);
}
@ -27,12 +22,12 @@ extern "C" {
pub struct Locker<'a> {
has_lock: bool,
top_level: bool,
isolate: &'a mut LockedIsolate,
isolate: &'a mut CxxIsolate,
phantom: PhantomData<&'a Isolate>,
}
impl<'a> Locker<'a> {
fn new(isolate: &UnlockedIsolate) -> Self {
pub fn new(isolate: &CxxIsolate) -> Self {
let mut buf = MaybeUninit::<Self>::uninit();
unsafe {
v8__Locker__CONSTRUCT(&mut buf, isolate);
@ -47,15 +42,8 @@ impl<'a> Drop for Locker<'a> {
}
}
impl<'a> Deref for Locker<'a> {
type Target = LockedIsolate;
fn deref(&self) -> &LockedIsolate {
self.isolate
}
}
impl<'a> DerefMut for Locker<'a> {
fn deref_mut(&mut self) -> &mut LockedIsolate {
impl<'a> LockedIsolate for Locker<'a> {
fn cxx_isolate(&mut self) -> &mut CxxIsolate {
self.isolate
}
}

View file

@ -16,9 +16,20 @@ namespace support {
template <class T>
using uninit_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
template <class T, class... Args>
class construct_in_place_helper {
public:
construct_in_place_helper(uninit_t<T>& buf, Args... args)
: inner_(std::forward<Args>(args)...) {}
private:
T inner_;
};
template <class T, class... Args>
void construct_in_place(uninit_t<T>& buf, Args... args) {
new (&buf) T(std::forward<Args>(args)...);
new (&buf)
construct_in_place_helper<T, Args...>(buf, std::forward<Args>(args)...);
}
} // namespace support

View file

@ -2,7 +2,6 @@ use std::marker::PhantomData;
use std::mem::replace;
use std::mem::size_of;
use std::mem::transmute;
use std::mem::MaybeUninit;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr::NonNull;
@ -180,58 +179,23 @@ impl<F> FieldOffset<F> {
}
}
pub(crate) trait ConstructOnStack
where
Self: Sized,
{
type Args;
pub struct Scope<'a, T>(pub(crate) &'a mut T);
// The `buf` parameter represents a pointer to uninitialized memory.
fn construct(buf: &mut MaybeUninit<Self>, args: &Self::Args);
fn destruct(buf: &mut Self);
}
pub(crate) struct StackOnly<T>(MaybeUninit<T>)
where
T: ConstructOnStack;
impl<T> StackOnly<T>
where
T: ConstructOnStack,
{
unsafe fn uninit() -> Self {
Self(MaybeUninit::<T>::uninit())
}
unsafe fn init(&mut self, args: &T::Args) {
T::construct(&mut self.0, args);
impl<'a, T> Scope<'a, T> {
pub(crate) fn new(inner: &'a mut T) -> Self {
Self(inner)
}
}
impl<T> Deref for StackOnly<T>
where
T: ConstructOnStack,
{
impl<'a, T> Deref for Scope<'a, T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*(self as *const StackOnly<T> as *const T) }
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for StackOnly<T>
where
T: ConstructOnStack,
{
fn deref_mut(&mut self) -> &mut T {
unsafe { &mut *(self as *mut StackOnly<T> as *mut T) }
}
}
impl<T> Drop for StackOnly<T>
where
T: ConstructOnStack,
{
fn drop(&mut self) {
T::destruct(&mut *self)
impl<'a, T> DerefMut for Scope<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}