This commit is contained in:
Bert Belder 2019-10-20 14:22:18 -07:00
parent 7b8e882546
commit 1e3f137cc2
No known key found for this signature in database
GPG Key ID: 7A77887B2E2ED461
5 changed files with 172 additions and 149 deletions

View File

@ -11,15 +11,15 @@ mod example {
pub struct Example {
a: i32,
channel_extender: ChannelExtender,
channel_extender: ChannelBase,
b: i32,
}
impl ChannelOverrides for Example {
fn extender(&self) -> &ChannelExtender {
impl ChannelImpl for Example {
fn base(&self) -> &ChannelBase {
&self.channel_extender
}
fn extender_mut(&mut self) -> &mut ChannelExtender {
fn base_mut(&mut self) -> &mut ChannelBase {
&mut self.channel_extender
}
fn sendResponse(
@ -40,7 +40,7 @@ mod example {
impl Example {
pub fn new() -> Self {
Self {
channel_extender: ChannelExtender::new::<Self>(),
channel_extender: ChannelBase::new::<Self>(),
a: 2,
b: 3,
}

View File

@ -2,56 +2,63 @@
#include <utility>
using namespace v8_inspector;
using Channel = V8Inspector::Channel;
extern "C" {
void v8_inspector__Channel__EXTENDER__sendResponse(Channel& self,
int callId,
StringBuffer* message);
void v8_inspector__Channel__EXTENDER__sendNotification(Channel& self,
StringBuffer* message);
void v8_inspector__Channel__EXTENDER__flushProtocolNotifications(Channel& self);
void v8_inspector__V8Inspector__Channel__BASE__sendResponse(
V8Inspector::Channel& self,
int callId,
StringBuffer* message);
void v8_inspector__V8Inspector__Channel__BASE__sendNotification(
V8Inspector::Channel& self,
StringBuffer* message);
void v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications(
V8Inspector::Channel& self);
} // extern "C"
namespace v8_inspector {
struct Channel__EXTENDER : public Channel {
using Channel::Channel;
struct V8Inspector__Channel__BASE : public V8Inspector::Channel {
using V8Inspector::Channel::Channel;
static_assert(sizeof(std::unique_ptr<StringBuffer>) == sizeof(StringBuffer*),
"sizeof(T*) != sizeof(unique_ptr<T>)");
void sendResponse(int callId,
std::unique_ptr<StringBuffer> message) override {
v8_inspector__Channel__EXTENDER__sendResponse(*this, callId,
message.release());
v8_inspector__V8Inspector__Channel__BASE__sendResponse(*this, callId,
message.release());
}
void sendNotification(std::unique_ptr<StringBuffer> message) override {
v8_inspector__Channel__EXTENDER__sendNotification(*this, message.release());
v8_inspector__V8Inspector__Channel__BASE__sendNotification(
*this, message.release());
}
void flushProtocolNotifications() override {
v8_inspector__Channel__EXTENDER__flushProtocolNotifications(*this);
v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications(*this);
}
};
} // namespace v8_inspector
extern "C" {
void v8_inspector__Channel__EXTENDER__CTOR(uninit_t<Channel__EXTENDER>& buf) {
new (launder(&buf)) Channel__EXTENDER();
void v8_inspector__V8Inspector__Channel__BASE__CTOR(
uninit_t<V8Inspector__Channel__BASE>& buf) {
new (launder(&buf)) V8Inspector__Channel__BASE();
}
void v8_inspector__Channel__DTOR(Channel& self) {
void v8_inspector__V8Inspector__Channel__DTOR(V8Inspector::Channel& self) {
self.~Channel();
}
void v8_inspector__Channel__sendResponse(Channel& self,
int callId,
StringBuffer* message) {
void v8_inspector__V8Inspector__Channel__sendResponse(
V8Inspector::Channel& self,
int callId,
StringBuffer* message) {
self.sendResponse(callId,
static_cast<std::unique_ptr<StringBuffer>>(message));
}
void v8_inspector__Channel__sendNotification(Channel& self,
StringBuffer* message) {
void v8_inspector__V8Inspector__Channel__sendNotification(
V8Inspector::Channel& self,
StringBuffer* message) {
self.sendNotification(static_cast<std::unique_ptr<StringBuffer>>(message));
}
void v8_inspector__Channel__flushProtocolNotifications(Channel& self) {
void v8_inspector__V8Inspector__Channel__flushProtocolNotifications(
V8Inspector::Channel& self) {
self.flushProtocolNotifications();
}
} // extern "C"

View File

@ -17,47 +17,47 @@ use super::StringBuffer;
// };
extern "C" {
fn v8_inspector__Channel__EXTENDER__CTOR(
fn v8_inspector__V8Inspector__Channel__BASE__CTOR(
buf: &mut std::mem::MaybeUninit<Channel>,
) -> ();
fn v8_inspector__Channel__DTOR(this: &mut Channel) -> ();
fn v8_inspector__V8Inspector__Channel__DTOR(this: &mut Channel) -> ();
fn v8_inspector__Channel__sendResponse(
fn v8_inspector__V8Inspector__Channel__sendResponse(
this: &mut Channel,
callId: int,
message: UniquePtr<StringBuffer>,
) -> ();
fn v8_inspector__Channel__sendNotification(
fn v8_inspector__V8Inspector__Channel__sendNotification(
this: &mut Channel,
message: UniquePtr<StringBuffer>,
) -> ();
fn v8_inspector__Channel__flushProtocolNotifications(
fn v8_inspector__V8Inspector__Channel__flushProtocolNotifications(
this: &mut Channel,
) -> ();
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__sendResponse(
pub unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__sendResponse(
this: &mut Channel,
callId: int,
message: UniquePtr<StringBuffer>,
) -> () {
ChannelExtender::dispatch_mut(this).sendResponse(callId, message)
ChannelBase::dispatch_mut(this).sendResponse(callId, message)
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__sendNotification(
pub unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__sendNotification(
this: &mut Channel,
message: UniquePtr<StringBuffer>,
) -> () {
ChannelExtender::dispatch_mut(this).sendNotification(message)
ChannelBase::dispatch_mut(this).sendNotification(message)
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__flushProtocolNotifications(
pub unsafe extern "C" fn v8_inspector__V8Inspector__Channel__BASE__flushProtocolNotifications(
this: &mut Channel,
) -> () {
ChannelExtender::dispatch_mut(this).flushProtocolNotifications()
ChannelBase::dispatch_mut(this).flushProtocolNotifications()
}
#[repr(C)]
@ -71,19 +71,25 @@ impl Channel {
callId: int,
message: UniquePtr<StringBuffer>,
) -> () {
unsafe { v8_inspector__Channel__sendResponse(self, callId, message) }
unsafe {
v8_inspector__V8Inspector__Channel__sendResponse(self, callId, message)
}
}
pub fn sendNotification(&mut self, message: UniquePtr<StringBuffer>) -> () {
unsafe { v8_inspector__Channel__sendNotification(self, message) }
unsafe {
v8_inspector__V8Inspector__Channel__sendNotification(self, message)
}
}
pub fn flushProtocolNotifications(&mut self) -> () {
unsafe { v8_inspector__Channel__flushProtocolNotifications(self) }
unsafe {
v8_inspector__V8Inspector__Channel__flushProtocolNotifications(self)
}
}
}
impl Drop for Channel {
fn drop(&mut self) {
unsafe { v8_inspector__Channel__DTOR(self) }
unsafe { v8_inspector__V8Inspector__Channel__DTOR(self) }
}
}
@ -103,19 +109,19 @@ impl AsChannel for Channel {
impl<T> AsChannel for T
where
T: ChannelOverrides,
T: ChannelImpl,
{
fn as_channel(&self) -> &Channel {
&self.extender().cxx_channel
&self.base().cxx_base
}
fn as_channel_mut(&mut self) -> &mut Channel {
&mut self.extender_mut().cxx_channel
&mut self.base_mut().cxx_base
}
}
pub trait ChannelOverrides: AsChannel {
fn extender(&self) -> &ChannelExtender;
fn extender_mut(&mut self) -> &mut ChannelExtender;
pub trait ChannelImpl: AsChannel {
fn base(&self) -> &ChannelBase;
fn base_mut(&mut self) -> &mut ChannelBase;
fn sendResponse(
&mut self,
@ -126,38 +132,43 @@ pub trait ChannelOverrides: AsChannel {
fn flushProtocolNotifications(&mut self) -> ();
}
pub struct ChannelExtender {
cxx_channel: Channel,
extender_offset: FieldOffset<Self>,
rust_vtable: RustVTable<&'static dyn ChannelOverrides>,
pub struct ChannelBase {
cxx_base: Channel,
offset_within_embedder: FieldOffset<Self>,
rust_vtable: RustVTable<&'static dyn ChannelImpl>,
}
impl ChannelExtender {
fn construct_cxx_channel() -> Channel {
impl ChannelBase {
fn construct_cxx_base() -> Channel {
unsafe {
let mut buf = std::mem::MaybeUninit::<Channel>::uninit();
v8_inspector__Channel__EXTENDER__CTOR(&mut buf);
v8_inspector__V8Inspector__Channel__BASE__CTOR(&mut buf);
buf.assume_init()
}
}
fn get_extender_offset<T>() -> FieldOffset<Self>
fn get_cxx_base_offset() -> FieldOffset<Channel> {
let buf = std::mem::MaybeUninit::<Self>::uninit();
FieldOffset::from_ptrs(buf.as_ptr(), unsafe { &(*buf.as_ptr()).cxx_base })
}
fn get_offset_within_embedder<T>() -> FieldOffset<Self>
where
T: ChannelOverrides,
T: ChannelImpl,
{
let buf = std::mem::MaybeUninit::<T>::uninit();
let embedder_ptr: *const T = buf.as_ptr();
let self_ptr: *const Self = unsafe { (*embedder_ptr).extender() };
let self_ptr: *const Self = unsafe { (*embedder_ptr).base() };
FieldOffset::from_ptrs(embedder_ptr, self_ptr)
}
fn get_rust_vtable<T>() -> RustVTable<&'static dyn ChannelOverrides>
fn get_rust_vtable<T>() -> RustVTable<&'static dyn ChannelImpl>
where
T: ChannelOverrides,
T: ChannelImpl,
{
let buf = std::mem::MaybeUninit::<T>::uninit();
let embedder_ptr = buf.as_ptr();
let trait_object: *const dyn ChannelOverrides = embedder_ptr;
let trait_object: *const dyn ChannelImpl = embedder_ptr;
let (data_ptr, vtable): (*const T, RustVTable<_>) =
unsafe { std::mem::transmute(trait_object) };
assert_eq!(data_ptr, embedder_ptr);
@ -166,34 +177,25 @@ impl ChannelExtender {
pub fn new<T>() -> Self
where
T: ChannelOverrides,
T: ChannelImpl,
{
Self {
cxx_channel: Self::construct_cxx_channel(),
extender_offset: Self::get_extender_offset::<T>(),
cxx_base: Self::construct_cxx_base(),
offset_within_embedder: Self::get_offset_within_embedder::<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);
pub unsafe fn dispatch(channel: &Channel) -> &dyn ChannelImpl {
let this = Self::get_cxx_base_offset().to_embedder::<Self>(channel);
let embedder = this.offset_within_embedder.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);
pub unsafe fn dispatch_mut(channel: &mut Channel) -> &mut dyn ChannelImpl {
let this = Self::get_cxx_base_offset().to_embedder_mut::<Self>(channel);
let vtable = this.rust_vtable;
let embedder = this.extender_offset.to_embedder_mut::<Opaque>(this);
let embedder = this.offset_within_embedder.to_embedder_mut::<Opaque>(this);
std::mem::transmute((embedder, vtable))
}
}

View File

@ -2,53 +2,57 @@
#include <utility>
using namespace v8_inspector;
using Client = V8InspectorClient;
extern "C" {
void v8_inspector__Client__EXTENDER__runMessageLoopOnPause(Client& self,
int contextGroupId);
void v8_inspector__Client__EXTENDER__quitMessageLoopOnPause(Client& self);
void v8_inspector__Client__EXTENDER__runIfWaitingForDebugger(
Client& self,
void v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause(
V8InspectorClient& self,
int contextGroupId);
void v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause(
V8InspectorClient& self);
void v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger(
V8InspectorClient& self,
int contextGroupId);
} // extern "C"
namespace v8_inspector {
struct Client__EXTENDER : public Client {
using Client::Client;
struct Client__BASE : public V8InspectorClient {
using V8InspectorClient::V8InspectorClient;
void runMessageLoopOnPause(int contextGroupId) override {
v8_inspector__Client__EXTENDER__runMessageLoopOnPause(*this,
contextGroupId);
v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause(
*this, contextGroupId);
}
void quitMessageLoopOnPause() override {
v8_inspector__Client__EXTENDER__quitMessageLoopOnPause(*this);
v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause(*this);
}
void runIfWaitingForDebugger(int contextGroupId) override {
v8_inspector__Client__EXTENDER__runIfWaitingForDebugger(*this,
contextGroupId);
v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger(
*this, contextGroupId);
}
};
} // namespace v8_inspector
extern "C" {
void v8_inspector__Client__EXTENDER__CTOR(uninit_t<Client__EXTENDER>& buf) {
new (launder(&buf)) Client__EXTENDER();
void v8_inspector__V8InspectorClient__BASE__CTOR(uninit_t<Client__BASE>& buf) {
new (launder(&buf)) Client__BASE();
}
void v8_inspector__Client__DTOR(Client& self) {
self.~Client();
void v8_inspector__V8InspectorClient__DTOR(V8InspectorClient& self) {
self.~V8InspectorClient();
}
void v8_inspector__Client__runMessageLoopOnPause(Client& self,
int contextGroupId) {
void v8_inspector__V8InspectorClient__runMessageLoopOnPause(
V8InspectorClient& self,
int contextGroupId) {
self.runMessageLoopOnPause(contextGroupId);
}
void v8_inspector__Client__quitMessageLoopOnPause(Client& self) {
void v8_inspector__V8InspectorClient__quitMessageLoopOnPause(
V8InspectorClient& self) {
self.quitMessageLoopOnPause();
}
void v8_inspector__Client__runIfWaitingForDebugger(Client& self,
int contextGroupId) {
void v8_inspector__V8InspectorClient__runIfWaitingForDebugger(
V8InspectorClient& self,
int contextGroupId) {
self.runIfWaitingForDebugger(contextGroupId);
}
} // extern "C"

View File

@ -67,43 +67,45 @@ use crate::cxx_util::RustVTable;
// };
extern "C" {
fn v8_inspector__Client__EXTENDER__CTOR(
fn v8_inspector__V8InspectorClient__BASE__CTOR(
buf: &mut std::mem::MaybeUninit<Client>,
) -> ();
fn v8_inspector__Client__DTOR(this: &mut Client) -> ();
fn v8_inspector__V8InspectorClient__DTOR(this: &mut Client) -> ();
fn v8_inspector__Client__runMessageLoopOnPause(
fn v8_inspector__V8InspectorClient__runMessageLoopOnPause(
this: &mut Client,
contextGroupId: int,
) -> ();
fn v8_inspector__Client__quitMessageLoopOnPause(this: &mut Client) -> ();
fn v8_inspector__Client__runIfWaitingForDebugger(
fn v8_inspector__V8InspectorClient__quitMessageLoopOnPause(
this: &mut Client,
) -> ();
fn v8_inspector__V8InspectorClient__runIfWaitingForDebugger(
this: &mut Client,
contextGroupId: int,
) -> ();
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Client__EXTENDER__runMessageLoopOnPause(
pub unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__runMessageLoopOnPause(
this: &mut Client,
contextGroupId: int,
) -> () {
ClientExtender::dispatch_mut(this).runMessageLoopOnPause(contextGroupId)
ClientBase::dispatch_mut(this).runMessageLoopOnPause(contextGroupId)
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Client__EXTENDER__quitMessageLoopOnPause(
pub unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__quitMessageLoopOnPause(
this: &mut Client,
) -> () {
ClientExtender::dispatch_mut(this).quitMessageLoopOnPause()
ClientBase::dispatch_mut(this).quitMessageLoopOnPause()
}
#[no_mangle]
pub unsafe extern "C" fn v8_inspector__Client__EXTENDER__runIfWaitingForDebugger(
pub unsafe extern "C" fn v8_inspector__V8InspectorClient__BASE__runIfWaitingForDebugger(
this: &mut Client,
contextGroupId: int,
) -> () {
ClientExtender::dispatch_mut(this).runIfWaitingForDebugger(contextGroupId)
ClientBase::dispatch_mut(this).runIfWaitingForDebugger(contextGroupId)
}
#[repr(C)]
@ -113,21 +115,29 @@ pub struct Client {
impl Client {
pub fn runMessageLoopOnPause(&mut self, contextGroupId: int) -> () {
unsafe { v8_inspector__Client__runMessageLoopOnPause(self, contextGroupId) }
unsafe {
v8_inspector__V8InspectorClient__runMessageLoopOnPause(
self,
contextGroupId,
)
}
}
pub fn quitMessageLoopOnPause(&mut self) -> () {
unsafe { v8_inspector__Client__quitMessageLoopOnPause(self) }
unsafe { v8_inspector__V8InspectorClient__quitMessageLoopOnPause(self) }
}
pub fn runIfWaitingForDebugger(&mut self, contextGroupId: int) -> () {
unsafe {
v8_inspector__Client__runIfWaitingForDebugger(self, contextGroupId)
v8_inspector__V8InspectorClient__runIfWaitingForDebugger(
self,
contextGroupId,
)
}
}
}
impl Drop for Client {
fn drop(&mut self) {
unsafe { v8_inspector__Client__DTOR(self) }
unsafe { v8_inspector__V8InspectorClient__DTOR(self) }
}
}
@ -147,58 +157,63 @@ impl AsClient for Client {
impl<T> AsClient for T
where
T: ClientOverrides,
T: ClientImpl,
{
fn as_client(&self) -> &Client {
&self.extender().cxx_client
&self.base().cxx_base
}
fn as_client_mut(&mut self) -> &mut Client {
&mut self.extender_mut().cxx_client
&mut self.base_mut().cxx_base
}
}
#[allow(unused_variables)]
pub trait ClientOverrides: AsClient {
fn extender(&self) -> &ClientExtender;
fn extender_mut(&mut self) -> &mut ClientExtender;
pub trait ClientImpl: AsClient {
fn base(&self) -> &ClientBase;
fn base_mut(&mut self) -> &mut ClientBase;
fn runMessageLoopOnPause(&mut self, contextGroupId: int) -> () {}
fn quitMessageLoopOnPause(&mut self) -> () {}
fn runIfWaitingForDebugger(&mut self, contextGroupId: int) -> () {}
}
pub struct ClientExtender {
cxx_client: Client,
extender_offset: FieldOffset<Self>,
rust_vtable: RustVTable<&'static dyn ClientOverrides>,
pub struct ClientBase {
cxx_base: Client,
offset_within_embedder: FieldOffset<Self>,
rust_vtable: RustVTable<&'static dyn ClientImpl>,
}
impl ClientExtender {
fn construct_cxx_client() -> Client {
impl ClientBase {
fn construct_cxx_base() -> Client {
unsafe {
let mut buf = std::mem::MaybeUninit::<Client>::uninit();
v8_inspector__Client__EXTENDER__CTOR(&mut buf);
v8_inspector__V8InspectorClient__BASE__CTOR(&mut buf);
buf.assume_init()
}
}
fn get_extender_offset<T>() -> FieldOffset<Self>
fn get_cxx_base_offset() -> FieldOffset<Client> {
let buf = std::mem::MaybeUninit::<Self>::uninit();
FieldOffset::from_ptrs(buf.as_ptr(), unsafe { &(*buf.as_ptr()).cxx_base })
}
fn get_offset_within_embedder<T>() -> FieldOffset<Self>
where
T: ClientOverrides,
T: ClientImpl,
{
let buf = std::mem::MaybeUninit::<T>::uninit();
let embedder_ptr: *const T = buf.as_ptr();
let self_ptr: *const Self = unsafe { (*embedder_ptr).extender() };
let self_ptr: *const Self = unsafe { (*embedder_ptr).base() };
FieldOffset::from_ptrs(embedder_ptr, self_ptr)
}
fn get_rust_vtable<T>() -> RustVTable<&'static dyn ClientOverrides>
fn get_rust_vtable<T>() -> RustVTable<&'static dyn ClientImpl>
where
T: ClientOverrides,
T: ClientImpl,
{
let buf = std::mem::MaybeUninit::<T>::uninit();
let embedder_ptr = buf.as_ptr();
let trait_object: *const dyn ClientOverrides = embedder_ptr;
let trait_object: *const dyn ClientImpl = embedder_ptr;
let (data_ptr, vtable): (*const T, RustVTable<_>) =
unsafe { std::mem::transmute(trait_object) };
assert_eq!(data_ptr, embedder_ptr);
@ -207,30 +222,25 @@ impl ClientExtender {
pub fn new<T>() -> Self
where
T: ClientOverrides,
T: ClientImpl,
{
Self {
cxx_client: Self::construct_cxx_client(),
extender_offset: Self::get_extender_offset::<T>(),
cxx_base: Self::construct_cxx_base(),
offset_within_embedder: Self::get_offset_within_embedder::<T>(),
rust_vtable: Self::get_rust_vtable::<T>(),
}
}
fn get_client_offset() -> FieldOffset<Client> {
let buf = std::mem::MaybeUninit::<Self>::uninit();
FieldOffset::from_ptrs(buf.as_ptr(), unsafe { &(*buf.as_ptr()).cxx_client })
}
pub unsafe fn dispatch(client: &Client) -> &dyn ClientOverrides {
let this = Self::get_client_offset().to_embedder::<Self>(client);
let embedder = this.extender_offset.to_embedder::<Opaque>(this);
pub unsafe fn dispatch(client: &Client) -> &dyn ClientImpl {
let this = Self::get_cxx_base_offset().to_embedder::<Self>(client);
let embedder = this.offset_within_embedder.to_embedder::<Opaque>(this);
std::mem::transmute((embedder, this.rust_vtable))
}
pub unsafe fn dispatch_mut(client: &mut Client) -> &mut dyn ClientOverrides {
let this = Self::get_client_offset().to_embedder_mut::<Self>(client);
pub unsafe fn dispatch_mut(client: &mut Client) -> &mut dyn ClientImpl {
let this = Self::get_cxx_base_offset().to_embedder_mut::<Self>(client);
let vtable = this.rust_vtable;
let embedder = this.extender_offset.to_embedder_mut::<Opaque>(this);
let embedder = this.offset_within_embedder.to_embedder_mut::<Opaque>(this);
std::mem::transmute((embedder, vtable))
}
}