feat: return raw i32 instead of ExitCode

This commit is contained in:
Erica Marigold 2024-04-12 15:00:46 +05:30
parent 3ca27fa51c
commit 22d15b3649
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
6 changed files with 16 additions and 20 deletions

View file

@ -31,7 +31,7 @@ pub fn main() -> LuaResult<()> {
// Verify that we got a correct exit code // Verify that we got a correct exit code
let code = sched.get_exit_code().unwrap_or_default(); let code = sched.get_exit_code().unwrap_or_default();
assert!(format!("{code:?}").contains("(1)")); assert_eq!(code, 1);
Ok(()) Ok(())
} }

View file

@ -1,10 +1,10 @@
use std::{cell::Cell, process::ExitCode, rc::Rc}; use std::{cell::Cell, rc::Rc};
use event_listener::Event; use event_listener::Event;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub(crate) struct Exit { pub(crate) struct Exit {
code: Rc<Cell<Option<ExitCode>>>, code: Rc<Cell<Option<i32>>>,
event: Rc<Event>, event: Rc<Event>,
} }
@ -16,12 +16,12 @@ impl Exit {
} }
} }
pub fn set(&self, code: ExitCode) { pub fn set(&self, code: i32) {
self.code.set(Some(code)); self.code.set(Some(code));
self.event.notify(usize::MAX); self.event.notify(usize::MAX);
} }
pub fn get(&self) -> Option<ExitCode> { pub fn get(&self) -> Option<i32> {
self.code.get() self.code.get()
} }

View file

@ -1,7 +1,7 @@
#![allow(unused_imports)] #![allow(unused_imports)]
#![allow(clippy::too_many_lines)] #![allow(clippy::too_many_lines)]
use std::process::ExitCode; use std::process::{ExitCode, ExitStatus};
use mlua::prelude::*; use mlua::prelude::*;
@ -118,7 +118,7 @@ impl<'lua> Functions<'lua> {
let _span = tracing::trace_span!("Scheduler::fn_resume").entered(); let _span = tracing::trace_span!("Scheduler::fn_resume").entered();
match thread.resume::<_, LuaMultiValue>(args.clone()) { match thread.resume::<_, LuaMultiValue>(args.clone()) {
Ok(v) => { Ok(v) => {
if v.get(0).map(is_poll_pending).unwrap_or_default() { if v.get(0).is_some_and(is_poll_pending) {
// Pending, defer to scheduler and return nil // Pending, defer to scheduler and return nil
resume_queue.push_item(lua, &thread, args)?; resume_queue.push_item(lua, &thread, args)?;
(true, LuaValue::Nil).into_lua_multi(lua) (true, LuaValue::Nil).into_lua_multi(lua)
@ -174,7 +174,7 @@ impl<'lua> Functions<'lua> {
// and only if we get the pending value back we can spawn to async executor // and only if we get the pending value back we can spawn to async executor
match thread.resume::<_, LuaMultiValue>(args.clone()) { match thread.resume::<_, LuaMultiValue>(args.clone()) {
Ok(v) => { Ok(v) => {
if v.get(0).map(is_poll_pending).unwrap_or_default() { if v.get(0).is_some_and(is_poll_pending) {
spawn_queue.push_item(lua, &thread, args)?; spawn_queue.push_item(lua, &thread, args)?;
} else { } else {
// Not pending, store the value if thread is done // Not pending, store the value if thread is done
@ -230,9 +230,9 @@ impl<'lua> Functions<'lua> {
let exit_env = lua.create_table_from(vec![ let exit_env = lua.create_table_from(vec![
( (
"exit", "exit",
lua.create_function(|lua, code: Option<u8>| { lua.create_function(|lua, code: Option<i32>| {
let _span = tracing::trace_span!("Scheduler::fn_exit").entered(); let _span = tracing::trace_span!("Scheduler::fn_exit").entered();
let code = code.map(ExitCode::from).unwrap_or_default(); let code = code.unwrap_or_default();
lua.set_exit_code(code); lua.set_exit_code(code);
Ok(()) Ok(())
})?, })?,

View file

@ -2,7 +2,6 @@
use std::{ use std::{
cell::Cell, cell::Cell,
process::ExitCode,
rc::{Rc, Weak as WeakRc}, rc::{Rc, Weak as WeakRc},
sync::{Arc, Weak as WeakArc}, sync::{Arc, Weak as WeakArc},
thread::panicking, thread::panicking,
@ -168,7 +167,7 @@ impl<'lua> Scheduler<'lua> {
Gets the exit code for this scheduler, if one has been set. Gets the exit code for this scheduler, if one has been set.
*/ */
#[must_use] #[must_use]
pub fn get_exit_code(&self) -> Option<ExitCode> { pub fn get_exit_code(&self) -> Option<i32> {
self.exit.get() self.exit.get()
} }
@ -177,7 +176,7 @@ impl<'lua> Scheduler<'lua> {
This will cause [`Scheduler::run`] to exit immediately. This will cause [`Scheduler::run`] to exit immediately.
*/ */
pub fn set_exit_code(&self, code: ExitCode) { pub fn set_exit_code(&self, code: i32) {
self.exit.set(code); self.exit.set(code);
} }

View file

@ -1,9 +1,7 @@
#![allow(unused_imports)] #![allow(unused_imports)]
#![allow(clippy::missing_errors_doc)] #![allow(clippy::missing_errors_doc)]
use std::{ use std::{cell::Cell, future::Future, rc::Weak as WeakRc, sync::Weak as WeakArc};
cell::Cell, future::Future, process::ExitCode, rc::Weak as WeakRc, sync::Weak as WeakArc,
};
use async_executor::{Executor, Task}; use async_executor::{Executor, Task};
use mlua::prelude::*; use mlua::prelude::*;
@ -82,7 +80,7 @@ pub trait LuaSchedulerExt<'lua> {
Panics if called outside of a running [`Scheduler`]. Panics if called outside of a running [`Scheduler`].
*/ */
fn set_exit_code(&self, code: ExitCode); fn set_exit_code(&self, code: i32);
/** /**
Pushes (spawns) a lua thread to the **front** of the current scheduler. Pushes (spawns) a lua thread to the **front** of the current scheduler.
@ -283,7 +281,7 @@ pub trait LuaSpawnExt<'lua> {
} }
impl<'lua> LuaSchedulerExt<'lua> for Lua { impl<'lua> LuaSchedulerExt<'lua> for Lua {
fn set_exit_code(&self, code: ExitCode) { fn set_exit_code(&self, code: i32) {
let exit = self let exit = self
.app_data_ref::<Exit>() .app_data_ref::<Exit>()
.expect("exit code can only be set from within an active scheduler"); .expect("exit code can only be set from within an active scheduler");

View file

@ -37,8 +37,7 @@ pub(crate) async fn run_until_yield<'lua>(
pub(crate) fn is_poll_pending(value: &LuaValue) -> bool { pub(crate) fn is_poll_pending(value: &LuaValue) -> bool {
value value
.as_light_userdata() .as_light_userdata()
.map(|l| l == Lua::poll_pending()) .is_some_and(|l| l == Lua::poll_pending())
.unwrap_or_default()
} }
/** /**