Skip to content
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
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ jobs:
uses: actions/configure-pages@v5
- uses: mlugg/setup-zig@v2
with:
version: "0.16.0"
version: "master"
- run: make docs
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: 'zig-out/docs'
path: "zig-out/docs"
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
21 changes: 10 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: tests

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
test_zig:
Expand All @@ -15,14 +15,13 @@ jobs:
runs-on: ${{matrix.os}}

steps:
- name: Clone Ziglua
uses: actions/checkout@v3
- name: Clone Ziglua
uses: actions/checkout@v3

- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: "0.16.0"

- name: Run tests
run: make test
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: "master"

- name: Run tests
run: make test
4 changes: 2 additions & 2 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ pub fn build(b: *Build) void {
.luau => @panic("luau not supported for system lua"),
};
zlua.linkSystemLibrary(system_library_name, .{ .preferred_link_mode = link_mode });
t.linkSystemLibrary(system_library_name, .{ .preferred_link_mode = link_mode });
t.mod.linkSystemLibrary(system_library_name, .{ .preferred_link_mode = link_mode });
} else if (b.lazyDependency(@tagName(lang), .{})) |upstream| {
const lib = switch (lang) {
.luajit => luajit_setup.configure(b, target, optimize, upstream, shared),
Expand Down Expand Up @@ -171,7 +171,7 @@ pub fn build(b: *Build) void {

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

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);
Expand Down
4 changes: 2 additions & 2 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
},

.translate_c = .{
.url = "git+https://codeberg.org/ziglang/translate-c#46b5609b5ac4c0a896217d1d984f3ae50e4810b5",
.hash = "translate_c-0.0.0-Q_BUWpf0BgAwrh5AM-acJcslN_YPEhcoCVKbbNjwuUTJ",
.url = "git+https://codeberg.org/ziglang/translate-c?ref=master#ba4288f6c0e8054d7894266949d4fa08bfc0212a",
.hash = "translate_c-0.0.0-Q_BUWvI3BwAFZWG2aNKspzHT7uv3yKuNMC5sP4C0hoYl",
},
},
}
22 changes: 10 additions & 12 deletions examples/multithreaded.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
const std = @import("std");
const zlua = @import("zlua");

var mutex = std.Thread.Mutex{};
var mutex = std.Io.Mutex.init;
var io: std.Io = undefined;

export fn lua_zlock(L: *zlua.LuaState) callconv(.c) void {
_ = L;
mutex.lock();
mutex.lock(io) catch {};
}

export fn lua_zunlock(L: *zlua.LuaState) callconv(.c) void {
_ = L;
mutex.unlock();
mutex.unlock(io);
}

fn add_to_x(lua: *zlua.Lua, num: usize) void {
Expand All @@ -25,7 +26,7 @@ fn add_to_x(lua: *zlua.Lua, num: usize) void {
}

const size = 256;
var buf = [_:0]u8{0} ** size;
var buf = std.mem.zeroes([size:0]u8);
_ = std.fmt.bufPrint(&buf, "print(\"{}: \", x)", .{std.Thread.getCurrentId()}) catch return;

// The printing from different threads does not always work nicely
Expand All @@ -36,6 +37,7 @@ fn add_to_x(lua: *zlua.Lua, num: usize) void {

pub fn main(init: std.process.Init) anyerror!void {
const gpa = init.gpa;
io = init.io;

// Initialize The Lua vm and get a reference to the main thread
var lua = try zlua.Lua.init(gpa);
Expand All @@ -52,22 +54,18 @@ pub fn main(init: std.process.Init) anyerror!void {
const n_jobs = 5;
var subs: [n_jobs]*zlua.Lua = undefined;

// create a thread pool to run all the functions
var pool: std.Thread.Pool = undefined;
try pool.init(.{ .allocator = gpa, .n_jobs = n_jobs });
defer pool.deinit();

var wg: std.Thread.WaitGroup = .{};
// create a wait group to run all the functions
var wg = std.Io.Group.init;

for (0..n_jobs) |i| {
subs[i] = lua.newThread();
pool.spawnWg(&wg, add_to_x, .{ subs[i], num });
wg.async(io, add_to_x, .{ subs[i], num });
}

// also do the thing from the main thread
add_to_x(lua, num);

wg.wait();
try wg.await(io);

for (subs) |sub| {
try lua.closeThread(sub);
Expand Down
14 changes: 8 additions & 6 deletions src/define.zig
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ fn addEnum(
try item.appendSlice(gpa, name(T));
try item.appendSlice(gpa, "\n");

inline for (@typeInfo(T).@"enum".fields) |field| {
inline for (@typeInfo(T).@"enum".field_names) |fname| {
try item.appendSlice(gpa, "---|\' \"");
try item.appendSlice(gpa, field.name);
try item.appendSlice(gpa, fname);
try item.appendSlice(gpa, "\" \'\n");
}
}
Expand All @@ -98,15 +98,17 @@ pub fn addClass(
try item.appendSlice(gpa, name(T));
try item.appendSlice(gpa, "\n");

inline for (@typeInfo(T).@"struct".fields) |field| {
const str = @typeInfo(T).@"struct";

inline for (str.field_names, str.field_types, str.field_attrs) |fname, ftype, attr| {
try item.appendSlice(gpa, "---@field ");
try item.appendSlice(gpa, field.name);
try item.appendSlice(gpa, fname);

if (field.defaultValue() != null) {
if (attr.defaultValue(ftype) != null) {
try item.appendSlice(gpa, "?");
}
try item.appendSlice(gpa, " ");
try luaTypeName(state, gpa, idx, field.type);
try luaTypeName(state, gpa, idx, ftype);
try state.definitions.items[idx].appendSlice(gpa, "\n");
}
}
Expand Down
Loading
Loading