More work on task lib

This commit is contained in:
Filip Tibell 2023-01-22 17:05:01 -05:00
parent 02fd4e8733
commit f22ef577cf
No known key found for this signature in database

View file

@ -33,6 +33,16 @@ fn get_or_create_thread_from_arg<'a>(lua: &'a Lua, arg: Value<'a>) -> Result<Thr
}) })
} }
async fn resume_thread(lua: &Lua, thread: Thread<'_>, args: Vararg<'_>) -> Result<()> {
let coroutine: Table = lua.globals().raw_get("coroutine")?;
let resume: Function = coroutine.raw_get("resume")?;
// FIXME: This is blocking, we should spawn a local tokio task,
// but doing that moves "thread" and "args", that both have
// the lifetime of the outer function, so it doesn't work
resume.call_async((thread, args)).await?;
Ok(())
}
async fn task_cancel(lua: &Lua, thread: Thread<'_>) -> Result<()> { async fn task_cancel(lua: &Lua, thread: Thread<'_>) -> Result<()> {
let coroutine: Table = lua.globals().raw_get("coroutine")?; let coroutine: Table = lua.globals().raw_get("coroutine")?;
let close: Function = coroutine.raw_get("close")?; let close: Function = coroutine.raw_get("close")?;
@ -40,30 +50,27 @@ async fn task_cancel(lua: &Lua, thread: Thread<'_>) -> Result<()> {
Ok(()) Ok(())
} }
async fn task_defer<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> { async fn task_defer<'a>(lua: &'a Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<Thread<'a>> {
task_wait(lua, None).await?; // TODO: Defer (sleep a minimum amount of time)
get_or_create_thread_from_arg(lua, tof)? let thread = get_or_create_thread_from_arg(lua, tof)?;
.into_async::<_, Vararg<'_>>(args) resume_thread(lua, thread.clone(), args).await?;
.await?; Ok(thread)
Ok(())
} }
async fn task_delay<'a>( async fn task_delay<'a>(
lua: &Lua, lua: &'a Lua,
(delay, tof, args): (Option<f32>, Value<'a>, Vararg<'a>), (_delay, tof, args): (Option<f32>, Value<'a>, Vararg<'a>),
) -> Result<()> { ) -> Result<Thread<'a>> {
task_wait(lua, delay).await?; // TODO: Delay by the amount of time wanted
get_or_create_thread_from_arg(lua, tof)? let thread = get_or_create_thread_from_arg(lua, tof)?;
.into_async::<_, Vararg<'_>>(args) resume_thread(lua, thread.clone(), args).await?;
.await?; Ok(thread)
Ok(())
} }
async fn task_spawn<'a>(lua: &Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<()> { async fn task_spawn<'a>(lua: &'a Lua, (tof, args): (Value<'a>, Vararg<'a>)) -> Result<Thread<'a>> {
get_or_create_thread_from_arg(lua, tof)? let thread = get_or_create_thread_from_arg(lua, tof)?;
.into_async::<_, Vararg<'_>>(args) resume_thread(lua, thread.clone(), args).await?;
.await?; Ok(thread)
Ok(())
} }
// FIXME: It doesn't seem possible to properly make an async wait // FIXME: It doesn't seem possible to properly make an async wait