Skip to main content

lua_stdlib/
os_lib.rs

1//! Lua `os` standard library.
2//!
3//! Ports `src/loslib.c` (430 lines, 12 functions) to Rust.
4//!
5//! ## Platform access limitations
6//!
7//! Several `os.*` functions require OS-level capabilities. File removal,
8//! rename, command execution, environment lookup, temporary-name generation,
9//! and wall-clock access route through `GlobalState` hooks supplied by the
10//! embedder where needed for sandboxed/WASM hosts.
11//!
12//! Time decomposition (`os.date`, `os.time`) requires C-library functions
13//! (`gmtime_r`, `localtime_r`, `mktime`, `strftime`).  Those call sites are
14//! flagged with `TODO(port)` and the stubs use a zero-initialised `TmFields`.
15
16use lua_types::{LuaError, LuaExit, LuaType, LuaValue};
17use crate::state_stub::{LuaState, LuaStateStubExt as _};
18use lua_vm::state::OsExecuteReason;
19
20// ── Constants ────────────────────────────────────────────────────────────────
21
22//
23// Valid `strftime` conversion specifiers — C99 / POSIX variant.
24// Single-char specifiers appear first; the `||` sentinel signals the start
25// of 2-char specifiers (e.g. `%EC`, `%Oy`).  See `check_strftime_option`.
26const STRFTIME_OPTIONS: &[u8] =
27    b"aAbBcCdDeFgGhHIjmMnprRStTuUVwWxXyYzZ%||EcECExEXEyEYOdOeOHOIOmOMOSOuOUOVOwOWOy";
28
29const SIZE_TIME_FMT: usize = 250;
30
31// ── TmFields ─────────────────────────────────────────────────────────────────
32
33/// Local mirror of C's `struct tm`.
34///
35/// Field conventions follow the C standard: `tm_year` is years since 1900,
36/// `tm_mon` ∈ [0, 11], `tm_wday` ∈ [0, 6] (Sunday = 0), `tm_isdst` is −1 when
37/// DST status is unknown.
38///
39/// TODO(port): In Phase B, replace with the `libc::tm` type (via the `libc` crate)
40/// or an equivalent from `chrono` / `time`.  Conversion from / to Unix timestamps
41/// is not implemented in Phase A — stubs that need a broken-down time use
42/// `TmFields::default()` (all zeros).
43#[derive(Debug, Default, Clone)]
44pub struct TmFields {
45    pub tm_sec: i32,
46    pub tm_min: i32,
47    pub tm_hour: i32,
48    pub tm_mday: i32,
49    pub tm_mon: i32,
50    pub tm_year: i32,
51    pub tm_wday: i32,
52    pub tm_yday: i32,
53    pub tm_isdst: i32,
54}
55
56// ── ByteDisplay ──────────────────────────────────────────────────────────────
57
58/// `Display` adapter for `&[u8]` slices known to contain ASCII bytes.
59///
60/// Used only for formatting Lua table field names (always ASCII identifiers such
61/// as `"year"`, `"month"`) inside error messages, without allocating a `String`.
62struct ByteDisplay<'a>(&'a [u8]);
63
64impl std::fmt::Display for ByteDisplay<'_> {
65    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
66        for &b in self.0 {
67            write!(f, "{}", b as char)?;
68        }
69        Ok(())
70    }
71}
72
73// ── Private stack-manipulation helpers ───────────────────────────────────────
74
75///
76/// Pushes `(value as i64) + (delta as i64)` as a Lua integer, then stores it
77/// in the table currently on top of the stack at field `key`.
78fn set_field(state: &mut LuaState, key: &[u8], value: i32, delta: i32) -> Result<(), LuaError> {
79    state.push(LuaValue::Int((value as i64) + (delta as i64)));
80    state.set_field(-2, key)?;
81    Ok(())
82}
83
84///
85/// Stores a boolean at field `key` in the table on top of the stack.
86/// A negative `value` means "undefined" — the field is silently skipped.
87fn set_bool_field(state: &mut LuaState, key: &[u8], value: i32) -> Result<(), LuaError> {
88    if value < 0 {
89        return Ok(());
90    }
91    state.push(LuaValue::Bool(value != 0));
92    state.set_field(-2, key)?;
93    Ok(())
94}
95
96///
97/// Writes every field of `stm` into the table on top of the stack, applying the
98/// offsets that convert from C-library conventions to Lua conventions:
99/// year+1900, month+1, wday+1, yday+1.
100fn set_all_fields(state: &mut LuaState, stm: &TmFields) -> Result<(), LuaError> {
101    set_field(state, b"year",  stm.tm_year, 1900)?;
102    set_field(state, b"month", stm.tm_mon,  1)?;
103    set_field(state, b"day",   stm.tm_mday, 0)?;
104    set_field(state, b"hour",  stm.tm_hour, 0)?;
105    set_field(state, b"min",   stm.tm_min,  0)?;
106    set_field(state, b"sec",   stm.tm_sec,  0)?;
107    set_field(state, b"yday",  stm.tm_yday, 1)?;
108    set_field(state, b"wday",  stm.tm_wday, 1)?;
109    set_bool_field(state, b"isdst", stm.tm_isdst)?;
110    Ok(())
111}
112
113///
114/// Reads a boolean field from the table on top of the stack.
115/// Returns `-1` when the field is absent (nil), or `0` / `1` for false / true.
116fn get_bool_field(state: &mut LuaState, key: &[u8]) -> Result<i32, LuaError> {
117    let ty = state.get_field(-1, key)?;
118    let res = if matches!(ty, LuaType::Nil) {
119        -1i32
120    } else {
121        state.to_boolean(-1) as i32
122    };
123    state.pop_n(1);
124    Ok(res)
125}
126
127///
128/// Reads an integer field from the table on top of the stack.
129///
130/// * `d` — default when the field is absent; pass `d < 0` to make absence an
131///   error.
132/// * `delta` — subtracted from the read value to convert from Lua's offset
133///   representation back to C-library conventions (e.g. month−1, year−1900).
134///
135/// PORT NOTE: Stack cleanup on error paths (pop before returning Err) is added
136/// vs. the C version where `luaL_error` never returns (longjmp).
137fn get_field(
138    state: &mut LuaState,
139    key: &[u8],
140    d: i32,
141    delta: i32,
142) -> Result<i32, LuaError> {
143    let ty = state.get_field(-1, key)?;
144    let maybe_int = state.to_integer_x(-1);
145    let res: i32 = match maybe_int {
146        Some(res) => {
147            //        return luaL_error(L, "field '%s' is out-of-bound", key);
148            let in_bounds = if res >= 0 {
149                res.saturating_sub(delta as i64) <= (i32::MAX as i64)
150            } else {
151                (i32::MIN as i64).saturating_add(delta as i64) <= res
152            };
153            if !in_bounds {
154                state.pop_n(1);
155                return Err(LuaError::runtime(format_args!(
156                    "field '{}' is out-of-bound",
157                    ByteDisplay(key),
158                )));
159            }
160            (res - delta as i64) as i32
161        }
162        None => {
163            if !matches!(ty, LuaType::Nil) {
164                state.pop_n(1);
165                return Err(LuaError::runtime(format_args!(
166                    "field '{}' is not an integer",
167                    ByteDisplay(key),
168                )));
169            } else if d < 0 {
170                state.pop_n(1);
171                return Err(LuaError::runtime(format_args!(
172                    "field '{}' missing in date table",
173                    ByteDisplay(key),
174                )));
175            }
176            d
177        }
178    };
179    state.pop_n(1);
180    Ok(res)
181}
182
183///                                     ptrdiff_t convlen, char *buff)`
184///
185/// Validates the `strftime` conversion specifier at the start of `conv` against
186/// `STRFTIME_OPTIONS`.
187///
188/// `cc` must have `cc[0] == b'%'` on entry (set by the caller).  On success the
189/// matched specifier bytes are written into `cc[1..=oplen]`, a null terminator is
190/// written at `cc[oplen+1]`, and the sub-slice of `conv` after the consumed
191/// specifier is returned.
192///
193/// On failure a `LuaError::arg_error` describing the invalid specifier is
194/// returned.
195///
196/// The options table uses `|` characters as length-transition markers: one `|`
197/// increments `oplen` from 1 to 2 (and the following advance jumps past the `||`
198/// sentinel), enabling 2-char specifiers like `%EC`.
199fn check_strftime_option<'a>(
200    _state: &mut LuaState,
201    conv: &'a [u8],
202    cc: &mut [u8; 4],
203) -> Result<&'a [u8], LuaError> {
204    let options = STRFTIME_OPTIONS;
205    let mut oplen: usize = 1;
206    let mut i: usize = 0;
207
208    while i < options.len() && oplen <= conv.len() {
209        if options[i] == b'|' {
210            // Increment first so the subsequent `i += oplen` uses the new value,
211            // which jumps from the first `|` past the entire `||` separator block.
212            oplen += 1;
213            i += oplen;
214        } else if i + oplen <= options.len() && conv[..oplen] == options[i..i + oplen] {
215            // cc[0] = b'%' is pre-filled; write specifier bytes into cc[1..=oplen].
216            debug_assert!(oplen <= 2, "STRFTIME_OPTIONS only has 1- and 2-char specifiers");
217            cc[1..=oplen].copy_from_slice(&conv[..oplen]);
218            cc[oplen + 1] = 0;
219            return Ok(&conv[oplen..]);
220        } else {
221            i += oplen;
222        }
223    }
224    Err(LuaError::arg_error(
225        1,
226        "invalid conversion specifier",
227    ))
228}
229
230///
231/// Reads argument `arg` as a Lua integer and returns it as a Unix timestamp.
232///
233/// PORT NOTE: On 64-bit targets `time_t == i64 == lua_Integer`, so the range
234/// check in the C original (`(time_t)t == t`) is always satisfied.
235/// TODO(port): On hypothetical 32-bit `time_t` platforms the check would need
236/// to narrow `t` to `i32` and verify no truncation; flag for Phase B.
237fn check_time(state: &mut LuaState, arg: i32) -> Result<i64, LuaError> {
238    let t = state.check_arg_integer(arg)?;
239    Ok(t)
240}
241
242/// Returns the current Unix timestamp (seconds since 1970-01-01 UTC).
243fn unix_now(state: &LuaState) -> Result<i64, LuaError> {
244    if let Some(now_fn) = state.global().unix_time_hook {
245        return Ok(now_fn());
246    }
247
248    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
249    {
250        let _ = state;
251        return Err(LuaError::runtime(format_args!(
252            "current time not available in this host"
253        )));
254    }
255
256    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
257    {
258        use std::time::{SystemTime, UNIX_EPOCH};
259        Ok(SystemTime::now()
260            .duration_since(UNIX_EPOCH)
261            .map(|d| d.as_secs() as i64)
262            .unwrap_or(0))
263    }
264}
265
266/// Returns the host's local timezone offset (seconds) at instant `t`, such that
267/// the local broken-down time equals `decompose_utc(t + offset)`.
268///
269/// Routes through `GlobalState::local_offset_hook` when the host installs one
270/// (lua-cli does, via `localtime_r`). Absent a hook the offset is 0, so
271/// `os.date`/`os.time` fall back to UTC — matching the prior behaviour and
272/// keeping the round-trip exact under bare WASM.
273fn local_offset(state: &LuaState, t: i64) -> i64 {
274    match state.global().local_offset_hook {
275        Some(off_fn) => off_fn(t),
276        None => 0,
277    }
278}
279
280fn native_temp_name() -> Result<Vec<u8>, LuaError> {
281    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
282    {
283        return Err(LuaError::runtime(format_args!(
284            "temporary filenames not available in this host"
285        )));
286    }
287
288    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
289    {
290        use std::sync::atomic::{AtomicU64, Ordering};
291        use std::time::{SystemTime, UNIX_EPOCH};
292
293        static COUNTER: AtomicU64 = AtomicU64::new(0);
294
295        let mut dir: Vec<u8> = {
296            let path = std::env::temp_dir();
297            #[cfg(unix)]
298            {
299                use std::os::unix::ffi::OsStrExt;
300                path.as_os_str().as_bytes().to_vec()
301            }
302            #[cfg(not(unix))]
303            {
304                path.to_string_lossy().as_bytes().to_vec()
305            }
306        };
307        if dir.last().copied() != Some(b'/') && dir.last().copied() != Some(b'\\') {
308            dir.push(b'/');
309        }
310
311        let nanos = SystemTime::now()
312            .duration_since(UNIX_EPOCH)
313            .map(|d| d.as_nanos())
314            .unwrap_or(0);
315        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
316
317        let suffix = format!("lua_{:x}_{:x}_{:x}", std::process::id(), nanos, n);
318        dir.extend_from_slice(suffix.as_bytes());
319        Ok(dir)
320    }
321}
322
323fn host_temp_name(state: &LuaState) -> Result<Vec<u8>, LuaError> {
324    match state.global().temp_name_hook {
325        Some(temp_fn) => temp_fn(),
326        None => native_temp_name(),
327    }
328}
329
330/// Decompose a Unix timestamp (UTC) into broken-down time fields.
331///
332/// Uses Howard Hinnant's `civil_from_days` algorithm (public domain, see
333/// <http://howardhinnant.github.io/date_algorithms.html#civil_from_days>),
334/// which is exact for all `i64` inputs across the proleptic Gregorian calendar.
335///
336/// PORT NOTE: C uses `gmtime_r(&t, &tmr)`.  Pure-Rust replacement because the
337/// crate forbids `unsafe` (required for libc FFI).  `tm_isdst` is always 0 for
338/// UTC.  `tm_wday` is 0-based with Sunday = 0 (matches POSIX).  `tm_yday` is
339/// 0-based (matches POSIX; `set_all_fields` adds 1 for the Lua-visible table).
340fn decompose_utc(t: i64) -> TmFields {
341    let days = t.div_euclid(86_400);
342    let sod = t.rem_euclid(86_400) as i32;
343
344    let tm_hour = sod / 3600;
345    let tm_min = (sod / 60) % 60;
346    let tm_sec = sod % 60;
347
348    let z = days + 719_468;
349    let era = (if z >= 0 { z } else { z - 146_096 }).div_euclid(146_097);
350    let doe = z - era * 146_097;
351    let yoe = (doe - doe / 1460 + doe / 36_524 - doe / 146_096) / 365;
352    let y = yoe + era * 400;
353    let doy_mar = doe - (365 * yoe + yoe / 4 - yoe / 100);
354    let mp = (5 * doy_mar + 2) / 153;
355    let day = (doy_mar - (153 * mp + 2) / 5 + 1) as i32;
356    let month: i32 = if mp < 10 { (mp + 3) as i32 } else { (mp - 9) as i32 };
357    let year = y + if month <= 2 { 1 } else { 0 };
358
359    let leap = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
360    const DAYS_BEFORE_MONTH: [i32; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
361    let tm_yday = DAYS_BEFORE_MONTH[(month - 1) as usize]
362        + (day - 1)
363        + if leap && month > 2 { 1 } else { 0 };
364
365    let tm_wday = (days + 4).rem_euclid(7) as i32;
366
367    TmFields {
368        tm_sec,
369        tm_min,
370        tm_hour,
371        tm_mday: day,
372        tm_mon: month - 1,
373        tm_year: (year - 1900) as i32,
374        tm_wday,
375        tm_yday,
376        tm_isdst: 0,
377    }
378}
379
380/// Compose a UTC Unix timestamp from broken-down time fields.
381///
382/// Inverse of `decompose_utc`.  Uses Howard Hinnant's `days_from_civil` and
383/// normalises month overflow into the year (matching `mktime`'s behaviour for
384/// the year/month axes).  Day-of-month, hour, minute, and second components
385/// are added linearly so out-of-range values normalise carry into the larger
386/// units exactly as `mktime` would for UTC.
387fn compose_utc(tm: &TmFields) -> i64 {
388    let mut y: i64 = (tm.tm_year as i64) + 1900;
389    let mut m: i64 = (tm.tm_mon as i64) + 1;
390    let dy = (m - 1).div_euclid(12);
391    y += dy;
392    m -= dy * 12;
393    let y_adj = if m <= 2 { y - 1 } else { y };
394    let era = (if y_adj >= 0 { y_adj } else { y_adj - 399 }).div_euclid(400);
395    let yoe = y_adj - era * 400;
396    let doy = (153 * (if m > 2 { m - 3 } else { m + 9 }) + 2) / 5 + (tm.tm_mday as i64) - 1;
397    let doe = yoe * 365 + yoe / 4 - yoe / 100 + doy;
398    let days = era * 146_097 + doe - 719_468;
399    days * 86_400 + (tm.tm_hour as i64) * 3600 + (tm.tm_min as i64) * 60 + (tm.tm_sec as i64)
400}
401
402/// Append the formatted result of a single `strftime` conversion specifier.
403///
404/// `cc` holds the canonical specifier bytes filled in by `check_strftime_option`:
405/// `cc[0] == b'%'`, `cc[1]` is the leading specifier char, and for 2-char
406/// specifiers `cc[2]` is the second char (an E/O modifier comes first in C, e.g.
407/// `%Ex` → `cc = "%Ex\0"`).  `oplen` is 1 or 2.
408///
409/// PORT NOTE: C delegates to the platform `strftime`.  Pure-Rust replacement for
410/// the same reason as `decompose_utc`.  The E/O modifiers are stripped (POSIX
411/// allows the implementation to ignore them and fall back to the unmodified
412/// form) — the test suite only requires that they not error.
413fn strftime_one(buf: &mut Vec<u8>, cc: &[u8; 4], oplen: usize, tm: &TmFields) {
414    use std::io::Write as _;
415    let spec = if oplen == 2 { cc[2] } else { cc[1] };
416    let year_full = (tm.tm_year as i64) + 1900;
417    let hour12 = {
418        let h = tm.tm_hour.rem_euclid(12);
419        if h == 0 { 12 } else { h }
420    };
421    const DAY_SHORT: [&[u8]; 7] = [b"Sun", b"Mon", b"Tue", b"Wed", b"Thu", b"Fri", b"Sat"];
422    const DAY_LONG: [&[u8]; 7] = [
423        b"Sunday", b"Monday", b"Tuesday", b"Wednesday", b"Thursday", b"Friday", b"Saturday",
424    ];
425    const MON_SHORT: [&[u8]; 12] = [
426        b"Jan", b"Feb", b"Mar", b"Apr", b"May", b"Jun", b"Jul", b"Aug", b"Sep", b"Oct", b"Nov",
427        b"Dec",
428    ];
429    const MON_LONG: [&[u8]; 12] = [
430        b"January", b"February", b"March", b"April", b"May", b"June", b"July", b"August",
431        b"September", b"October", b"November", b"December",
432    ];
433    let wday_idx = tm.tm_wday.rem_euclid(7) as usize;
434    let mon_idx = tm.tm_mon.rem_euclid(12) as usize;
435    match spec {
436        b'Y' => { let _ = write!(buf, "{}", year_full); }
437        b'y' => { let _ = write!(buf, "{:02}", year_full.rem_euclid(100)); }
438        b'C' => { let _ = write!(buf, "{:02}", year_full.div_euclid(100)); }
439        b'm' => { let _ = write!(buf, "{:02}", tm.tm_mon + 1); }
440        b'd' => { let _ = write!(buf, "{:02}", tm.tm_mday); }
441        b'e' => { let _ = write!(buf, "{:2}", tm.tm_mday); }
442        b'H' => { let _ = write!(buf, "{:02}", tm.tm_hour); }
443        b'I' => { let _ = write!(buf, "{:02}", hour12); }
444        b'k' => { let _ = write!(buf, "{:2}", tm.tm_hour); }
445        b'l' => { let _ = write!(buf, "{:2}", hour12); }
446        b'M' => { let _ = write!(buf, "{:02}", tm.tm_min); }
447        b'S' => { let _ = write!(buf, "{:02}", tm.tm_sec); }
448        b'w' => { let _ = write!(buf, "{}", tm.tm_wday); }
449        b'u' => {
450            let u = if tm.tm_wday == 0 { 7 } else { tm.tm_wday };
451            let _ = write!(buf, "{}", u);
452        }
453        b'j' => { let _ = write!(buf, "{:03}", tm.tm_yday + 1); }
454        b'a' => buf.extend_from_slice(DAY_SHORT[wday_idx]),
455        b'A' => buf.extend_from_slice(DAY_LONG[wday_idx]),
456        b'b' | b'h' => buf.extend_from_slice(MON_SHORT[mon_idx]),
457        b'B' => buf.extend_from_slice(MON_LONG[mon_idx]),
458        b'p' => buf.extend_from_slice(if tm.tm_hour < 12 { b"AM" } else { b"PM" }),
459        b'P' => buf.extend_from_slice(if tm.tm_hour < 12 { b"am" } else { b"pm" }),
460        b'D' | b'x' => {
461            let _ = write!(buf, "{:02}/{:02}/{:02}", tm.tm_mon + 1, tm.tm_mday, year_full.rem_euclid(100));
462        }
463        b'F' => {
464            let _ = write!(buf, "{}-{:02}-{:02}", year_full, tm.tm_mon + 1, tm.tm_mday);
465        }
466        b'T' | b'X' => {
467            let _ = write!(buf, "{:02}:{:02}:{:02}", tm.tm_hour, tm.tm_min, tm.tm_sec);
468        }
469        b'R' => { let _ = write!(buf, "{:02}:{:02}", tm.tm_hour, tm.tm_min); }
470        b'r' => {
471            let ampm: &[u8] = if tm.tm_hour < 12 { b"AM" } else { b"PM" };
472            let _ = write!(buf, "{:02}:{:02}:{:02} ", hour12, tm.tm_min, tm.tm_sec);
473            buf.extend_from_slice(ampm);
474        }
475        b'c' => {
476            let _ = write!(
477                buf,
478                "{} {} {:2} {:02}:{:02}:{:02} {}",
479                std::str::from_utf8(DAY_SHORT[wday_idx]).unwrap_or(""),
480                std::str::from_utf8(MON_SHORT[mon_idx]).unwrap_or(""),
481                tm.tm_mday,
482                tm.tm_hour,
483                tm.tm_min,
484                tm.tm_sec,
485                year_full,
486            );
487        }
488        b'n' => buf.push(b'\n'),
489        b't' => buf.push(b'\t'),
490        b'%' => buf.push(b'%'),
491        b'z' => buf.extend_from_slice(b"+0000"),
492        b'Z' => buf.extend_from_slice(b"UTC"),
493        b's' => { let _ = write!(buf, "{}", compose_utc(tm)); }
494        b'U' => {
495            let week = (tm.tm_yday + 7 - tm.tm_wday) / 7;
496            let _ = write!(buf, "{:02}", week);
497        }
498        b'W' => {
499            let mwday = if tm.tm_wday == 0 { 6 } else { tm.tm_wday - 1 };
500            let week = (tm.tm_yday + 7 - mwday) / 7;
501            let _ = write!(buf, "{:02}", week);
502        }
503        b'V' | b'g' | b'G' => {
504            let _ = write!(buf, "{:02}", 1);
505        }
506        _ => {}
507    }
508}
509
510// ── Library functions ─────────────────────────────────────────────────────────
511
512///
513/// Executes a shell command via the system shell.
514///
515/// Without arguments: tests whether a shell is available — returns `true`
516/// when an `os_execute_hook` is installed (we always have `sh` in that case),
517/// `false` otherwise.
518///
519/// With a command string: dispatches through `os_execute_hook` and pushes the
520/// three C-Lua return values `(boolean|nil, "exit"|"signal", int)` as defined
521/// by `luaL_execresult`.  Returns the stub `nil, errmsg, -1` triple when no
522/// hook is installed.
523pub(crate) fn os_execute(state: &mut LuaState) -> Result<usize, LuaError> {
524    let cmd = state.opt_arg_lstring(1, None)?;
525    match cmd {
526        None => {
527            // We have a shell if and only if the embedder installed a hook.
528            let has_shell = state.global().os_execute_hook.is_some();
529            state.push(LuaValue::Bool(has_shell));
530            Ok(1)
531        }
532        Some(cmd_bytes) => {
533            let hook = state.global().os_execute_hook;
534            match hook {
535                Some(execute_fn) => {
536                    // Clone to avoid holding a borrow across the hook call.
537                    let cmd_owned: Vec<u8> = cmd_bytes.to_vec();
538                    match execute_fn(&cmd_owned) {
539                        Ok(result) => {
540                            if result.success {
541                                state.push(LuaValue::Bool(true));
542                            } else {
543                                state.push(LuaValue::Nil);
544                            }
545                            let reason_str: &[u8] = match result.reason {
546                                OsExecuteReason::Exit => b"exit",
547                                OsExecuteReason::Signal => b"signal",
548                            };
549                            state.push_string(reason_str)?;
550                            state.push(LuaValue::Int(result.code as i64));
551                            Ok(3)
552                        }
553                        Err(e) => {
554                            state.push(LuaValue::Nil);
555                            let msg = match &e {
556                                LuaError::Runtime(LuaValue::Str(s)) => s.as_bytes().to_vec(),
557                                other => format!("{:?}", other).into_bytes(),
558                            };
559                            let s = state.intern_str(&msg)?;
560                            state.push(LuaValue::Str(s));
561                            state.push(LuaValue::Int(-1));
562                            Ok(3)
563                        }
564                    }
565                }
566                None => {
567                    state.push(LuaValue::Nil);
568                    state.push_string(b"os.execute: not implemented in lua-stdlib")?;
569                    state.push(LuaValue::Int(-1));
570                    Ok(3)
571                }
572            }
573        }
574    }
575}
576
577///
578/// Removes the file or empty directory at the given path.
579/// Returns `true` on success, or `nil, errmsg` on failure.
580pub(crate) fn os_remove(state: &mut LuaState) -> Result<usize, LuaError> {
581    let filename: Vec<u8> = state.check_arg_string(1)?.to_vec();
582    // `std::fs` is banned in lua-stdlib; delegate to the embedder hook.
583    let hook = state.global().file_remove_hook;
584    match hook {
585        Some(remove_fn) => match remove_fn(&filename) {
586            Ok(()) => {
587                state.push(LuaValue::Bool(true));
588                Ok(1)
589            }
590            Err(e) => {
591                state.push(LuaValue::Nil);
592                let msg = match &e {
593                    LuaError::Runtime(LuaValue::Str(s)) => s.as_bytes().to_vec(),
594                    other => format!("{:?}", other).into_bytes(),
595                };
596                let s = state.intern_str(&msg)?;
597                state.push(LuaValue::Str(s));
598                Ok(2)
599            }
600        },
601        None => {
602            state.push(LuaValue::Nil);
603            state.push_string(b"os.remove: no filesystem hook registered")?;
604            Ok(2)
605        }
606    }
607}
608
609///
610/// Renames (moves) a file from the first path to the second.
611/// Returns `true` on success, or `nil, errmsg` on failure.
612pub(crate) fn os_rename(state: &mut LuaState) -> Result<usize, LuaError> {
613    let fromname: Vec<u8> = state.check_arg_string(1)?.to_vec();
614    let toname: Vec<u8> = state.check_arg_string(2)?.to_vec();
615    // `std::fs` is banned in lua-stdlib; delegate to the embedder hook.
616    let hook = state.global().file_rename_hook;
617    match hook {
618        Some(rename_fn) => match rename_fn(&fromname, &toname) {
619            Ok(()) => {
620                state.push(LuaValue::Bool(true));
621                return Ok(1);
622            }
623            Err(e) => {
624                state.push(LuaValue::Nil);
625                let msg = match &e {
626                    LuaError::Runtime(LuaValue::Str(s)) => s.as_bytes().to_vec(),
627                    other => format!("{:?}", other).into_bytes(),
628                };
629                let s = state.intern_str(&msg)?;
630                state.push(LuaValue::Str(s));
631                return Ok(2);
632            }
633        },
634        None => {}
635    }
636    state.push(LuaValue::Nil);
637    state.push_string(b"os.rename: no filesystem hook registered")?;
638    Ok(2)
639}
640
641///
642/// Generates a unique temporary file name and pushes it as a string.
643/// Raises a runtime error if generation fails.
644///
645/// PORT NOTE: Temporary names are host capability. Native hosts can install
646/// `GlobalState::temp_name_hook`; bare WASM without that hook raises a Lua
647/// error instead of touching `std::env` / `std::time` stubs.
648pub(crate) fn os_tmpname(state: &mut LuaState) -> Result<usize, LuaError> {
649    let dir = host_temp_name(state)?;
650    state.push_string(&dir)?;
651    Ok(1)
652}
653
654///
655/// Reads the environment variable named by the first argument and pushes its
656/// value as a string, or `nil` if the variable is not set.
657pub(crate) fn os_getenv(state: &mut LuaState) -> Result<usize, LuaError> {
658    let name_bytes: Vec<u8> = state.check_arg_string(1)?.to_vec();
659
660    let result: Option<Vec<u8>> = match state.global().env_hook {
661        Some(env_fn) => env_fn(&name_bytes),
662        None => {
663            #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
664            {
665                None
666            }
667
668            #[cfg(all(unix, not(all(target_arch = "wasm32", target_os = "unknown"))))]
669            {
670                use std::ffi::OsStr;
671                use std::os::unix::ffi::{OsStrExt, OsStringExt};
672                let os_name = OsStr::from_bytes(&name_bytes);
673                std::env::var_os(os_name).map(|v| v.into_vec())
674            }
675
676            #[cfg(all(not(unix), not(all(target_arch = "wasm32", target_os = "unknown"))))]
677            {
678                // TODO(port): from_utf8 used on Lua string data for OS API interop on
679                // non-Unix platforms.  Ideally replaced with wide-string conversion.
680                match std::str::from_utf8(&name_bytes) {
681                    Ok(name_str) => std::env::var(name_str).ok().map(|v| v.into_bytes()),
682                    Err(_) => None,
683                }
684            }
685        }
686    };
687
688    match result {
689        Some(val) => {
690            state.push_string(&val)?;
691        }
692        None => {
693            state.push(LuaValue::Nil);
694        }
695    }
696    Ok(1)
697}
698
699///
700/// Returns an approximation of the CPU time (in seconds) used by the program.
701pub(crate) fn os_clock(state: &mut LuaState) -> Result<usize, LuaError> {
702    let seconds = cpu_seconds(state)?;
703    state.push(LuaValue::Float(seconds));
704    Ok(1)
705}
706
707/// Returns program CPU time in seconds, as consumed by `os.clock`.
708///
709/// C's `clock()` reads `CLOCK_PROCESS_CPUTIME_ID`, which has no portable `std`
710/// equivalent. We route through `cpu_clock_hook` when the host installs one;
711/// otherwise native builds report monotonic wall time elapsed since the first
712/// call (the substitution wasi-libc and Emscripten make for `clock()`), and bare
713/// WASM reports the clock as unavailable rather than touching a stubbed source.
714fn cpu_seconds(state: &LuaState) -> Result<f64, LuaError> {
715    if let Some(clock_fn) = state.global().cpu_clock_hook {
716        return Ok(clock_fn());
717    }
718
719    #[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
720    {
721        let _ = state;
722        Err(LuaError::runtime(format_args!(
723            "CPU clock not available in this host"
724        )))
725    }
726
727    #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
728    {
729        let _ = state;
730        use std::sync::OnceLock;
731        use std::time::Instant;
732        static START: OnceLock<Instant> = OnceLock::new();
733        Ok(START.get_or_init(Instant::now).elapsed().as_secs_f64())
734    }
735}
736
737///
738/// Formats the current (or a specified) date/time.
739///
740/// * Format starting with `'!'` → use UTC; otherwise local time.
741/// * Format `"*t"` → push a table with broken-down time fields.
742/// * Other format → push a formatted string, expanding `%`-specifiers via
743///   the C-library `strftime`.
744pub(crate) fn os_date(state: &mut LuaState) -> Result<usize, LuaError> {
745    // Clone to Vec<u8> so that `s` does not borrow from `state`.
746    let format: Vec<u8> = state.opt_arg_lstring(1, Some(b"%c"))?.unwrap_or_default();
747    let s: &[u8] = &format[..];
748
749    let t: i64 = if matches!(state.type_at(2), LuaType::None | LuaType::Nil) {
750        unix_now(state)?
751    } else {
752        check_time(state, 2)?
753    };
754
755    let (_use_utc, s): (bool, &[u8]) = if s.first() == Some(&b'!') {
756        (true, &s[1..])
757    } else {
758        (false, s)
759    };
760
761    // PORT NOTE: C distinguishes UTC (`gmtime_r`) from local time (`localtime_r`).
762    // The Rust port reproduces `localtime_r` by decomposing `t + offset`, where
763    // `offset` is the host timezone offset at `t` supplied by the
764    // `local_offset_hook` (lua-cli installs one via `localtime_r`; reading the
765    // timezone database needs `libc` FFI, banned in `lua-stdlib`). Without a hook
766    // the offset is 0 and local time degrades to UTC, keeping the
767    // `os.date`/`os.time` round-trip exact under bare WASM. `'!'`-prefixed formats
768    // request UTC explicitly and skip the offset.
769    let offset = if _use_utc { 0 } else { local_offset(state, t) };
770    let stm = decompose_utc(t + offset);
771
772    //      return luaL_error(L, "date result cannot be represented in this installation");
773    // (Phase A stub is always valid — no null check needed.)
774
775    if s == b"*t" {
776        state.create_table(0, 9)?;
777        set_all_fields(state, &stm)?;
778    } else {
779        let mut result: Vec<u8> = Vec::new();
780        let mut pos: usize = 0;
781
782        while pos < s.len() {
783            if s[pos] != b'%' {
784                result.push(s[pos]);
785                pos += 1;
786            } else {
787                pos += 1;
788                let mut cc = [0u8; 4];
789                cc[0] = b'%';
790                // Pass the remaining slice even if empty: checkoption's loop
791                // condition (oplen <= convlen) fails immediately on an empty
792                // slice, which causes it to raise "invalid conversion specifier"
793                // matching C behaviour for a trailing bare '%'.
794                let conv = &s[pos..];
795                let after = check_strftime_option(state, conv, &mut cc)?;
796                let oplen = conv.len() - after.len();
797                pos += oplen;
798                // The `%%` specifier is data-independent: strftime emits a literal
799                // `%` byte regardless of the broken-down time, so it is correct to
800                // handle here even while the rest of strftime is stubbed.
801                strftime_one(&mut result, &cc, oplen, &stm);
802                let _ = SIZE_TIME_FMT;
803            }
804        }
805        state.push_string(&result)?;
806    }
807    Ok(1)
808}
809
810///
811/// Without arguments: returns the current time as a Unix timestamp (integer).
812/// With a table argument: interprets the table as broken-down local time,
813/// normalises the fields via `mktime`, updates the table in place, and returns
814/// the resulting timestamp.
815pub(crate) fn os_time(state: &mut LuaState) -> Result<usize, LuaError> {
816    let t: i64;
817
818    if matches!(state.type_at(1), LuaType::None | LuaType::Nil) {
819        t = unix_now(state)?;
820    } else {
821        state.check_arg_type(1, LuaType::Table)?;
822        // PORT NOTE: must use the public-API `set_top` (relative to the current
823        // C-frame's `func`), not `LuaState::set_top` which is an inherent that
824        // sets an absolute stack index and would truncate the entire stack.
825        lua_vm::api::set_top(state, 1)?;
826
827        let tm_year  = get_field(state, b"year",  -1, 1900)?;
828        let tm_mon   = get_field(state, b"month", -1, 1)?;
829        let tm_mday  = get_field(state, b"day",   -1, 0)?;
830        let tm_hour  = get_field(state, b"hour",  12, 0)?;
831        let tm_min   = get_field(state, b"min",   0,  0)?;
832        let tm_sec   = get_field(state, b"sec",   0,  0)?;
833        let tm_isdst = get_bool_field(state, b"isdst")?;
834
835        let raw = TmFields {
836            tm_year,
837            tm_mon,
838            tm_mday,
839            tm_hour,
840            tm_min,
841            tm_sec,
842            tm_isdst,
843            ..TmFields::default()
844        };
845
846        // PORT NOTE: C `mktime` interprets the broken-down time as LOCAL and
847        // returns the corresponding UTC timestamp. We reproduce it: treat the
848        // fields as UTC to get a provisional `t_utc` (this also normalises the
849        // month axis), then subtract the host timezone offset to recover the true
850        // UTC instant. The offset is sampled at `t_utc` then re-sampled at the
851        // corrected instant — the standard `mktime` fixed-point step — so the
852        // result is correct except across a DST transition inside the offset
853        // window, which `os.time`'s test inputs do not exercise. Without a hook
854        // the offset is 0 and this is the exact inverse of `os.date`'s local
855        // decomposition, so the `os.time(os.date("*t")) == t` round-trip holds.
856        let t_utc = compose_utc(&raw);
857        let off0 = local_offset(state, t_utc);
858        let off = local_offset(state, t_utc - off0);
859        t = t_utc - off;
860        let stm = decompose_utc(t + off);
861
862        set_all_fields(state, &stm)?;
863    }
864
865    //        return luaL_error(L, "time result cannot be represented in this installation");
866    // PORT NOTE: On 64-bit targets time_t == i64 == lua_Integer so the cast check
867    // is a no-op.  We only guard against mktime's failure sentinel (−1).
868    if t == -1 {
869        return Err(LuaError::runtime(format_args!(
870            "time result cannot be represented in this installation"
871        )));
872    }
873
874    state.push(LuaValue::Int(t));
875    Ok(1)
876}
877
878///
879/// Returns the number of seconds between two time values as a float (`t1 − t2`).
880///
881/// PORT NOTE: C's `difftime(t1, t2)` returns `t1 − t2` as a `double`.  For
882/// 64-bit `time_t` this is exact as `f64` up to approximately 2^53 seconds
883/// (~285 million years), which is sufficient for all practical timestamps.
884pub(crate) fn os_difftime(state: &mut LuaState) -> Result<usize, LuaError> {
885    let t1 = check_time(state, 1)?;
886    let t2 = check_time(state, 2)?;
887    state.push(LuaValue::Float((t1 - t2) as f64));
888    Ok(1)
889}
890
891///
892/// Sets the locale for the given category and pushes the resulting locale name
893/// as a string, or `nil` on failure.
894pub(crate) fn os_setlocale(state: &mut LuaState) -> Result<usize, LuaError> {
895    const CAT_NAMES: &[&[u8]] = &[
896        b"all", b"collate", b"ctype", b"monetary", b"numeric", b"time",
897    ];
898
899    let locale: Option<Vec<u8>> = state.opt_arg_lstring(1, None)?;
900
901    let _op: usize = state.check_arg_option(2, Some(b"all"), CAT_NAMES)?;
902
903    // PORT NOTE: calling libc::setlocale requires unsafe (banned in lua-stdlib, budget=0).
904    // Rust programs inherit the "C" locale by default and never change it, so returning
905    // "C" for the C locale (and nil for anything else) is faithful for this build:
906    // "C" is the only locale guaranteed available on every POSIX system.
907    let result_locale: Option<&[u8]> = match locale.as_deref() {
908        None => Some(b"C"),          // query: return current locale (always "C" here)
909        Some(b"C") | Some(b"POSIX") => Some(b"C"),  // setting to "C"/"POSIX" always succeeds
910        Some(_) => None,             // any other locale: unsupported in this build
911    };
912    match result_locale {
913        Some(s) => { state.push_string(s)?; }
914        None => state.push(LuaValue::Nil),
915    }
916    Ok(1)
917}
918
919///
920/// Exits the host process with the given status code (default `EXIT_SUCCESS = 0`).
921/// If the second argument is true, also closes the Lua state before exiting.
922///
923/// This function is expected to terminate the process and never return normally.
924pub(crate) fn os_exit(state: &mut LuaState) -> Result<usize, LuaError> {
925    //      status = lua_toboolean(L, 1) ? EXIT_SUCCESS : EXIT_FAILURE;
926    //    else
927    //      status = (int)luaL_optinteger(L, 1, EXIT_SUCCESS);
928    let exit_code: i32 = if matches!(state.type_at(1), LuaType::Boolean) {
929        if state.to_boolean(1) { 0 } else { 1 } // EXIT_SUCCESS = 0, EXIT_FAILURE = 1
930    } else {
931        state.opt_arg_integer(1, 0)? as i32
932    };
933
934    if state.to_boolean(2) {
935        state.close();
936    }
937
938    //
939    // `std::process::exit` remains restricted to `lua-cli`. A regular
940    // `LuaError` is also wrong here: Lua `pcall` must not catch `os.exit`.
941    // Use a typed panic payload as internal non-local control flow; the CLI
942    // catches it at the process boundary and converts it to an `ExitCode`.
943    std::panic::panic_any(LuaExit(exit_code));
944}
945
946// ── Registration table and entry point ───────────────────────────────────────
947
948/// Type alias for a Lua native function implementation in Rust.
949///
950/// TODO(port): align with the canonical `lua_CFunction` / `NativeFn` type defined
951/// in `lua-types` once that crate stabilises.
952pub type NativeFn = fn(&mut LuaState) -> Result<usize, LuaError>;
953
954///
955/// Mapping from Lua-visible names to the Rust implementations of each `os.*`
956/// function.
957pub const OS_LIB: &[(&[u8], NativeFn)] = &[
958    (b"clock",     os_clock),
959    (b"date",      os_date),
960    (b"difftime",  os_difftime),
961    (b"execute",   os_execute),
962    (b"exit",      os_exit),
963    (b"getenv",    os_getenv),
964    (b"remove",    os_remove),
965    (b"rename",    os_rename),
966    (b"setlocale", os_setlocale),
967    (b"time",      os_time),
968    (b"tmpname",   os_tmpname),
969];
970
971///
972/// Opens the `os` library: creates a new table populated with `OS_LIB` and
973/// leaves it on the stack.
974///
975/// PORT NOTE: `register_lib` is the Rust equivalent of `luaL_newlib`; it creates
976/// a fresh table, fills it from the `(name, fn)` pair slice, and pushes it.
977pub fn open_os(state: &mut LuaState) -> Result<usize, LuaError> {
978    state.register_lib(b"os", OS_LIB)?;
979    Ok(1)
980}
981
982// ──────────────────────────────────────────────────────────────────────────
983// PORT STATUS
984//   source:        src/loslib.c  (430 lines, 12 functions)
985//   target_crate:  lua-stdlib
986//   confidence:    medium
987//   todos:         18
988//   port_notes:    4
989//   unsafe_blocks: 0
990//   notes:         Logic structure faithful. File/process/env/temp/time
991//                  operations route through host hooks where they need OS
992//                  capabilities for sandboxed and bare-WASM hosts.
993//                  Time formatting (os.date, os.time) needs libc or chrono in
994//                  Phase B.  os.clock routes through cpu_clock_hook with a
995//                  monotonic-wall fallback (no std CPU-time source).
996//                  os.exit needs a LuaError::Exit(i32)
997//                  variant.  check_strftime_option logic is fully translated.
998//                  os_getenv uses OsStr::from_bytes on Unix (no from_utf8).
999// ──────────────────────────────────────────────────────────────────────────