Skip to main content

rusteal_codegen/cpp_gen/
fill_table.rs

1// C++ RustealFillFuncTable.cpp generation.
2
3use std::collections::BTreeMap;
4
5use crate::context::FuncEntry;
6use crate::cpp_gen::wrapper::cpp_wrapper_name;
7
8/// Generate the RustealFillFuncTable.cpp file.
9pub fn generate_fill_table(
10    entries: &[FuncEntry],
11    _by_class: &BTreeMap<(String, String), Vec<&FuncEntry>>,
12) -> String {
13    let mut out = String::with_capacity(entries.len() * 80 + 1024);
14
15    out.push_str("// Auto-generated by rusteal-codegen. Do not edit.\n\n");
16    out.push_str("#include \"RustealFuncIds.h\"\n\n");
17
18    // Forward-declare all wrapper functions
19    out.push_str("// Forward declarations of wrapper functions\n");
20    for entry in entries {
21        let c_name = cpp_wrapper_name(&entry.class_name, &entry.func_name);
22        out.push_str(&format!("extern \"C\" uint32_t {c_name}(...);\n"));
23    }
24    out.push('\n');
25
26    // Global function table
27    out.push_str("static void* GRustealFuncTable[RustealFuncId::FUNC_COUNT];\n\n");
28
29    // Fill function
30    out.push_str("void RustealFillFuncTable() {\n");
31    for entry in entries {
32        let const_name =
33            crate::rust_gen::func_ids::func_id_const_name(&entry.class_name, &entry.func_name);
34        let c_name = cpp_wrapper_name(&entry.class_name, &entry.func_name);
35        out.push_str(&format!(
36            "    GRustealFuncTable[RustealFuncId::{const_name}] = (void*)&{c_name};\n"
37        ));
38    }
39    out.push_str("}\n\n");
40
41    // Getter for the table pointer
42    out.push_str("void** RustealGetFuncTable() {\n");
43    out.push_str("    return GRustealFuncTable;\n");
44    out.push_str("}\n\n");
45
46    out.push_str("uint32_t RustealGetFuncCount() {\n    return RustealFuncId::FUNC_COUNT;\n}\n");
47
48    out
49}