@@ -6,45 +6,6 @@ use harp::utils::r_str_to_owned_utf8_unchecked;
6
6
use crate :: interface:: RMain ;
7
7
use crate :: interface:: CAPTURE_CONSOLE_OUTPUT ;
8
8
9
- /// Run closure and capture its console output.
10
- ///
11
- /// Useful for debugging. For instance you can use this to call code from the
12
- /// lldb interpreter. Output from stdout and stderr is returned instead of being
13
- /// sent over IOPub.
14
- ///
15
- /// The closure is run in a `harp::try_catch()` context to prevent R errors and
16
- /// other C longjumps from collapsing the debugging context. If a Rust panic
17
- /// occurs however, it is propagated as normal.
18
- ///
19
- /// Note that the resulting string is stored on the R heap and never freed. This
20
- /// should only be used in a debugging context where leaking is not an issue.
21
- pub fn capture_console_output ( cb : impl FnOnce ( ) ) -> * const ffi:: c_char {
22
- let old = CAPTURE_CONSOLE_OUTPUT . swap ( true , Ordering :: SeqCst ) ;
23
-
24
- // We protect from panics to correctly restore `CAPTURE_CONSOLE_OUTPUT`'s
25
- // state. The panic is resumed right after.
26
- let result = std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || harp:: try_catch ( cb) ) ) ;
27
-
28
- CAPTURE_CONSOLE_OUTPUT . store ( old, Ordering :: SeqCst ) ;
29
- let mut out = std:: mem:: take ( & mut RMain :: get_mut ( ) . captured_output ) ;
30
-
31
- // Unwrap catch-unwind's result and resume panic if needed
32
- let result = match result {
33
- Ok ( res) => res,
34
- Err ( err) => {
35
- std:: panic:: resume_unwind ( err) ;
36
- } ,
37
- } ;
38
-
39
- // Unwrap try-catch's result
40
- if let Err ( err) = result {
41
- out = format ! ( "{out}\n Unexpected longjump in `capture_console_output()`: {err:?}" ) ;
42
- }
43
-
44
- // Intentionally leaks, should only be used in the debugger
45
- ffi:: CString :: new ( out) . unwrap ( ) . into_raw ( )
46
- }
47
-
48
9
// Implementations for entry points in `debug.c`.
49
10
50
11
#[ no_mangle]
@@ -126,3 +87,42 @@ pub fn tidy_kind(kind: libr::SEXPTYPE) -> &'static str {
126
87
_ => "unknown" ,
127
88
}
128
89
}
90
+
91
+ /// Run closure and capture its console output.
92
+ ///
93
+ /// Useful for debugging. For instance you can use this to call code from the
94
+ /// lldb interpreter. Output from stdout and stderr is returned instead of being
95
+ /// sent over IOPub.
96
+ ///
97
+ /// The closure is run in a `harp::try_catch()` context to prevent R errors and
98
+ /// other C longjumps from collapsing the debugging context. If a Rust panic
99
+ /// occurs however, it is propagated as normal.
100
+ ///
101
+ /// Note that the resulting string is stored on the R heap and never freed. This
102
+ /// should only be used in a debugging context where leaking is not an issue.
103
+ pub fn capture_console_output ( cb : impl FnOnce ( ) ) -> * const ffi:: c_char {
104
+ let old = CAPTURE_CONSOLE_OUTPUT . swap ( true , Ordering :: SeqCst ) ;
105
+
106
+ // We protect from panics to correctly restore `CAPTURE_CONSOLE_OUTPUT`'s
107
+ // state. The panic is resumed right after.
108
+ let result = std:: panic:: catch_unwind ( std:: panic:: AssertUnwindSafe ( || harp:: try_catch ( cb) ) ) ;
109
+
110
+ CAPTURE_CONSOLE_OUTPUT . store ( old, Ordering :: SeqCst ) ;
111
+ let mut out = std:: mem:: take ( & mut RMain :: get_mut ( ) . captured_output ) ;
112
+
113
+ // Unwrap catch-unwind's result and resume panic if needed
114
+ let result = match result {
115
+ Ok ( res) => res,
116
+ Err ( err) => {
117
+ std:: panic:: resume_unwind ( err) ;
118
+ } ,
119
+ } ;
120
+
121
+ // Unwrap try-catch's result
122
+ if let Err ( err) = result {
123
+ out = format ! ( "{out}\n Unexpected longjump in `capture_console_output()`: {err:?}" ) ;
124
+ }
125
+
126
+ // Intentionally leaks, should only be used in the debugger
127
+ ffi:: CString :: new ( out) . unwrap ( ) . into_raw ( )
128
+ }
0 commit comments