@@ -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 }
0 commit comments