handbindings: avoid unsafe.Pointer when marshalling cgo.Handle

This commit is contained in:
mappu 2024-10-13 18:31:18 +13:00
parent 204f886760
commit 0e705a4e6e
3 changed files with 6 additions and 6 deletions

View File

@ -4,7 +4,7 @@
#include <QPushButton>
extern "C" {
extern void miqt_exec_callback(void* cb, int argc, void* argv);
extern void miqt_exec_callback(intptr_t cb, int argc, void* argv);
}
PQApplication QApplication_new(int* argc, char** argv) {
@ -28,7 +28,7 @@ void QPushButton_show(PQPushButton self) {
static_cast<QPushButton*>(self)->show();
}
void QPushButton_connect_pressed(PQPushButton self, void* cb) {
void QPushButton_connect_pressed(PQPushButton self, intptr_t cb) {
QPushButton::connect(static_cast<QPushButton*>(self), &QPushButton::pressed, [=]() {
miqt_exec_callback(cb, 0, nullptr);
});

View File

@ -28,11 +28,11 @@ func CArray(data []string) (C.int, **C.char) {
type CallbackFunc func(argc C.int, args *C.void)
//export miqt_exec_callback
func miqt_exec_callback(cb *C.void, argc C.int, args *C.void) {
func miqt_exec_callback(cb C.intptr_t, argc C.int, args *C.void) {
// Our CABI for all callbacks is void(int, void*).
// Our Go ABI is CallbackFunc
// Then the Go bindings can unmarshal the arguments and C.free() them as necessary
cfunc, ok := (cgo.Handle(uintptr(unsafe.Pointer(cb))).Value()).(CallbackFunc)
cfunc, ok := (cgo.Handle(cb)).Value().(CallbackFunc)
if !ok {
panic("miqt: callback of non-callback type (heap corruption?)")
}
@ -105,6 +105,6 @@ func (this *QPushButton) OnPressed(cb func()) {
cb()
}
C.QPushButton_connect_pressed(this.h, unsafe.Pointer(uintptr(cgo.NewHandle(cbWrapper))))
C.QPushButton_connect_pressed(this.h, C.intptr_t(cgo.NewHandle(cbWrapper)))
// TODO allow disconnect'ing, or tie lifespan for handle.Delete(), ...
}

View File

@ -23,7 +23,7 @@ PQPushButton QPushButton_new(const char* label, PQWidget parent);
void QPushButton_show(PQPushButton self);
void QPushButton_connect_pressed(PQPushButton self, void* cb);
void QPushButton_connect_pressed(PQPushButton self, intptr_t cb);
int QApplication_exec(PQApplication self);