use crate::error::IsleError;
use crate::hook;
use crate::protect;
use crate::Request;
use std::sync::mpsc;
pub(crate) fn run_loop(lua: mlua::Lua, rx: mpsc::Receiver<Request>) {
while let Ok(req) = rx.recv() {
match req {
Request::Run { job, cancel } => job(&lua, &cancel),
Request::Shutdown => break,
}
}
}
pub(crate) fn execute_eval<T: mlua::FromLuaMulti>(
lua: &mlua::Lua,
code: &str,
cancel: &hook::CancelToken,
) -> Result<T, IsleError> {
crate::runtime::ensure_attached(lua)?;
let _enter = hook::EnterGuard::new(cancel);
let result = load_eval(lua, code)
.map_err(IsleError::from)
.and_then(|func| protect::call(lua, func, mlua::MultiValue::new()));
let values = cancelled_wins(result, cancel)?;
Ok(T::from_lua_multi(values, lua)?)
}
pub(crate) const EVAL_CHUNK: &str = "=eval";
fn load_eval(lua: &mlua::Lua, code: &str) -> mlua::Result<mlua::Function> {
match lua
.load(format!("return {code}"))
.set_name(EVAL_CHUNK)
.into_function()
{
Ok(f) => Ok(f),
Err(_) => lua.load(code).set_name(EVAL_CHUNK).into_function(),
}
}
pub(crate) fn cancelled_wins<T>(
result: Result<T, IsleError>,
cancel: &hook::CancelToken,
) -> Result<T, IsleError> {
match result {
Err(_) if cancel.is_cancelled() => Err(IsleError::Cancelled),
other => other,
}
}
pub(crate) fn execute_exec<T>(
lua: &mlua::Lua,
f: impl FnOnce(&mlua::Lua) -> Result<T, IsleError>,
cancel: &hook::CancelToken,
) -> Result<T, IsleError> {
crate::runtime::ensure_attached(lua)?;
let _enter = hook::EnterGuard::new(cancel);
f(lua)
}
pub(crate) fn global_function(lua: &mlua::Lua, name: &str) -> Result<mlua::Function, IsleError> {
match lua.globals().get::<mlua::Value>(name)? {
mlua::Value::Function(f) => Ok(f),
_ => Err(IsleError::NotFound(name.to_string())),
}
}
pub(crate) fn execute_call<A: mlua::IntoLuaMulti, T: mlua::FromLuaMulti>(
lua: &mlua::Lua,
func_name: &str,
args: A,
cancel: &hook::CancelToken,
) -> Result<T, IsleError> {
crate::runtime::ensure_attached(lua)?;
let _enter = hook::EnterGuard::new(cancel);
let func = global_function(lua, func_name)?;
let multi = args.into_lua_multi(lua)?;
let values = cancelled_wins(protect::call(lua, func, multi), cancel)?;
Ok(T::from_lua_multi(values, lua)?)
}