Skip to content

Commit ab019ed

Browse files
committed
Don't require to have at least 1 pub fn when registering userdata
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
1 parent a3b5ab2 commit ab019ed

File tree

2 files changed

+6
-38
lines changed

2 files changed

+6
-38
lines changed

src/lua.zig

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,27 +1124,8 @@ pub const Lua = struct {
11241124

11251125
const type_name: [:0]const u8 = @typeName(T);
11261126

1127-
// Count methods at compile time for validation
1128-
const struct_info = type_info.@"struct";
1129-
comptime var method_count = 0;
1130-
inline for (struct_info.decls) |decl| {
1131-
if (@hasDecl(T, decl.name)) {
1132-
const decl_info = @typeInfo(@TypeOf(@field(T, decl.name)));
1133-
if (decl_info == .@"fn" and
1134-
!comptime std.mem.eql(u8, decl.name, "deinit"))
1135-
{
1136-
method_count += 1;
1137-
}
1138-
}
1139-
}
1140-
1141-
// Ensure the type has at least one method to register
1142-
if (comptime method_count == 0) {
1143-
@compileError("Type " ++ @typeName(T) ++ " has no public methods to register as userdata");
1144-
}
1145-
11461127
// Use the userdata module's createMetaTable function to handle all registration
1147-
_ = userdata.createMetaTable(T, @constCast(&self.state), type_name);
1128+
userdata.createMetaTable(T, @constCast(&self.state), type_name);
11481129

11491130
// Extract just the type name without module prefix for global registration
11501131
// Example: "myapp.data.User" -> "User", "TestUserData" -> "TestUserData"

src/userdata.zig

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -358,20 +358,15 @@ fn createUserDataFunc(comptime T: type, comptime method_name: []const u8, method
358358

359359
/// Creates and registers a complete metatable for userdata type T.
360360
///
361-
/// This is the main public API for userdata registration. It counts methods,
362-
/// creates the metatable, registers all methods (init, instance, static),
363-
/// and registers metamethods. Returns the number of methods registered.
361+
/// This is the main public API for userdata registration. It creates the metatable,
362+
/// registers all methods (init, instance, static), and registers metamethods.
364363
///
365364
/// The type_name will be used for Lua userdata type checking and must be
366365
/// a compile-time string literal ending with null terminator.
367-
pub fn createMetaTable(comptime T: type, lua_state: *State, comptime type_name: [:0]const u8) u32 {
366+
pub fn createMetaTable(comptime T: type, lua_state: *State, comptime type_name: [:0]const u8) void {
368367
const struct_info = @typeInfo(T).@"struct";
369368

370-
// Detect what kind of methods we have in this struct
371-
// We're interested in:
372-
// - How many public methods (not deinit, not metamethods)
373-
// - Whether __index metamethod is going to be defined by the user
374-
var method_count: u32 = 0;
369+
// Check if user defines __index metamethod
375370
comptime var has_user_index = false;
376371
inline for (struct_info.decls) |decl| {
377372
if (@hasDecl(T, decl.name)) {
@@ -380,13 +375,7 @@ pub fn createMetaTable(comptime T: type, lua_state: *State, comptime type_name:
380375
// Check for __index metamethod
381376
if (comptime std.mem.eql(u8, decl.name, "__index")) {
382377
has_user_index = true;
383-
}
384-
385-
// Count regular methods (not deinit, not metamethods)
386-
if (!comptime std.mem.eql(u8, decl.name, "deinit") and
387-
MetaMethod.fromStr(decl.name) == null)
388-
{
389-
method_count += 1;
378+
break;
390379
}
391380
}
392381
}
@@ -441,6 +430,4 @@ pub fn createMetaTable(comptime T: type, lua_state: *State, comptime type_name:
441430

442431
// Store the metatable in the registry for later use
443432
lua_state.setField(State.REGISTRYINDEX, type_name);
444-
445-
return method_count;
446433
}

0 commit comments

Comments
 (0)