Skip to main content

luaur_repl_cli/functions/
counters_function_callback.rs

1use alloc::string::String;
2use core::ffi::{c_char, c_void, CStr};
3
4use crate::records::function_counters::FunctionCounters;
5use crate::records::module_counters::ModuleCounters;
6
7// Faithful port of Counters.cpp's `countersFunctionCallback`.
8pub unsafe fn counters_function_callback(
9    context: *mut c_void,
10    function: *const c_char,
11    line_defined: i32,
12) {
13    let counters = &mut *(context as *mut ModuleCounters);
14
15    let name: String = if function.is_null() && line_defined == 1 {
16        "<main>".into()
17    } else if !function.is_null() {
18        let func = CStr::from_ptr(function).to_string_lossy();
19        alloc::format!("{}:{}", func, line_defined)
20    } else {
21        alloc::format!("<anonymous>:{}", line_defined)
22    };
23
24    counters.functions.push(FunctionCounters {
25        name,
26        ..Default::default()
27    });
28}