From 9b4ca94c131df04ebcbbdd8711f0504f6ed2de81 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sun, 20 Aug 2023 19:54:29 -0500 Subject: [PATCH] Add back print and warn globals --- src/lune/globals/mod.rs | 6 +++++- src/lune/globals/print.rs | 14 ++++++++++++++ src/lune/globals/warn.rs | 21 +++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 src/lune/globals/print.rs create mode 100644 src/lune/globals/warn.rs diff --git a/src/lune/globals/mod.rs b/src/lune/globals/mod.rs index dfcb928..9605aa0 100644 --- a/src/lune/globals/mod.rs +++ b/src/lune/globals/mod.rs @@ -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)?; } diff --git a/src/lune/globals/print.rs b/src/lune/globals/print.rs new file mode 100644 index 0000000..57b849b --- /dev/null +++ b/src/lune/globals/print.rs @@ -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> { + 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(()) + }) +} diff --git a/src/lune/globals/warn.rs b/src/lune/globals/warn.rs new file mode 100644 index 0000000..ba98b8a --- /dev/null +++ b/src/lune/globals/warn.rs @@ -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> { + 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(()) + }) +}