fix: resolve CGo type conflict in error handler

Use pure C callback instead of //export to avoid const char* vs
GoString type mismatch in cgo-generated headers.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Claude 2026-02-16 01:53:36 +00:00 committed by Snider
parent a0435a84ea
commit 828a5c0853

View file

@ -25,10 +25,18 @@ package mlx
#include <stdlib.h> #include <stdlib.h>
#include "mlx/c/mlx.h" #include "mlx/c/mlx.h"
extern void goMLXErrorHandler(const char *msg, void *data); static const char *last_mlx_error = NULL;
static void mlx_go_error_handler(const char *msg, void *data) {
last_mlx_error = msg;
}
static void set_error_handler() { static void set_error_handler() {
mlx_set_error_handler(&goMLXErrorHandler, NULL, NULL); mlx_set_error_handler(&mlx_go_error_handler, NULL, NULL);
}
static const char* get_last_error() {
return last_mlx_error;
} }
*/ */
import "C" import "C"
@ -36,7 +44,6 @@ import "C"
import ( import (
"log/slog" "log/slog"
"sync" "sync"
"unsafe"
) )
var initOnce sync.Once var initOnce sync.Once
@ -49,9 +56,11 @@ func Init() {
}) })
} }
//export goMLXErrorHandler // checkError logs the last MLX error if any occurred.
func goMLXErrorHandler(msg *C.char, data unsafe.Pointer) { func checkError() {
slog.Error("mlx", "error", C.GoString(msg)) if msg := C.get_last_error(); msg != nil {
slog.Error("mlx", "error", C.GoString(msg))
}
} }
// Materialize synchronously evaluates arrays, computing their values on the GPU. // Materialize synchronously evaluates arrays, computing their values on the GPU.