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
c4a7c969ba
commit
8d97e79d46
10 changed files with 830 additions and 18 deletions
|
@ -1,8 +1,90 @@
|
|||
use std::marker::PhantomData;
|
||||
use std::mem::size_of;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub type Opaque = [usize; 0];
|
||||
|
||||
pub trait Delete
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
fn delete(&'static mut self) -> ();
|
||||
}
|
||||
|
||||
/// Pointer to object allocated on the C++ heap.
|
||||
#[repr(transparent)]
|
||||
pub struct UniquePtr<T>(Option<&'static mut T>)
|
||||
where
|
||||
T: Delete;
|
||||
|
||||
impl<T> Deref for UniquePtr<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
type Target = Option<&'static mut T>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for UniquePtr<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for UniquePtr<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
self.0.take().map(Delete::delete);
|
||||
}
|
||||
}
|
||||
|
||||
/// Reference to object allocated on the C++ heap, similar to UniquePtr<T>,
|
||||
/// but guaranteed to never contain a nullptr.
|
||||
#[repr(transparent)]
|
||||
pub struct UniqueRef<T>(&'static mut T)
|
||||
where
|
||||
T: Delete;
|
||||
|
||||
impl<T> Deref for UniqueRef<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
type Target = T;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for UniqueRef<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for UniqueRef<T>
|
||||
where
|
||||
T: Delete,
|
||||
{
|
||||
fn drop(&mut self) {
|
||||
let ptr: &'static mut T = unsafe { std::mem::transmute_copy(self.0) };
|
||||
ptr.delete();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
#[repr(transparent)]
|
||||
pub struct CxxVTable(pub *const Opaque);
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct RustVTable<DynT>(pub *const Opaque, pub PhantomData<DynT>);
|
||||
|
||||
|
|
76
src/lib.cpp
76
src/lib.cpp
|
@ -1,37 +1,77 @@
|
|||
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <new>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace v8 {
|
||||
namespace v8_inspector {
|
||||
class StringView {
|
||||
public:
|
||||
StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
|
||||
|
||||
StringView(const uint8_t* characters, size_t length)
|
||||
: m_is8Bit(true), m_length(length), m_characters8(characters) {}
|
||||
|
||||
StringView(const uint16_t* characters, size_t length)
|
||||
: m_is8Bit(false), m_length(length), m_characters16(characters) {}
|
||||
|
||||
bool is8Bit() const { return m_is8Bit; }
|
||||
size_t length() const { return m_length; }
|
||||
|
||||
// TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
|
||||
// here.
|
||||
const uint8_t* characters8() const { return m_characters8; }
|
||||
const uint16_t* characters16() const { return m_characters16; }
|
||||
|
||||
private:
|
||||
bool m_is8Bit;
|
||||
size_t m_length;
|
||||
union {
|
||||
const uint8_t* m_characters8;
|
||||
const uint16_t* m_characters16;
|
||||
};
|
||||
};
|
||||
|
||||
class StringBuffer {
|
||||
public:
|
||||
virtual ~StringBuffer() = default;
|
||||
virtual const StringView& string() = 0;
|
||||
// This method copies contents.
|
||||
static std::unique_ptr<StringBuffer> create(const StringView&);
|
||||
};
|
||||
|
||||
class Channel {
|
||||
public:
|
||||
Channel() {}
|
||||
virtual ~Channel() {}
|
||||
|
||||
virtual void method1(int32_t arg) {
|
||||
std::cout << "default v8::Channel::method1(" << arg << ") called"
|
||||
std::cout << "default v8_inspector::Channel::method1(" << arg << ") called"
|
||||
<< std::endl;
|
||||
}
|
||||
|
||||
virtual int32_t method2() const = 0;
|
||||
};
|
||||
} // namespace v8
|
||||
} // namespace v8_inspector
|
||||
|
||||
#include "v8_inspector/string_buffer.h"
|
||||
|
||||
extern "C" {
|
||||
void v8_inspector__Channel__EXTENDER__method1(v8::Channel& self, int32_t arg);
|
||||
int32_t v8_inspector__Channel__EXTENDER__method2(const v8::Channel& self);
|
||||
void v8_inspector__Channel__EXTENDER__method1(v8_inspector::Channel& self,
|
||||
int32_t arg);
|
||||
int32_t v8_inspector__Channel__EXTENDER__method2(
|
||||
const v8_inspector::Channel& self);
|
||||
}
|
||||
|
||||
namespace extender {
|
||||
template <class T>
|
||||
using uninit_t = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
|
||||
using uninit_t = typename ::std::aligned_storage<sizeof(T), alignof(T)>::type;
|
||||
|
||||
namespace v8 {
|
||||
struct Channel : public ::v8::Channel {
|
||||
using ::v8::Channel::Channel;
|
||||
namespace v8_inspector {
|
||||
struct Channel : public ::v8_inspector::Channel {
|
||||
using ::v8_inspector::Channel::Channel;
|
||||
|
||||
void method1(int32_t arg) override {
|
||||
v8_inspector__Channel__EXTENDER__method1(*this, arg);
|
||||
|
@ -41,24 +81,26 @@ struct Channel : public ::v8::Channel {
|
|||
return v8_inspector__Channel__EXTENDER__method2(*this);
|
||||
}
|
||||
};
|
||||
} // namespace v8
|
||||
} // namespace v8_inspector
|
||||
} // namespace extender
|
||||
|
||||
extern "C" {
|
||||
void v8_inspector__Channel__DTOR(v8::Channel& self) {
|
||||
void v8_inspector__Channel__DTOR(::v8_inspector::Channel& self) {
|
||||
self.~Channel();
|
||||
}
|
||||
void v8_inspector__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
void v8_inspector__Channel__method1(::v8_inspector::Channel& self,
|
||||
int32_t arg) {
|
||||
self.method1(arg);
|
||||
}
|
||||
void v8_inspector__Channel__Channel__method1(v8::Channel& self, int32_t arg) {
|
||||
self.::v8::Channel::method1(arg);
|
||||
void v8_inspector__Channel__Channel__method1(::v8_inspector::Channel& self,
|
||||
int32_t arg) {
|
||||
self.::v8_inspector::Channel::method1(arg);
|
||||
}
|
||||
int32_t v8_inspector__Channel__method2(const v8::Channel& self) {
|
||||
int32_t v8_inspector__Channel__method2(const ::v8_inspector::Channel& self) {
|
||||
return self.method2();
|
||||
}
|
||||
void v8_inspector__Channel__EXTENDER__CTOR(
|
||||
extender::uninit_t<extender::v8::Channel>& buf) {
|
||||
new (std::launder(&buf)) extender::v8::Channel();
|
||||
::extender::uninit_t<::extender::v8_inspector::Channel>& buf) {
|
||||
new (::std::launder(&buf))::extender::v8_inspector::Channel();
|
||||
}
|
||||
} // extern "C"
|
|
@ -1,3 +1,5 @@
|
|||
#![allow(dead_code)]
|
||||
|
||||
mod cxx_util;
|
||||
mod v8_inspector;
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::cxx_util::CxxVTable;
|
||||
use crate::cxx_util::FieldOffset;
|
||||
use crate::cxx_util::Opaque;
|
||||
use crate::cxx_util::RustVTable;
|
||||
|
@ -42,7 +43,7 @@ pub unsafe extern "C" fn v8_inspector__Channel__EXTENDER__method2(
|
|||
|
||||
#[repr(C)]
|
||||
pub struct Channel {
|
||||
_cxx_vtable: *const Opaque,
|
||||
_cxx_vtable: CxxVTable,
|
||||
}
|
||||
|
||||
impl Channel {
|
||||
|
|
176
src/v8_inspector/channel.rs.template
Normal file
176
src/v8_inspector/channel.rs.template
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))
|
||||
}
|
||||
}
|
|
@ -1 +1,7 @@
|
|||
pub mod channel;
|
||||
pub mod string_buffer;
|
||||
pub mod string_view;
|
||||
|
||||
pub use channel::Channel;
|
||||
pub use string_buffer::StringBuffer;
|
||||
pub use string_view::StringView;
|
||||
|
|
16
src/v8_inspector/string_buffer.h
Normal file
16
src/v8_inspector/string_buffer.h
Normal file
|
@ -0,0 +1,16 @@
|
|||
|
||||
extern "C" {
|
||||
void v8_inspector__StringBuffer__DELETE(::v8_inspector::StringBuffer& self) {
|
||||
delete &self;
|
||||
}
|
||||
|
||||
const ::v8_inspector::StringView& v8_inspector__StringBuffer__string(
|
||||
::v8_inspector::StringBuffer& self) {
|
||||
return self.string();
|
||||
}
|
||||
|
||||
::v8_inspector::StringBuffer* v8_inspector__StringBuffer__create(
|
||||
const ::v8_inspector::StringView& source) {
|
||||
return ::v8_inspector::StringBuffer::create(source).release();
|
||||
}
|
||||
}
|
44
src/v8_inspector/string_buffer.rs
Normal file
44
src/v8_inspector/string_buffer.rs
Normal file
|
@ -0,0 +1,44 @@
|
|||
use crate::cxx_util::CxxVTable;
|
||||
use crate::cxx_util::Delete;
|
||||
use crate::cxx_util::UniquePtr;
|
||||
|
||||
use super::StringView;
|
||||
|
||||
// class V8_EXPORT StringBuffer {
|
||||
// public:
|
||||
// virtual ~StringBuffer() = default;
|
||||
// virtual const StringView& string() = 0;
|
||||
// // This method copies contents.
|
||||
// static std::unique_ptr<StringBuffer> create(const StringView&);
|
||||
// };
|
||||
|
||||
extern "C" {
|
||||
fn v8_inspector__StringBuffer__DELETE(this: &'static mut StringBuffer) -> ();
|
||||
fn v8_inspector__StringBuffer__string(this: &mut StringBuffer)
|
||||
-> &StringView;
|
||||
fn v8_inspector__StringBuffer__create(
|
||||
source: &StringView,
|
||||
) -> UniquePtr<StringBuffer>;
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Debug)]
|
||||
pub struct StringBuffer {
|
||||
_cxx_vtable: CxxVTable,
|
||||
}
|
||||
|
||||
impl StringBuffer {
|
||||
pub fn string(&mut self) -> &StringView {
|
||||
unsafe { v8_inspector__StringBuffer__string(self) }
|
||||
}
|
||||
|
||||
pub fn create(source: &StringView) -> UniquePtr<StringBuffer> {
|
||||
unsafe { v8_inspector__StringBuffer__create(source) }
|
||||
}
|
||||
}
|
||||
|
||||
impl Delete for StringBuffer {
|
||||
fn delete(&'static mut self) {
|
||||
unsafe { v8_inspector__StringBuffer__DELETE(self) }
|
||||
}
|
||||
}
|
137
src/v8_inspector/string_view.rs
Normal file
137
src/v8_inspector/string_view.rs
Normal file
|
@ -0,0 +1,137 @@
|
|||
use std::marker::PhantomData;
|
||||
use std::ops::Deref;
|
||||
use std::ptr::null;
|
||||
use std::ptr::NonNull;
|
||||
use std::slice;
|
||||
|
||||
// Notes:
|
||||
// * This class is ported, not wrapped using bindings.
|
||||
// * Since Rust `repr(bool)` is not allowed, assume `bool` and `u8` have the
|
||||
// same size. TODO: find/open upstream issue to allow #[repr(bool)] support.
|
||||
|
||||
// ```cpp
|
||||
// class V8_EXPORT StringView {
|
||||
// public:
|
||||
// StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
|
||||
//
|
||||
// StringView(const uint8_t* characters, size_t length)
|
||||
// : m_is8Bit(true), m_length(length), m_characters8(characters) {}
|
||||
//
|
||||
// StringView(const uint16_t* characters, size_t length)
|
||||
// : m_is8Bit(false), m_length(length), m_characters16(characters) {}
|
||||
//
|
||||
// bool is8Bit() const { return m_is8Bit; }
|
||||
// size_t length() const { return m_length; }
|
||||
//
|
||||
// const uint8_t* characters8() const { return m_characters8; }
|
||||
// const uint16_t* characters16() const { return m_characters16; }
|
||||
//
|
||||
// private:
|
||||
// bool m_is8Bit;
|
||||
// size_t m_length;
|
||||
// union {
|
||||
// const uint8_t* m_characters8;
|
||||
// const uint16_t* m_characters16;
|
||||
// };
|
||||
// };
|
||||
// ```
|
||||
|
||||
#[repr(u8)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub enum StringView<'a> {
|
||||
// Do not reorder!
|
||||
U16(CharacterArray<'a, u16>),
|
||||
U8(CharacterArray<'a, u8>),
|
||||
}
|
||||
|
||||
impl StringView<'static> {
|
||||
pub fn empty() -> Self {
|
||||
Self::U8(CharacterArray::<'static, u8>::empty())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u8]> for StringView<'a> {
|
||||
fn from(v: &'a [u8]) -> Self {
|
||||
Self::U8(CharacterArray::<'a, u8>::from(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> From<&'a [u16]> for StringView<'a> {
|
||||
fn from(v: &'a [u16]) -> Self {
|
||||
Self::U16(CharacterArray::<'a, u16>::from(v))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> StringView<'a> {
|
||||
pub fn is_8bit(&self) -> bool {
|
||||
match self {
|
||||
Self::U16(..) => false,
|
||||
Self::U8(..) => true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn length(&self) -> usize {
|
||||
match self {
|
||||
Self::U16(v) => v.m_length,
|
||||
Self::U8(v) => v.m_length,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn characters8(&self) -> Option<&[u8]> {
|
||||
match self {
|
||||
Self::U16(..) => None,
|
||||
Self::U8(v) => Some(v),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn characters16(&self) -> Option<&[u16]> {
|
||||
match self {
|
||||
Self::U16(v) => Some(v),
|
||||
Self::U8(..) => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct CharacterArray<'a, T> {
|
||||
m_length: usize,
|
||||
m_characters: *const T,
|
||||
_phantom: PhantomData<&'a T>,
|
||||
}
|
||||
|
||||
impl CharacterArray<'static, u8> {
|
||||
fn empty() -> Self {
|
||||
Self {
|
||||
m_length: 0,
|
||||
m_characters: null(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> From<&'a [T]> for CharacterArray<'a, T> {
|
||||
fn from(v: &'a [T]) -> Self {
|
||||
Self {
|
||||
m_length: v.len(),
|
||||
m_characters: v.as_ptr(),
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T> Deref for CharacterArray<'a, T> {
|
||||
type Target = [T];
|
||||
fn deref(&self) -> &[T] {
|
||||
let Self {
|
||||
m_length,
|
||||
mut m_characters,
|
||||
..
|
||||
} = *self;
|
||||
if m_characters.is_null() {
|
||||
assert_eq!(m_length, 0);
|
||||
m_characters = NonNull::dangling().as_ptr()
|
||||
};
|
||||
unsafe { slice::from_raw_parts(m_characters, m_length) }
|
||||
}
|
||||
}
|
306
v8-inspector.h
Normal file
306
v8-inspector.h
Normal file
|
@ -0,0 +1,306 @@
|
|||
// Copyright 2016 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef V8_V8_INSPECTOR_H_
|
||||
#define V8_V8_INSPECTOR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <cctype>
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "v8.h" // NOLINT(build/include)
|
||||
|
||||
namespace v8_inspector {
|
||||
|
||||
namespace protocol {
|
||||
namespace Debugger {
|
||||
namespace API {
|
||||
class SearchMatch;
|
||||
}
|
||||
}
|
||||
namespace Runtime {
|
||||
namespace API {
|
||||
class RemoteObject;
|
||||
class StackTrace;
|
||||
class StackTraceId;
|
||||
}
|
||||
}
|
||||
namespace Schema {
|
||||
namespace API {
|
||||
class Domain;
|
||||
}
|
||||
}
|
||||
} // namespace protocol
|
||||
|
||||
class V8_EXPORT StringView {
|
||||
public:
|
||||
StringView() : m_is8Bit(true), m_length(0), m_characters8(nullptr) {}
|
||||
|
||||
StringView(const uint8_t* characters, size_t length)
|
||||
: m_is8Bit(true), m_length(length), m_characters8(characters) {}
|
||||
|
||||
StringView(const uint16_t* characters, size_t length)
|
||||
: m_is8Bit(false), m_length(length), m_characters16(characters) {}
|
||||
|
||||
bool is8Bit() const { return m_is8Bit; }
|
||||
size_t length() const { return m_length; }
|
||||
|
||||
// TODO(dgozman): add DCHECK(m_is8Bit) to accessors once platform can be used
|
||||
// here.
|
||||
const uint8_t* characters8() const { return m_characters8; }
|
||||
const uint16_t* characters16() const { return m_characters16; }
|
||||
|
||||
private:
|
||||
bool m_is8Bit;
|
||||
size_t m_length;
|
||||
union {
|
||||
const uint8_t* m_characters8;
|
||||
const uint16_t* m_characters16;
|
||||
};
|
||||
};
|
||||
|
||||
class V8_EXPORT StringBuffer {
|
||||
public:
|
||||
virtual ~StringBuffer() = default;
|
||||
virtual const StringView& string() = 0;
|
||||
// This method copies contents.
|
||||
static std::unique_ptr<StringBuffer> create(const StringView&);
|
||||
};
|
||||
|
||||
class V8_EXPORT V8ContextInfo {
|
||||
public:
|
||||
V8ContextInfo(v8::Local<v8::Context> context, int contextGroupId,
|
||||
const StringView& humanReadableName)
|
||||
: context(context),
|
||||
contextGroupId(contextGroupId),
|
||||
humanReadableName(humanReadableName),
|
||||
hasMemoryOnConsole(false) {}
|
||||
|
||||
v8::Local<v8::Context> context;
|
||||
// Each v8::Context is a part of a group. The group id must be non-zero.
|
||||
int contextGroupId;
|
||||
StringView humanReadableName;
|
||||
StringView origin;
|
||||
StringView auxData;
|
||||
bool hasMemoryOnConsole;
|
||||
|
||||
static int executionContextId(v8::Local<v8::Context> context);
|
||||
|
||||
// Disallow copying and allocating this one.
|
||||
enum NotNullTagEnum { NotNullLiteral };
|
||||
void* operator new(size_t) = delete;
|
||||
void* operator new(size_t, NotNullTagEnum, void*) = delete;
|
||||
void* operator new(size_t, void*) = delete;
|
||||
V8ContextInfo(const V8ContextInfo&) = delete;
|
||||
V8ContextInfo& operator=(const V8ContextInfo&) = delete;
|
||||
};
|
||||
|
||||
class V8_EXPORT V8StackTrace {
|
||||
public:
|
||||
virtual StringView firstNonEmptySourceURL() const = 0;
|
||||
virtual bool isEmpty() const = 0;
|
||||
virtual StringView topSourceURL() const = 0;
|
||||
virtual int topLineNumber() const = 0;
|
||||
virtual int topColumnNumber() const = 0;
|
||||
virtual StringView topScriptId() const = 0;
|
||||
virtual StringView topFunctionName() const = 0;
|
||||
|
||||
virtual ~V8StackTrace() = default;
|
||||
virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
|
||||
buildInspectorObject() const = 0;
|
||||
virtual std::unique_ptr<protocol::Runtime::API::StackTrace>
|
||||
buildInspectorObject(int maxAsyncDepth) const = 0;
|
||||
virtual std::unique_ptr<StringBuffer> toString() const = 0;
|
||||
|
||||
// Safe to pass between threads, drops async chain.
|
||||
virtual std::unique_ptr<V8StackTrace> clone() = 0;
|
||||
};
|
||||
|
||||
class V8_EXPORT V8InspectorSession {
|
||||
public:
|
||||
virtual ~V8InspectorSession() = default;
|
||||
|
||||
// Cross-context inspectable values (DOM nodes in different worlds, etc.).
|
||||
class V8_EXPORT Inspectable {
|
||||
public:
|
||||
virtual v8::Local<v8::Value> get(v8::Local<v8::Context>) = 0;
|
||||
virtual ~Inspectable() = default;
|
||||
};
|
||||
virtual void addInspectedObject(std::unique_ptr<Inspectable>) = 0;
|
||||
|
||||
// Dispatching protocol messages.
|
||||
static bool canDispatchMethod(const StringView& method);
|
||||
virtual void dispatchProtocolMessage(const StringView& message) = 0;
|
||||
virtual std::vector<uint8_t> state() = 0;
|
||||
virtual std::vector<std::unique_ptr<protocol::Schema::API::Domain>>
|
||||
supportedDomains() = 0;
|
||||
|
||||
// Debugger actions.
|
||||
virtual void schedulePauseOnNextStatement(const StringView& breakReason,
|
||||
const StringView& breakDetails) = 0;
|
||||
virtual void cancelPauseOnNextStatement() = 0;
|
||||
virtual void breakProgram(const StringView& breakReason,
|
||||
const StringView& breakDetails) = 0;
|
||||
virtual void setSkipAllPauses(bool) = 0;
|
||||
virtual void resume() = 0;
|
||||
virtual void stepOver() = 0;
|
||||
virtual std::vector<std::unique_ptr<protocol::Debugger::API::SearchMatch>>
|
||||
searchInTextByLines(const StringView& text, const StringView& query,
|
||||
bool caseSensitive, bool isRegex) = 0;
|
||||
|
||||
// Remote objects.
|
||||
virtual std::unique_ptr<protocol::Runtime::API::RemoteObject> wrapObject(
|
||||
v8::Local<v8::Context>, v8::Local<v8::Value>, const StringView& groupName,
|
||||
bool generatePreview) = 0;
|
||||
|
||||
virtual bool unwrapObject(std::unique_ptr<StringBuffer>* error,
|
||||
const StringView& objectId, v8::Local<v8::Value>*,
|
||||
v8::Local<v8::Context>*,
|
||||
std::unique_ptr<StringBuffer>* objectGroup) = 0;
|
||||
virtual void releaseObjectGroup(const StringView&) = 0;
|
||||
};
|
||||
|
||||
class V8_EXPORT V8InspectorClient {
|
||||
public:
|
||||
virtual ~V8InspectorClient() = default;
|
||||
|
||||
virtual void runMessageLoopOnPause(int contextGroupId) {}
|
||||
virtual void quitMessageLoopOnPause() {}
|
||||
virtual void runIfWaitingForDebugger(int contextGroupId) {}
|
||||
|
||||
virtual void muteMetrics(int contextGroupId) {}
|
||||
virtual void unmuteMetrics(int contextGroupId) {}
|
||||
|
||||
virtual void beginUserGesture() {}
|
||||
virtual void endUserGesture() {}
|
||||
|
||||
virtual std::unique_ptr<StringBuffer> valueSubtype(v8::Local<v8::Value>) {
|
||||
return nullptr;
|
||||
}
|
||||
virtual bool formatAccessorsAsProperties(v8::Local<v8::Value>) {
|
||||
return false;
|
||||
}
|
||||
virtual bool isInspectableHeapObject(v8::Local<v8::Object>) { return true; }
|
||||
|
||||
virtual v8::Local<v8::Context> ensureDefaultContextInGroup(
|
||||
int contextGroupId) {
|
||||
return v8::Local<v8::Context>();
|
||||
}
|
||||
virtual void beginEnsureAllContextsInGroup(int contextGroupId) {}
|
||||
virtual void endEnsureAllContextsInGroup(int contextGroupId) {}
|
||||
|
||||
virtual void installAdditionalCommandLineAPI(v8::Local<v8::Context>,
|
||||
v8::Local<v8::Object>) {}
|
||||
virtual void consoleAPIMessage(int contextGroupId,
|
||||
v8::Isolate::MessageErrorLevel level,
|
||||
const StringView& message,
|
||||
const StringView& url, unsigned lineNumber,
|
||||
unsigned columnNumber, V8StackTrace*) {}
|
||||
virtual v8::MaybeLocal<v8::Value> memoryInfo(v8::Isolate*,
|
||||
v8::Local<v8::Context>) {
|
||||
return v8::MaybeLocal<v8::Value>();
|
||||
}
|
||||
|
||||
virtual void consoleTime(const StringView& title) {}
|
||||
virtual void consoleTimeEnd(const StringView& title) {}
|
||||
virtual void consoleTimeStamp(const StringView& title) {}
|
||||
virtual void consoleClear(int contextGroupId) {}
|
||||
virtual double currentTimeMS() { return 0; }
|
||||
typedef void (*TimerCallback)(void*);
|
||||
virtual void startRepeatingTimer(double, TimerCallback, void* data) {}
|
||||
virtual void cancelTimer(void* data) {}
|
||||
|
||||
// TODO(dgozman): this was added to support service worker shadow page. We
|
||||
// should not connect at all.
|
||||
virtual bool canExecuteScripts(int contextGroupId) { return true; }
|
||||
|
||||
virtual void maxAsyncCallStackDepthChanged(int depth) {}
|
||||
|
||||
virtual std::unique_ptr<StringBuffer> resourceNameToUrl(
|
||||
const StringView& resourceName) {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
// These stack trace ids are intended to be passed between debuggers and be
|
||||
// resolved later. This allows to track cross-debugger calls and step between
|
||||
// them if a single client connects to multiple debuggers.
|
||||
struct V8_EXPORT V8StackTraceId {
|
||||
uintptr_t id;
|
||||
std::pair<int64_t, int64_t> debugger_id;
|
||||
bool should_pause = false;
|
||||
|
||||
V8StackTraceId();
|
||||
V8StackTraceId(const V8StackTraceId&) = default;
|
||||
V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id);
|
||||
V8StackTraceId(uintptr_t id, const std::pair<int64_t, int64_t> debugger_id,
|
||||
bool should_pause);
|
||||
explicit V8StackTraceId(const StringView&);
|
||||
V8StackTraceId& operator=(const V8StackTraceId&) = default;
|
||||
V8StackTraceId& operator=(V8StackTraceId&&) noexcept = default;
|
||||
~V8StackTraceId() = default;
|
||||
|
||||
bool IsInvalid() const;
|
||||
std::unique_ptr<StringBuffer> ToString();
|
||||
};
|
||||
|
||||
class V8_EXPORT V8Inspector {
|
||||
public:
|
||||
static std::unique_ptr<V8Inspector> create(v8::Isolate*, V8InspectorClient*);
|
||||
virtual ~V8Inspector() = default;
|
||||
|
||||
// Contexts instrumentation.
|
||||
virtual void contextCreated(const V8ContextInfo&) = 0;
|
||||
virtual void contextDestroyed(v8::Local<v8::Context>) = 0;
|
||||
virtual void resetContextGroup(int contextGroupId) = 0;
|
||||
virtual v8::MaybeLocal<v8::Context> contextById(int contextId) = 0;
|
||||
|
||||
// Various instrumentation.
|
||||
virtual void idleStarted() = 0;
|
||||
virtual void idleFinished() = 0;
|
||||
|
||||
// Async stack traces instrumentation.
|
||||
virtual void asyncTaskScheduled(const StringView& taskName, void* task,
|
||||
bool recurring) = 0;
|
||||
virtual void asyncTaskCanceled(void* task) = 0;
|
||||
virtual void asyncTaskStarted(void* task) = 0;
|
||||
virtual void asyncTaskFinished(void* task) = 0;
|
||||
virtual void allAsyncTasksCanceled() = 0;
|
||||
|
||||
virtual V8StackTraceId storeCurrentStackTrace(
|
||||
const StringView& description) = 0;
|
||||
virtual void externalAsyncTaskStarted(const V8StackTraceId& parent) = 0;
|
||||
virtual void externalAsyncTaskFinished(const V8StackTraceId& parent) = 0;
|
||||
|
||||
// Exceptions instrumentation.
|
||||
virtual unsigned exceptionThrown(
|
||||
v8::Local<v8::Context>, const StringView& message,
|
||||
v8::Local<v8::Value> exception, const StringView& detailedMessage,
|
||||
const StringView& url, unsigned lineNumber, unsigned columnNumber,
|
||||
std::unique_ptr<V8StackTrace>, int scriptId) = 0;
|
||||
virtual void exceptionRevoked(v8::Local<v8::Context>, unsigned exceptionId,
|
||||
const StringView& message) = 0;
|
||||
|
||||
// Connection.
|
||||
class V8_EXPORT Channel {
|
||||
public:
|
||||
virtual ~Channel() = default;
|
||||
virtual void sendResponse(int callId,
|
||||
std::unique_ptr<StringBuffer> message) = 0;
|
||||
virtual void sendNotification(std::unique_ptr<StringBuffer> message) = 0;
|
||||
virtual void flushProtocolNotifications() = 0;
|
||||
};
|
||||
virtual std::unique_ptr<V8InspectorSession> connect(
|
||||
int contextGroupId, Channel*, const StringView& state) = 0;
|
||||
|
||||
// API methods.
|
||||
virtual std::unique_ptr<V8StackTrace> createStackTrace(
|
||||
v8::Local<v8::StackTrace>) = 0;
|
||||
virtual std::unique_ptr<V8StackTrace> captureStackTrace(bool fullStack) = 0;
|
||||
};
|
||||
|
||||
} // namespace v8_inspector
|
||||
|
||||
#endif // V8_V8_INSPECTOR_H_
|
Loading…
Reference in a new issue