@@ -244,6 +244,14 @@ pub const Lua = struct {
244
244
}
245
245
}
246
246
247
+ fn vectorCtor (l : * Lua ) i32 {
248
+ const x = l .toNumber (1 ) catch unreachable ;
249
+ const y = l .toNumber (2 ) catch unreachable ;
250
+ const z = l .toNumber (3 ) catch unreachable ;
251
+ c .lua_pushvector (l .state , @floatCast (x ), @floatCast (y ), @floatCast (z ));
252
+ return 1 ;
253
+ }
254
+
247
255
/// Initialize a Lua state with the given allocator
248
256
/// See https://www.lua.org/manual/5.1/manual.html#lua_newstate
249
257
pub fn init (allocator : Allocator ) ! Lua {
@@ -255,10 +263,12 @@ pub const Lua = struct {
255
263
allocator_ptr .* = allocator ;
256
264
257
265
const state = c .lua_newstate (alloc , allocator_ptr ) orelse return error .Memory ;
258
- return Lua {
266
+ var lua = Lua {
259
267
.allocator = allocator_ptr ,
260
268
.state = state ,
261
269
};
270
+ lua .register ("vector" , wrap (vectorCtor ));
271
+ return lua ;
262
272
}
263
273
264
274
/// Deinitialize a Lua state and free all memory
@@ -670,6 +680,12 @@ pub const Lua = struct {
670
680
c .lua_pushvalue (lua .state , index );
671
681
}
672
682
683
+ /// Pushes a floating point 3-vector (or 4-vector if configured) `v` onto the stack
684
+ /// See https://www.lua.org/manual/5.1/manual.html#lua_pushinteger
685
+ pub fn pushVector (lua : * Lua , v : @Vector (3 , f32 )) void {
686
+ c .lua_pushvector (lua .state , v [0 ], v [1 ], v [2 ]);
687
+ }
688
+
673
689
/// Returns true if the two values in indices `index1` and `index2` are primitively equal
674
690
/// Bypasses __eq metamethods
675
691
/// Returns false if not equal, or if any index is invalid
@@ -879,6 +895,17 @@ pub const Lua = struct {
879
895
return error .Fail ;
880
896
}
881
897
898
+ /// Converts the Lua value at the given `index` to a f32 3-vector
899
+ /// The Lua value must be an integer, or a number, or a string convertible to an integer otherwise toInteger returns 0
900
+ /// See https://www.lua.org/manual/5.1/manual.html#lua_tointeger
901
+ pub fn toVector (lua : * Lua , index : i32 ) ! @Vector (3 , f32 ) {
902
+ const res = c .lua_tovector (lua .state , index );
903
+ if (res ) | r | {
904
+ return @Vector (3 , f32 ){ r [0 ], r [1 ], r [2 ] };
905
+ }
906
+ return @Vector (3 , f32 ){ 0 , 0 , 0 };
907
+ }
908
+
882
909
/// Returns the `LuaType` of the value at the given index
883
910
/// Note that this is equivalent to lua_type but because type is a Zig primitive it is renamed to `typeOf`
884
911
/// See https://www.lua.org/manual/5.1/manual.html#lua_type
@@ -1136,7 +1163,15 @@ pub const Lua = struct {
1136
1163
/// TODO: does it make sense to have this in Luau?
1137
1164
pub fn loadString (lua : * Lua , str : [:0 ]const u8 ) ! void {
1138
1165
var size : usize = 0 ;
1139
- const bytecode = c .luau_compile (str .ptr , str .len , null , & size );
1166
+ var opts = c.lua_CompileOptions {
1167
+ .optimizationLevel = 1 ,
1168
+ .debugLevel = 1 ,
1169
+ .coverageLevel = 0 ,
1170
+ .vectorLib = null ,
1171
+ .vectorCtor = "vector" ,
1172
+ .mutableGlobals = null ,
1173
+ };
1174
+ const bytecode = c .luau_compile (str .ptr , str .len , & opts , & size );
1140
1175
1141
1176
// Failed to allocate memory for the out buffer
1142
1177
if (bytecode == null ) return error .Memory ;
0 commit comments