rilua 0.1.6

Lua 5.1.1 implemented in Rust, targeting the World of Warcraft addon variant.
Documentation
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
//! Centralized platform abstraction layer.
//!
//! Libc FFI bindings that cannot be replaced live here so that consumer
//! modules (`io.rs`, `os.rs`, `execute.rs`) stay platform-agnostic.
//! Platform differences are hidden behind safe wrapper functions with
//! `#[cfg(target_os)]` dispatch.
//!
//! Where possible, C functions are replaced with Rust std equivalents:
//! - `time(NULL)` -> `current_time()` via `SystemTime`
//! - `errno`/`strerror` -> `std::io::Error::last_os_error()` (in consumers)
//! - `mkstemp`/`tmpnam` -> `c_tmpname()` via `File::create_new()`
//! - `isatty` -> `std::io::IsTerminal` (in `rilua.rs` binary)
//!
//! Remaining FFI (locale, broken-down time, FILE* streams) has no Rust
//! std equivalent and must stay as C bindings.
//!
//! Reference: PUC-Rio's `luaconf.h` for the equivalent C approach.

// ---------------------------------------------------------------------------
// Opaque C FILE type
// ---------------------------------------------------------------------------

/// Opaque C `FILE` type -- never instantiated, only used as `*mut`.
pub(crate) enum LibcFile {}

// ---------------------------------------------------------------------------
// Portable C functions (identical on all platforms)
// ---------------------------------------------------------------------------

#[allow(unsafe_code)]
unsafe extern "C" {
    pub(crate) fn fopen(filename: *const u8, mode: *const u8) -> *mut LibcFile;
    pub(crate) fn fclose(file: *mut LibcFile) -> i32;
    pub(crate) fn fflush(file: *mut LibcFile) -> i32;
    pub(crate) fn fread(ptr: *mut u8, size: usize, nmemb: usize, stream: *mut LibcFile) -> usize;
    pub(crate) fn fwrite(ptr: *const u8, size: usize, nmemb: usize, stream: *mut LibcFile)
    -> usize;
    pub(crate) fn fgets(s: *mut u8, n: i32, stream: *mut LibcFile) -> *mut u8;
    pub(crate) fn fseek(stream: *mut LibcFile, offset: i64, whence: i32) -> i32;
    pub(crate) fn ftell(stream: *mut LibcFile) -> i64;
    pub(crate) fn ferror(stream: *mut LibcFile) -> i32;
    pub(crate) fn clearerr(stream: *mut LibcFile);
    #[allow(dead_code)]
    pub(crate) fn feof(stream: *mut LibcFile) -> i32;
    pub(crate) fn getc(stream: *mut LibcFile) -> i32;
    pub(crate) fn ungetc(c: i32, stream: *mut LibcFile) -> i32;
    pub(crate) fn setvbuf(stream: *mut LibcFile, buf: *mut u8, mode: i32, size: usize) -> i32;
    pub(crate) fn tmpfile() -> *mut LibcFile;
    pub(crate) fn fscanf(stream: *mut LibcFile, format: *const u8, ...) -> i32;
    pub(crate) fn fprintf(stream: *mut LibcFile, format: *const u8, ...) -> i32;
    pub(crate) fn strlen(s: *const u8) -> usize;

    // Number parsing / locale
    pub(crate) fn strtod(nptr: *const u8, endptr: *mut *mut u8) -> f64;
    pub(crate) fn localeconv() -> *const LConv;
    pub(crate) fn strcoll(s1: *const u8, s2: *const u8) -> i32;

    // Time functions (clock, mktime, strftime, setlocale still need FFI;
    // time(NULL) replaced by current_time() using SystemTime)
    pub(crate) fn clock() -> ClockT;
    pub(crate) fn mktime(tm: *mut Tm) -> TimeT;
    pub(crate) fn strftime(s: *mut u8, max: usize, format: *const u8, tm: *const Tm) -> usize;
    pub(crate) fn setlocale(category: i32, locale: *const u8) -> *const u8;
}

// ---------------------------------------------------------------------------
// Minimal struct lconv -- only decimal_point is needed
// ---------------------------------------------------------------------------

/// Minimal `struct lconv` -- we only need the `decimal_point` field.
#[repr(C)]
pub(crate) struct LConv {
    pub(crate) decimal_point: *const u8,
    // remaining fields omitted
}

// ---------------------------------------------------------------------------
// Time types
// ---------------------------------------------------------------------------

/// C `time_t` -- signed 64-bit on modern Linux/macOS, also on 64-bit Windows.
pub(crate) type TimeT = i64;

/// C `clock_t` type.
#[cfg(not(target_os = "windows"))]
pub(crate) type ClockT = i64;

#[cfg(target_os = "windows")]
pub(crate) type ClockT = i32;

