rust-bindgen
rust-bindgen copied to clipboard
Generate methods that invoke vtable pointers
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.
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.
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
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.
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.
Is this fixed via #2145?
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)
.