@@ -22,10 +22,8 @@ use parking_lot::Mutex;
22
22
use std:: cell:: RefCell ;
23
23
use std:: collections:: HashMap ;
24
24
use std:: ffi:: c_void;
25
- use std:: mem:: MaybeUninit ;
26
25
use std:: mem:: take;
27
26
use std:: pin:: Pin ;
28
- use std:: ptr;
29
27
use std:: ptr:: NonNull ;
30
28
use std:: rc:: Rc ;
31
29
use std:: sync:: Arc ;
@@ -190,10 +188,14 @@ impl JsRuntimeInspectorState {
190
188
loop {
191
189
loop {
192
190
// Do one "handshake" with a newly connected session at a time.
193
- if let Some ( mut session) = sessions. handshake . take ( ) {
194
- let mut fut = pump_inspector_session_messages ( session) . boxed_local ( ) ;
191
+ if let Some ( session) = sessions. handshake . take ( ) {
192
+ let mut fut =
193
+ pump_inspector_session_messages ( session. clone ( ) ) . boxed_local ( ) ;
195
194
let _ = fut. poll_unpin ( cx) ;
196
195
sessions. established . push ( fut) ;
196
+ let id = sessions. next_local_id ;
197
+ sessions. next_local_id += 1 ;
198
+ sessions. local . insert ( id, session) ;
197
199
continue ;
198
200
}
199
201
@@ -533,18 +535,16 @@ impl SessionContainer {
533
535
has_active : !self . established . is_empty ( )
534
536
|| self . handshake . is_some ( )
535
537
|| !self . local . is_empty ( ) ,
536
- has_blocking : self . local . values ( ) . any ( |s| {
537
- matches ! ( s. state. borrow( ) . kind, InspectorSessionKind :: Blocking )
538
- } ) ,
538
+ has_blocking : self
539
+ . local
540
+ . values ( )
541
+ . any ( |s| matches ! ( s. state. kind, InspectorSessionKind :: Blocking ) ) ,
539
542
has_nonblocking : self . local . values ( ) . any ( |s| {
540
- matches ! (
541
- s. state. borrow( ) . kind,
542
- InspectorSessionKind :: NonBlocking { .. }
543
- )
543
+ matches ! ( s. state. kind, InspectorSessionKind :: NonBlocking { .. } )
544
544
} ) ,
545
545
has_nonblocking_wait_for_disconnect : self . local . values ( ) . any ( |s| {
546
546
matches ! (
547
- s. state. borrow ( ) . kind,
547
+ s. state. kind,
548
548
InspectorSessionKind :: NonBlocking {
549
549
wait_for_disconnect: true
550
550
}
@@ -655,31 +655,27 @@ impl task::ArcWake for InspectorWaker {
655
655
}
656
656
}
657
657
658
- #[ derive( Debug ) ]
658
+ #[ derive( Clone , Copy , Debug ) ]
659
659
pub enum InspectorSessionKind {
660
660
Blocking ,
661
661
NonBlocking { wait_for_disconnect : bool } ,
662
662
}
663
663
664
+ #[ derive( Clone ) ]
664
665
struct InspectorSessionState {
665
666
is_dispatching_message : Rc < RefCell < bool > > ,
666
- send : InspectorSessionSend ,
667
- rx : Option < SessionProxyReceiver > ,
667
+ send : Rc < InspectorSessionSend > ,
668
+ rx : Rc < RefCell < Option < SessionProxyReceiver > > > ,
668
669
// Describes if session should keep event loop alive, eg. a local REPL
669
670
// session should keep event loop alive, but a Websocket session shouldn't.
670
671
kind : InspectorSessionKind ,
671
672
}
672
673
673
- #[ derive( Clone ) ]
674
- struct InspectorSessionInner {
675
- state : Rc < RefCell < InspectorSessionState > > ,
676
- }
677
-
678
674
/// An inspector session that proxies messages to concrete "transport layer",
679
675
/// eg. Websocket or another set of channels.
680
676
struct InspectorSession {
681
677
v8_session : v8:: inspector:: V8InspectorSession ,
682
- state : Rc < RefCell < InspectorSessionState > > ,
678
+ state : InspectorSessionState ,
683
679
}
684
680
685
681
impl InspectorSession {
@@ -694,33 +690,27 @@ impl InspectorSession {
694
690
) -> Rc < Self > {
695
691
let state = InspectorSessionState {
696
692
is_dispatching_message,
697
- send,
698
- rx,
693
+ send : Rc :: new ( send ) ,
694
+ rx : Rc :: new ( RefCell :: new ( rx ) ) ,
699
695
kind : options. kind ,
700
696
} ;
701
- let inner = InspectorSessionInner {
702
- state : Rc :: new ( RefCell :: new ( state) ) ,
703
- } ;
704
697
705
698
let v8_session = v8_inspector. connect (
706
699
Self :: CONTEXT_GROUP_ID ,
707
- v8:: inspector:: Channel :: new ( Box :: new ( inner . clone ( ) ) ) ,
700
+ v8:: inspector:: Channel :: new ( Box :: new ( state . clone ( ) ) ) ,
708
701
v8:: inspector:: StringView :: empty ( ) ,
709
702
v8:: inspector:: V8InspectorClientTrustLevel :: FullyTrusted ,
710
703
) ;
711
704
712
- Rc :: new ( Self {
713
- v8_session,
714
- state : inner. state . clone ( ) ,
715
- } )
705
+ Rc :: new ( Self { v8_session, state } )
716
706
}
717
707
718
708
// Dispatch message to V8 session
719
709
fn dispatch_message ( & self , msg : String ) {
720
- * self . state . borrow ( ) . is_dispatching_message . borrow_mut ( ) = true ;
710
+ * self . state . is_dispatching_message . borrow_mut ( ) = true ;
721
711
let msg = v8:: inspector:: StringView :: from ( msg. as_bytes ( ) ) ;
722
712
self . v8_session . dispatch_protocol_message ( msg) ;
723
- * self . state . borrow ( ) . is_dispatching_message . borrow_mut ( ) = false ;
713
+ * self . state . is_dispatching_message . borrow_mut ( ) = false ;
724
714
}
725
715
726
716
pub fn break_on_next_statement ( & self ) {
@@ -732,21 +722,21 @@ impl InspectorSession {
732
722
}
733
723
}
734
724
735
- impl InspectorSessionInner {
725
+ impl InspectorSessionState {
736
726
fn send_message (
737
727
& self ,
738
728
msg_kind : InspectorMsgKind ,
739
729
msg : v8:: UniquePtr < v8:: inspector:: StringBuffer > ,
740
730
) {
741
731
let msg = msg. unwrap ( ) . string ( ) . to_string ( ) ;
742
- ( self . state . borrow ( ) . send ) ( InspectorMsg {
732
+ ( self . send ) ( InspectorMsg {
743
733
kind : msg_kind,
744
734
content : msg,
745
735
} ) ;
746
736
}
747
737
}
748
738
749
- impl v8:: inspector:: ChannelImpl for InspectorSessionInner {
739
+ impl v8:: inspector:: ChannelImpl for InspectorSessionState {
750
740
fn send_response (
751
741
& self ,
752
742
call_id : i32 ,
@@ -768,7 +758,7 @@ impl v8::inspector::ChannelImpl for InspectorSessionInner {
768
758
type InspectorSessionPumpMessages = Pin < Box < dyn Future < Output = ( ) > > > ;
769
759
770
760
async fn pump_inspector_session_messages ( session : Rc < InspectorSession > ) {
771
- let mut rx = session. state . borrow_mut ( ) . rx . take ( ) . unwrap ( ) ;
761
+ let mut rx = session. state . rx . borrow_mut ( ) . take ( ) . unwrap ( ) ;
772
762
while let Some ( msg) = rx. next ( ) . await {
773
763
session. dispatch_message ( msg) ;
774
764
}
0 commit comments