Skip to content

Commit 6515d4b

Browse files
committed
chore: update for Zig 0.10.0
In anticipation for the Zig release coming soon (and the new self-hosted compiler finally free of bugs that prevented this project from compiling), this updates the sources for each version of Lua to work in the new compiler. Mostly trivial changes. Note that at this point Lua 5.1 doesn't yet compile due to a bug that was just fixed in Zig, and will be addressed later on.
1 parent 0874099 commit 6515d4b

File tree

9 files changed

+146
-139
lines changed

9 files changed

+146
-139
lines changed

build.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn linkAndPackage(b: *Builder, step: *LibExeObjStep, options: Options) std.b
5454
const lib_path = libPath(options.version);
5555
return .{
5656
.name = "ziglua",
57-
.path = .{ .path = std.fs.path.join(b.allocator, &.{ dir(), lib_path }) catch unreachable },
57+
.source = .{ .path = std.fs.path.join(b.allocator, &.{ dir(), lib_path }) catch unreachable },
5858
};
5959
}
6060

@@ -84,9 +84,9 @@ fn buildLua(b: *Builder, step: *LibExeObjStep, options: Options) *LibExeObjStep
8484

8585
const apicheck = step.build_mode == .Debug and options.use_apicheck;
8686

87-
step.addIncludeDir(std.fs.path.join(b.allocator, &.{ dir(), lib_dir }) catch unreachable);
87+
step.addIncludePath(std.fs.path.join(b.allocator, &.{ dir(), lib_dir }) catch unreachable);
8888

89-
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, step.target) catch unreachable).target;
89+
const target = (std.zig.system.NativeTargetInfo.detect(step.target) catch unreachable).target;
9090

9191
const flags = [_][]const u8{
9292
// Standard version used in Lua Makefile

src/ziglua-5.1/lib.zig

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3131
const 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
115115
pub const HookMask = packed struct {
@@ -199,7 +199,7 @@ pub const mult_return = c.LUA_MULTRET;
199199
pub 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`
205205
pub const ResumeStatus = enum(u1) {
@@ -251,7 +251,7 @@ pub const err_file = c.LUA_ERRFILE;
251251
pub 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;

src/ziglua-5.1/tests.zig

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ const sub = struct {
4242

4343
fn alloc(data: ?*anyopaque, ptr: ?*anyopaque, osize: usize, nsize: usize) callconv(.C) ?*anyopaque {
4444
_ = data;
45-
_ = osize;
4645

4746
const alignment = @alignOf(std.c.max_align_t);
4847
if (@ptrCast(?[*]align(alignment) u8, @alignCast(alignment, ptr))) |prev_ptr| {
@@ -53,6 +52,8 @@ fn alloc(data: ?*anyopaque, ptr: ?*anyopaque, osize: usize, nsize: usize) callco
5352
}
5453
const new_ptr = testing.allocator.reallocAdvanced(prev_slice, alignment, nsize, .exact) catch return null;
5554
return new_ptr.ptr;
55+
} else if (nsize == 0) {
56+
return null;
5657
} else {
5758
const new_ptr = testing.allocator.alignedAlloc(u8, alignment, nsize) catch return null;
5859
return new_ptr.ptr;
@@ -1040,7 +1041,7 @@ test "userdata" {
10401041
defer lua.deinit();
10411042

10421043
const Type = struct { a: i32, b: f32 };
1043-
try lua.newMetatable("Type");
1044+
try lua.newMetatable(@typeName(Type));
10441045

10451046
const checkUdata = ziglua.wrap(struct {
10461047
fn inner(l: *Lua) i32 {
@@ -1061,7 +1062,7 @@ test "userdata" {
10611062

10621063
{
10631064
var t = lua.newUserdata(Type);
1064-
lua.getField(ziglua.registry_index, "Type");
1065+
lua.getField(ziglua.registry_index, @typeName(Type));
10651066
lua.setMetatable(-2);
10661067
t.a = 1234;
10671068
t.b = 3.14;

0 commit comments

Comments
 (0)