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
//! Initialization of standard libraries for Lua.
//!
//! Opens all standard libraries via `require`-style loading and registers
//! them into the global table.
//!
//! Port of `src/linit.c` (66 lines, 1 function).
use crate;
use LuaError;
// Matches types.tsv: lua_CFunction → fn(&mut LuaState) -> Result<usize, LuaError>
type LuaCFunction = fn ;
// ── Library-name byte-string constants ────────────────────────────────────
//
// These replace the C macros from lualib.h and lauxlib.h:
// LUA_GNAME = "_G" (lauxlib.h)
// LUA_LOADLIBNAME = "package" (lualib.h)
// LUA_COLIBNAME = "coroutine" (lualib.h)
// LUA_TABLIBNAME = "table" (lualib.h)
// LUA_IOLIBNAME = "io" (lualib.h)
// LUA_OSLIBNAME = "os" (lualib.h)
// LUA_STRLIBNAME = "string" (lualib.h)
// LUA_MATHLIBNAME = "math" (lualib.h)
// LUA_UTF8LIBNAME = "utf8" (lualib.h)
// LUA_DBLIBNAME = "debug" (lualib.h)
//
// Per PORTING.md §3.1 all Lua string data uses &[u8], not &str.
// {LUA_GNAME, luaopen_base},
// {LUA_LOADLIBNAME, luaopen_package},
// {LUA_COLIBNAME, luaopen_coroutine},
// {LUA_TABLIBNAME, luaopen_table},
// {LUA_IOLIBNAME, luaopen_io},
// {LUA_OSLIBNAME, luaopen_os},
// {LUA_STRLIBNAME, luaopen_string},
// {LUA_MATHLIBNAME, luaopen_math},
// {LUA_UTF8LIBNAME, luaopen_utf8},
// {LUA_DBLIBNAME, luaopen_debug},
// {NULL, NULL}
// };
//
// PORT NOTE: C sentinel `{NULL, NULL}` dropped — Rust slices carry their
// own length, so no terminator is needed.
//
// PORT NOTE: Per PORTING.md §7, `luaopen_X` → `open` inside the module
// (e.g. `crate::base::open`, `crate::string_lib::open`). As of Phase A
// the individual stdlib modules exported inconsistent names:
// base.rs → `pub fn open` (canonical; matches here)
// string_lib.rs → `pub fn luaopen_string` (needs rename in Phase B)
// table_lib.rs → `pub fn open_table` (needs rename in Phase B)
// math_lib.rs → `pub fn luaopen_math` (needs rename in Phase B)
// io_lib.rs → `pub fn luaopen_io` (needs rename in Phase B)
// os_lib.rs → `pub fn open_os` (needs rename in Phase B)
// utf8_lib, debug_lib, coro_lib, loadlib → not yet ported (Phase B)
// Phase B should rename every stdlib opener to `pub fn open` and update
// this table accordingly.
static LOADED_LIBS: & = &;
// const luaL_Reg *lib;
// /* "require" functions from 'loadedlibs' and set results to global table */
// for (lib = loadedlibs; lib->func; lib++) {
// luaL_requiref(L, lib->name, lib->func, 1);
// lua_pop(L, 1); /* remove lib */
// }
// }
//
// PORT NOTE: `LUALIB_API` → `pub` (PORTING.md §4.1 / macros.tsv).
// `luaL_requiref(L, name, func, 1)` → `state.require_lib(name, func, true)?`
// The final `1` argument means "set global" — the loaded module value is
// assigned to the global table under `name` and the value left on the
// stack is then discarded by `lua_pop(L, 1)`.
// `lua_pop(L, 1)` → `state.pop_n(1)` (macros.tsv).
/// Open all standard Lua libraries into `state`, registering each into the
/// global table.
///
/// Corresponds to `luaL_openlibs` in `linit.c`.
// ──────────────────────────────────────────────────────────────────────────
// PORT STATUS
// source: src/linit.c (66 lines, 1 function)
// target_crate: lua-stdlib
// confidence: high
// todos: 1
// port_notes: 3
// unsafe_blocks: 0
// notes: Trivial file. Cross-crate refs (state.require_lib,
// state.pop_n, crate::*::open) resolve in Phase B.
// Phase B must also reconcile inconsistent open-function
// names in the existing stdlib modules (see PORT NOTEs).
// ──────────────────────────────────────────────────────────────────────────