Make sure print and warn globals also flush stdout

This commit is contained in:
Filip Tibell 2023-06-08 12:30:16 +02:00
parent 71cf5e7c96
commit 63f623647b
No known key found for this signature in database

View file

@ -1,4 +1,5 @@
use mlua::prelude::*; use mlua::prelude::*;
use std::io::{self, Write as _};
#[cfg(feature = "roblox")] #[cfg(feature = "roblox")]
use lune_roblox::datatypes::extension::RobloxUserdataTypenameExt; use lune_roblox::datatypes::extension::RobloxUserdataTypenameExt;
@ -8,28 +9,31 @@ use crate::lua::{
task::TaskReference, task::TaskReference,
}; };
// HACK: We need to preserve the default behavior of the pub fn print(_: &Lua, args: LuaMultiValue) -> LuaResult<()> {
// print and error functions, for pcall and such, which let formatted = format!("{}\n", pretty_format_multi_value(&args)?);
// is really tricky to do from scratch so we will just let mut stdout = io::stdout();
// proxy the default print and error functions here stdout.write_all(formatted.as_bytes())?;
stdout.flush()?;
pub fn print(lua: &Lua, args: LuaMultiValue) -> LuaResult<()> {
let formatted = pretty_format_multi_value(&args)?;
let print: LuaFunction = lua.named_registry_value("print")?;
print.call(formatted)?;
Ok(()) Ok(())
} }
pub fn warn(lua: &Lua, args: LuaMultiValue) -> LuaResult<()> { pub fn warn(_: &Lua, args: LuaMultiValue) -> LuaResult<()> {
let print: LuaFunction = lua.named_registry_value("print")?; let formatted = format!(
print.call(format!(
"{}\n{}", "{}\n{}",
format_label("warn"), format_label("warn"),
pretty_format_multi_value(&args)? pretty_format_multi_value(&args)?
))?; );
let mut stdout = io::stdout();
stdout.write_all(formatted.as_bytes())?;
stdout.flush()?;
Ok(()) Ok(())
} }
// HACK: We need to preserve the default behavior of
// the lua error function, for pcall and such, which
// is really tricky to do from scratch so we will
// just proxy the default function here instead
pub fn error(lua: &Lua, (arg, level): (LuaValue, Option<u32>)) -> LuaResult<()> { pub fn error(lua: &Lua, (arg, level): (LuaValue, Option<u32>)) -> LuaResult<()> {
let error: LuaFunction = lua.named_registry_value("error")?; let error: LuaFunction = lua.named_registry_value("error")?;
let trace: LuaFunction = lua.named_registry_value("dbg.trace")?; let trace: LuaFunction = lua.named_registry_value("dbg.trace")?;