Skip to content

remove const qualifier if required #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/lib.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3196,10 +3196,18 @@ pub const Lua = opaque {
const string: [*:0]const u8 = try lua.toString(index);
const end = std.mem.indexOfSentinel(u8, 0, string);

if (info.sentinel == null) {
return string[0..end];
if (!info.is_const) {
if (!allow_alloc) {
@compileError("toAny cannot allocate memory, try using toAnyAlloc");
}

if (info.sentinel != null) {
return try a.?.dupeZ(u8, string[0..end]);
} else {
return try a.?.dupe(u8, string[0..end]);
}
} else {
return string[0..end :0];
return if (info.sentinel == null) string[0..end] else string[0..end :0];
}
} else switch (info.size) {
.Slice, .Many => {
Expand Down Expand Up @@ -3306,6 +3314,7 @@ pub const Lua = opaque {
var result: T = undefined;

inline for (@typeInfo(T).Struct.fields) |field| {
const field_type_info = comptime @typeInfo(field.type);
const field_name = comptime field.name ++ "";
_ = lua.pushStringZ(field_name);

Expand All @@ -3314,7 +3323,7 @@ pub const Lua = opaque {
if (lua_field_type == .nil) {
if (field.default_value) |default_value| {
@field(result, field.name) = @as(*const field.type, @ptrCast(@alignCast(default_value))).*;
} else {
} else if (field_type_info != .Optional) {
return error.LuaTableMissingValue;
}
} else {
Expand Down
40 changes: 40 additions & 0 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2446,6 +2446,46 @@ test "toAny struct" {
));
}

test "toAny mutable string" {
var lua = try Lua.init(&testing.allocator);
defer lua.deinit();

//[] u8
_ = lua.pushStringZ("hello world");
const parsed = try lua.toAnyAlloc([]u8, -1);
defer parsed.deinit();

const my_string = parsed.value;

try testing.expect(std.mem.eql(u8, my_string, "hello world"));
}

test "toAny mutable string in struct" {
var lua = try Lua.init(&testing.allocator);
defer lua.deinit();

const MyType = struct {
name: []u8,
sentinel: [:0]u8,
bar: bool,
};
try lua.doString("value = {[\"name\"] = \"hi\", [\"sentinel\"] = \"ss\", [\"bar\"] = false}");
const lua_type = try lua.getGlobal("value");
try testing.expect(lua_type == .table);
const parsed = try lua.toAnyAlloc(MyType, 1);
defer parsed.deinit();

const my_struct = parsed.value;

var name: [2]u8 = .{ 'h', 'i' };
var sentinel: [2:0]u8 = .{ 's', 's' };

try testing.expectEqualDeep(
MyType{ .name = &name, .sentinel = &sentinel, .bar = false },
my_struct,
);
}

test "toAny struct recursive" {
var lua = try Lua.init(&testing.allocator);
defer lua.deinit();
Expand Down
Loading