From 0e705a4e6ed5df9a2f8465f55f5a623ded6bfe8c Mon Sep 17 00:00:00 2001 From: mappu Date: Sun, 13 Oct 2024 18:31:18 +1300 Subject: [PATCH] handbindings: avoid unsafe.Pointer when marshalling cgo.Handle --- cmd/handbindings/binding.cpp | 4 ++-- cmd/handbindings/binding.go | 6 +++--- cmd/handbindings/binding.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/cmd/handbindings/binding.cpp b/cmd/handbindings/binding.cpp index 0606778..549e1c0 100644 --- a/cmd/handbindings/binding.cpp +++ b/cmd/handbindings/binding.cpp @@ -4,7 +4,7 @@ #include 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(self)->show(); } -void QPushButton_connect_pressed(PQPushButton self, void* cb) { +void QPushButton_connect_pressed(PQPushButton self, intptr_t cb) { QPushButton::connect(static_cast(self), &QPushButton::pressed, [=]() { miqt_exec_callback(cb, 0, nullptr); }); diff --git a/cmd/handbindings/binding.go b/cmd/handbindings/binding.go index 64e2a77..16d05e4 100644 --- a/cmd/handbindings/binding.go +++ b/cmd/handbindings/binding.go @@ -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(), ... } diff --git a/cmd/handbindings/binding.h b/cmd/handbindings/binding.h index e45a089..28d75d2 100644 --- a/cmd/handbindings/binding.h +++ b/cmd/handbindings/binding.h @@ -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);