Add back print and warn globals

This commit is contained in:
Filip Tibell 2023-08-20 19:54:29 -05:00
parent 029873fd5f
commit 9b4ca94c13
3 changed files with 40 additions and 1 deletions

View file

@ -3,18 +3,22 @@ use mlua::prelude::*;
use super::util::TableBuilder;
mod g_table;
mod print;
mod require;
mod version;
mod warn;
pub fn inject_all(lua: &'static Lua) -> LuaResult<()> {
let all = TableBuilder::new(lua)?
.with_value("_G", g_table::create(lua)?)?
.with_value("_VERSION", version::create(lua)?)?
.with_value("print", print::create(lua)?)?
.with_value("require", require::create(lua)?)?
.with_value("warn", warn::create(lua)?)?
.build_readonly()?;
for res in all.pairs() {
let (key, value): (LuaValue, LuaValue) = res?;
let (key, value): (LuaValue, LuaValue) = res.unwrap();
lua.globals().set(key, value)?;
}

14
src/lune/globals/print.rs Normal file
View file

@ -0,0 +1,14 @@
use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt};
use crate::lune::{scheduler::LuaSchedulerExt, util::formatting::pretty_format_multi_value};
pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_async_function(|_, args: LuaMultiValue| async move {
let formatted = format!("{}\n", pretty_format_multi_value(&args)?);
let mut stdout = io::stdout();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
Ok(())
})
}

21
src/lune/globals/warn.rs Normal file
View file

@ -0,0 +1,21 @@
use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt};
use crate::lune::{
scheduler::LuaSchedulerExt,
util::formatting::{format_label, pretty_format_multi_value},
};
pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_async_function(|_, args: LuaMultiValue| async move {
let formatted = format!(
"{}\n{}",
format_label("warn"),
pretty_format_multi_value(&args)?
);
let mut stdout = io::stderr();
stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?;
Ok(())
})
}