Skip to content

Add Luau support #36

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 11 commits into from
Jan 2, 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
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
test:
strategy:
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{matrix.os}}

steps:
Expand Down
196 changes: 154 additions & 42 deletions build.zig
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const std = @import("std");

const Build = std.Build;
const Step = std.Build.Step;

pub const LuaVersion = enum {
lua_51,
lua_52,
lua_53,
lua_54,
// lua_jit,
luau,
};

pub fn build(b: *Build) void {
Expand All @@ -27,10 +28,65 @@ pub fn build(b: *Build) void {
.lua_52 => .{ .path = "src/ziglua-5.2/lib.zig" },
.lua_53 => .{ .path = "src/ziglua-5.3/lib.zig" },
.lua_54 => .{ .path = "src/ziglua-5.4/lib.zig" },
.luau => .{ .path = "src/zigluau/lib.zig" },
},
});

// Lua library artifact
const lib = switch (lua_version) {
.lua_51, .lua_52, .lua_53, .lua_54 => buildLua(b, target, optimize, lua_version, shared),
.luau => buildLuau(b, target, optimize, shared),
};

b.installArtifact(lib);

// Tests
const tests = b.addTest(.{
.root_source_file = switch (lua_version) {
.lua_51 => .{ .path = "src/ziglua-5.1/tests.zig" },
.lua_52 => .{ .path = "src/ziglua-5.2/tests.zig" },
.lua_53 => .{ .path = "src/ziglua-5.3/tests.zig" },
.lua_54 => .{ .path = "src/ziglua-5.4/tests.zig" },
.luau => .{ .path = "src/zigluau/tests.zig" },
},
.target = target,
.optimize = optimize,
});
tests.linkLibrary(lib);

const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run ziglua tests");
test_step.dependOn(&run_tests.step);

// Examples
const examples = [_]struct { []const u8, []const u8 }{
.{ "interpreter", "examples/interpreter.zig" },
.{ "zig-function", "examples/zig-fn.zig" },
};

for (examples) |example| {
const exe = b.addExecutable(.{
.name = example[0],
.root_source_file = .{ .path = example[1] },
.target = target,
.optimize = optimize,
});
exe.addModule("ziglua", ziglua);
exe.linkLibrary(lib);

const artifact = b.addInstallArtifact(exe, .{});
const exe_step = b.step(b.fmt("install-example-{s}", .{example[0]}), b.fmt("Install {s} example", .{example[0]}));
exe_step.dependOn(&artifact.step);

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);

const run_step = b.step(b.fmt("run-example-{s}", .{example[0]}), b.fmt("Run {s} example", .{example[0]}));
run_step.dependOn(&run_cmd.step);
}
}

fn buildLua(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, lua_version: LuaVersion, shared: bool) *Step.Compile {
const lib_opts = .{
.name = "lua",
.target = target,
Expand All @@ -40,6 +96,7 @@ pub fn build(b: *Build) void {
.lua_52 => std.SemanticVersion{ .major = 5, .minor = 2, .patch = 4 },
.lua_53 => std.SemanticVersion{ .major = 5, .minor = 3, .patch = 6 },
.lua_54 => std.SemanticVersion{ .major = 5, .minor = 4, .patch = 6 },
else => unreachable,
},
};
const lib = if (shared)
Expand All @@ -52,8 +109,9 @@ pub fn build(b: *Build) void {
.lua_52 => "lib/lua-5.2/src",
.lua_53 => "lib/lua-5.3/src",
.lua_54 => "lib/lua-5.4/src",
else => unreachable,
};
lib.addIncludePath(.{ .path = lib_dir });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ lib_dir, "include" }) });

