Working commit

This commit is contained in:
2025-06-20 11:21:38 +10:00
parent f9676a1436
commit 94e1cf2eb0
8 changed files with 1597 additions and 16 deletions

10
crates/lb_core/Cargo.toml Normal file
View File

@@ -0,0 +1,10 @@
[package]
name = "lb_core"
version = "0.1.0"
edition = "2024"
[dependencies]
luaffi = { version = "0.1.0", path = "../luaffi" }
luajit = { version = "0.1.0", path = "../luajit" }
owo-colors = "4.2.1"
tokio = { version = "1.45.1", features = ["full"] }

92
crates/lb_core/src/lib.rs Normal file
View File

@@ -0,0 +1,92 @@
use luaffi::{cdef, metatype};
use luajit::State;
use owo_colors::OwoColorize;
use std::{cell::RefCell, fmt, process};
#[derive(Debug)]
pub struct GlobalState(State);
impl GlobalState {
thread_local! {
static STATE: RefCell<Option<GlobalState>> = RefCell::new(None);
}
pub fn set(state: State) -> Option<State> {
Self::STATE.with_borrow_mut(|s| s.replace(Self(state)).map(|s| s.0))
}
pub fn with_current<T>(f: impl FnOnce(&State) -> T) -> T {
Self::STATE.with_borrow(|s| f(&s.as_ref().expect("lua state not initialised").0))
}
pub fn with_current_mut<T>(f: impl FnOnce(&mut State) -> T) -> T {
Self::STATE.with_borrow_mut(|s| f(&mut s.as_mut().expect("lua state not initialised").0))
}
pub fn new_thread() -> State {
Self::with_current(|s| s.new_thread())
}
pub fn uncaught_error(err: luajit::Error) {
let mut err = PrettyError::from(err);
if let Some(task) = tokio::task::try_id() {
err.prepend(format_args!("uncaught error in task {task}"));
}
eprintln!("{err}");
process::abort()
}
}
#[derive(Debug, Clone)]
pub struct PrettyError {
msg: String,
trace: Option<String>,
}
impl PrettyError {
pub fn new(msg: impl fmt::Display) -> Self {
Self {
msg: format!("{msg}"),
trace: None,
}
}
pub fn with_trace(mut self, trace: impl fmt::Display) -> Self {
self.trace = Some(format!("{trace}"));
self
}
pub fn prepend(&mut self, msg: impl fmt::Display) -> &mut Self {
if self.msg.is_empty() {
self.msg = format!("{msg}");
} else {
self.msg = format!("{msg}:\n{}", self.msg);
}
self
}
}
impl From<luajit::Error> for PrettyError {
fn from(value: luajit::Error) -> Self {
match value {
luajit::Error::Resume { msg, trace } => Self::new(msg).with_trace(trace),
err => Self::new(err),
}
}
}
impl fmt::Display for PrettyError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.trace {
Some(ref trace) => write!(f, "{}\n{trace}", self.msg.red()),
None => write!(f, "{}", self.msg.red()),
}
}
}
#[cdef]
pub struct lb_core;
#[metatype]
impl lb_core {}

View File

@@ -32,7 +32,7 @@ unsafe extern "C" fn __is_utf8(ptr: *const u8, len: usize) -> bool {
}
const CACHE_LIBS: &[(&str, &str)] = &[
// preloaded
// libs in global
("table", "table"),
("string", "string"),
("math", "math"),
@@ -143,13 +143,18 @@ impl Registry {
}
pub fn declare<T: Type>(&mut self, name: impl Display) -> &mut Self {
self.include::<T>();
self.funcs
self.include::<T>()
.funcs
.insert(name.to_string())
.then(|| writeln!(self.cdef, "{};", T::cdecl(name)).unwrap());
self
}
pub fn preload<T: Type>(&mut self, name: impl Display) -> &mut Self {
self.include::<T>();
self
}
pub fn done(&self) -> String {
self.to_string()
}

View File

@@ -5,11 +5,9 @@ use quote::{format_ident, quote};
use syn::{spanned::*, *};
#[derive(Debug, FromMeta)]
pub struct Args {
module: Option<String>,
}
pub struct Args {}
pub fn transform(args: Args, mut item: Item) -> Result<TokenStream> {
pub fn transform(_args: Args, mut item: Item) -> Result<TokenStream> {
let (name, impl_type, impl_cdef) = match item {
Item::Struct(ref mut str) => (
str.ident.clone(),