pub struct Libs(/* private fields */);Expand description
The libraries a script gets: its capabilities. Combine with |.
Implementations§
Source§impl Libs
impl Libs
Sourcepub const OS: Libs
pub const OS: Libs
os.time, os.clock, os.date, os.difftime, os.setlocale (the "C" locale
only; os.getenv is ENV’s).
Sourcepub const PRINT: Libs
pub const PRINT: Libs
print (to the host’s sink or Lua::take_output).
Sourcepub const DEBUG_INFO: Libs
pub const DEBUG_INFO: Libs
debug.traceback and a read-only debug.getinfo (no hooks, locals, upvalues,
registry or functions handed out). Not in SAFE, STANDARD or ALL: a host opts in.
Sourcepub const REQUIRE: Libs
pub const REQUIRE: Libs
require and package (loaded, preload, searchers), with modules only from the
host (Lua::add_module, Lua::set_module_loader): no search path, no files. Not
in SAFE, STANDARD or ALL: a host opts in.
Sourcepub const ENV: Libs
pub const ENV: Libs
os.getenv, reading only what the host put in the state’s map (Lua::set_env),
never the process environment. Not in SAFE, STANDARD or ALL: a host opts in.
Sourcepub const IO: Libs
pub const IO: Libs
io over streams the host gives: io.stdin, io.stdout and io.stderr are the
host’s (Lua::set_stdin, Lua::set_stdout, Lua::set_stderr; by default
stdin is empty and the others write where print writes), and io.open asks the
host’s opener (Lua::set_file_opener): no file system, no process (io.popen and
io.tmpfile are absent). Not in SAFE, STANDARD or ALL: a host opts in.
Sourcepub const ALL: Libs
pub const ALL: Libs
Every library of the standard set, load included (not the opt-in DEBUG_INFO,
REQUIRE, ENV and IO).
Sourcepub const fn union(self, other: Libs) -> Libs
pub const fn union(self, other: Libs) -> Libs
Both sets (| in a const).
Examples found in repository?
More examples
8fn main() {
9 let a: Vec<String> = std::env::args().collect();
10 let src = a.get(1).cloned().unwrap_or_default();
11 let mem: usize = a.get(2).and_then(|s| s.parse().ok()).unwrap_or(64) << 20;
12 let fuel: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(1 << 32);
13 let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
14 let mut lua = Lua::new(Limits::default().memory(mem).fuel(fuel).hash_seed(7), libs);
15 lua.set_print(|_| {});
16 let h0 = hwm();
17 let t = std::time::Instant::now();
18 let r = lua.exec(src, "=s");
19 let r = match r { Ok(v) => format!("ok {:?}", v.first().and_then(|x| lua.to_string_lossy(*x))), Err(e) => format!("err {e}") };
20 let now = std::fs::read_to_string("/proc/self/statm").ok().and_then(|s| s.split_whitespace().nth(1)?.parse::<usize>().ok()).unwrap_or(0) * 4096;
21 println!(
22 "peak rss {} MiB (from {} MiB) under a {} MiB limit, rss now {} MiB with {} MiB counted, {:?}, fuel {}, {}",
23 hwm() >> 10,
24 h0 >> 10,
25 mem >> 20,
26 now >> 20,
27 lua.memory_used() >> 20,
28 t.elapsed(),
29 fuel - lua.fuel_left(),
30 &r[..r.len().min(200)]
31 );
32}10fn main() {
11 let a: Vec<String> = std::env::args().collect();
12 let setup = a.get(1).cloned().unwrap_or_default();
13 let body = a.get(2).cloned().unwrap_or_default();
14 let ms: u64 = a.get(3).and_then(|s| s.parse().ok()).unwrap_or(10);
15 let mem: usize = a.get(4).and_then(|s| s.parse().ok()).unwrap_or(512) << 20;
16 let libs = Libs::ALL.union(Libs::DEBUG_INFO).union(Libs::REQUIRE);
17 let mut worst = Duration::ZERO;
18 let mut last = String::new();
19 for _ in 0..5 {
20 let mut lua = Lua::new(Limits::default().memory(mem).string_len(64 << 20).fuel(1 << 40).hash_seed(7), libs);
21 lua.set_print(|_| {});
22 lua.exec(&setup, "=setup").unwrap();
23 let t0 = Instant::now();
24 let clock: remedy_lua::Clock = Arc::new(move || t0.elapsed());
25 lua.set_time_limit(Some((clock, Duration::from_millis(ms))));
26 let f = lua.load(&body, "=body").unwrap();
27 let start = Instant::now();
28 let r = lua.call(f.value(), &[]);
29 let el = start.elapsed();
30 worst = worst.max(el.saturating_sub(Duration::from_millis(ms)));
31 last = format!("{:?}", r.err());
32 }
33 println!("worst overrun {:?} past {ms} ms: {}", worst, &last[..last.len().min(120)]);
34}