Skip to content

Commit

Permalink
update to master 0.11.0-dev.3731+c6e2e1ae4 (ranciere#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane authored Jun 21, 2023
1 parent f669841 commit 881a879
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 32 deletions.
42 changes: 23 additions & 19 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const std = @import("std");

pub fn build(b: *std.build.Builder) void {
pub fn build(b: *std.Build) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
Expand All @@ -9,15 +9,19 @@ pub fn build(b: *std.build.Builder) void {

// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const optimize = b.standardOptimizeOption(.{});

const exe = b.addExecutable("zoltan", "src/lua.zig");
addLuaLibrary(exe, "");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const exe = b.addExecutable(.{
.name = "zoltan",
.root_source_file = .{ .path = "src/lua.zig" },
.target = target,
.optimize = optimize,
});
addLuaLibrary(b, exe, "");

const run_cmd = exe.run();
b.installArtifact(exe);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
Expand All @@ -26,21 +30,21 @@ pub fn build(b: *std.build.Builder) void {
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);

const exe_tests = b.addTest("src/tests.zig");
// Lua
addLuaLibrary(exe_tests, "" );

//
exe_tests.setBuildMode(mode);
const exe_tests = b.addTest(.{
.root_source_file = .{ .path = "src/tests.zig" },
.target = target,
.optimize = optimize,
});
// Lua
addLuaLibrary(b, exe_tests, "");

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}

pub fn addLuaLibrary(exe: *std.build.LibExeObjStep, installPath: [] const u8) void {
var buf: [1024]u8 = undefined;
pub fn addLuaLibrary(b: *std.Build, exe: *std.Build.Step.Compile, installPath: []const u8) void {
// Lua headers + required source files
var path = std.fmt.bufPrint(buf[0..], "{s}{s}", .{ installPath, "src/lua-5.4.3/src"}) catch unreachable;
var path = b.fmt("{s}{s}", .{ installPath, "src/lua-5.4.3/src" });

exe.addIncludePath(path);
// C compile flags
Expand All @@ -49,13 +53,13 @@ pub fn addLuaLibrary(exe: *std.build.LibExeObjStep, installPath: [] const u8) vo
"-O2",
};
for (luaFiles) |luaFile| {
var cPath = std.fmt.bufPrint(buf[0..], "{s}{s}", .{ installPath, luaFile}) catch unreachable;
var cPath = b.fmt("{s}{s}", .{ installPath, luaFile });
exe.addCSourceFile(cPath, &flags);
}
exe.linkLibC();
}

const luaFiles = [_] []const u8{
const luaFiles = [_][]const u8{
"src/lua-5.4.3/src/lapi.c",
"src/lua-5.4.3/src/lauxlib.c",
"src/lua-5.4.3/src/lbaselib.c",
Expand Down
17 changes: 7 additions & 10 deletions src/lua.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,21 @@ pub const Lua = struct {
registeredTypes: std.StringArrayHashMap([]const u8) = undefined,

fn init(_allocator: std.mem.Allocator) LuaUserData {
return LuaUserData {
.allocator = _allocator,
.registeredTypes = std.StringArrayHashMap([]const u8).init(_allocator)
};
return LuaUserData{ .allocator = _allocator, .registeredTypes = std.StringArrayHashMap([]const u8).init(_allocator) };
}

fn destroy(self: *LuaUserData) void {
self.registeredTypes.clearAndFree();
}
};

L: *lualib.lua_State,
ud: *LuaUserData,

pub fn init(allocator: std.mem.Allocator) !Lua {
var _ud = try allocator.create(LuaUserData);
_ud.* = LuaUserData.init(allocator);

var _state = lualib.lua_newstate(alloc, _ud) orelse return error.OutOfMemory;
var state = Lua{
.L = _state,
Expand Down Expand Up @@ -378,7 +375,7 @@ pub const Lua = struct {
fn pushSlice(comptime T: type, L: *lualib.lua_State, values: []const T) void {
lualib.lua_createtable(L, @intCast(c_int, values.len), 0);

for (values) |value, i| {
for (values, 0..) |value, i| {
push(L, i + 1);
push(L, value);
lualib.lua_settable(L, -3);
Expand Down Expand Up @@ -646,7 +643,7 @@ pub const Lua = struct {

fn destroyArgs(self: *Self, L: ?*lualib.lua_State) !void {
if (self.args.len <= 0) return;
comptime var i:i32 = self.args.len - 1;
comptime var i: i32 = self.args.len - 1;
inline while (i > -1) : (i -= 1) {
_ = allocateDeallocateHelper(@TypeOf(self.args[i]), true, getAllocator(L), self.args[i]);
}
Expand Down Expand Up @@ -679,8 +676,8 @@ pub const Lua = struct {
}

fn getUserData(L: ?*lualib.lua_State) *Lua.LuaUserData {
var ud : *anyopaque = undefined;
_ = lualib.lua_getallocf (L, @ptrCast([*c]?*anyopaque, &ud));
var ud: *anyopaque = undefined;
_ = lualib.lua_getallocf(L, @ptrCast([*c]?*anyopaque, &ud));
const userData = @ptrCast(*Lua.LuaUserData, @alignCast(@alignOf(Lua.LuaUserData), ud));
return userData;
}
Expand Down
6 changes: 3 additions & 3 deletions src/tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ test "set/get slice of primitive type (scalar, unmutable string)" {
const retStrSlice = try lua.getResource([][]const u8, "strSlice");
defer lua.release(retStrSlice);

for (retIntSlice) |v, i| {
for (retIntSlice, 0..) |v, i| {
try std.testing.expectEqual(v, intSlice[i]);
}

for (retStrSlice) |v, i| {
for (retStrSlice, 0..) |v, i| {
try std.testing.expect(std.mem.eql(u8, v, strSlice[i]));
}
}
Expand Down Expand Up @@ -666,7 +666,7 @@ test "Custom types II: set as global, get without ownership" {

_ = try lua.newUserType(TestCustomType, "TestCustomType");
// Creation from Zig
var ojjectum = try lua.createUserType(TestCustomType, .{42, 42.0, "life", true});
var ojjectum = try lua.createUserType(TestCustomType, .{ 42, 42.0, "life", true });
defer lua.release(ojjectum);

lua.set("zig", ojjectum);
Expand Down

0 comments on commit 881a879

Please sign in to comment.