@@ -21,11 +21,11 @@ const Allocator = std.mem.Allocator;
2121/// `osize` is the original size or a code, and `nsize` is the new size
2222///
2323/// See https://www.lua.org/manual/5.4/manual.html#lua_Alloc for more details
24- pub const AllocFn = fn (data : ? * anyopaque , ptr : ? * anyopaque , osize : usize , nsize : usize ) callconv (.C ) ? * anyopaque ;
24+ pub const AllocFn = * const fn (data : ? * anyopaque , ptr : ? * anyopaque , osize : usize , nsize : usize ) callconv (.C ) ? * anyopaque ;
2525
2626/// Type for C functions
2727/// See https://www.lua.org/manual/5.4/manual.html#lua_CFunction for the protocol
28- pub const CFn = fn (state : ? * LuaState ) callconv (.C ) c_int ;
28+ pub const CFn = * const fn (state : ? * LuaState ) callconv (.C ) c_int ;
2929
3030/// The internal Lua debug structure
3131const Debug = c .lua_Debug ;
@@ -109,7 +109,7 @@ pub const FnReg = struct {
109109};
110110
111111/// Type for debugging hook functions
112- pub const CHookFn = fn (state : ? * LuaState , ar : ? * Debug ) callconv (.C ) void ;
112+ pub const CHookFn = * const fn (state : ? * LuaState , ar : ? * Debug ) callconv (.C ) void ;
113113
114114/// Specifies on which events the hook will be called
115115pub const HookMask = packed struct {
@@ -199,7 +199,7 @@ pub const mult_return = c.LUA_MULTRET;
199199pub const Number = c .lua_Number ;
200200
201201/// The type of the reader function used by `Lua.load()`
202- pub const CReaderFn = fn (state : ? * LuaState , data : ? * anyopaque , size : [* c ]usize ) callconv (.C ) [* c ]const u8 ;
202+ pub const CReaderFn = * const fn (state : ? * LuaState , data : ? * anyopaque , size : [* c ]usize ) callconv (.C ) [* c ]const u8 ;
203203
204204/// The possible status of a call to `Lua.resumeThread`
205205pub const ResumeStatus = enum (u1 ) {
@@ -251,7 +251,7 @@ pub const err_file = c.LUA_ERRFILE;
251251pub const Stream = c .luaL_Stream ;
252252
253253/// The type of the writer function used by `Lua.dump()`
254- pub const CWriterFn = fn (state : ? * LuaState , buf : ? * const anyopaque , size : usize , data : ? * anyopaque ) callconv (.C ) c_int ;
254+ pub const CWriterFn = * const fn (state : ? * LuaState , buf : ? * const anyopaque , size : usize , data : ? * anyopaque ) callconv (.C ) c_int ;
255255
256256/// A Zig wrapper around the Lua C API
257257/// Represents a Lua state or thread and contains the entire state of the Lua interpreter
@@ -262,8 +262,6 @@ pub const Lua = struct {
262262 /// Allows Lua to allocate memory using a Zig allocator passed in via data.
263263 /// See https://www.lua.org/manual/5.4/manual.html#lua_Alloc for more details
264264 fn alloc (data : ? * anyopaque , ptr : ? * anyopaque , osize : usize , nsize : usize ) callconv (.C ) ? * anyopaque {
265- _ = osize ; // unused
266-
267265 // just like malloc() returns a pointer "which is suitably aligned for any built-in type",
268266 // the memory allocated by this function should also be aligned for any type that Lua may
269267 // desire to allocate. use the largest alignment for the target
@@ -282,6 +280,8 @@ pub const Lua = struct {
282280 // when nsize is not zero the allocator must behave like realloc
283281 const new_ptr = allocator .reallocAdvanced (prev_slice , alignment , nsize , .exact ) catch return null ;
284282 return new_ptr .ptr ;
283+ } else if (nsize == 0 ) {
284+ return null ;
285285 } else {
286286 // ptr is null, allocate a new block of memory
287287 const new_ptr = allocator .alignedAlloc (u8 , alignment , nsize ) catch return null ;
0 commit comments