mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
rename storage to context
This commit is contained in:
parent
370c7f8187
commit
cbd7cb6480
2 changed files with 18 additions and 18 deletions
|
@ -4,19 +4,19 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
/// The private struct that's stored in mlua's app data container
|
/// The private struct that's stored in mlua's app data container
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct RequireStorageData<'a> {
|
struct RequireContextData<'a> {
|
||||||
std: HashMap<&'a str, HashMap<&'a str, Box<dyn StandardLibrary>>>,
|
std: HashMap<&'a str, HashMap<&'a str, Box<dyn StandardLibrary>>>,
|
||||||
std_cache: HashMap<RequireAlias, LuaRegistryKey>,
|
std_cache: HashMap<RequireAlias, LuaRegistryKey>,
|
||||||
cache: HashMap<&'a str, LuaRegistryKey>,
|
cache: HashMap<&'a str, LuaRegistryKey>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct RequireStorage {}
|
pub struct RequireContext {}
|
||||||
|
|
||||||
impl RequireStorage {
|
impl RequireContext {
|
||||||
pub fn init(lua: &Lua) -> LuaResult<()> {
|
pub fn init(lua: &Lua) -> LuaResult<()> {
|
||||||
if lua.set_app_data(RequireStorageData::default()).is_some() {
|
if lua.set_app_data(RequireContextData::default()).is_some() {
|
||||||
Err(LuaError::runtime("RequireStorage::init got called twice"))
|
Err(LuaError::runtime("RequireContext::init got called twice"))
|
||||||
} else {
|
} else {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -24,16 +24,16 @@ impl RequireStorage {
|
||||||
|
|
||||||
pub fn std_exists(lua: &Lua, alias: &str) -> LuaResult<bool> {
|
pub fn std_exists(lua: &Lua, alias: &str) -> LuaResult<bool> {
|
||||||
let data_ref = lua
|
let data_ref = lua
|
||||||
.app_data_ref::<RequireStorageData>()
|
.app_data_ref::<RequireContextData>()
|
||||||
.ok_or(LuaError::runtime("Couldn't find RequireStorageData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
.ok_or(LuaError::runtime("Couldn't find RequireContextData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
||||||
|
|
||||||
Ok(data_ref.std.contains_key(alias))
|
Ok(data_ref.std.contains_key(alias))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn require_std(lua: &Lua, require_alias: RequireAlias) -> LuaResult<LuaMultiValue<'_>> {
|
pub fn require_std(lua: &Lua, require_alias: RequireAlias) -> LuaResult<LuaMultiValue<'_>> {
|
||||||
let data_ref = lua
|
let data_ref = lua
|
||||||
.app_data_ref::<RequireStorageData>()
|
.app_data_ref::<RequireContextData>()
|
||||||
.ok_or(LuaError::runtime("Couldn't find RequireStorageData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
.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) {
|
if let Some(cached) = data_ref.std_cache.get(&require_alias) {
|
||||||
return cached.into_lua(lua)?.into_lua_multi(lua);
|
return cached.into_lua(lua)?.into_lua_multi(lua);
|
||||||
|
@ -60,8 +60,8 @@ impl RequireStorage {
|
||||||
let multi_reg = lua.create_registry_value(mutli_clone.into_vec())?;
|
let multi_reg = lua.create_registry_value(mutli_clone.into_vec())?;
|
||||||
|
|
||||||
let mut data = lua
|
let mut data = lua
|
||||||
.app_data_mut::<RequireStorageData>()
|
.app_data_mut::<RequireContextData>()
|
||||||
.ok_or(LuaError::runtime("Couldn't find RequireStorageData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
.ok_or(LuaError::runtime("Couldn't find RequireContextData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
||||||
|
|
||||||
data.std_cache.insert(require_alias, multi_reg);
|
data.std_cache.insert(require_alias, multi_reg);
|
||||||
|
|
||||||
|
@ -74,8 +74,8 @@ impl RequireStorage {
|
||||||
std: impl StandardLibrary + 'static,
|
std: impl StandardLibrary + 'static,
|
||||||
) -> LuaResult<()> {
|
) -> LuaResult<()> {
|
||||||
let mut data = lua
|
let mut data = lua
|
||||||
.app_data_mut::<RequireStorageData>()
|
.app_data_mut::<RequireContextData>()
|
||||||
.ok_or(LuaError::runtime("Couldn't find RequireStorageData in app data container, make sure RequireStorage::init is called on this lua instance"))?;
|
.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(map) = data.std.get_mut(alias) {
|
if let Some(map) = data.std.get_mut(alias) {
|
||||||
map.insert(std.name(), Box::new(std));
|
map.insert(std.name(), Box::new(std));
|
|
@ -2,15 +2,15 @@ use crate::{luaurc::path_to_alias, path::get_parent_path, LuneStandardLibrary};
|
||||||
use mlua::prelude::*;
|
use mlua::prelude::*;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
pub mod storage;
|
pub mod context;
|
||||||
|
|
||||||
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
||||||
let require_path_rel = PathBuf::from(path);
|
let require_path_rel = PathBuf::from(path);
|
||||||
let require_alias = path_to_alias(&require_path_rel)?;
|
let require_alias = path_to_alias(&require_path_rel)?;
|
||||||
|
|
||||||
if let Some(require_alias) = require_alias {
|
if let Some(require_alias) = require_alias {
|
||||||
if storage::RequireStorage::std_exists(lua, &require_alias.alias)? {
|
if context::RequireContext::std_exists(lua, &require_alias.alias)? {
|
||||||
storage::RequireStorage::require_std(lua, require_alias)
|
context::RequireContext::require_std(lua, require_alias)
|
||||||
} else {
|
} else {
|
||||||
Err(LuaError::runtime(format!(
|
Err(LuaError::runtime(format!(
|
||||||
"Tried requiring a custom alias '{}'\nbut aliases are not implemented yet.",
|
"Tried requiring a custom alias '{}'\nbut aliases are not implemented yet.",
|
||||||
|
@ -31,10 +31,10 @@ pub async fn lua_require(lua: &Lua, path: String) -> LuaResult<LuaMultiValue> {
|
||||||
pub fn create(lua: &Lua) -> LuaResult<LuaValue> {
|
pub fn create(lua: &Lua) -> LuaResult<LuaValue> {
|
||||||
let f = lua.create_async_function(lua_require)?;
|
let f = lua.create_async_function(lua_require)?;
|
||||||
|
|
||||||
storage::RequireStorage::init(lua)?;
|
context::RequireContext::init(lua)?;
|
||||||
|
|
||||||
for std in LuneStandardLibrary::ALL {
|
for std in LuneStandardLibrary::ALL {
|
||||||
storage::RequireStorage::inject_std(lua, "lune", *std)?;
|
context::RequireContext::inject_std(lua, "lune", *std)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
f.into_lua(lua)
|
f.into_lua(lua)
|
||||||
|
|
Loading…
Add table
Reference in a new issue