use lua_types::value::LuaValue;
use lua_types::LuaError;
use crate::state_stub::LuaState;
pub const STRICT_REMOVED_GLOBALS: &[&[u8]] = &[
b"dofile",
b"loadfile",
b"load",
b"loadstring",
b"require",
b"package",
b"io",
b"debug",
b"os.execute",
b"os.exit",
b"os.remove",
b"os.rename",
b"os.tmpname",
b"os.getenv",
b"os.setlocale",
];
pub fn strip_globals(state: &mut LuaState, names: &[&[u8]]) -> Result<(), LuaError> {
let globals = match state.global().globals.clone() {
LuaValue::Table(t) => t,
_ => return Ok(()),
};
for name in names {
match name.iter().position(|&b| b == b'.') {
Some(dot) => {
let head = &name[..dot];
let tail = &name[dot + 1..];
if let LuaValue::Table(sub) = globals.get_str_bytes(head) {
let key = LuaValue::Str(state.new_string(tail)?);
sub.raw_set(key, LuaValue::Nil);
}
}
None => {
let key = LuaValue::Str(state.new_string(name)?);
globals.raw_set(key, LuaValue::Nil);
}
}
}
Ok(())
}