Simplify item vtable retrieval in C++

Use a macro instead of a template function to make the getter vs. global
symbol choice.
This commit is contained in:
Simon Hausmann 2021-05-11 07:55:51 +02:00
parent 28701d2f90
commit c6a4841cd4
2 changed files with 8 additions and 18 deletions

View file

@ -204,23 +204,16 @@ inline vtable::Layout drop_in_place(ComponentRef component)
return vtable::Layout { sizeof(T), alignof(T) }; return vtable::Layout { sizeof(T), alignof(T) };
} }
template<typename VTableType>
constexpr inline const VTableType *get_vtable(const VTableType *vtable_symbol,
const VTableType *(*getter)())
{
// On Windows cross-dll data relocations are not supported:
// https://docs.microsoft.com/en-us/cpp/c-language/rules-and-limitations-for-dllimport-dllexport?view=msvc-160
// so we have a relocation to a function that returns the address we seek. That
// relocation will be resolved to the locally linked stub library, the implementation of
// which will be patched.
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
(void)vtable_symbol; // On Windows cross-dll data relocations are not supported:
return getter(); // https://docs.microsoft.com/en-us/cpp/c-language/rules-and-limitations-for-dllimport-dllexport?view=msvc-160
// so we have a relocation to a function that returns the address we seek. That
// relocation will be resolved to the locally linked stub library, the implementation of
// which will be patched.
# define SIXTYFPS_GET_ITEM_VTABLE(VTableName) sixtyfps::private_api::sixtyfps_get_##VTableName()
#else #else
(void)getter; # define SIXTYFPS_GET_ITEM_VTABLE(VTableName) (&sixtyfps::private_api::VTableName)
return vtable_symbol;
#endif #endif
}
template<typename T> template<typename T>
struct ReturnWrapper struct ReturnWrapper

View file

@ -501,10 +501,7 @@ pub struct NativeClass {
impl NativeClass { impl NativeClass {
pub fn new(class_name: &str) -> Self { pub fn new(class_name: &str) -> Self {
let cpp_vtable_getter = format!( let cpp_vtable_getter = format!("SIXTYFPS_GET_ITEM_VTABLE({}VTable)", class_name);
"sixtyfps::private_api::get_vtable(&sixtyfps::private_api::{}VTable, &sixtyfps::private_api::sixtyfps_get_{}VTable)",
class_name, class_name
);
Self { Self {
class_name: class_name.into(), class_name: class_name.into(),
cpp_vtable_getter, cpp_vtable_getter,