Skip to content

Simplify the structure of the library #46

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 2 commits into from
Jan 12, 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
20 changes: 7 additions & 13 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ pub fn build(b: *Build) void {
// Zig module
const ziglua = b.addModule("ziglua", .{
.root_source_file = switch (lang) {
.lua51 => .{ .path = "src/ziglua-5.1/lib.zig" },
.lua52 => .{ .path = "src/ziglua-5.2/lib.zig" },
.lua53 => .{ .path = "src/ziglua-5.3/lib.zig" },
.lua54 => .{ .path = "src/ziglua-5.4/lib.zig" },
.luau => .{ .path = "src/zigluau/lib.zig" },
.lua51 => .{ .path = "src/lib51.zig" },
.lua52 => .{ .path = "src/lib52.zig" },
.lua53 => .{ .path = "src/lib53.zig" },
.lua54 => .{ .path = "src/lib54.zig" },
.luau => .{ .path = "src/libluau.zig" },
},
});

Expand Down Expand Up @@ -58,13 +58,7 @@ pub fn build(b: *Build) void {

// Tests
const tests = b.addTest(.{
.root_source_file = switch (lang) {
.lua51 => .{ .path = "src/ziglua-5.1/tests.zig" },
.lua52 => .{ .path = "src/ziglua-5.2/tests.zig" },
.lua53 => .{ .path = "src/ziglua-5.3/tests.zig" },
.lua54 => .{ .path = "src/ziglua-5.4/tests.zig" },
.luau => .{ .path = "src/zigluau/tests.zig" },
},
.root_source_file = .{ .path = "src/tests.zig" },
.target = target,
.optimize = optimize,
});
Expand Down Expand Up @@ -181,7 +175,7 @@ fn buildLuau(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.Opti
for (luau_source_files) |file| {
lib.addCSourceFile(.{ .file = upstream.path(file), .flags = &flags });
}
lib.addCSourceFile(.{ .file = .{ .path = "src/zigluau/luau.cpp" }, .flags = &flags });
lib.addCSourceFile(.{ .file = .{ .path = "src/luau.cpp" }, .flags = &flags });
lib.linkLibCpp();

return lib;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 16 additions & 2 deletions src/zigluau/lib.zig → src/libluau.zig
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub const Integer = c.lua_Integer;
/// Bitflag for the Lua standard libraries
pub const Libs = packed struct {
base: bool = false,
coroutine: bool = false,
package: bool = false,
string: bool = false,
utf8: bool = false,
Expand Down Expand Up @@ -411,8 +412,10 @@ pub const Lua = struct {

/// Pushes onto the stack the value of the global name
/// See https://www.lua.org/manual/5.1/manual.html#lua_getglobal
pub fn getGlobal(lua: *Lua, name: [:0]const u8) LuaType {
return @enumFromInt(c.lua_getglobal(lua.state, name.ptr));
pub fn getGlobal(lua: *Lua, name: [:0]const u8) !LuaType {
const lua_type: LuaType = @enumFromInt(c.lua_getglobal(lua.state, name.ptr));
if (lua_type == .nil) return error.Fail;
return lua_type;
}

/// If the value at the given index has a metatable, the function pushes that metatable onto the stack
Expand Down Expand Up @@ -1258,6 +1261,7 @@ pub const Lua = struct {
/// See https://www.lua.org/manual/5.1/manual.html#luaL_openlibs
pub fn open(lua: *Lua, libs: Libs) void {
if (libs.base) lua.requireF("", c.luaopen_base);
if (libs.coroutine) lua.requireF(c.LUA_COLIBNAME, c.luaopen_coroutine);
if (libs.string) lua.requireF(c.LUA_STRLIBNAME, c.luaopen_string);
if (libs.table) lua.requireF(c.LUA_TABLIBNAME, c.luaopen_table);
if (libs.math) lua.requireF(c.LUA_MATHLIBNAME, c.luaopen_math);
Expand All @@ -1282,11 +1286,21 @@ pub const Lua = struct {
_ = c.luaopen_base(lua.state);
}

/// Open the coroutine standard library
pub fn openCoroutine(lua: *Lua) void {
_ = c.luaopen_coroutine(lua.state);
}

/// Open the string standard library
pub fn openString(lua: *Lua) void {
_ = c.luaopen_string(lua.state);
}

/// Open the UTF-8 standard library
pub fn openUtf8(lua: *Lua) void {
_ = c.luaopen_utf8(lua.state);
}

/// Open the table standard library
pub fn openTable(lua: *Lua) void {
_ = c.luaopen_table(lua.state);
Expand Down
File renamed without changes.
Loading