-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeclare.zig
More file actions
52 lines (45 loc) · 1.69 KB
/
Copy pathdeclare.zig
File metadata and controls
52 lines (45 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//! Tooling: project a Zig host API into Luau type definitions (`declsFor`),
//! print them, and type-check a mod script against them. This is the file you'd
//! write to `types/host.d.luau` for `luau-lsp` editor autocomplete.
const std = @import("std");
const luau = @import("luau");
const Vec2 = struct { x: f64, y: f64 };
const GameWorld = struct {
tick: f64,
gravity: Vec2,
names: []const []const u8,
pub fn init() GameWorld { // lifecycle hook — skipped in the projection
return undefined;
}
pub fn spawn(name: []const u8, pos: Vec2) u32 {
_ = name;
_ = pos;
return 0;
}
pub fn paused() bool {
return false;
}
};
pub fn main() !void {
// 1) project the Zig struct into Luau definitions (comptime, no allocation).
const defs = luau.declsFor(GameWorld, "world");
std.debug.print("--- generated Luau definitions (types/host.d.luau) ---\n{s}\n", .{defs});
// 2) a mod that uses the API correctly type-checks clean.
{
var r = luau.analysis.defs.checkWithDefinitions(defs,
\\local id = world.spawn("goblin", { x = 1, y = 2 })
\\return id + world.tick
);
defer r.deinit();
std.debug.print("correct mod -> ok={}\n", .{r.ok()});
}
// 3) a mod that misuses it is rejected, with the checker's diagnostics.
{
var r = luau.analysis.defs.checkWithDefinitions(defs, "return world.nonexistent()");
defer r.deinit();
std.debug.print("broken mod -> ok={}\n", .{r.ok()});
var it = r.errors();
while (it.next()) |e|
std.debug.print(" {d}:{d} {s}\n", .{ e.position.line, e.position.column, e.message });
}
}