@@ -381,6 +381,10 @@ test "typenames" {
381381 try expectEqualStrings ("function" , lua .typeName (.function ));
382382 try expectEqualStrings ("userdata" , lua .typeName (.userdata ));
383383 try expectEqualStrings ("thread" , lua .typeName (.thread ));
384+
385+ if (ziglua .lang == .luau ) {
386+ try expectEqualStrings ("vector" , lua .typeName (.vector ));
387+ }
384388}
385389
386390test "unsigned" {
@@ -2249,3 +2253,68 @@ test "tagged userdata" {
22492253 lua .pushInteger (13 );
22502254 try expectError (error .Fail , lua .userdataTag (-1 ));
22512255}
2256+
2257+ fn vectorCtor (l : * Lua ) i32 {
2258+ const x = l .toNumber (1 ) catch unreachable ;
2259+ const y = l .toNumber (2 ) catch unreachable ;
2260+ const z = l .toNumber (3 ) catch unreachable ;
2261+ if (ziglua .luau_vector_size == 4 ) {
2262+ const w = l .optNumber (4 , 0 );
2263+ l .pushVector (@floatCast (x ), @floatCast (y ), @floatCast (z ), @floatCast (w ));
2264+ } else {
2265+ l .pushVector (@floatCast (x ), @floatCast (y ), @floatCast (z ));
2266+ }
2267+ return 1 ;
2268+ }
2269+
2270+ test "luau vectors" {
2271+ if (ziglua .lang != .luau ) return ;
2272+
2273+ var lua = try Lua .init (testing .allocator );
2274+ defer lua .deinit ();
2275+ lua .openLibs ();
2276+ lua .register ("vector" , ziglua .wrap (vectorCtor ));
2277+
2278+ try lua .doString (
2279+ \\function test()
2280+ \\ local a = vector(1, 2, 3)
2281+ \\ local b = vector(4, 5, 6)
2282+ \\ local c = (a + b) * vector(2, 2, 2)
2283+ \\ return vector(c.x, c.y, c.z)
2284+ \\end
2285+ );
2286+ _ = try lua .getGlobal ("test" );
2287+ try lua .protectedCall (0 , 1 , 0 );
2288+ var v = try lua .toVector (-1 );
2289+ try testing .expectEqualSlices (f32 , &[3 ]f32 { 10 , 14 , 18 }, v [0.. 3]);
2290+
2291+ if (ziglua .luau_vector_size == 3 ) lua .pushVector (1 , 2 , 3 ) else lua .pushVector (1 , 2 , 3 , 4 );
2292+ try expect (lua .isVector (-1 ));
2293+ v = try lua .toVector (-1 );
2294+ const expected = if (ziglua .luau_vector_size == 3 ) [3 ]f32 { 1 , 2 , 3 } else [4 ]f32 { 1 , 2 , 3 , 4 };
2295+ try expectEqual (expected , v );
2296+ try expectEqualStrings ("vector" , lua .typeNameIndex (-1 ));
2297+
2298+ lua .pushInteger (5 );
2299+ try expect (! lua .isVector (-1 ));
2300+ }
2301+
2302+ test "luau 4-vectors" {
2303+ if (ziglua .lang != .luau ) return ;
2304+
2305+ var lua = try Lua .init (testing .allocator );
2306+ defer lua .deinit ();
2307+ lua .openLibs ();
2308+ lua .register ("vector" , ziglua .wrap (vectorCtor ));
2309+
2310+ // More specific 4-vector tests
2311+ if (ziglua .luau_vector_size == 4 ) {
2312+ try lua .doString (
2313+ \\local a = vector(1, 2, 3, 4)
2314+ \\local b = vector(5, 6, 7, 8)
2315+ \\return a + b
2316+ );
2317+ const vec4 = try lua .toVector (-1 );
2318+ try expectEqual ([4 ]f32 { 6 , 8 , 10 , 12 }, vec4 );
2319+ }
2320+ }
0 commit comments