Fix print and warn globals yielding

This commit is contained in:
Filip Tibell 2023-12-30 16:28:04 +01:00
parent 0e54d7f124
commit cd78fea1f5
No known key found for this signature in database
3 changed files with 33 additions and 17 deletions

View file

@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Fixed the `print` and `warn` global functions yielding the thread, preventing them from being used in places such as the callback to `table.sort`.
- Fixed the `overwrite` option for `fs.move` not correctly removing existing files / directories. ([#133]) - Fixed the `overwrite` option for `fs.move` not correctly removing existing files / directories. ([#133])
[#133]: https://github.com/filiptibell/lune/pull/133 [#133]: https://github.com/filiptibell/lune/pull/133

View file

@ -1,14 +1,23 @@
use mlua::prelude::*; use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt}; use tokio::{
io::{self, AsyncWriteExt},
task,
};
use crate::lune::{scheduler::LuaSchedulerExt, util::formatting::pretty_format_multi_value}; use crate::lune::util::formatting::pretty_format_multi_value;
pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> { pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_async_function(|_, args: LuaMultiValue| async move { lua.create_function(|_, args: LuaMultiValue| {
let formatted = format!("{}\n", pretty_format_multi_value(&args)?); let formatted = format!("{}\n", pretty_format_multi_value(&args)?);
task::spawn(async move {
let _res = async move {
let mut stdout = io::stdout(); let mut stdout = io::stdout();
stdout.write_all(formatted.as_bytes()).await?; stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?; stdout.flush().await?;
Ok::<_, LuaError>(())
};
// FUTURE: Send any error back to scheduler and emit it properly
});
Ok(()) Ok(())
}) })
} }

View file

@ -1,21 +1,27 @@
use mlua::prelude::*; use mlua::prelude::*;
use tokio::io::{self, AsyncWriteExt}; use tokio::{
io::{self, AsyncWriteExt},
use crate::lune::{ task,
scheduler::LuaSchedulerExt,
util::formatting::{format_label, pretty_format_multi_value},
}; };
pub fn create(lua: &'static Lua) -> LuaResult<impl IntoLua<'_>> { use crate::lune::util::formatting::{format_label, pretty_format_multi_value};
lua.create_async_function(|_, args: LuaMultiValue| async move {
pub fn create(lua: &Lua) -> LuaResult<impl IntoLua<'_>> {
lua.create_function(|_, args: LuaMultiValue| {
let formatted = format!( let formatted = format!(
"{}\n{}\n", "{}\n{}\n",
format_label("warn"), format_label("warn"),
pretty_format_multi_value(&args)? pretty_format_multi_value(&args)?
); );
task::spawn(async move {
let _res = async move {
let mut stdout = io::stderr(); let mut stdout = io::stderr();
stdout.write_all(formatted.as_bytes()).await?; stdout.write_all(formatted.as_bytes()).await?;
stdout.flush().await?; stdout.flush().await?;
Ok::<_, LuaError>(())
};
// FUTURE: Send any error back to scheduler and emit it properly
});
Ok(()) Ok(())
}) })
} }