const os_tag = target.os_tag orelse
(std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target.os.tag;
Expand All @@ -79,10 +137,10 @@ pub fn build(b: *Build) void {
.lua_52 => &lua_52_source_files,
.lua_53 => &lua_53_source_files,
.lua_54 => &lua_54_source_files,
else => unreachable,
};
for (lua_source_files) |file| {
const path = std.fs.path.join(b.allocator, &.{ lib_dir, file }) catch unreachable;
lib.addCSourceFile(.{ .file = .{ .path = path }, .flags = &flags });
lib.addCSourceFile(.{ .file = .{ .path = b.pathJoin(&.{ lib_dir, file }) }, .flags = &flags });
}
lib.linkLibC();

Expand All @@ -91,51 +149,51 @@ pub fn build(b: *Build) void {
lib.installHeader(b.pathJoin(&.{ lib_dir, "lauxlib.h" }), "lua/lauxlib.h");
lib.installHeader(b.pathJoin(&.{ lib_dir, "luaconf.h" }), "lua/luaconf.h");

b.installArtifact(lib);
return lib;
}

// Tests
const tests = b.addTest(.{
.root_source_file = switch (lua_version) {
.lua_51 => .{ .path = "src/ziglua-5.1/tests.zig" },
.lua_52 => .{ .path = "src/ziglua-5.2/tests.zig" },
.lua_53 => .{ .path = "src/ziglua-5.3/tests.zig" },
.lua_54 => .{ .path = "src/ziglua-5.4/tests.zig" },
},
/// Luau has diverged enough from Lua (C++, project structure, ...) that it is easier to separate the build logic
fn buildLuau(b: *Build, target: std.zig.CrossTarget, optimize: std.builtin.OptimizeMode, shared: bool) *Step.Compile {
const lib_opts = .{
.name = "luau",
.target = target,
.optimize = optimize,
});
tests.addIncludePath(.{ .path = lib_dir });
tests.linkLibrary(lib);

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

// Examples
const examples = [_]struct { []const u8, []const u8 }{
.{ "interpreter", "examples/interpreter.zig" },
.{ "zig-function", "examples/zig-fn.zig" },
.version = std.SemanticVersion{ .major = 0, .minor = 607, .patch = 0 },
};
const lib = if (shared)
b.addSharedLibrary(lib_opts)
else
b.addStaticLibrary(lib_opts);

for (examples) |example| {
const exe = b.addExecutable(.{
.name = example[0],
.root_source_file = .{ .path = example[1] },
.target = target,
.optimize = optimize,
});
exe.addModule("ziglua", ziglua);
exe.linkLibrary(lib);
const lib_dir = "lib/luau/VM";
lib.addIncludePath(.{ .path = "lib/luau/Common/include" });
lib.addIncludePath(.{ .path = "lib/luau/Compiler/include" });
lib.addIncludePath(.{ .path = "lib/luau/Ast/include" });
lib.addIncludePath(.{ .path = b.pathJoin(&.{ lib_dir, "include" }) });

const artifact = b.addInstallArtifact(exe, .{});
const exe_step = b.step(b.fmt("install-example-{s}", .{example[0]}), b.fmt("Install {s} example", .{example[0]}));
exe_step.dependOn(&artifact.step);
const os_tag = target.os_tag orelse
(std.zig.system.NativeTargetInfo.detect(target) catch unreachable).target.os.tag;
_ = os_tag;

const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args);
const flags = [_][]const u8{
"-DLUA_USE_LONGJMP=1",
"-DLUA_API=extern\"C\"",
"-DLUACODE_API=extern\"C\"",
"-DLUACODEGEN_API=extern\"C\"",
};

const run_step = b.step(b.fmt("run-example-{s}", .{example[0]}), b.fmt("Run {s} example", .{example[0]}));
run_step.dependOn(&run_cmd.step);
for (luau_source_files) |file| {
lib.addCSourceFile(.{ .file = .{ .path = file }, .flags = &flags });
}
lib.addCSourceFile(.{ .file = .{ .path = "src/zigluau/luau.cpp" }, .flags = &flags });
lib.linkLibCpp();

lib.installHeader("lib/luau/VM/include/lua.h", "lua/lua.h");
lib.installHeader("lib/luau/VM/include/lualib.h", "lua/lualib.h");
lib.installHeader("lib/luau/VM/include/luaconf.h", "lua/luaconf.h");
lib.installHeader("lib/luau/Compiler/include/luacode.h", "lua/luacode.h");

return lib;
}

const lua_51_source_files = [_][]const u8{
Expand Down Expand Up @@ -275,3 +333,57 @@ const lua_54_source_files = [_][]const u8{
"lutf8lib.c",
"linit.c",
};

const luau_source_files = [_][]const u8{
"lib/luau/Compiler/src/BuiltinFolding.cpp",
"lib/luau/Compiler/src/Builtins.cpp",
"lib/luau/Compiler/src/BytecodeBuilder.cpp",
"lib/luau/Compiler/src/Compiler.cpp",
"lib/luau/Compiler/src/ConstantFolding.cpp",
"lib/luau/Compiler/src/CostModel.cpp",
"lib/luau/Compiler/src/TableShape.cpp",
"lib/luau/Compiler/src/Types.cpp",
"lib/luau/Compiler/src/ValueTracking.cpp",
"lib/luau/Compiler/src/lcode.cpp",

"lib/luau/VM/src/lapi.cpp",
"lib/luau/VM/src/laux.cpp",
"lib/luau/VM/src/lbaselib.cpp",
"lib/luau/VM/src/lbitlib.cpp",
"lib/luau/VM/src/lbuffer.cpp",
"lib/luau/VM/src/lbuflib.cpp",
"lib/luau/VM/src/lbuiltins.cpp",
"lib/luau/VM/src/lcorolib.cpp",
"lib/luau/VM/src/ldblib.cpp",
"lib/luau/VM/src/ldebug.cpp",
"lib/luau/VM/src/ldo.cpp",
"lib/luau/VM/src/lfunc.cpp",
"lib/luau/VM/src/lgc.cpp",
"lib/luau/VM/src/lgcdebug.cpp",
"lib/luau/VM/src/linit.cpp",
"lib/luau/VM/src/lmathlib.cpp",
"lib/luau/VM/src/lmem.cpp",
"lib/luau/VM/src/lnumprint.cpp",
"lib/luau/VM/src/lobject.cpp",
"lib/luau/VM/src/loslib.cpp",
"lib/luau/VM/src/lperf.cpp",
"lib/luau/VM/src/lstate.cpp",
"lib/luau/VM/src/lstring.cpp",
"lib/luau/VM/src/lstrlib.cpp",
"lib/luau/VM/src/ltable.cpp",
"lib/luau/VM/src/ltablib.cpp",
"lib/luau/VM/src/ltm.cpp",
"lib/luau/VM/src/ludata.cpp",
"lib/luau/VM/src/lutf8lib.cpp",
"lib/luau/VM/src/lvmexecute.cpp",
"lib/luau/VM/src/lvmload.cpp",
"lib/luau/VM/src/lvmutils.cpp",

"lib/luau/Ast/src/Ast.cpp",
"lib/luau/Ast/src/Confusables.cpp",
"lib/luau/Ast/src/Lexer.cpp",
"lib/luau/Ast/src/Location.cpp",
"lib/luau/Ast/src/Parser.cpp",
"lib/luau/Ast/src/StringUtils.cpp",
"lib/luau/Ast/src/TimeTrace.cpp",
};
Loading