From cd78fea1f519b56ba81e57c71d6ed686bb9ec205 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 30 Dec 2023 16:28:04 +0100 Subject: [PATCH] Fix print and warn globals yielding --- CHANGELOG.md | 1 + src/lune/globals/print.rs | 23 ++++++++++++++++------- src/lune/globals/warn.rs | 26 ++++++++++++++++---------- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d9d891..18d2646 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### 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]) [#133]: https://github.com/filiptibell/lune/pull/133 diff --git a/src/lune/globals/print.rs b/src/lune/globals/print.rs index 57b849b..dfdacfd 100644 --- a/src/lune/globals/print.rs +++ b/src/lune/globals/print.rs @@ -1,14 +1,23 @@ 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> { - lua.create_async_function(|_, args: LuaMultiValue| async move { +pub fn create(lua: &Lua) -> LuaResult> { + lua.create_function(|_, args: LuaMultiValue| { let formatted = format!("{}\n", pretty_format_multi_value(&args)?); - let mut stdout = io::stdout(); - stdout.write_all(formatted.as_bytes()).await?; - stdout.flush().await?; + task::spawn(async move { + let _res = async move { + let mut stdout = io::stdout(); + stdout.write_all(formatted.as_bytes()).await?; + stdout.flush().await?; + Ok::<_, LuaError>(()) + }; + // FUTURE: Send any error back to scheduler and emit it properly + }); Ok(()) }) } diff --git a/src/lune/globals/warn.rs b/src/lune/globals/warn.rs index bf7cb18..5a4c303 100644 --- a/src/lune/globals/warn.rs +++ b/src/lune/globals/warn.rs @@ -1,21 +1,27 @@ use mlua::prelude::*; -use tokio::io::{self, AsyncWriteExt}; - -use crate::lune::{ - scheduler::LuaSchedulerExt, - util::formatting::{format_label, pretty_format_multi_value}, +use tokio::{ + io::{self, AsyncWriteExt}, + task, }; -pub fn create(lua: &'static Lua) -> LuaResult> { - lua.create_async_function(|_, args: LuaMultiValue| async move { +use crate::lune::util::formatting::{format_label, pretty_format_multi_value}; + +pub fn create(lua: &Lua) -> LuaResult> { + lua.create_function(|_, args: LuaMultiValue| { let formatted = format!( "{}\n{}\n", format_label("warn"), pretty_format_multi_value(&args)? ); - let mut stdout = io::stderr(); - stdout.write_all(formatted.as_bytes()).await?; - stdout.flush().await?; + task::spawn(async move { + let _res = async move { + let mut stdout = io::stderr(); + stdout.write_all(formatted.as_bytes()).await?; + stdout.flush().await?; + Ok::<_, LuaError>(()) + }; + // FUTURE: Send any error back to scheduler and emit it properly + }); Ok(()) }) }