/// `CLOCKS_PER_SEC`: 1_000_000 on POSIX, 1_000 on Windows.
#[cfg(not(target_os = "windows"))]
pub(crate) const CLOCKS_PER_SEC: f64 = 1_000_000.0;

#[cfg(target_os = "windows")]
pub(crate) const CLOCKS_PER_SEC: f64 = 1_000.0;

// ---------------------------------------------------------------------------
// struct tm
// ---------------------------------------------------------------------------

/// C `struct tm` for broken-down time.
///
/// Field names mirror the C `struct tm` convention (`tm_sec`, `tm_min`, etc.)
/// to maintain clarity about the FFI mapping.
#[repr(C)]
#[allow(clippy::struct_field_names)]
pub(crate) struct Tm {
    pub(crate) tm_sec: i32,
    pub(crate) tm_min: i32,
    pub(crate) tm_hour: i32,
    pub(crate) tm_mday: i32,
    pub(crate) tm_mon: i32,
    pub(crate) tm_year: i32,
    pub(crate) tm_wday: i32,
    pub(crate) tm_yday: i32,
    pub(crate) tm_isdst: i32,
    // glibc/BSD extensions (must be present for correct struct size on
    // Linux and macOS):
    #[cfg(not(target_os = "windows"))]
    tm_gmtoff: i64,
    #[cfg(not(target_os = "windows"))]
    tm_zone: *const i8,
}

// On Windows (no extra fields) clippy says this is derivable, but on
// Linux/macOS the *const i8 field prevents #[derive(Default)].
#[allow(clippy::derivable_impls)]
impl Default for Tm {
    fn default() -> Self {
        Self {
            tm_sec: 0,
            tm_min: 0,
            tm_hour: 0,
            tm_mday: 0,
            tm_mon: 0,
            tm_year: 0,
            tm_wday: 0,
            tm_yday: 0,
            tm_isdst: 0,
            #[cfg(not(target_os = "windows"))]
            tm_gmtoff: 0,
            #[cfg(not(target_os = "windows"))]
            tm_zone: std::ptr::null(),
        }
    }
}

// ---------------------------------------------------------------------------
// Current time (pure Rust replacement for time(NULL))
// ---------------------------------------------------------------------------

/// Returns the current time as seconds since the Unix epoch.
///
/// Equivalent to C's `time(NULL)`. Uses `SystemTime` to avoid FFI.
pub(crate) fn current_time() -> TimeT {
    use std::time::{SystemTime, UNIX_EPOCH};
    // SystemTime::now() can theoretically be before UNIX_EPOCH on misconfigured
    // systems; fall back to 0 (same as what time(NULL) would do on error: -1,
    // but 0 is safer for Lua's os.time()).
    #[allow(clippy::cast_possible_wrap)]
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(d) => d.as_secs() as TimeT,
        Err(_) => 0,
    }
}

// ---------------------------------------------------------------------------
// Standard streams (stdin, stdout, stderr)
// ---------------------------------------------------------------------------

#[cfg(target_os = "linux")]
#[allow(unsafe_code)]
unsafe extern "C" {
    static stdin: *mut LibcFile;
    static stdout: *mut LibcFile;
    static stderr: *mut LibcFile;
}

#[cfg(target_os = "macos")]
#[allow(unsafe_code)]
unsafe extern "C" {
    #[link_name = "__stdinp"]
    static stdin: *mut LibcFile;
    #[link_name = "__stdoutp"]
    static stdout: *mut LibcFile;
    #[link_name = "__stderrp"]
    static stderr: *mut LibcFile;
}

#[cfg(target_os = "windows")]
#[allow(unsafe_code)]
unsafe extern "C" {
    fn __acrt_iob_func(index: u32) -> *mut LibcFile;
}

/// Returns a pointer to C `stdin`.
#[allow(unsafe_code)]
pub(crate) fn c_stdin() -> *mut LibcFile {
    unsafe {
        #[cfg(not(target_os = "windows"))]
        {
            stdin
        }
        #[cfg(target_os = "windows")]
        {
            __acrt_iob_func(0)
        }
    }
}

/// Returns a pointer to C `stdout`.
#[allow(unsafe_code)]
pub(crate) fn c_stdout() -> *mut LibcFile {
    unsafe {
        #[cfg(not(target_os = "windows"))]
        {
            stdout
        }
        #[cfg(target_os = "windows")]
        {
            __acrt_iob_func(1)
        }
    }
}

/// Returns a pointer to C `stderr`.
#[allow(unsafe_code)]
pub(crate) fn c_stderr() -> *mut LibcFile {
    unsafe {
        #[cfg(not(target_os = "windows"))]
        {
            stderr
        }
        #[cfg(target_os = "windows")]
        {
            __acrt_iob_func(2)
        }
    }
}

