lua_stdlib/init.rs
1//! Initialization of standard libraries for Lua.
2//!
3//! Opens all standard libraries via `require`-style loading and registers
4//! them into the global table.
5//!
6//! Port of `src/linit.c` (66 lines, 1 function).
7
8use crate::state_stub::{LuaState, LuaStateStubExt as _};
9use lua_types::error::LuaError;
10
11// Matches types.tsv: lua_CFunction → fn(&mut LuaState) -> Result<usize, LuaError>
12type LuaCFunction = fn(&mut LuaState) -> Result<usize, LuaError>;
13
14// ── Library-name byte-string constants ────────────────────────────────────
15//
16// These replace the C macros from lualib.h and lauxlib.h:
17// LUA_GNAME = "_G" (lauxlib.h)
18// LUA_LOADLIBNAME = "package" (lualib.h)
19// LUA_COLIBNAME = "coroutine" (lualib.h)
20// LUA_TABLIBNAME = "table" (lualib.h)
21// LUA_IOLIBNAME = "io" (lualib.h)
22// LUA_OSLIBNAME = "os" (lualib.h)
23// LUA_STRLIBNAME = "string" (lualib.h)
24// LUA_MATHLIBNAME = "math" (lualib.h)
25// LUA_UTF8LIBNAME = "utf8" (lualib.h)
26// LUA_DBLIBNAME = "debug" (lualib.h)
27//
28// Per PORTING.md §3.1 all Lua string data uses &[u8], not &str.
29
30// {LUA_GNAME, luaopen_base},
31// {LUA_LOADLIBNAME, luaopen_package},
32// {LUA_COLIBNAME, luaopen_coroutine},
33// {LUA_TABLIBNAME, luaopen_table},
34// {LUA_IOLIBNAME, luaopen_io},
35// {LUA_OSLIBNAME, luaopen_os},
36// {LUA_STRLIBNAME, luaopen_string},
37// {LUA_MATHLIBNAME, luaopen_math},
38// {LUA_UTF8LIBNAME, luaopen_utf8},
39// {LUA_DBLIBNAME, luaopen_debug},
40// {NULL, NULL}
41// };
42//
43// PORT NOTE: C sentinel `{NULL, NULL}` dropped — Rust slices carry their
44// own length, so no terminator is needed.
45//
46// PORT NOTE: Per PORTING.md §7, `luaopen_X` → `open` inside the module
47// (e.g. `crate::base::open`, `crate::string_lib::open`). As of Phase A
48// the individual stdlib modules exported inconsistent names:
49// base.rs → `pub fn open` (canonical; matches here)
50// string_lib.rs → `pub fn luaopen_string` (needs rename in Phase B)
51// table_lib.rs → `pub fn open_table` (needs rename in Phase B)
52// math_lib.rs → `pub fn luaopen_math` (needs rename in Phase B)
53// io_lib.rs → `pub fn luaopen_io` (needs rename in Phase B)
54// os_lib.rs → `pub fn open_os` (needs rename in Phase B)
55// utf8_lib, debug_lib, coro_lib, loadlib → not yet ported (Phase B)
56// Phase B should rename every stdlib opener to `pub fn open` and update
57// this table accordingly.
58static LOADED_LIBS: &[(&[u8], LuaCFunction)] = &[
59 (b"_G", crate::base::open),
60 (b"package", crate::loadlib::luaopen_package),
61 (b"coroutine", crate::coro_lib::open_coroutine),
62 (b"table", crate::table_lib::open_table),
63 (b"io", crate::io_lib::luaopen_io),
64 (b"os", crate::os_lib::open_os),
65 (b"string", crate::string_lib::luaopen_string),
66 (b"math", crate::math_lib::luaopen_math),
67 (b"utf8", crate::utf8_lib::open_utf8),
68 (b"debug", crate::debug_lib::open_debug),
69];
70
71// const luaL_Reg *lib;
72// /* "require" functions from 'loadedlibs' and set results to global table */
73// for (lib = loadedlibs; lib->func; lib++) {
74// luaL_requiref(L, lib->name, lib->func, 1);
75// lua_pop(L, 1); /* remove lib */
76// }
77// }
78//
79// PORT NOTE: `LUALIB_API` → `pub` (PORTING.md §4.1 / macros.tsv).
80// `luaL_requiref(L, name, func, 1)` → `state.require_lib(name, func, true)?`
81// The final `1` argument means "set global" — the loaded module value is
82// assigned to the global table under `name` and the value left on the
83// stack is then discarded by `lua_pop(L, 1)`.
84// `lua_pop(L, 1)` → `state.pop_n(1)` (macros.tsv).
85/// Open all standard Lua libraries into `state`, registering each into the
86/// global table.
87///
88/// Corresponds to `luaL_openlibs` in `linit.c`.
89pub fn open_libs(state: &mut LuaState) -> Result<(), LuaError> {
90 for &(name, func) in LOADED_LIBS {
91 state.require_lib(name, func, true)?;
92 state.pop_n(1);
93 }
94 Ok(())
95}
96
97// ──────────────────────────────────────────────────────────────────────────
98// PORT STATUS
99// source: src/linit.c (66 lines, 1 function)
100// target_crate: lua-stdlib
101// confidence: high
102// todos: 1
103// port_notes: 3
104// unsafe_blocks: 0
105// notes: Trivial file. Cross-crate refs (state.require_lib,
106// state.pop_n, crate::*::open) resolve in Phase B.
107// Phase B must also reconcile inconsistent open-function
108// names in the existing stdlib modules (see PORT NOTEs).
109// ──────────────────────────────────────────────────────────────────────────