rust-bindgen icon indicating copy to clipboard operation
rust-bindgen copied to clipboard

Generate methods that invoke vtable pointers

Open jdm opened this issue 8 years ago • 5 comments

We generate glue for C++ classes with virtual tables (for example, CustomAutoRooter in rust-mozjs), but we don't generate methods that fetch the function pointer from the vtable and invoke it with the this pointer. That would be a significant improvement in usability.

jdm avatar Jul 28 '16 17:07 jdm

Just a quick update: The vtables before were absolutely wrong anyway (didn't take into account destructors, etc.).

Also, MSVC packs virtual method overrides differently from the rest of the compilers, so this will require target-specific code (which is not a huge deal though, we already do it).

In any case, this is still a major amount of work, though certainly useful. I'll consider implementing it if there's enough need for it, though the logic is surely complex.

emilio avatar Sep 22 '16 01:09 emilio

I was searching for information regarding this feature in bindgen. As I understand, it still has to be done as of may 2018. Is there any hope it will be considered at some point? Thanks

ggris avatar May 17 '18 08:05 ggris

It's not really trivial, someone needs to go ahead and implement it properly, and add a bunch of tests. I'd be happy to mentor, though I'm not sure if I'd have the time to do it myself.

emilio avatar May 17 '18 14:05 emilio

This is basically a matter of implementing this:

https://github.com/rust-lang/rust-bindgen/blob/91d9600c33260202f3b438f14ad077be3bdc81e7/src/codegen/mod.rs#L997-L1018

Which already has a reference to all methods and base classes. Making this work for simple casses (no base classes, no overloaded methods) should be trivial. From there we can improve.

emilio avatar Aug 29 '20 15:08 emilio

Is this fixed via #2145?

pvdrz avatar Sep 15 '22 22:09 pvdrz

Yes, use this to generate a basic vtable

    let bindings = bindgen::Builder::default()
        .header("wrapper.hpp")
        .clang_arg("-x")
        .clang_arg("c++")
        .clang_arg("-std=c++11")
        .derive_default(true)
        .derive_debug(true)
        .vtable_generation(true)
        .generate_comments(false)
        .layout_tests(false)
        .generate_comments(false)
        .derive_copy(true)
        .derive_hash(false)
        .parse_callbacks(Box::new(MyCallback {}))
        .generate()
        .expect("Unable to generate bindings");

With .vtable_generation(true).

linonetwo avatar Mar 14 '24 08:03 linonetwo