// ---------------------------------------------------------------------------
// popen / pclose
// ---------------------------------------------------------------------------

#[cfg(not(target_os = "windows"))]
#[allow(unsafe_code)]
unsafe extern "C" {
    fn popen(command: *const u8, r#type: *const u8) -> *mut LibcFile;
    fn pclose(stream: *mut LibcFile) -> i32;
}

#[cfg(target_os = "windows")]
#[allow(unsafe_code)]
unsafe extern "C" {
    #[link_name = "_popen"]
    fn popen(command: *const u8, r#type: *const u8) -> *mut LibcFile;
    #[link_name = "_pclose"]
    fn pclose(stream: *mut LibcFile) -> i32;
}

/// Opens a pipe to a command (POSIX `popen` / Windows `_popen`).
#[allow(unsafe_code)]
pub(crate) fn c_popen(command: *const u8, mode: *const u8) -> *mut LibcFile {
    unsafe { popen(command, mode) }
}

/// Closes a pipe opened with `c_popen`.
#[allow(unsafe_code)]
pub(crate) fn c_pclose(stream: *mut LibcFile) -> i32 {
    unsafe { pclose(stream) }
}

// ---------------------------------------------------------------------------
// localtime / gmtime (thread-safe variants)
// ---------------------------------------------------------------------------

#[cfg(not(target_os = "windows"))]
#[allow(unsafe_code)]
unsafe extern "C" {
    fn localtime_r(timep: *const TimeT, result: *mut Tm) -> *mut Tm;
    fn gmtime_r(timep: *const TimeT, result: *mut Tm) -> *mut Tm;
}

#[cfg(target_os = "windows")]
#[allow(unsafe_code)]
unsafe extern "C" {
    // Windows reverses parameter order and returns errno_t.
    fn localtime_s(result: *mut Tm, timep: *const TimeT) -> i32;
    fn gmtime_s(result: *mut Tm, timep: *const TimeT) -> i32;
}

/// Thread-safe `localtime_r` / `localtime_s`. Returns `true` on success.
#[allow(unsafe_code)]
pub(crate) fn c_localtime(timep: *const TimeT, result: *mut Tm) -> bool {
    unsafe {
        #[cfg(not(target_os = "windows"))]
        {
            !localtime_r(timep, result).is_null()
        }
        #[cfg(target_os = "windows")]
        {
            localtime_s(result, timep) == 0
        }
    }
}

/// Thread-safe `gmtime_r` / `gmtime_s`. Returns `true` on success.
#[allow(unsafe_code)]
pub(crate) fn c_gmtime(timep: *const TimeT, result: *mut Tm) -> bool {
    unsafe {
        #[cfg(not(target_os = "windows"))]
        {
            !gmtime_r(timep, result).is_null()
        }
        #[cfg(target_os = "windows")]
        {
            gmtime_s(result, timep) == 0
        }
    }
}

// ---------------------------------------------------------------------------
// tmpname (pure Rust, no FFI)
// ---------------------------------------------------------------------------

/// Creates a temporary filename using Rust std.
///
/// Uses `std::env::temp_dir()` for the platform-appropriate temp directory
/// and `File::create_new()` for atomic creation. Retries with different
/// names on collision, similar to how mkstemp works.
///
/// Returns the filename bytes on success, or `None` on failure.
pub(crate) fn c_tmpname() -> Option<Vec<u8>> {
    use std::fs::File;
    use std::sync::atomic::{AtomicU32, Ordering};

    static COUNTER: AtomicU32 = AtomicU32::new(0);

    let tmp_dir = std::env::temp_dir();
    let pid = std::process::id();

    // Try a few times in case of collision (unlikely but possible).
    for _ in 0..16 {
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let name = format!("lua_{pid}_{n}");
        let path = tmp_dir.join(&name);
        if File::create_new(&path).is_ok() {
            // File created and immediately dropped (closed).
            // Return the path as bytes.
            return path.to_str().map(|s| s.as_bytes().to_vec());
        }
    }
    None
}

// ---------------------------------------------------------------------------
// Locale category constants
// ---------------------------------------------------------------------------

/// Locale category constants for `setlocale`.
pub(crate) mod locale {
    // Linux/macOS (glibc/BSD) values.
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_ALL: i32 = 6;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_COLLATE: i32 = 3;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_CTYPE: i32 = 0;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_MONETARY: i32 = 4;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_NUMERIC: i32 = 1;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const LC_TIME: i32 = 2;

    // MSVCRT values.
    #[cfg(target_os = "windows")]
    pub(crate) const LC_ALL: i32 = 0;
    #[cfg(target_os = "windows")]
    pub(crate) const LC_COLLATE: i32 = 1;
    #[cfg(target_os = "windows")]
    pub(crate) const LC_CTYPE: i32 = 2;
    #[cfg(target_os = "windows")]
    pub(crate) const LC_MONETARY: i32 = 3;
    #[cfg(target_os = "windows")]
    pub(crate) const LC_NUMERIC: i32 = 4;
    #[cfg(target_os = "windows")]
    pub(crate) const LC_TIME: i32 = 5;
}

// ---------------------------------------------------------------------------
// setvbuf mode constants
// ---------------------------------------------------------------------------

/// Buffer mode constants for `setvbuf`.
pub(crate) mod bufmode {
    #[cfg(not(target_os = "windows"))]
    pub(crate) const IONBF: i32 = 2;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const IOFBF: i32 = 0;
    #[cfg(not(target_os = "windows"))]
    pub(crate) const IOLBF: i32 = 1;

    #[cfg(target_os = "windows")]
    pub(crate) const IONBF: i32 = 0x0004;
    #[cfg(target_os = "windows")]
    pub(crate) const IOFBF: i32 = 0x0000;
    #[cfg(target_os = "windows")]
    pub(crate) const IOLBF: i32 = 0x0040;
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic, unsafe_code)]
mod tests {
    use super::*;

    #[test]
    fn current_time_reasonable() {
        let t = current_time();
        // Should be after 2024-01-01 (timestamp 1704067200).
        assert!(t > 1_704_067_200);
    }

    #[test]
    fn std_streams_not_null() {
        assert!(!c_stdin().is_null());
        assert!(!c_stdout().is_null());
        assert!(!c_stderr().is_null());
    }

    #[test]
    fn tmpname_succeeds() {
        let name = c_tmpname().expect("c_tmpname should succeed");
        assert!(!name.is_empty());
        // Clean up the temp file.
        let name_str = std::str::from_utf8(&name).expect("valid utf8");
        let _ = std::fs::remove_file(name_str);
    }

    #[test]
    fn tm_default_zeroed() {
        let tm = Tm::default();
        assert_eq!(tm.tm_sec, 0);
        assert_eq!(tm.tm_min, 0);
        assert_eq!(tm.tm_hour, 0);
        assert_eq!(tm.tm_mday, 0);
        assert_eq!(tm.tm_mon, 0);
        assert_eq!(tm.tm_year, 0);
        assert_eq!(tm.tm_wday, 0);
        assert_eq!(tm.tm_yday, 0);
        assert_eq!(tm.tm_isdst, 0);
    }

    #[test]
    fn c_localtime_roundtrip() {
        let t: TimeT = 1_000_000_000; // 2001-09-09 01:46:40 UTC
        let mut tm = Tm::default();
        assert!(c_localtime(&raw const t, &raw mut tm));
        let t2 = unsafe { mktime(&raw mut tm) };
        assert_eq!(t, t2);
    }

    #[test]
    fn c_gmtime_epoch() {
        let t: TimeT = 0; // 1970-01-01 00:00:00 UTC
        let mut tm = Tm::default();
        assert!(c_gmtime(&raw const t, &raw mut tm));
        assert_eq!(tm.tm_year, 70);
        assert_eq!(tm.tm_mon, 0);
        assert_eq!(tm.tm_mday, 1);
        assert_eq!(tm.tm_hour, 0);
        assert_eq!(tm.tm_min, 0);
        assert_eq!(tm.tm_sec, 0);
    }

    #[test]
    fn locale_constants_valid() {
        // Verify constants are distinct (platform-specific values).
        let cats = [
            locale::LC_ALL,
            locale::LC_COLLATE,
            locale::LC_CTYPE,
            locale::LC_MONETARY,
            locale::LC_NUMERIC,
            locale::LC_TIME,
        ];
        // All must be non-negative.
        for &c in &cats {
            assert!(c >= 0);
        }
    }

    #[test]
    fn bufmode_constants_distinct() {
        assert_ne!(bufmode::IONBF, bufmode::IOFBF);
        assert_ne!(bufmode::IONBF, bufmode::IOLBF);
        assert_ne!(bufmode::IOFBF, bufmode::IOLBF);
    }

    #[cfg(not(target_os = "windows"))]
    #[test]
    fn popen_echo() {
        let cmd = b"echo hello\0";
        let mode = b"r\0";
        let fp = c_popen(cmd.as_ptr(), mode.as_ptr());
        assert!(!fp.is_null());
        let mut buf = [0u8; 64];
        let n = unsafe { fread(buf.as_mut_ptr(), 1, buf.len(), fp) };
        assert!(n > 0);
        assert!(
            std::str::from_utf8(&buf[..n])
                .expect("valid utf8")
                .starts_with("hello")
        );
        assert_eq!(c_pclose(fp), 0);
    }
}