Skip to content

Commit a3ad5d2

Browse files
committed
Fix callback clearing in setCallbacks
Clear unused callback pointers to null when setting new callbacks to prevent calling stale function pointers from previously registered callback structs.
1 parent ab3997e commit a3ad5d2

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Lua.zig

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ pub fn enable_codegen(self: Self) bool {
244244
/// Only methods that exist on the callbacks object will be set. Missing methods are ignored.
245245
///
246246
/// Note: Callbacks are set globally on the Lua state and remain active until the state is destroyed
247-
/// or new callbacks are set.
247+
/// or new callbacks are set. When setting new callbacks, any callback methods not present in the
248+
/// new callback struct will be cleared (set to null) to prevent calling stale function pointers.
248249
///
249250
/// Supports two modes:
250251
/// 1. Static methods: Pass a struct instance - methods are called as static functions.
@@ -315,6 +316,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
315316
}
316317
}
317318
}.wrapper;
319+
} else {
320+
cb.interrupt = null;
318321
}
319322

320323
if (@hasDecl(CallbackType, "panic")) {
@@ -331,6 +334,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
331334
}
332335
}
333336
}.wrapper;
337+
} else {
338+
cb.panic = null;
334339
}
335340

336341
if (@hasDecl(CallbackType, "userthread")) {
@@ -348,6 +353,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
348353
}
349354
}
350355
}.wrapper;
356+
} else {
357+
cb.userthread = null;
351358
}
352359

353360
if (@hasDecl(CallbackType, "useratom")) {
@@ -364,6 +371,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
364371
}
365372
}
366373
}.wrapper;
374+
} else {
375+
cb.useratom = null;
367376
}
368377

369378
if (@hasDecl(CallbackType, "debugbreak")) {
@@ -382,6 +391,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
382391
}
383392
}
384393
}.wrapper;
394+
} else {
395+
cb.debugbreak = null;
385396
}
386397

387398
if (@hasDecl(CallbackType, "debugstep")) {
@@ -400,6 +411,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
400411
}
401412
}
402413
}.wrapper;
414+
} else {
415+
cb.debugstep = null;
403416
}
404417

405418
if (@hasDecl(CallbackType, "debuginterrupt")) {
@@ -418,6 +431,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
418431
}
419432
}
420433
}.wrapper;
434+
} else {
435+
cb.debuginterrupt = null;
421436
}
422437

423438
if (@hasDecl(CallbackType, "debugprotectederror")) {
@@ -435,6 +450,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
435450
}
436451
}
437452
}.wrapper;
453+
} else {
454+
cb.debugprotectederror = null;
438455
}
439456

440457
if (@hasDecl(CallbackType, "onallocate")) {
@@ -453,6 +470,8 @@ pub fn setCallbacks(self: Self, callbacks: anytype) void {
453470
}
454471
}
455472
}.wrapper;
473+
} else {
474+
cb.onallocate = null;
456475
}
457476
}
458477

0 commit comments

Comments
 (0)