0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-03 17:09:00 -05:00
This commit is contained in:
Bert Belder 2019-10-08 02:06:48 +02:00
parent fdfa52f4bf
commit 48db2272ca
No known key found for this signature in database
GPG key ID: 7A77887B2E2ED461
2 changed files with 207 additions and 201 deletions

3
.rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
# Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
max_width = 80
tab_spaces = 2

View file

@ -14,7 +14,8 @@ mod channel {
// 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 Channel__OVERRIDE__CTOR(this: &mut std::mem::MaybeUninit<Channel>) -> ();
fn Channel__OVERRIDE__CTOR(this: &mut std::mem::MaybeUninit<Channel>)
-> ();
}
#[repr(C)]
@ -32,6 +33,12 @@ mod channel {
}
}
impl Drop for Channel {
fn drop(&mut self) {
unsafe { Channel__DTOR(self) }
}
}
pub struct ChannelDefaults;
impl ChannelDefaults {
pub fn a(this: &mut Channel) {
@ -40,37 +47,31 @@ mod channel {
}
pub trait ChannelOverrides {
fn base(&self) -> &Override;
fn base_mut(&mut self) -> &mut Override;
fn extender(&self) -> &ChannelExtender;
fn extender_mut(&mut self) -> &mut ChannelExtender;
fn a(&mut self) {
ChannelDefaults::a(self.base_mut())
ChannelDefaults::a(self.extender_mut())
}
fn b(&self) -> i32;
}
pub struct Override {
pub struct ChannelExtender {
cxx_channel: Channel,
base_offset: usize,
extender_offset: usize,
rust_vtable: util::RustVTable<&'static dyn ChannelOverrides>,
}
#[no_mangle]
unsafe extern "C" fn Channel__OVERRIDE__a__DISPATCH(this: &mut Channel) {
Override::dispatch_mut(this).a()
ChannelExtender::dispatch_mut(this).a()
}
#[no_mangle]
unsafe extern "C" fn Channel__OVERRIDE__b__DISPATCH(this: &Channel) -> i32 {
Override::dispatch(this).b()
ChannelExtender::dispatch(this).b()
}
impl Drop for Channel {
fn drop(&mut self) {
unsafe { Channel__DTOR(self) }
}
}
impl Override {
impl ChannelExtender {
fn construct_cxx_channel() -> Channel {
unsafe {
let mut buf = std::mem::MaybeUninit::<Channel>::uninit();
@ -79,14 +80,14 @@ mod channel {
}
}
fn get_base_offset<T>() -> usize
fn get_extender_offset<T>() -> usize
where
T: ChannelOverrides,
{
let buf = std::mem::MaybeUninit::<T>::uninit();
let top_ptr: *const T = buf.as_ptr();
let self_ptr: *const Self = unsafe { (*top_ptr).base() };
util::FieldOffset::from_ptrs(top_ptr, self_ptr).offset()
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).offset()
}
fn get_rust_vtable<T>() -> util::RustVTable<&'static dyn ChannelOverrides>
@ -108,18 +109,20 @@ mod channel {
{
Self {
cxx_channel: Self::construct_cxx_channel(),
base_offset: Self::get_base_offset::<T>(),
extender_offset: Self::get_extender_offset::<T>(),
rust_vtable: Self::get_rust_vtable::<T>(),
}
}
fn channel_offset() -> util::FieldOffset<Self, Channel> {
let buf = std::mem::MaybeUninit::<Self>::uninit();
util::FieldOffset::from_ptrs(buf.as_ptr(), unsafe { &(*buf.as_ptr()).cxx_channel })
util::FieldOffset::from_ptrs(buf.as_ptr(), unsafe {
&(*buf.as_ptr()).cxx_channel
})
}
fn embedder_offset(&self) -> util::FieldOffset<util::Opaque, Self> {
util::FieldOffset::<util::Opaque, Self>::from_offset(self.base_offset)
util::FieldOffset::<util::Opaque, Self>::from_offset(self.extender_offset)
}
unsafe fn dispatch(channel: &Channel) -> &dyn ChannelOverrides {
@ -136,14 +139,14 @@ mod channel {
}
}
impl std::ops::Deref for Override {
impl std::ops::Deref for ChannelExtender {
type Target = Channel;
fn deref(&self) -> &Channel {
&self.cxx_channel
}
}
impl std::ops::DerefMut for Override {
impl std::ops::DerefMut for ChannelExtender {
fn deref_mut(&mut self) -> &mut Channel {
&mut self.cxx_channel
}
@ -157,21 +160,21 @@ mod trying {
pub struct Session {
a: i32,
b: String,
c: Override,
c: ChannelExtender,
}
impl ChannelOverrides for Session {
fn base(&self) -> &Override {
fn extender(&self) -> &ChannelExtender {
&self.c
}
fn base_mut(&mut self) -> &mut Override {
fn extender_mut(&mut self) -> &mut ChannelExtender {
&mut self.c
}
fn a(&mut self) {
println!("Override a!");
println!("ChannelExtender a!");
}
fn b(&self) -> i32 {
println!("Override b!");
println!("ChannelExtender b!");
42
}
}
@ -181,7 +184,7 @@ mod trying {
Self {
a: 1,
b: "abc".to_owned(),
c: Override::new::<Self>(),
c: ChannelExtender::new::<Self>(),
}
}
}