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 5e2d941b4d
commit a0f77960a1

View file

@ -25,10 +25,18 @@ package mlx
#include <stdlib.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() {
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"
@ -36,7 +44,6 @@ import "C"
import (
"log/slog"
"sync"
"unsafe"
)
var initOnce sync.Once
@ -49,9 +56,11 @@ func Init() {
})
}
//export goMLXErrorHandler
func goMLXErrorHandler(msg *C.char, data unsafe.Pointer) {
// checkError logs the last MLX error if any occurred.
func checkError() {
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.