mirror of
https://github.com/denoland/rusty_v8.git
synced 2024-11-25 15:29:43 -05:00
wip
This commit is contained in:
parent
f9d0a6ba77
commit
c4a7c969ba
6 changed files with 232 additions and 231 deletions
42
src/cxx_util.rs
Normal file
42
src/cxx_util.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
|
||||
pub type Opaque = [usize; 0];
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct RustVTable<DynT>(pub *const Opaque, pub PhantomData<DynT>);
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FieldOffset<F>(usize, PhantomData<F>);
|
||||
|
||||
unsafe impl<F> Send for FieldOffset<F> where F: Send {}
|
||||
unsafe impl<F> Sync for FieldOffset<F> where F: Sync {}
|
||||
impl<F> Copy for FieldOffset<F> {}
|
||||
|
||||
impl<F> Clone for FieldOffset<F> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> FieldOffset<F> {
|
||||
pub fn from_ptrs<E>(embedder_ptr: *const E, field_ptr: *const F) -> Self {
|
||||
let embedder_addr = embedder_ptr as usize;
|
||||
let field_addr = field_ptr as usize;
|
||||
assert!(field_addr >= embedder_addr);
|
||||
assert!((field_addr + size_of::<F>()) <= (embedder_addr + size_of::<E>()));
|
||||
Self(embedder_addr - field_addr, PhantomData)
|
||||
}
|
||||
|
||||
pub unsafe fn to_embedder<E>(self, field: &F) -> &E {
|
||||
(((field as *const _ as usize) - self.0) as *const E)
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub unsafe fn to_embedder_mut<E>(self, field: &mut F) -> &mut E {
|
||||
(((field as *mut _ as usize) - self.0) as *mut E)
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
18
src/lib.cpp
18
src/lib.cpp
|
@ -21,8 +21,8 @@ class Channel {
|
|||
} // namespace v8
|
||||
|
||||
extern "C" {
|
||||
void v8__Channel__EXTENDER__method1(v8::Channel& self, int32_t arg);
|
||||
int32_t v8__Channel__EXTENDER__method2(const v8::Channel& self);
|
||||
void v8_inspector__Channel__EXTENDER__method1(v8::Channel& self, int32_t arg);
|
||||
int32_t v8_inspector__Channel__EXTENDER__method2(const v8::Channel& self);
|
||||
}
|
||||
|
||||
namespace extender {
|
||||
|
@ -34,30 +34,30 @@ struct Channel : public ::v8::Channel {
|
|||
using ::v8::Channel::Channel;
|
||||
|
||||
void method1(int32_t arg) override {
|
||||
v8__Channel__EXTENDER__method1(*this, arg);
|
||||
v8_inspector__Channel__EXTENDER__method1(*this, arg);
|
||||
}
|
||||
|
||||
int32_t method2() const override {
|
||||
return v8__Channel__EXTENDER__method2(*this);
|
||||
return v8_inspector__Channel__EXTENDER__method2(*this);
|
||||
}
|
||||
};
|
||||
} // namespace v8
|
||||
} // namespace extender
|
||||
|
||||
extern "C" {
|
||||
void v8__Channel__DTOR(v8::Channel& self) {
|
||||
void v8_inspector__Channel__DTOR(v8::Channel& self) {
|
||||
self.~Channel();
|
||||
}
|
||||
void v8__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
void v8_inspector__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
self.method1(arg);
|
||||
}
|
||||
void v8__Channel__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
void v8_inspector__Channel__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
self.::v8::Channel::method1(arg);
|
||||
}
|
||||
int32_t v8__Channel__method2(const v8::Channel& self) {
|
||||
int32_t v8_inspector__Channel__method2(const v8::Channel& self) {
|
||||
return self.method2();
|
||||
}
|
||||
void v8__Channel__EXTENDER__CTOR(
|
||||
void v8_inspector__Channel__EXTENDER__CTOR(
|
||||
extender::uninit_t<extender::v8::Channel>& buf) {
|
||||
new (std::launder(&buf)) extender::v8::Channel();
|
||||
}
|
||||
|
|
226
src/main.rs
226
src/main.rs
|
@ -1,226 +1,8 @@
|
|||
mod channel {
|
||||
use super::util;
|
||||
|
||||
extern "C" {
|
||||
// Call a method/destructor; virtual methods use C++ dynamic dispatch.
|
||||
fn v8__Channel__DTOR(this: &mut Channel) -> ();
|
||||
fn v8__Channel__method1(this: &mut Channel, arg: i32) -> ();
|
||||
fn v8__Channel__method2(this: &Channel) -> i32;
|
||||
|
||||
// Call a method of a specific class implementation, bypassing dynamic
|
||||
// dispatch. C++ equivalent: `my_channel.Channel::a()`.
|
||||
fn v8__Channel__Channel__method1(this: &mut Channel, arg: i32) -> ();
|
||||
|
||||
// Constructs a special class derived from Channel that forwards all
|
||||
// virtual method invocations to rust. It is assumed that this subclass
|
||||
// has the same size and memory layout as the class it's deriving from.
|
||||
fn v8__Channel__EXTENDER__CTOR(
|
||||
buf: &mut std::mem::MaybeUninit<Channel>,
|
||||
) -> ();
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn v8__Channel__EXTENDER__method1(
|
||||
this: &mut Channel,
|
||||
arg: i32,
|
||||
) {
|
||||
ChannelExtender::dispatch_mut(this).method1(arg)
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn v8__Channel__EXTENDER__method2(
|
||||
this: &Channel,
|
||||
) -> i32 {
|
||||
ChannelExtender::dispatch(this).method2()
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Channel {
|
||||
_cxx_vtable: *const util::Opaque,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn method1(&mut self, arg: i32) {
|
||||
unsafe { v8__Channel__method1(self, arg) }
|
||||
}
|
||||
pub fn method2(&self) -> i32 {
|
||||
unsafe { v8__Channel__method2(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Channel {
|
||||
fn drop(&mut self) {
|
||||
unsafe { v8__Channel__DTOR(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsChannel {
|
||||
fn as_channel(&self) -> &Channel;
|
||||
fn as_channel_mut(&mut self) -> &mut Channel;
|
||||
}
|
||||
|
||||
impl AsChannel for Channel {
|
||||
fn as_channel(&self) -> &Channel {
|
||||
self
|
||||
}
|
||||
fn as_channel_mut(&mut self) -> &mut Channel {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AsChannel for T
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
fn as_channel(&self) -> &Channel {
|
||||
&self.extender().cxx_channel
|
||||
}
|
||||
fn as_channel_mut(&mut self) -> &mut Channel {
|
||||
&mut self.extender_mut().cxx_channel
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ChannelDefaults;
|
||||
impl ChannelDefaults {
|
||||
pub fn method1(channel: &mut Channel, arg: i32) {
|
||||
unsafe { v8__Channel__Channel__method1(channel, arg) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ChannelOverrides: AsChannel {
|
||||
fn extender(&self) -> &ChannelExtender;
|
||||
fn extender_mut(&mut self) -> &mut ChannelExtender;
|
||||
|
||||
fn method1(&mut self, arg: i32) {
|
||||
ChannelDefaults::method1(self.as_channel_mut(), arg)
|
||||
}
|
||||
fn method2(&self) -> i32;
|
||||
}
|
||||
|
||||
pub struct ChannelExtender {
|
||||
cxx_channel: Channel,
|
||||
extender_offset: util::FieldOffset<Self>,
|
||||
rust_vtable: util::RustVTable<&'static dyn ChannelOverrides>,
|
||||
}
|
||||
|
||||
impl ChannelExtender {
|
||||
fn construct_cxx_channel() -> Channel {
|
||||
unsafe {
|
||||
let mut buf = std::mem::MaybeUninit::<Channel>::uninit();
|
||||
v8__Channel__EXTENDER__CTOR(&mut buf);
|
||||
buf.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_extender_offset<T>() -> util::FieldOffset<Self>
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
let buf = std::mem::MaybeUninit::<T>::uninit();
|
||||
let embedder_ptr: *const T = buf.as_ptr();
|
||||
let self_ptr: *const Self = unsafe { (*embedder_ptr).extender() };
|
||||
util::FieldOffset::from_ptrs(embedder_ptr, self_ptr)
|
||||
}
|
||||
|
||||
fn get_rust_vtable<T>() -> util::RustVTable<&'static dyn ChannelOverrides>
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
let buf = std::mem::MaybeUninit::<T>::uninit();
|
||||
let embedder_ptr = buf.as_ptr();
|
||||
let trait_object: *const dyn ChannelOverrides = embedder_ptr;
|
||||
let (data_ptr, vtable): (*const T, util::RustVTable<_>) =
|
||||
unsafe { std::mem::transmute(trait_object) };
|
||||
assert_eq!(data_ptr, embedder_ptr);
|
||||
vtable
|
||||
}
|
||||
|
||||
pub fn new<T>() -> Self
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
Self {
|
||||
cxx_channel: Self::construct_cxx_channel(),
|
||||
extender_offset: Self::get_extender_offset::<T>(),
|
||||
rust_vtable: Self::get_rust_vtable::<T>(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_channel_offset() -> util::FieldOffset<Channel> {
|
||||
let buf = std::mem::MaybeUninit::<Self>::uninit();
|
||||
util::FieldOffset::from_ptrs(buf.as_ptr(), unsafe {
|
||||
&(*buf.as_ptr()).cxx_channel
|
||||
})
|
||||
}
|
||||
|
||||
pub unsafe fn dispatch(channel: &Channel) -> &dyn ChannelOverrides {
|
||||
let this = Self::get_channel_offset().to_embedder::<Self>(channel);
|
||||
let embedder = this.extender_offset.to_embedder::<util::Opaque>(this);
|
||||
std::mem::transmute((embedder, this.rust_vtable))
|
||||
}
|
||||
|
||||
pub unsafe fn dispatch_mut(channel: &mut Channel) -> &mut dyn ChannelOverrides {
|
||||
let this = Self::get_channel_offset().to_embedder_mut::<Self>(channel);
|
||||
let vtable = this.rust_vtable;
|
||||
let embedder = this.extender_offset.to_embedder_mut::<util::Opaque>(this);
|
||||
std::mem::transmute((embedder, vtable))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mod util {
|
||||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
|
||||
pub type Opaque = [usize; 0];
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct RustVTable<DynT>(pub *const Opaque, pub PhantomData<DynT>);
|
||||
|
||||
#[repr(transparent)]
|
||||
#[derive(Debug)]
|
||||
pub struct FieldOffset<F>(usize, PhantomData<F>);
|
||||
|
||||
unsafe impl<F> Send for FieldOffset<F> where F: Send {}
|
||||
unsafe impl<F> Sync for FieldOffset<F> where F: Sync {}
|
||||
impl<F> Copy for FieldOffset<F> {}
|
||||
|
||||
impl<F> Clone for FieldOffset<F> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0, self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<F> FieldOffset<F> {
|
||||
pub fn from_ptrs<E>(embedder_ptr: *const E, field_ptr: *const F) -> Self {
|
||||
let embedder_addr = embedder_ptr as usize;
|
||||
let field_addr = field_ptr as usize;
|
||||
assert!(field_addr >= embedder_addr);
|
||||
assert!(
|
||||
(field_addr + size_of::<F>()) <= (embedder_addr + size_of::<E>())
|
||||
);
|
||||
Self(embedder_addr - field_addr, PhantomData)
|
||||
}
|
||||
|
||||
pub unsafe fn to_embedder<E>(self, field: &F) -> &E {
|
||||
(((field as *const _ as usize) - self.0) as *const E)
|
||||
.as_ref()
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub unsafe fn to_embedder_mut<E>(self, field: &mut F) -> &mut E {
|
||||
(((field as *mut _ as usize) - self.0) as *mut E)
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
}
|
||||
}
|
||||
}
|
||||
mod cxx_util;
|
||||
mod v8_inspector;
|
||||
|
||||
mod example {
|
||||
use super::channel::*;
|
||||
use crate::v8_inspector::channel::*;
|
||||
|
||||
pub struct Example {
|
||||
a: i32,
|
||||
|
@ -259,7 +41,7 @@ mod example {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
use channel::*;
|
||||
use crate::v8_inspector::channel::*;
|
||||
use example::*;
|
||||
let mut ex = Example::new();
|
||||
let chan = ex.as_channel_mut();
|
||||
|
|
176
src/v8_inspector/channel.rs
Normal file
176
src/v8_inspector/channel.rs
Normal file
|
@ -0,0 +1,176 @@
|
|||
use crate::cxx_util::FieldOffset;
|
||||
use crate::cxx_util::Opaque;
|
||||
use crate::cxx_util::RustVTable;
|
||||
|
||||
extern "C" {
|
||||
// Call a method/destructor; virtual methods use C++ dynamic dispatch.
|
||||
fn v8_inspector__Channel__DTOR(this: &mut Channel) -> ();
|
||||
fn v8_inspector__Channel__method1(this: &mut Channel, arg: i32) -> ();
|
||||
fn v8_inspector__Channel__method2(this: &Channel) -> i32;
|
||||
|
||||
// Call a method of a specific class implementation, bypassing dynamic
|
||||
// dispatch. C++ equivalent: `my_channel.Channel::a()`.
|
||||
fn v8_inspector__Channel__Channel__method1(
|
||||
this: &mut Channel,
|
||||
arg: i32,
|
||||
) -> ();
|
||||
|
||||
// Constructs a special class derived from Channel that forwards all
|
||||
// virtual method invocations to rust. It is assumed that this subclass
|
||||
// has the same size and memory layout as the class it's deriving from.
|
||||
fn v8_inspector__Channel__EXTENDER__CTOR(
|
||||
buf: &mut std::mem::MaybeUninit<Channel>,
|
||||
) -> ();
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__method1(
|
||||
this: &mut Channel,
|
||||
arg: i32,
|
||||
) {
|
||||
ChannelExtender::dispatch_mut(this).method1(arg)
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__method2(
|
||||
this: &Channel,
|
||||
) -> i32 {
|
||||
ChannelExtender::dispatch(this).method2()
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Channel {
|
||||
_cxx_vtable: *const Opaque,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
pub fn method1(&mut self, arg: i32) {
|
||||
unsafe { v8_inspector__Channel__method1(self, arg) }
|
||||
}
|
||||
pub fn method2(&self) -> i32 {
|
||||
unsafe { v8_inspector__Channel__method2(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Channel {
|
||||
fn drop(&mut self) {
|
||||
unsafe { v8_inspector__Channel__DTOR(self) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait AsChannel {
|
||||
fn as_channel(&self) -> &Channel;
|
||||
fn as_channel_mut(&mut self) -> &mut Channel;
|
||||
}
|
||||
|
||||
impl AsChannel for Channel {
|
||||
fn as_channel(&self) -> &Channel {
|
||||
self
|
||||
}
|
||||
fn as_channel_mut(&mut self) -> &mut Channel {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> AsChannel for T
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
fn as_channel(&self) -> &Channel {
|
||||
&self.extender().cxx_channel
|
||||
}
|
||||
fn as_channel_mut(&mut self) -> &mut Channel {
|
||||
&mut self.extender_mut().cxx_channel
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ChannelDefaults;
|
||||
impl ChannelDefaults {
|
||||
pub fn method1(channel: &mut Channel, arg: i32) {
|
||||
unsafe { v8_inspector__Channel__Channel__method1(channel, arg) }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait ChannelOverrides: AsChannel {
|
||||
fn extender(&self) -> &ChannelExtender;
|
||||
fn extender_mut(&mut self) -> &mut ChannelExtender;
|
||||
|
||||
fn method1(&mut self, arg: i32) {
|
||||
ChannelDefaults::method1(self.as_channel_mut(), arg)
|
||||
}
|
||||
fn method2(&self) -> i32;
|
||||
}
|
||||
|
||||
pub struct ChannelExtender {
|
||||
cxx_channel: Channel,
|
||||
extender_offset: FieldOffset<Self>,
|
||||
rust_vtable: RustVTable<&'static dyn ChannelOverrides>,
|
||||
}
|
||||
|
||||
impl ChannelExtender {
|
||||
fn construct_cxx_channel() -> Channel {
|
||||
unsafe {
|
||||
let mut buf = std::mem::MaybeUninit::<Channel>::uninit();
|
||||
v8_inspector__Channel__EXTENDER__CTOR(&mut buf);
|
||||
buf.assume_init()
|
||||
}
|
||||
}
|
||||
|
||||
fn get_extender_offset<T>() -> FieldOffset<Self>
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
let buf = std::mem::MaybeUninit::<T>::uninit();
|
||||
let embedder_ptr: *const T = buf.as_ptr();
|
||||
let self_ptr: *const Self = unsafe { (*embedder_ptr).extender() };
|
||||
FieldOffset::from_ptrs(embedder_ptr, self_ptr)
|
||||
}
|
||||
|
||||
fn get_rust_vtable<T>() -> RustVTable<&'static dyn ChannelOverrides>
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
let buf = std::mem::MaybeUninit::<T>::uninit();
|
||||
let embedder_ptr = buf.as_ptr();
|
||||
let trait_object: *const dyn ChannelOverrides = embedder_ptr;
|
||||
let (data_ptr, vtable): (*const T, RustVTable<_>) =
|
||||
unsafe { std::mem::transmute(trait_object) };
|
||||
assert_eq!(data_ptr, embedder_ptr);
|
||||
vtable
|
||||
}
|
||||
|
||||
pub fn new<T>() -> Self
|
||||
where
|
||||
T: ChannelOverrides,
|
||||
{
|
||||
Self {
|
||||
cxx_channel: Self::construct_cxx_channel(),
|
||||
extender_offset: Self::get_extender_offset::<T>(),
|
||||
rust_vtable: Self::get_rust_vtable::<T>(),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_channel_offset() -> FieldOffset<Channel> {
|
||||
let buf = std::mem::MaybeUninit::<Self>::uninit();
|
||||
FieldOffset::from_ptrs(buf.as_ptr(), unsafe {
|
||||
&(*buf.as_ptr()).cxx_channel
|
||||
})
|
||||
}
|
||||
|
||||
pub unsafe fn dispatch(channel: &Channel) -> &dyn ChannelOverrides {
|
||||
let this = Self::get_channel_offset().to_embedder::<Self>(channel);
|
||||
let embedder = this.extender_offset.to_embedder::<Opaque>(this);
|
||||
std::mem::transmute((embedder, this.rust_vtable))
|
||||
}
|
||||
|
||||
pub unsafe fn dispatch_mut(
|
||||
channel: &mut Channel,
|
||||
) -> &mut dyn ChannelOverrides {
|
||||
let this = Self::get_channel_offset().to_embedder_mut::<Self>(channel);
|
||||
let vtable = this.rust_vtable;
|
||||
let embedder = this.extender_offset.to_embedder_mut::<Opaque>(this);
|
||||
std::mem::transmute((embedder, vtable))
|
||||
}
|
||||
}
|
0
src/v8_inspector/client.rs
Normal file
0
src/v8_inspector/client.rs
Normal file
1
src/v8_inspector/mod.rs
Normal file
1
src/v8_inspector/mod.rs
Normal file
|
@ -0,0 +1 @@
|
|||
pub mod channel;
|
Loading…
Reference in a new issue