Skip to content

Commit 1c2fb9a

Browse files
axdankrobbielyman
authored andcommitted
changes ziglua_{toAny, pushAny} to fromLua and toLua
1 parent 2864477 commit 1c2fb9a

File tree

2 files changed

+126
-15
lines changed

2 files changed

+126
-15
lines changed

src/lib.zig

Lines changed: 121 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4400,6 +4400,70 @@ pub const Lua = opaque {
44004400
}
44014401
}
44024402

4403+
if (type_info == .@"struct" or type_info == .@"union" or type_info == .@"enum") {
4404+
if (@hasDecl(T, "toLua")) {
4405+
const toLuaArgs = .{ value, lua };
4406+
const fnSignature = comptime fn_sign: {
4407+
var b: []const u8 = "pub fn toLua(";
4408+
4409+
for (0..toLuaArgs.len) |i| {
4410+
b = b ++ std.fmt.comptimePrint("{s}{s}", .{ @typeName(@TypeOf(toLuaArgs[i])), if (i == (toLuaArgs.len - 1)) "" else ", " });
4411+
}
4412+
4413+
b = b ++ ") !void";
4414+
4415+
break :fn_sign b;
4416+
};
4417+
4418+
const fl = @field(T, "toLua");
4419+
const flt = @TypeOf(fl);
4420+
const fli = @typeInfo(flt);
4421+
switch (fli) {
4422+
.@"fn" => |f| {
4423+
const args_ok = comptime args_ok: {
4424+
const f_params = f.params;
4425+
4426+
if (f_params.len != toLuaArgs.len) break :args_ok false;
4427+
4428+
for (0..toLuaArgs.len) |i| {
4429+
if (f_params[i].type != @TypeOf(toLuaArgs[i])) break :args_ok false;
4430+
}
4431+
4432+
break :args_ok true;
4433+
};
4434+
4435+
if (args_ok) {
4436+
if (f.return_type) |rt| {
4437+
const rti = @typeInfo(rt);
4438+
switch (rti) {
4439+
.error_union => {
4440+
if (rti.error_union.payload == void) {
4441+
try @call(.auto, fl, toLuaArgs);
4442+
} else {
4443+
@compileError("toLua invalid return type, required fn signature: " ++ fnSignature);
4444+
}
4445+
},
4446+
.void => {
4447+
@call(.auto, fl, toLuaArgs);
4448+
},
4449+
else => {
4450+
@compileError("toLua invalid return type, required fn signature: " ++ fnSignature);
4451+
},
4452+
}
4453+
} else {
4454+
@call(.auto, fl, toLuaArgs);
4455+
}
4456+
} else {
4457+
@compileError("toLua has invalid args, required fn signature: " ++ fnSignature);
4458+
}
4459+
},
4460+
else => {
4461+
@compileError("toLua is not a function, required fn signature: " ++ fnSignature);
4462+
},
4463+
}
4464+
}
4465+
}
4466+
44034467
switch (type_info) {
44044468
.int, .comptime_int => {
44054469
lua.pushInteger(@intCast(value));
@@ -4536,18 +4600,66 @@ pub const Lua = opaque {
45364600
const type_info = @typeInfo(T);
45374601

45384602
if (type_info == .@"struct" or type_info == .@"union" or type_info == .@"enum") {
4539-
if (@hasDecl(T, "ziglua_toAny")) {
4540-
const fnInfo = @typeInfo(@TypeOf(T.ziglua_toAny)).@"fn";
4541-
switch (fnInfo.params.len) {
4542-
// fn(lua_state, alloc, allow_alloc, index) -> T
4543-
4 => {
4544-
if (@typeInfo(fnInfo.return_type.?) == .error_union) {
4545-
return try T.ziglua_toAny(lua, a, allow_alloc, index);
4603+
if (@hasDecl(T, "fromLua")) {
4604+
const fromLuaArgs = .{ lua, a, index };
4605+
const fnSignature = comptime fn_sign: {
4606+
var b: []const u8 = "pub fn fromLua(";
4607+
4608+
for (0..fromLuaArgs.len) |i| {
4609+
b = b ++ std.fmt.comptimePrint("{s}{s}", .{ @typeName(@TypeOf(fromLuaArgs[i])), if (i == (fromLuaArgs.len - 1)) "" else ", " });
4610+
}
4611+
4612+
b = b ++ ") !" ++ @typeName(T);
4613+
4614+
break :fn_sign b;
4615+
};
4616+
4617+
const fl = @field(T, "fromLua");
4618+
const flt = @TypeOf(fl);
4619+
const fli = @typeInfo(flt);
4620+
switch (fli) {
4621+
.@"fn" => |f| {
4622+
const args_ok = comptime args_ok: {
4623+
const f_params = f.params;
4624+
4625+
if (f_params.len != fromLuaArgs.len) break :args_ok false;
4626+
4627+
for (0..fromLuaArgs.len) |i| {
4628+
if (f_params[i].type != @TypeOf(fromLuaArgs[i])) break :args_ok false;
4629+
}
4630+
4631+
break :args_ok true;
4632+
};
4633+
4634+
if (args_ok) {
4635+
if (f.return_type) |rt| {
4636+
if (rt == T) {
4637+
return @call(.auto, fl, fromLuaArgs);
4638+
} else {
4639+
const rti = @typeInfo(rt);
4640+
switch (rti) {
4641+
.error_union => {
4642+
if (rti.error_union.payload == T) {
4643+
return try @call(.auto, fl, fromLuaArgs);
4644+
} else {
4645+
@compileError("fromLua invalid return type, required fn signature: " ++ fnSignature);
4646+
}
4647+
},
4648+
else => {
4649+
@compileError("fromLua invalid return type, required fn signature: " ++ fnSignature);
4650+
},
4651+
}
4652+
}
4653+
} else {
4654+
@compileError("fromLua require a fn signature: " ++ fnSignature);
4655+
}
45464656
} else {
4547-
return T.ziglua_toAny(lua, a, allow_alloc, index);
4657+
@compileError("fromLua has invalid args, required fn signature: " ++ fnSignature);
45484658
}
45494659
},
4550-
else => @compileError(@typeName(T) ++ ".ziglua_toAny has invalid signature, required: fn(lua: *Lua, alloc: ?std.mem.Allocator, comptime allow_alloc: bool, index: i32) T"),
4660+
else => {
4661+
@compileError("fromLua is not a function, required fn signature: " ++ fnSignature);
4662+
},
45514663
}
45524664
}
45534665
}

src/tests.zig

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2432,7 +2432,7 @@ test "toAny tuple from struct" {
24322432
));
24332433
}
24342434

2435-
test "toAny from struct with custom toAny" {
2435+
test "toAny from struct with fromLua" {
24362436
var lua = try Lua.init(testing.allocator);
24372437
defer lua.deinit();
24382438

@@ -2442,8 +2442,8 @@ test "toAny from struct with custom toAny" {
24422442
const Self = @This();
24432443
foo: i32,
24442444

2445-
pub fn ziglua_toAny(l: *Lua, a: ?std.mem.Allocator, comptime aa: bool, i: i32) !Self {
2446-
return try ziglua.Internals.toStruct(l, Self, a, aa, i);
2445+
pub fn fromLua(l: *Lua, a: ?std.mem.Allocator, i: i32) !Self {
2446+
return try ziglua.Internals.toStruct(l, Self, a, false, i);
24472447
}
24482448
},
24492449
};
@@ -2460,7 +2460,6 @@ test "toAny from struct with custom toAny" {
24602460
const lua_type = try lua.getGlobal("value");
24612461
try testing.expect(lua_type == .table);
24622462
const my_struct = try lua.toAny(MyType, 1);
2463-
lua.pop(-1);
24642463
try testing.expect(std.meta.eql(
24652464
my_struct,
24662465
MyType{ .foo = true, .bar = .{ .foo = 12 } },
@@ -2689,7 +2688,7 @@ test "pushAny tuple" {
26892688
try testing.expect(std.meta.eql(result, .{ 500, false, 600 }));
26902689
}
26912690

2692-
test "pushAny from struct with custom pushAny" {
2691+
test "pushAny from struct with toLua" {
26932692
var lua = try Lua.init(testing.allocator);
26942693
defer lua.deinit();
26952694

@@ -2698,7 +2697,7 @@ test "pushAny from struct with custom pushAny" {
26982697
foo: i32,
26992698
tuple: std.meta.Tuple(&.{ i32, i32 }),
27002699

2701-
pub fn ziglua_pushAny(self: *const Self, l: *Lua) !void {
2700+
pub fn toLua(self: Self, l: *Lua) void {
27022701
l.newTable();
27032702

27042703
inline for (@typeInfo(Self).@"struct".fields) |f| {

0 commit comments

Comments
 (0)