mirror of
https://github.com/lune-org/lune.git
synced 2025-04-08 04:20:54 +01:00
fixed not converting cached results
This commit is contained in:
parent
fedaf9a625
commit
25b09b18ac
4 changed files with 45 additions and 9 deletions
|
@ -30,7 +30,7 @@ impl RequireContext {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn std_exists(lua: &Lua, alias: &str) -> LuaResult<bool> {
|
||||
pub(crate) fn std_exists(lua: &Lua, alias: &str) -> LuaResult<bool> {
|
||||
let data_ref = lua
|
||||
.app_data_ref::<RequireContextData>()
|
||||
.ok_or(LuaError::runtime("Couldn't find RequireContextData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
||||
|
@ -38,13 +38,18 @@ impl RequireContext {
|
|||
Ok(data_ref.std.contains_key(alias))
|
||||
}
|
||||
|
||||
pub fn require_std(lua: &Lua, require_alias: RequireAlias) -> LuaResult<LuaMultiValue<'_>> {
|
||||
pub(crate) fn require_std(
|
||||
lua: &Lua,
|
||||
require_alias: RequireAlias,
|
||||
) -> LuaResult<LuaMultiValue<'_>> {
|
||||
let data_ref = lua
|
||||
.app_data_ref::<RequireContextData>()
|
||||
.ok_or(LuaError::runtime("Couldn't find RequireContextData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
||||
|
||||
if let Some(cached) = data_ref.std_cache.get(&require_alias) {
|
||||
return cached.into_lua(lua)?.into_lua_multi(lua);
|
||||
let multi_vec = lua.registry_value::<Vec<LuaValue>>(cached)?;
|
||||
|
||||
return Ok(LuaMultiValue::from_vec(multi_vec));
|
||||
}
|
||||
|
||||
let libraries =
|
||||
|
@ -76,7 +81,7 @@ impl RequireContext {
|
|||
Ok(multi)
|
||||
}
|
||||
|
||||
pub async fn require(
|
||||
pub(crate) async fn require(
|
||||
lua: &Lua,
|
||||
path_rel: PathBuf,
|
||||
path_abs: PathBuf,
|
||||
|
@ -105,7 +110,9 @@ impl RequireContext {
|
|||
let cache = data_ref.cache.lock().await;
|
||||
|
||||
if let Some(cached) = cache.get(&path_abs) {
|
||||
return cached.into_lua(lua).into_lua_multi(lua);
|
||||
let multi_vec = lua.registry_value::<Vec<LuaValue>>(cached)?;
|
||||
|
||||
return Ok(LuaMultiValue::from_vec(multi_vec));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ use mlua::prelude::*;
|
|||
use path::resolve_path;
|
||||
use std::path::PathBuf;
|
||||
|
||||
mod context;
|
||||
pub mod context;
|
||||
mod path;
|
||||
|
||||
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use mlua::prelude::*;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use tokio::fs;
|
||||
|
||||
/// tries these alternatives on given path:
|
||||
|
@ -14,20 +14,47 @@ pub async fn resolve_path(path: &Path) -> LuaResult<PathBuf> {
|
|||
let path = append_extension(path, ext);
|
||||
|
||||
if fs::try_exists(&path).await? {
|
||||
return Ok(path);
|
||||
return Ok(normalize_path(&path));
|
||||
};
|
||||
|
||||
// try extension on given path's init
|
||||
let init_path = append_extension(init_path, ext);
|
||||
|
||||
if fs::try_exists(&init_path).await? {
|
||||
return Ok(init_path);
|
||||
return Ok(normalize_path(&init_path));
|
||||
};
|
||||
}
|
||||
|
||||
Err(LuaError::runtime("Could not resolve path"))
|
||||
}
|
||||
|
||||
pub fn normalize_path(path: &Path) -> PathBuf {
|
||||
let mut components = path.components().peekable();
|
||||
let mut ret = if let Some(c @ Component::Prefix(..)) = components.clone().peek() {
|
||||
components.next();
|
||||
PathBuf::from(c.as_os_str())
|
||||
} else {
|
||||
PathBuf::new()
|
||||
};
|
||||
|
||||
for component in components {
|
||||
match component {
|
||||
Component::Prefix(..) => unreachable!(),
|
||||
Component::RootDir => {
|
||||
ret.push(component.as_os_str());
|
||||
}
|
||||
Component::CurDir => {}
|
||||
Component::ParentDir => {
|
||||
ret.pop();
|
||||
}
|
||||
Component::Normal(c) => {
|
||||
ret.push(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
ret
|
||||
}
|
||||
|
||||
fn append_extension(path: impl Into<PathBuf>, ext: &'static str) -> PathBuf {
|
||||
let mut new = path.into();
|
||||
match new.extension() {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
#![allow(clippy::cargo_common_metadata)]
|
||||
|
||||
pub use globals::require::context::RequireContext;
|
||||
|
||||
use mlua::prelude::*;
|
||||
|
||||
mod global;
|
||||
|
|
Loading…
Add table
Reference in a new issue