@@ -860,6 +860,45 @@ pub const Lua = struct {
860860 self .state ().pop (1 ); // Pop table
861861 }
862862
863+ /// Creates a shallow copy of the table.
864+ ///
865+ /// Returns a new table with the same key-value pairs as the original.
866+ /// This is a shallow copy: primitive values (numbers, strings, booleans) are
867+ /// duplicated, but reference types (tables, functions, userdata) are shared
868+ /// between the original and cloned table.
869+ ///
870+ /// The clone inherits the original table's metatable reference (not cloned).
871+ /// The readonly and safeenv flags are not copied to the clone.
872+ ///
873+ /// Examples:
874+ /// ```zig
875+ /// const original = lua.createTable(.{});
876+ /// defer original.deinit();
877+ /// try original.set("name", "Alice");
878+ ///
879+ /// const cloned = try original.clone();
880+ /// defer cloned.deinit();
881+ ///
882+ /// // Clone has same values
883+ /// const name = try cloned.get("name", []const u8);
884+ /// try expect(std.mem.eql(u8, name.?, "Alice"));
885+ /// ```
886+ ///
887+ /// Returns: `Table` - A new table containing a shallow copy of the original
888+ /// Errors: `Error.OutOfMemory` if allocation fails
889+ pub fn clone (self : Table ) ! Table {
890+ try self .ref .lua .checkStack (2 );
891+
892+ stack .push (self .ref .lua , self .ref ); // Push table ref
893+ self .state ().cloneTable (-1 ); // Clone table, pushes clone on stack
894+
895+ // Create a reference to the cloned table on the stack
896+ const cloned_ref = Ref .init (self .ref .lua , -1 );
897+ self .state ().pop (2 ); // Pop both original and cloned tables
898+
899+ return Table { .ref = cloned_ref };
900+ }
901+
863902 /// Entry representing a key-value pair from table iteration.
864903 /// Resources are automatically managed when using the `Iterator` type.
865904 pub const Entry = struct {
0 commit comments