diff --git a/build.zig b/build.zig index 1cdfe61..ccb249e 100644 --- a/build.zig +++ b/build.zig @@ -11,6 +11,9 @@ pub const LuaVersion = enum { }; pub fn build(b: *Build) void { + // Remove the default install and uninstall steps + b.top_level_steps = .{}; + const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -18,7 +21,7 @@ pub fn build(b: *Build) void { const shared = b.option(bool, "shared", "Build shared library instead of static") orelse false; // Zig module - _ = b.addModule("ziglua", .{ + const ziglua = b.addModule("ziglua", .{ .source_file = switch (lua_version) { .lua_51 => .{ .path = "src/ziglua-5.1/lib.zig" }, .lua_52 => .{ .path = "src/ziglua-5.2/lib.zig" }, @@ -105,6 +108,34 @@ pub fn build(b: *Build) void { 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" }, + }; + + 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); + } } const lua_51_source_files = [_][]const u8{ diff --git a/examples/interpreter.zig b/examples/interpreter.zig new file mode 100644 index 0000000..9f9705c --- /dev/null +++ b/examples/interpreter.zig @@ -0,0 +1,50 @@ +//! Simple Lua interpreter +//! This is a modified program from Programming in Lua 4th Edition + +const std = @import("std"); +const ziglua = @import("ziglua"); + +pub fn main() anyerror!void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + defer _ = gpa.deinit(); + + // Initialize The Lua vm and get a reference to the main thread + var lua = try ziglua.Lua.init(allocator); + defer lua.deinit(); + + // Open the standard libraries + lua.openLibs(); + + var stdin = std.io.getStdIn().reader(); + var stdout = std.io.getStdOut().writer(); + + var buffer: [256]u8 = undefined; + while (true) { + _ = try stdout.write("> "); + + // Read a line of input + const len = try stdin.read(&buffer); + if (len == 0) break; // EOF + if (len >= buffer.len - 1) { + try stdout.print("error: line too long!\n", .{}); + continue; + } + + // Ensure the buffer is null-terminated so the Lua API can read the length + buffer[len] = 0; + + // Compile a line of Lua code + lua.loadString(buffer[0..len :0]) catch { + try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable}); + lua.pop(1); + continue; + }; + + // Execute a line of Lua code + lua.protectedCall(0, 0, 0) catch { + try stdout.print("{s}\n", .{lua.toString(-1) catch unreachable}); + lua.pop(1); + }; + } +} diff --git a/examples/zig-fn.zig b/examples/zig-fn.zig new file mode 100644 index 0000000..a274235 --- /dev/null +++ b/examples/zig-fn.zig @@ -0,0 +1,45 @@ +//! Registering a Zig function to be called from Lua + +const std = @import("std"); +const ziglua = @import("ziglua"); + +// It can be convenient to store a short reference to the Lua struct when +// it is used multiple times throughout a file. +const Lua = ziglua.Lua; + +// A Zig function called by Lua must accept a single *Lua parameter and must return an i32. +fn adder(lua: *Lua) i32 { + const a = lua.toInteger(1) catch 0; + const b = lua.toInteger(2) catch 0; + lua.pushInteger(a + b); + return 1; +} + +pub fn main() anyerror!void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + const allocator = gpa.allocator(); + defer _ = gpa.deinit(); + + var lua = try Lua.init(allocator); + defer lua.deinit(); + + lua.pushFunction(ziglua.wrap(adder)); + lua.pushInteger(10); + lua.pushInteger(32); + + // assert that this function call will not error + lua.protectedCall(2, 1, 0) catch unreachable; + + std.debug.print("the result: {}\n", .{lua.toInteger(1) catch unreachable}); + + // Now register the function to a global and run from a Lua "program" + lua.pushFunction(ziglua.wrap(adder)); + lua.setGlobal("add"); + + lua.open(.{ .base = true }); + + try lua.doString( + \\local sum = add(10, 32) + \\print(sum) + ); +}