Modules should be registered in luby not lb

This commit is contained in:
2025-06-26 13:46:39 +10:00
parent bcb734846d
commit 88ab48cddb
9 changed files with 41 additions and 111 deletions

View File

@@ -1 +1,14 @@
pub use lb::*;
pub use lb::chan;
pub use lb::fs;
pub use lb::net;
pub use lb::task;
#[doc(hidden)]
pub fn load_modules(runtime: &mut lb::runtime::Builder) {
// core modules
runtime
.module::<task::lb_tasklib>("lb:task")
.module::<chan::lb_chanlib>("lb:channel")
.module::<fs::lb_fslib>("lb:fs")
.module::<net::lb_netlib>("lb:net");
}

View File

@@ -121,13 +121,6 @@ impl Args {
}
}
fn exit_err<T, E: Display>(code: ExitCode) -> impl FnOnce(E) -> T {
move |err| {
eprintln!("{}", err.red().bold());
code.exit()
}
}
fn main() {
panic::set_hook(Box::new(panic_cb));
@@ -160,6 +153,13 @@ fn print_version() {
);
}
fn unwrap_exit<T, E: Display>(code: ExitCode) -> impl FnOnce(E) -> T {
move |err| {
eprintln!("{}", err.red().bold());
code.exit()
}
}
fn init_logger(args: &Args) {
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{Layer, util::*};
@@ -202,17 +202,18 @@ fn init_tokio(args: &Args) -> tokio::runtime::Runtime {
.thread_name("luby")
.max_blocking_threads(args.blocking_threads.get())
.build()
.unwrap_or_else(exit_err(ExitCode::OsErr))
.unwrap_or_else(unwrap_exit(ExitCode::OsErr))
}
fn init_lua(args: &Args) -> lb::runtime::Runtime {
let rt = lb::runtime::Builder::new();
let mut rt = lb::runtime::Builder::new();
luby::load_modules(&mut rt);
if args.dump.iter().find(|s| *s == "cdef").is_some() {
print!("{}", rt.registry());
}
let mut rt = rt.build().unwrap_or_else(exit_err(ExitCode::Software));
let mut rt = rt.build().unwrap();
for arg in args.jit.iter() {
let mut s = rt.guard();
@@ -235,7 +236,7 @@ fn init_lua(args: &Args) -> lb::runtime::Runtime {
}
}
}
.unwrap_or_else(exit_err(ExitCode::Usage));
.unwrap_or_else(unwrap_exit(ExitCode::Usage));
}
rt
@@ -243,9 +244,9 @@ fn init_lua(args: &Args) -> lb::runtime::Runtime {
fn parse_jitlib_cmd(s: &str) -> Option<(&str, &str)> {
match s {
"p" => Some(("p", "Flspv10")),
"v" => Some(("v", "-")),
"dump" => Some(("dump", "tirs")),
"p" => Some(("p", "Flspv10")), // default -jp flags
"v" => Some(("v", "-")), // default -jv flags
"dump" => Some(("dump", "tirs")), // default -jdump flags
_ => s.split_once('='),
}
}
@@ -262,7 +263,7 @@ async fn main_async(args: Args, state: &mut luajit::State) {
};
s.load(&luajit::Chunk::new(chunk).path(path))
.unwrap_or_else(exit_err(ExitCode::NoInput));
.unwrap_or_else(unwrap_exit(ExitCode::NoInput));
if let Err(err) = s.call_async(0, 0).await {
match err.trace() {