Skip to content

Commit 19a663c

Browse files
committed
fixup! add load_from_std_lib function to Lua state
1 parent 6d8ea22 commit 19a663c

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/lua.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,38 @@ impl Lua {
125125
create_lua(lua_mod)
126126
}
127127

128-
/// Loads standard the specified set of the standard libraries into an existing Lua state.
128+
/// Loads the specified set of safe standard libraries into an existing Lua state.
129+
///
130+
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
131+
///
132+
/// Note that the `debug` library can't be loaded using this function as it can be used to break
133+
/// the safety guarantees of rlua. If you really want to load it, use the sister function
134+
/// [`Lua::unsafe_load_from_std_lib`].
135+
///
136+
/// # Panics
137+
///
138+
/// Panics if `lua_mod` contains `StdLib::DEBUG`
139+
pub fn load_from_std_lib(&self, lua_mod: StdLib) -> Result<()> {
140+
assert!(
141+
!lua_mod.contains(StdLib::DEBUG),
142+
"The lua debug module can't be loaded using `load_from_std_lib`. Use `unsafe_load_from_std_lib` instead."
143+
);
144+
145+
146+
unsafe {
147+
protect_lua_closure(self.main_state, 0, 0, |state| {
148+
load_from_std_lib(state, lua_mod);
149+
})
150+
}
151+
}
152+
153+
/// Loads the specified set of standard libraries into an existing Lua state.
129154
///
130155
/// Use the [`StdLib`] flags to specifiy the libraries you want to load.
131156
///
132157
/// This function is unsafe because it can be used to load the `debug` library which can be used
133158
/// to break the safety guarantees provided by rlua.
134-
pub unsafe fn load_from_std_lib(&self, lua_mod: StdLib) -> Result<()> {
159+
pub unsafe fn unsafe_load_from_std_lib(&self, lua_mod: StdLib) -> Result<()> {
135160
protect_lua_closure(self.main_state, 0, 0, |state| {
136161
load_from_std_lib(state, lua_mod);
137162
})

0 commit comments

Comments
 (0)