2023-01-23 07:38:32 +00:00
|
|
|
use std::{
|
|
|
|
sync::Weak,
|
|
|
|
time::{Duration, Instant},
|
|
|
|
};
|
2023-01-21 18:33:33 +00:00
|
|
|
|
2023-01-23 02:31:55 +00:00
|
|
|
use mlua::prelude::*;
|
2023-01-23 21:18:20 +00:00
|
|
|
use smol::prelude::*;
|
2023-01-23 07:38:32 +00:00
|
|
|
use smol::{channel::Sender, LocalExecutor, Timer};
|
|
|
|
|
|
|
|
use crate::{utils::table_builder::TableBuilder, LuneMessage};
|
|
|
|
|
|
|
|
pub fn create(lua: &Lua) -> LuaResult<()> {
|
2023-01-22 20:23:56 +00:00
|
|
|
lua.globals().raw_set(
|
|
|
|
"task",
|
2023-01-23 01:18:09 +00:00
|
|
|
TableBuilder::new(lua)?
|
2023-01-23 07:38:32 +00:00
|
|
|
.with_async_function("cancel", task_cancel)?
|
|
|
|
.with_async_function("delay", task_delay)?
|
|
|
|
.with_async_function("defer", task_defer)?
|
|
|
|
.with_async_function("spawn", task_spawn)?
|
|
|
|
.with_async_function("wait", task_wait)?
|
2023-01-23 01:18:09 +00:00
|
|
|
.build_readonly()?,
|
2023-01-22 21:26:45 +00:00
|
|
|
)
|
|
|
|
}
|
2023-01-23 07:38:32 +00:00
|
|
|
|
|
|
|
fn tof_to_thread<'a>(lua: &'a Lua, tof: LuaValue<'a>) -> LuaResult<LuaThread<'a>> {
|
|
|
|
match tof {
|
|
|
|
LuaValue::Thread(t) => Ok(t),
|
|
|
|
LuaValue::Function(f) => Ok(lua.create_thread(f)?),
|
|
|
|
value => Err(LuaError::RuntimeError(format!(
|
|
|
|
"Argument must be a thread or function, got {}",
|
|
|
|
value.type_name()
|
|
|
|
))),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 21:18:20 +00:00
|
|
|
async fn run_registered_task(
|
|
|
|
lua: &Lua,
|
|
|
|
to_run: impl Future<Output = LuaResult<()>> + 'static,
|
|
|
|
run_in_background: bool,
|
|
|
|
) -> LuaResult<()> {
|
|
|
|
// Fetch global references to task executor & message sender
|
|
|
|
let exec = lua
|
2023-01-23 07:38:32 +00:00
|
|
|
.app_data_ref::<Weak<LocalExecutor>>()
|
|
|
|
.unwrap()
|
|
|
|
.upgrade()
|
|
|
|
.unwrap();
|
2023-01-23 21:18:20 +00:00
|
|
|
let sender = lua
|
2023-01-23 07:38:32 +00:00
|
|
|
.app_data_ref::<Weak<Sender<LuneMessage>>>()
|
|
|
|
.unwrap()
|
|
|
|
.upgrade()
|
|
|
|
.unwrap();
|
2023-01-23 21:18:20 +00:00
|
|
|
// Send a message that we have started our task
|
2023-01-23 07:38:32 +00:00
|
|
|
sender
|
|
|
|
.send(LuneMessage::Spawned)
|
|
|
|
.await
|
|
|
|
.map_err(LuaError::external)?;
|
2023-01-23 21:18:20 +00:00
|
|
|
// Run the new task separately from the current one using the executor
|
|
|
|
// FIXME: This should run the task instantly and only stop at the first yield
|
|
|
|
let sender = sender.clone();
|
|
|
|
let task = exec.spawn(async move {
|
|
|
|
sender
|
|
|
|
.send(match to_run.await {
|
2023-01-23 07:38:32 +00:00
|
|
|
Ok(_) => LuneMessage::Finished,
|
|
|
|
Err(e) => LuneMessage::LuaError(e),
|
|
|
|
})
|
|
|
|
.await
|
2023-01-23 21:18:20 +00:00
|
|
|
});
|
|
|
|
// Wait for the task to complete OR let it run in the background
|
|
|
|
// Any lua errors will be sent through the message channel back
|
|
|
|
// to the main thread which will then handle them properly
|
|
|
|
if run_in_background {
|
|
|
|
task.detach();
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
task.await.map_err(LuaError::external)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn task_cancel<'a>(lua: &'a Lua, thread: LuaThread<'a>) -> LuaResult<()> {
|
|
|
|
let coroutine: LuaTable = lua.globals().raw_get("coroutine")?;
|
|
|
|
let close: LuaFunction = coroutine.raw_get("close")?;
|
|
|
|
close.call_async(thread).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn task_defer<'a>(lua: &'a Lua, tof: LuaValue<'a>) -> LuaResult<LuaThread<'a>> {
|
|
|
|
// Spawn a new detached task using a lua reference that we can use inside of our task
|
|
|
|
let task_lua = lua.app_data_ref::<Weak<Lua>>().unwrap().upgrade().unwrap();
|
|
|
|
let task_thread = tof_to_thread(lua, tof)?;
|
|
|
|
let task_thread_key = lua.create_registry_value(task_thread)?;
|
|
|
|
let lua_thread_to_return = lua.registry_value(&task_thread_key)?;
|
|
|
|
run_registered_task(
|
|
|
|
lua,
|
|
|
|
async move {
|
|
|
|
task_wait(&task_lua, None).await?;
|
|
|
|
let thread = task_lua.registry_value::<LuaThread>(&task_thread_key)?;
|
|
|
|
if thread.status() == LuaThreadStatus::Resumable {
|
|
|
|
thread.into_async::<_, LuaMultiValue>(()).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(lua_thread_to_return)
|
2023-01-23 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn task_delay<'a>(
|
2023-01-23 21:18:20 +00:00
|
|
|
lua: &'a Lua,
|
2023-01-23 07:38:32 +00:00
|
|
|
(duration, tof): (Option<f32>, LuaValue<'a>),
|
|
|
|
) -> LuaResult<LuaThread<'a>> {
|
2023-01-23 21:18:20 +00:00
|
|
|
// Spawn a new detached task using a lua reference that we can use inside of our task
|
|
|
|
let task_lua = lua.app_data_ref::<Weak<Lua>>().unwrap().upgrade().unwrap();
|
|
|
|
let task_thread = tof_to_thread(lua, tof)?;
|
|
|
|
let task_thread_key = lua.create_registry_value(task_thread)?;
|
|
|
|
let lua_thread_to_return = lua.registry_value(&task_thread_key)?;
|
|
|
|
run_registered_task(
|
|
|
|
lua,
|
|
|
|
async move {
|
|
|
|
task_wait(&task_lua, duration).await?;
|
|
|
|
let thread = task_lua.registry_value::<LuaThread>(&task_thread_key)?;
|
2023-01-23 07:38:32 +00:00
|
|
|
if thread.status() == LuaThreadStatus::Resumable {
|
|
|
|
thread.into_async::<_, LuaMultiValue>(()).await?;
|
|
|
|
}
|
2023-01-23 21:18:20 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(lua_thread_to_return)
|
2023-01-23 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 21:18:20 +00:00
|
|
|
async fn task_spawn<'a>(lua: &'a Lua, tof: LuaValue<'a>) -> LuaResult<LuaThread<'a>> {
|
|
|
|
// Spawn a new detached task using a lua reference that we can use inside of our task
|
|
|
|
let task_lua = lua.app_data_ref::<Weak<Lua>>().unwrap().upgrade().unwrap();
|
|
|
|
let task_thread = tof_to_thread(lua, tof)?;
|
|
|
|
let task_thread_key = lua.create_registry_value(task_thread)?;
|
|
|
|
let lua_thread_to_return = lua.registry_value(&task_thread_key)?;
|
|
|
|
run_registered_task(
|
|
|
|
lua,
|
|
|
|
async move {
|
|
|
|
let thread = task_lua.registry_value::<LuaThread>(&task_thread_key)?;
|
2023-01-23 07:38:32 +00:00
|
|
|
if thread.status() == LuaThreadStatus::Resumable {
|
|
|
|
thread.into_async::<_, LuaMultiValue>(()).await?;
|
|
|
|
}
|
2023-01-23 21:18:20 +00:00
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(lua_thread_to_return)
|
2023-01-23 07:38:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn task_wait(lua: &Lua, duration: Option<f32>) -> LuaResult<f32> {
|
|
|
|
let start = Instant::now();
|
2023-01-23 21:18:20 +00:00
|
|
|
run_registered_task(
|
|
|
|
lua,
|
|
|
|
async move {
|
|
|
|
Timer::after(
|
|
|
|
duration
|
|
|
|
.map(Duration::from_secs_f32)
|
|
|
|
.unwrap_or(Duration::ZERO),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
false,
|
2023-01-23 07:38:32 +00:00
|
|
|
)
|
2023-01-23 21:18:20 +00:00
|
|
|
.await?;
|
2023-01-23 07:38:32 +00:00
|
|
|
let end = Instant::now();
|
|
|
|
Ok((end - start).as_secs_f32())
|
|
|
|
}
|