Skip to content

Commit b970202

Browse files
committed
Add configurable vector size build option
Support building with either 3 or 4 component vectors via -Dvector-size flag. Default is 4 for better SIMD alignment. CI tests both configurations.
1 parent b4090c5 commit b970202

File tree

5 files changed

+41
-7
lines changed

5 files changed

+41
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ jobs:
4646
key: zig-cache-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('build.zig', 'build.zig.zon') }}
4747

4848
- run: zig build test --summary all
49+
- run: zig build test -Dvector-size=3 --summary all
4950

5051
- run: zig build luau-compile -- --help
5152
- run: zig build luau-analyze -- --help

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Added
8+
- Configurable vector size build option (`-Dvector-size=3` or `4`, default 4)
9+
710
### Changed
811
- Updated Luau dependency from version 0.684 to 0.686
912

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ const greeting = result.ok.?;
266266
> [!WARNING]
267267
> This library is still evolving and the API is not stable. Backward incompatible changes may be introduced up until the 1.0 release. Consider pinning to a specific commit or tag if you need stability.
268268
269+
## 🔧 Build Configuration
270+
271+
### Vector Size
272+
By default, luaz is built with 4-component vectors. To customize:
273+
```bash
274+
zig build -Dvector-size=3 # Build with 3-component vectors
275+
```
276+
269277
## 🛠️ Using Luau Tools
270278

271279
The build system provides prebuilt Luau tools that can be invoked directly:

build.zig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,23 @@ pub fn build(b: *std.Build) !void {
2525

2626
const opts = .{
2727
.cover = b.option(bool, "coverage", "Generate test coverage (requires kcov)") orelse false,
28+
.vector_size = b.option(u8, "vector-size", "Luau vector size (3 or 4, default 4)") orelse 4,
2829
};
2930

31+
// Validate vector size
32+
if (opts.vector_size != 3 and opts.vector_size != 4) {
33+
std.log.err("Invalid vector size: {}. Must be either 3 or 4", .{opts.vector_size});
34+
return error.InvalidVectorSize;
35+
}
36+
3037
// Luau VM lib
3138
const luau_vm = blk: {
3239
const mod = b.createModule(.{ .target = target, .optimize = optimize });
3340

3441
try addSrcFiles(b, mod, "luau/VM/src", flags);
3542

3643
mod.addCMacro("LUA_USE_LONGJMP", "1");
44+
mod.addCMacro("LUA_VECTOR_SIZE", b.fmt("{d}", .{opts.vector_size}));
3745

3846
mod.addIncludePath(b.path("luau/VM/include"));
3947
mod.addIncludePath(b.path("luau/VM/src"));
@@ -57,6 +65,7 @@ pub fn build(b: *std.Build) !void {
5765

5866
try addSrcFiles(b, mod, "luau/CodeGen/src", flags);
5967

68+
mod.addCMacro("LUA_VECTOR_SIZE", b.fmt("{d}", .{opts.vector_size}));
6069
mod.addIncludePath(b.path("luau/CodeGen/include"));
6170
mod.addIncludePath(b.path("luau/Common/include"));
6271
mod.addIncludePath(b.path("luau/VM/src"));
@@ -175,6 +184,7 @@ pub fn build(b: *std.Build) !void {
175184
.file = b.path("src/handler.cpp"),
176185
.flags = flags,
177186
});
187+
mod.addCMacro("LUA_VECTOR_SIZE", b.fmt("{d}", .{opts.vector_size}));
178188
mod.addIncludePath(b.path("luau/VM/include"));
179189
mod.addIncludePath(b.path("luau/Common/include"));
180190
mod.addIncludePath(b.path("src"));
@@ -219,6 +229,7 @@ pub fn build(b: *std.Build) !void {
219229
.file = b.path("src/handler.cpp"),
220230
.flags = flags,
221231
});
232+
unit_tests.root_module.addCMacro("LUA_VECTOR_SIZE", b.fmt("{d}", .{opts.vector_size}));
222233
unit_tests.root_module.addIncludePath(b.path("luau/VM/include"));
223234
unit_tests.root_module.addIncludePath(b.path("luau/Common/include"));
224235
unit_tests.root_module.addIncludePath(b.path("src"));

src/stack.zig

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -792,13 +792,24 @@ test "push and pop vector types" {
792792
const lua = try Lua.init(&std.testing.allocator);
793793
defer lua.deinit();
794794

795-
const vec3 = @Vector(3, f32){ 1.0, 2.0, 3.0 };
796-
push(&lua.state, vec3);
797-
try expect(lua.state.isVector(-1));
798-
const popped_vec3 = pop(lua, @Vector(3, f32)).?;
799-
try expectEq(popped_vec3[0], 1.0);
800-
try expectEq(popped_vec3[1], 2.0);
801-
try expectEq(popped_vec3[2], 3.0);
795+
if (State.VECTOR_SIZE == 3) {
796+
const vec = @Vector(3, f32){ 1.0, 2.0, 3.0 };
797+
push(&lua.state, vec);
798+
try expect(lua.state.isVector(-1));
799+
const popped_vec = pop(lua, @Vector(3, f32)).?;
800+
try expectEq(popped_vec[0], 1.0);
801+
try expectEq(popped_vec[1], 2.0);
802+
try expectEq(popped_vec[2], 3.0);
803+
} else {
804+
const vec = @Vector(4, f32){ 1.0, 2.0, 3.0, 4.0 };
805+
push(&lua.state, vec);
806+
try expect(lua.state.isVector(-1));
807+
const popped_vec = pop(lua, @Vector(4, f32)).?;
808+
try expectEq(popped_vec[0], 1.0);
809+
try expectEq(popped_vec[1], 2.0);
810+
try expectEq(popped_vec[2], 3.0);
811+
try expectEq(popped_vec[3], 4.0);
812+
}
802813

803814
try expectEq(lua.state.getTop(), 0);
804815
}

0 commit comments

Comments
 (0)