Skip to content

Commit 3ca57a7

Browse files
committed
Revert "Remove contributions from user https://github.com/eamonburns in preparation for relicensing"
This reverts commit 6bdb102.
1 parent 8bebfb1 commit 3ca57a7

38 files changed

Lines changed: 824 additions & 19 deletions

README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,6 @@ Some examples are provided in [examples/](./examples/) to aid users in learning
8484
small self-contained applications should always be working, please create an issue if they do not work for
8585
you.
8686

87-
```zig
88-
const std = @import("std");
89-
const Lua = @import("luajit").Lua;
90-
91-
pub fn main() !void {
92-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
93-
defer _ = gpa.deinit();
94-
95-
const lua = try Lua.init(gpa.allocator());
96-
defer lua.deinit();
97-
98-
lua.openBaseLib();
99-
100-
try lua.doString(
101-
\\ message = "Hello, world!"
102-
\\ print(message)
103-
);
104-
}
105-
```
10687

10788
## Language Binding Coverage Progress
10889

examples/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Examples
2+
3+
Unless otherwise noted, the `build.zig` files are functionally the same between them.
4+
5+
## Basic Examples
6+
7+
Inspired by this [Lua C API "Cheat Sheet"](https://www.codingwiththomas.com/blog/a-lua-c-api-cheat-sheet).
8+
9+
1. [Run Lua code from Zig](./example-01_run-lua)
10+
2. [Lua variables in Zig, and vice versa](./example-02_variables)
11+
3. [Lua functions in Zig](./example-03_lua-functions)
12+
4. [Zig functions in Lua](./example-04_zig-functions)
13+
5. [Zig structs (userdata) in Lua](./example-05_userdata)
14+
6. [Lua tables in Zig](./example-06_lua-tables)
15+
16+
## Miscellaneous
17+
18+
- [Simple REPL](./simple-repl)
19+
20+
## Userdata, in-depth
21+
22+
Based on [Programming in Lua, chapter 28](https://www.lua.org/pil/28.html).
23+
24+
(To-do)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0.15.0-dev.565+8e72a2528
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Run Lua code from Zig
2+
3+
This is a simple example of how to run Lua code from Zig.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
const exe = normalBuildStepsFromZigInit(b, target, optimize);
8+
installZigLuajit(b, target, optimize, exe);
9+
}
10+
11+
fn normalBuildStepsFromZigInit(
12+
b: *std.Build,
13+
target: std.Build.ResolvedTarget,
14+
optimize: std.builtin.OptimizeMode,
15+
) *std.Build.Step.Compile {
16+
const exe_mod = b.createModule(.{
17+
.root_source_file = b.path("src/main.zig"),
18+
.target = target,
19+
.optimize = optimize,
20+
});
21+
const exe = b.addExecutable(.{
22+
.name = "example",
23+
.root_module = exe_mod,
24+
});
25+
b.installArtifact(exe);
26+
const run_cmd = b.addRunArtifact(exe);
27+
run_cmd.step.dependOn(b.getInstallStep());
28+
29+
if (b.args) |args| {
30+
run_cmd.addArgs(args);
31+
}
32+
33+
const run_step = b.step("run", "Run the app");
34+
run_step.dependOn(&run_cmd.step);
35+
36+
return exe;
37+
}
38+
39+
fn installZigLuajit(
40+
b: *std.Build,
41+
target: std.Build.ResolvedTarget,
42+
optimize: std.builtin.OptimizeMode,
43+
exe: *std.Build.Step.Compile,
44+
) void {
45+
const luajit_dep = b.dependency("luajit", .{
46+
.target = target,
47+
.optimize = optimize,
48+
});
49+
const luajit = luajit_dep.module("luajit");
50+
exe.root_module.addImport("luajit", luajit);
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.{
2+
.name = .example01_run_lua,
3+
.fingerprint = 0xf5e7b2350a95160a,
4+
.version = "0.0.0",
5+
.dependencies = .{
6+
.luajit = .{
7+
.path = "../..",
8+
},
9+
},
10+
.paths = .{
11+
"build.zig",
12+
"build.zig.zon",
13+
"src",
14+
},
15+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//! Copyright (c) 2024-2025 Theodore Sackos
2+
//! SPDX-License-Identifier: AGPL-3.0-or-later
3+
4+
const std = @import("std");
5+
const print = std.debug.print;
6+
const Lua = @import("luajit").Lua;
7+
8+
pub fn main() !void {
9+
// Boilerplate
10+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
11+
defer _ = gpa.deinit();
12+
13+
const allocator = gpa.allocator();
14+
const lua = try Lua.init(allocator);
15+
defer lua.deinit();
16+
// End boilerplate
17+
18+
lua.openBaseLib();
19+
20+
print("[Zig] Hello, World!\n", .{});
21+
22+
// Run some Lua code contained in a string
23+
lua.doString(
24+
\\ print("[Lua] Hello, Lua!")
25+
) catch unreachable; // We don't care about error handling right now
26+
27+
// You can also run Lua code in a file
28+
lua.doFile("./src/script.lua") catch |err| switch (err) {
29+
error.FileOpenOrFileRead => print("[Zig] Can't find the file 'src/script.lua'. Are you in the correct directory?\n", .{}),
30+
error.Runtime => {
31+
// A "Runtime" error is an error that happened while the Lua code was running.
32+
// In this case, an error message will often be left at the top of the stack.
33+
34+
const message = lua.toString(-1) catch "unknown"; // If the error message can't be converted to a string, then just use the string "unknown"
35+
print("[Zig] An error occurred while running the script: {s}\n", .{message});
36+
},
37+
else => unreachable,
38+
};
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print("[Lua] Hello from a file!")
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
0.15.0-dev.565+8e72a2528
2+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Passing variables to/from Lua
2+
3+
This is an example of how to pass variables to Lua, and get variables from Lua.

0 commit comments

Comments
 (0)