Skip to content

Commit 2207b36

Browse files
committed
refactor: reuse rb_sys ffi bindings
1 parent 446517c commit 2207b36

File tree

2 files changed

+14
-78
lines changed

2 files changed

+14
-78
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
reduce extern C definitions referencing rb_sys

gems/codetracer-ruby-recorder/ext/native_tracer/src/lib.rs

Lines changed: 13 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -28,84 +28,19 @@ use runtime_tracing::{
2828
// Event hook function type from Ruby debug.h
2929
type rb_event_hook_func_t = Option<unsafe extern "C" fn(rb_event_flag_t, VALUE, VALUE, ID, VALUE)>;
3030

31-
// Event hook flags enum from Ruby debug.h
32-
#[repr(C)]
33-
#[derive(Clone, Copy)]
34-
enum rb_event_hook_flag_t {
35-
RUBY_EVENT_HOOK_FLAG_SAFE = 0x01,
36-
RUBY_EVENT_HOOK_FLAG_DELETED = 0x02,
37-
RUBY_EVENT_HOOK_FLAG_RAW_ARG = 0x04,
38-
}
39-
40-
#[repr(C)]
41-
struct RTypedData {
42-
_basic: [VALUE; 2],
43-
type_: *const rb_data_type_t,
44-
typed_flag: VALUE,
45-
data: *mut c_void,
46-
}
47-
48-
#[repr(C)]
49-
struct rb_data_type_function_struct {
50-
dmark: Option<unsafe extern "C" fn(*mut c_void)>,
51-
dfree: Option<unsafe extern "C" fn(*mut c_void)>,
52-
dsize: Option<unsafe extern "C" fn(*const c_void) -> usize>,
53-
dcompact: Option<unsafe extern "C" fn(*mut c_void)>,
54-
reserved: [*mut c_void; 1],
55-
}
56-
57-
#[repr(C)]
58-
struct rb_data_type_t {
59-
wrap_struct_name: *const c_char,
60-
function: rb_data_type_function_struct,
61-
parent: *const rb_data_type_t,
62-
data: *mut c_void,
63-
flags: VALUE,
64-
}
65-
66-
extern "C" {
67-
fn rb_data_typed_object_wrap(
68-
klass: VALUE,
69-
datap: *mut c_void,
70-
data_type: *const rb_data_type_t,
71-
) -> VALUE;
72-
fn rb_check_typeddata(obj: VALUE, data_type: *const rb_data_type_t) -> *mut c_void;
73-
fn rb_num2dbl(val: VALUE) -> f64;
74-
fn rb_obj_is_kind_of(obj: VALUE, class: VALUE) -> VALUE;
75-
fn rb_path2class(path: *const c_char) -> VALUE;
76-
fn rb_const_defined(klass: VALUE, name: ID) -> VALUE;
77-
fn rb_const_get(klass: VALUE, name: ID) -> VALUE;
78-
static rb_cTime: VALUE;
79-
static rb_cRegexp: VALUE;
80-
static rb_cStruct: VALUE;
81-
static rb_cRange: VALUE;
82-
fn rb_method_boundp(klass: VALUE, mid: ID, ex: c_int) -> VALUE;
83-
84-
// TracePoint API functions that aren't in rb_sys
85-
fn rb_add_event_hook2(
86-
func: rb_event_hook_func_t,
87-
events: rb_event_flag_t,
88-
data: VALUE,
89-
hook_flag: rb_event_hook_flag_t,
90-
);
91-
fn rb_remove_event_hook_with_data(
92-
func: rb_event_hook_func_t,
93-
data: VALUE,
94-
) -> c_int;
95-
fn rb_tracearg_event_flag(trace_arg: *mut rb_trace_arg_t) -> rb_event_flag_t;
96-
fn rb_tracearg_lineno(trace_arg: *mut rb_trace_arg_t) -> VALUE;
97-
fn rb_tracearg_path(trace_arg: *mut rb_trace_arg_t) -> VALUE;
98-
fn rb_tracearg_self(trace_arg: *mut rb_trace_arg_t) -> VALUE;
99-
fn rb_tracearg_binding(trace_arg: *mut rb_trace_arg_t) -> VALUE;
100-
fn rb_tracearg_callee_id(trace_arg: *mut rb_trace_arg_t) -> VALUE;
101-
fn rb_tracearg_return_value(trace_arg: *mut rb_trace_arg_t) -> VALUE;
102-
fn rb_tracearg_raised_exception(trace_arg: *mut rb_trace_arg_t) -> VALUE;
103-
}
31+
// Use event hook flags enum from rb_sys
32+
use rb_sys::rb_event_hook_flag_t;
10433

105-
#[repr(C)]
106-
struct rb_trace_arg_t {
107-
// Opaque struct representing rb_trace_arg_struct from Ruby
108-
}
34+
// Types from rb_sys bindings
35+
use rb_sys::{
36+
rb_data_typed_object_wrap, rb_check_typeddata, rb_num2dbl, rb_obj_is_kind_of,
37+
rb_const_defined, rb_const_get, rb_method_boundp, rb_cTime,
38+
rb_cRegexp, rb_cStruct, rb_cRange, rb_add_event_hook2,
39+
rb_remove_event_hook_with_data, rb_tracearg_event_flag, rb_tracearg_lineno,
40+
rb_tracearg_path, rb_tracearg_self, rb_tracearg_binding, rb_tracearg_callee_id,
41+
rb_tracearg_return_value, rb_tracearg_raised_exception, rb_trace_arg_t,
42+
rb_data_type_t, rb_data_type_struct__bindgen_ty_1,
43+
};
10944

11045
struct Recorder {
11146
tracer: Tracer,
@@ -186,7 +121,7 @@ unsafe extern "C" fn recorder_free(ptr: *mut c_void) {
186121

187122
static mut RECORDER_TYPE: rb_data_type_t = rb_data_type_t {
188123
wrap_struct_name: b"Recorder\0".as_ptr() as *const c_char,
189-
function: rb_data_type_function_struct {
124+
function: rb_data_type_struct__bindgen_ty_1 {
190125
dmark: None,
191126
dfree: Some(recorder_free),
192127
dsize: None,

0 commit comments

Comments
 (0)