Optimize more

pull/416/head
Arijit Basu 3 years ago committed by Arijit Basu
parent 41c387542f
commit 5b710070a3

@ -98,15 +98,52 @@ pub fn extend(lua: &Lua, path: &str) -> Result<Config> {
Ok(config)
}
/// Used to call lua functions.
fn resolve_fn_recursive<'lua, 'a>(
table: &mlua::Table<'lua>,
mut path: impl Iterator<Item = &'a str>,
) -> Result<mlua::Function<'lua>> {
if let Some(nxt) = path.next() {
match table.get(nxt)? {
mlua::Value::Table(t) => resolve_fn_recursive(&t, path),
mlua::Value::Function(f) => Ok(f),
t => bail!("{:?} is not a function", t),
}
} else {
bail!("Invalid path")
}
}
/// This function resolves paths like `builtin.func_foo`, `custom.func_bar` into lua functions.
pub fn resolve_fn<'lua>(
globals: &mlua::Table<'lua>,
path: &str,
) -> Result<mlua::Function<'lua>> {
resolve_fn_recursive(globals, path.split('.'))
}
pub fn call<'lua, R: Deserialize<'lua>>(
lua: &'lua Lua,
func: &str,
arg: mlua::Value<'lua>,
) -> Result<R> {
let func = format!("xplr.__CACHE__.caller(xplr.fn.{})", func);
let caller: mlua::Function = lua.load(&func).eval()?;
let res: mlua::Value = caller.call(arg)?;
let func = format!("xplr.fn.{}", func);
let func = resolve_fn(&lua.globals(), &func)?;
let res: mlua::Value = func.call(arg)?;
let res: R = lua.from_value(res)?;
Ok(res)
}
/// Used to call lua functions with cache support.
pub fn call_with_cache<'lua, R: Deserialize<'lua>>(
lua: &'lua Lua,
func: &str,
arg: mlua::Value<'lua>,
) -> Result<R> {
let caller: mlua::Function =
resolve_fn(&lua.globals(), "xplr.__CACHE__.call")?;
let func = format!("xplr.fn.{}", func);
let func: mlua::Function = resolve_fn(&lua.globals(), &func)?;
let res: mlua::Value = caller.call((func, arg))?;
let res: R = lua.from_value(res)?;
Ok(res)
}
@ -114,7 +151,7 @@ pub fn call<'lua, R: Deserialize<'lua>>(
/// Used to cache the directory nodes.
pub fn cache_directory_nodes(lua: &Lua, nodes: &[Node]) -> Result<()> {
let func = "xplr.__CACHE__.set_directory_nodes";
let func: mlua::Function = lua.load(func).eval()?;
let func: mlua::Function = resolve_fn(&lua.globals(), func)?;
func.call(lua.to_value(nodes)?)?;
Ok(())
}

@ -40,7 +40,7 @@ fn call_lua(
) -> Result<Option<Vec<app::ExternalMsg>>> {
let arg = app.to_lua_arg();
let arg = lua.to_value(&arg)?;
lua::call(lua, func, arg)
lua::call_with_cache(lua, func, arg)
}
fn call(app: &app::App, cmd: app::Command, silent: bool) -> Result<ExitStatus> {

@ -1034,7 +1034,7 @@ pub fn draw_custom_content<B: Backend>(
let render = lua
.to_value(&ctx)
.map(|arg| {
lua::call(lua, &render, arg)
lua::call_with_cache(lua, &render, arg)
.unwrap_or_else(|e| format!("{:?}", e))
})
.unwrap_or_else(|e| e.to_string());
@ -1076,7 +1076,7 @@ pub fn draw_custom_content<B: Backend>(
let items = lua
.to_value(&ctx)
.map(|arg| {
lua::call(lua, &render, arg)
lua::call_with_cache(lua, &render, arg)
.unwrap_or_else(|e| vec![format!("{:?}", e)])
})
.unwrap_or_else(|e| vec![e.to_string()])
@ -1146,7 +1146,7 @@ pub fn draw_custom_content<B: Backend>(
let rows = lua
.to_value(&ctx)
.map(|arg| {
lua::call(lua, &render, arg)
lua::call_with_cache(lua, &render, arg)
.unwrap_or_else(|e| vec![vec![format!("{:?}", e)]])
})
.unwrap_or_else(|e| vec![vec![e.to_string()]])

Loading…
Cancel
Save