@@ -44,7 +44,6 @@ pub const ArithOperator = enum(u4) {
44
44
45
45
/// Type for C functions
46
46
/// See https://www.lua.org/manual/5.4/manual.html#lua_CFunction for the protocol
47
- /// TODO: we really are passing Zig functions, maybe call `Function` and use `func` for params?
48
47
pub const CFn = fn (state : ? * LuaState ) callconv (.C ) c_int ;
49
48
50
49
/// Operations supported by `Lua.compare()`
@@ -198,6 +197,12 @@ pub const Number = c.lua_Number;
198
197
/// The type of the reader function used by `Lua.load()`
199
198
pub const CReaderFn = fn (state : ? * LuaState , data : ? * anyopaque , size : [* c ]usize ) callconv (.C ) [* c ]const u8 ;
200
199
200
+ /// The possible status of a call to `Lua.resumeThread`
201
+ pub const ResumeStatus = enum (u1 ) {
202
+ ok = StatusCode .ok ,
203
+ yield = StatusCode .yield ,
204
+ };
205
+
201
206
/// Reference constants
202
207
pub const ref_nil = c .LUA_REFNIL ;
203
208
pub const ref_no = c .LUA_NOREF ;
@@ -211,24 +216,35 @@ pub const ridx_globals = c.LUA_RIDX_GLOBALS;
211
216
/// Index of the main thread in the registry
212
217
pub const ridx_mainthread = c .LUA_RIDX_MAINTHREAD ;
213
218
219
+ /// Status that a thread can be in
220
+ /// Usually errors are reported by a Zig error rather than a status enum value
221
+ pub const Status = enum (u3 ) {
222
+ ok = StatusCode .ok ,
223
+ yield = StatusCode .yield ,
224
+ err_runtime = StatusCode .err_runtime ,
225
+ err_syntax = StatusCode .err_syntax ,
226
+ err_memory = StatusCode .err_memory ,
227
+ err_error = StatusCode .err_error ,
228
+ };
229
+
214
230
/// Status codes
215
231
/// Not public, because typically Status.ok is returned from a function implicitly;
216
232
/// Any function that returns an error usually returns a Zig error, and a void return
217
233
/// is an implicit Status.ok.
218
234
/// In the rare case that the status code is required from a function, an enum is
219
235
/// used for that specific function's return type
220
- const Status = struct {
236
+ const StatusCode = struct {
221
237
pub const ok = c .LUA_OK ;
222
238
pub const yield = c .LUA_YIELD ;
223
239
pub const err_runtime = c .LUA_ERRRUN ;
224
240
pub const err_syntax = c .LUA_ERRSYNTAX ;
225
241
pub const err_memory = c .LUA_ERRMEM ;
226
242
pub const err_error = c .LUA_ERRERR ;
227
-
228
- // Only used in loadFileX
229
- pub const err_file = c .LUA_ERRFILE ;
230
243
};
231
244
245
+ // Only used in loadFileX, so no need to group with Status
246
+ pub const err_file = c .LUA_ERRFILE ;
247
+
232
248
/// The standard representation for file handles used by the standard IO library
233
249
pub const Stream = c .luaL_Stream ;
234
250
@@ -575,12 +591,12 @@ pub const Lua = struct {
575
591
};
576
592
const ret = c .lua_load (lua .state , reader , data , chunk_name , mode_str );
577
593
switch (ret ) {
578
- Status .ok = > return ,
579
- Status .err_syntax = > return Error .Syntax ,
580
- Status .err_memory = > return Error .Memory ,
581
- // can also return any result of a pcall error
582
- Status .err_runtime = > return Error .Runtime ,
583
- Status .err_error = > return Error .MsgHandler ,
594
+ StatusCode .ok = > return ,
595
+ StatusCode .err_syntax = > return Error .Syntax ,
596
+ StatusCode .err_memory = > return Error .Memory ,
597
+ // lua_load runs pcall, so can also return any result of an pcall error
598
+ StatusCode .err_runtime = > return Error .Runtime ,
599
+ StatusCode .err_error = > return Error .MsgHandler ,
584
600
else = > unreachable ,
585
601
}
586
602
}
@@ -607,8 +623,6 @@ pub const Lua = struct {
607
623
/// This function creates and pushes a new full userdata onto the stack
608
624
/// with `num_uvalue` associated Lua values, plus an associated block of raw memory with `size` bytes
609
625
/// Returns the address of the block of memory
610
- /// TODO: use comptime to return the a pointer to a block of type T for safety?
611
- /// See how this is used if that would be useful
612
626
pub fn newUserdataUV (lua : * Lua , comptime T : type , new_uvalue : i32 ) * T {
613
627
// safe to .? because this function throws a Lua error on out of memory
614
628
// so the returned pointer should never be null
@@ -642,10 +656,10 @@ pub const Lua = struct {
642
656
pub fn protectedCallCont (lua : * Lua , num_args : i32 , num_results : i32 , msg_handler : i32 , ctx : KContext , k : ? CContFn ) ! void {
643
657
const ret = c .lua_pcallk (lua .state , num_args , num_results , msg_handler , ctx , k );
644
658
switch (ret ) {
645
- Status .ok = > return ,
646
- Status .err_runtime = > return Error .Runtime ,
647
- Status .err_memory = > return Error .Memory ,
648
- Status .err_error = > return Error .MsgHandler ,
659
+ StatusCode .ok = > return ,
660
+ StatusCode .err_runtime = > return Error .Runtime ,
661
+ StatusCode .err_memory = > return Error .Memory ,
662
+ StatusCode .err_error = > return Error .MsgHandler ,
649
663
else = > unreachable ,
650
664
}
651
665
}
@@ -737,8 +751,6 @@ pub const Lua = struct {
737
751
c .lua_pushvalue (lua .state , index );
738
752
}
739
753
740
- // TODO: pub fn pushVFString is that even worth?
741
-
742
754
/// Returns true if the two values in indices `index1` and `index2` are primitively equal
743
755
/// Bypasses __eq metamethods
744
756
/// Returns false if not equal, or if any index is invalid
@@ -815,22 +827,17 @@ pub const Lua = struct {
815
827
/// Resets a thread, cleaning its call stack and closing all pending to-be-closed variables
816
828
/// Returns an error if an error occured and leaves an error object on top of the stack
817
829
pub fn resetThread (lua : * Lua ) ! void {
818
- if (c .lua_resetthread (lua .state ) != Status .ok ) return Error .Fail ;
830
+ if (c .lua_resetthread (lua .state ) != StatusCode .ok ) return Error .Fail ;
819
831
}
820
832
821
- pub const ResumeStatus = enum (u1 ) {
822
- ok = Status .ok ,
823
- yield = Status .yield ,
824
- };
825
-
826
833
/// Starts and resumes a coroutine in the given thread
827
834
pub fn resumeThread (lua : * Lua , from : ? Lua , num_args : i32 , num_results : * i32 ) ! ResumeStatus {
828
835
const from_state = if (from ) | from_val | from_val .state else null ;
829
836
const thread_status = c .lua_resume (lua .state , from_state , num_args , num_results );
830
837
switch (thread_status ) {
831
- Status .err_runtime = > return Error .Runtime ,
832
- Status .err_memory = > return Error .Memory ,
833
- Status .err_error = > return Error .MsgHandler ,
838
+ StatusCode .err_runtime = > return Error .Runtime ,
839
+ StatusCode .err_memory = > return Error .Memory ,
840
+ StatusCode .err_error = > return Error .MsgHandler ,
834
841
else = > return @intToEnum (ResumeStatus , thread_status ),
835
842
}
836
843
}
@@ -897,19 +904,9 @@ pub const Lua = struct {
897
904
c .lua_setwarnf (lua .state , warn_fn , data );
898
905
}
899
906
900
- // TODO: this is not very clean
901
- pub const StatusType = enum (u3 ) {
902
- ok = Status .ok ,
903
- yield = Status .yield ,
904
- err_runtime = Status .err_runtime ,
905
- err_syntax = Status .err_syntax ,
906
- err_memory = Status .err_memory ,
907
- err_error = Status .err_error ,
908
- };
909
-
910
907
/// Returns the status of this thread
911
- pub fn status (lua : * Lua ) StatusType {
912
- return @intToEnum (StatusType , c .lua_status (lua .state ));
908
+ pub fn status (lua : * Lua ) Status {
909
+ return @intToEnum (Status , c .lua_status (lua .state ));
913
910
}
914
911
915
912
/// Converts the zero-terminated string `str` to a number, pushes that number onto the stack,
@@ -1037,16 +1034,18 @@ pub const Lua = struct {
1037
1034
c .lua_xmove (lua .state , to .state , num );
1038
1035
}
1039
1036
1040
- /// This function is equivalent to `Lua.yieldK ()` but has no continuation
1041
- /// TODO: return values?
1042
- pub fn yield (lua : * Lua , num_results : i32 ) i32 {
1037
+ /// This function is equivalent to `Lua.yieldCont ()` but has no continuation
1038
+ /// NOTE: look into the lua_yieldk docs about this and debug hooks and noreturn
1039
+ pub fn yield (lua : * Lua , num_results : i32 ) noreturn {
1043
1040
// translate-c failed to pass NULL correctly
1044
- return c .lua_yieldk (lua .state , num_results , 0 , null );
1041
+ _ = c .lua_yieldk (lua .state , num_results , 0 , null );
1042
+ unreachable ;
1045
1043
}
1046
1044
1047
1045
/// Yields this coroutine (thread)
1048
- pub fn yieldCont (lua : * Lua , num_results : i32 , ctx : KContext , k : CContFn ) i32 {
1049
- return c .lua_yieldk (lua .state , num_results , ctx , k );
1046
+ pub fn yieldCont (lua : * Lua , num_results : i32 , ctx : KContext , k : CContFn ) noreturn {
1047
+ _ = c .lua_yieldk (lua .state , num_results , ctx , k );
1048
+ unreachable ;
1050
1049
}
1051
1050
1052
1051
// Debug library functions
@@ -1325,24 +1324,25 @@ pub const Lua = struct {
1325
1324
};
1326
1325
const ret = c .luaL_loadfilex (lua .state , file_name , mode_str );
1327
1326
switch (ret ) {
1328
- Status .ok = > return ,
1329
- Status .err_syntax = > return Error .Syntax ,
1330
- Status .err_memory = > return Error .Memory ,
1331
- Status . err_file = > return Error .File ,
1327
+ StatusCode .ok = > return ,
1328
+ StatusCode .err_syntax = > return Error .Syntax ,
1329
+ StatusCode .err_memory = > return Error .Memory ,
1330
+ err_file = > return Error .File ,
1332
1331
// NOTE: the docs mention possible other return types, but I couldn't figure them out
1333
1332
else = > unreachable ,
1334
1333
}
1335
1334
}
1336
1335
1337
1336
/// Loads a string as a Lua chunk
1338
- /// TODO: investigate what kind of return codes this really can return
1339
1337
pub fn loadString (lua : * Lua , str : [:0 ]const u8 ) ! void {
1340
1338
const ret = c .luaL_loadstring (lua .state , str );
1341
1339
switch (ret ) {
1342
- Status .ok = > return ,
1343
- Status .err_syntax = > return Error .Syntax ,
1344
- Status .err_memory = > return Error .Memory ,
1345
- // NOTE: loadstring calls lua_load which can return more status codes than this?
1340
+ StatusCode .ok = > return ,
1341
+ StatusCode .err_syntax = > return Error .Syntax ,
1342
+ StatusCode .err_memory = > return Error .Memory ,
1343
+ // loadstring runs lua_load which runs pcall, so can also return any result of an pcall error
1344
+ StatusCode .err_runtime = > return Error .Runtime ,
1345
+ StatusCode .err_error = > return Error .MsgHandler ,
1346
1346
else = > unreachable ,
1347
1347
}
1348
1348
}
@@ -1485,8 +1485,6 @@ pub const Lua = struct {
1485
1485
}
1486
1486
1487
1487
// Standard library loading functions
1488
- //
1489
- // TODO: opening libs can run arbitrary Lua code and can throw any error
1490
1488
1491
1489
/// Opens the specified standard library functions
1492
1490
/// Behaves like openLibs, but allows specifying which libraries
@@ -1582,9 +1580,7 @@ pub const Buffer = struct {
1582
1580
1583
1581
/// Adds `byte` to the buffer
1584
1582
pub fn addChar (buf : * Buffer , byte : u8 ) void {
1585
- // the postfix inc/dec expression is not yet supported by translate-c
1586
- // c.luaL_addchar(&buf.b, byte);
1587
- // TODO: revisit this once Zig bug is fixed (perhaps fix bug?)
1583
+ // NOTE: could not be translated by translate-c
1588
1584
var lua_buf = & buf .b ;
1589
1585
if (lua_buf .n >= lua_buf .size ) _ = buf .prepSize (1 );
1590
1586
lua_buf .b [lua_buf .n ] = byte ;
@@ -1674,7 +1670,7 @@ pub inline fn opaqueCast(comptime T: type, ptr: *anyopaque) *T {
1674
1670
1675
1671
pub const ZigFn = fn (lua : * Lua ) i32 ;
1676
1672
pub const ZigHookFn = fn (lua : * Lua , ar : * DebugInfo ) void ;
1677
- pub const ZigContFn = fn (lua : * Lua , status : Lua.StatusType , ctx : KContext ) i32 ;
1673
+ pub const ZigContFn = fn (lua : * Lua , status : Status , ctx : KContext ) i32 ;
1678
1674
pub const ZigReaderFn = fn (lua : * Lua , data : * anyopaque ) ? []const u8 ;
1679
1675
pub const ZigWarnFn = fn (data : ? * anyopaque , msg : []const u8 , to_cont : bool ) void ;
1680
1676
pub const ZigWriterFn = fn (lua : * Lua , buf : []const u8 , data : * anyopaque ) bool ;
@@ -1716,7 +1712,6 @@ fn wrapZigFn(comptime f: ZigFn) CFn {
1716
1712
fn inner (state : ? * LuaState ) callconv (.C ) c_int {
1717
1713
// this is called by Lua, state should never be null
1718
1714
var lua : Lua = .{ .state = state .? };
1719
- // TODO: should we pass a pointer to the lua state? no real reason to
1720
1715
return @call (.{ .modifier = .always_inline }, f , .{& lua });
1721
1716
}
1722
1717
}.inner ;
@@ -1739,7 +1734,7 @@ fn wrapZigContFn(comptime f: ZigContFn) CContFn {
1739
1734
fn inner (state : ? * LuaState , status : c_int , ctx : KContext ) callconv (.C ) c_int {
1740
1735
// this is called by Lua, state should never be null
1741
1736
var lua : Lua = .{ .state = state .? };
1742
- return @call (.{ .modifier = .always_inline }, f , .{ & lua , @intToEnum (Lua . StatusType , status ), ctx });
1737
+ return @call (.{ .modifier = .always_inline }, f , .{ & lua , @intToEnum (Status , status ), ctx });
1743
1738
}
1744
1739
}.inner ;
1745
1740
}
0 commit comments