implement PathSearcher

to facilitate module reloading
This commit is contained in:
Andy Weidenbaum 2021-02-26 19:57:13 +11:00
parent a331b2bf8b
commit cf3a90faca
5 changed files with 209 additions and 2 deletions

View File

@ -1,6 +1,7 @@
mod error;
mod searcher;
mod types;
mod utils;
pub use crate::error::Error;
pub use crate::searcher::AddSearcher;

View File

@ -1,7 +1,11 @@
use rlua::prelude::LuaError;
use rlua::{Context, MetaMethod, RegistryKey, Table, UserData, UserDataMethods, Value};
use std::collections::HashMap;
use std::io::Read;
use std::path::Path;
use crate::types::Result;
use crate::utils;
/// Stores Lua modules indexed by module name, and provides an `rlua::MetaMethod`
/// to enable `require`ing the stored modules by name in an `rlua::Context`.
@ -53,7 +57,7 @@ impl StaticSearcher {
impl UserData for StaticSearcher {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::Call, |lua_ctx, this, name: String| {
match this.modules.get(&name.as_str()) {
match this.modules.get(name.as_str()) {
Some(content) => Ok(Value::Function(
lua_ctx
.load(content)
@ -67,6 +71,62 @@ impl UserData for StaticSearcher {
}
}
/// Like `Searcher`, but with `modules` values given as paths to files containing Lua
/// source code to facilitate module reloading.
struct PathSearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
modules: HashMap<String, P>,
globals: RegistryKey,
}
impl<P> PathSearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
fn new(modules: HashMap<String, P>, globals: RegistryKey) -> Self {
Self { modules, globals }
}
}
impl<P> UserData for PathSearcher<P>
where
P: 'static + AsRef<Path> + Send,
{
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_meta_method(MetaMethod::Call, |lua_ctx, this, name: String| {
match this.modules.get(&name) {
Some(ref path) => {
let path = path.as_ref();
// Ensure `module_path` is relative to `$CARGO_MANIFEST_DIR`.
let path = if path.is_relative() {
utils::runtime_root().join(path)
} else {
path.to_path_buf()
};
let mut content = String::new();
let mut file = std::fs::File::open(path)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
file.read_to_string(&mut content)
.map_err(|e| LuaError::RuntimeError(format!("io error: {:#?}", e)))?;
Ok(Value::Function(
lua_ctx
.load(&content)
.set_name(&name)?
.set_environment(lua_ctx.registry_value::<Table>(&this.globals)?)?
.into_function()?,
))
}
None => Ok(Value::Nil),
}
});
}
}
/// Extend `rlua::Context` to support `require`ing Lua modules by name.
pub trait AddSearcher {
/// Add a `HashMap` of Lua modules indexed by module name to Luas
@ -77,6 +137,12 @@ pub trait AddSearcher {
/// Like `add_searcher`, but with Fennel source code encoded as `&'static str`
/// to facilitate compile-time includes.
fn add_static_searcher(&self, modules: HashMap<&'static str, &'static str>) -> Result<()>;
/// Like `add_searcher`, but with `modules` values given as paths to files containing
/// Lua source code to facilitate module reloading.
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
where
P: 'static + AsRef<Path> + Send;
}
impl<'a> AddSearcher for Context<'a> {
@ -99,4 +165,17 @@ impl<'a> AddSearcher for Context<'a> {
.set(searchers.len()? + 1, searcher)
.map_err(|e| e.into())
}
fn add_path_searcher<P>(&self, modules: HashMap<String, P>) -> Result<()>
where
P: 'static + AsRef<Path> + Send,
{
let globals = self.globals();
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;
let registry_key = self.create_registry_value(globals)?;
let searcher = PathSearcher::new(modules, registry_key);
searchers
.set(searchers.len()? + 1, searcher)
.map_err(|e| e.into())
}
}

6
src/utils.rs Normal file
View File

@ -0,0 +1,6 @@
use std::path::PathBuf;
/// Return the value of `$CARGO_MANIFEST_DIR` at runtime.
pub(crate) fn runtime_root() -> PathBuf {
PathBuf::new().join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
}

1
tests/data/lume.lua Normal file
View File

@ -0,0 +1 @@
return "hello lume"

View File

@ -1,6 +1,9 @@
use rlua::Lua;
use rlua::{Lua, Table, Value};
use rlua_searcher::{AddSearcher, Result};
use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
#[test]
fn add_searcher_works() {
@ -40,6 +43,123 @@ fn add_static_searcher_works() {
assert_eq!("hello lume", hello);
}
#[test]
fn add_path_searcher_works() {
let name = "lume".to_string();
let path = PathBuf::new()
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("tests")
.join("data")
.join("lume.lua");
let mut map = HashMap::new();
map.insert(name, path);
let lua = Lua::new();
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
lua_ctx.add_path_searcher(map)?;
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
}
#[test]
fn module_reloading_works() {
let name = "lume".to_string();
let path = PathBuf::new()
.join(std::env::var("CARGO_MANIFEST_DIR").unwrap())
.join("tests")
.join("data")
.join("lume.lua");
let mut map = HashMap::new();
map.insert(name.clone(), path.clone());
let lua = Lua::new();
// Add searcher for lume module on disk, and read from it.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
lua_ctx.add_path_searcher(map)?;
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
// Twice.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
// Modify lume module on disk.
let mut out = File::create(path.clone()).expect("Could not create Lume module on disk");
write!(out, "{}\n", r#"return "hello again lume""#)
.expect("Could not modify Lume module on disk");
// Thrice. Should still be unchanged due to caching.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
// Remove lume module from Luas `package.loaded` cache to facilitate reload.
lua.context::<_, rlua::Result<()>>(|lua_ctx| {
let globals = lua_ctx.globals();
let loaded: Table = globals.get::<_, Table>("package")?.get("loaded")?;
loaded.set(name.clone(), Value::Nil)
})
.unwrap();
// Re-read from lume module on disk.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello again lume", hello);
// Twice.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello again lume", hello);
// Revert changes to lume module on disk.
let mut out = File::create(path).expect("Could not create Lume module on disk");
write!(out, "{}\n", r#"return "hello lume""#).expect("Could not modify Lume module on disk");
// Clear cache again.
lua.context::<_, rlua::Result<()>>(|lua_ctx| {
let globals = lua_ctx.globals();
let loaded: Table = globals.get::<_, Table>("package")?.get("loaded")?;
loaded.set(name, Value::Nil)
})
.unwrap();
// Ensure changes have been successfully reverted.
let hello = lua
.context::<_, Result<String>>(|lua_ctx| {
Ok(lua_ctx.load(r#"return require("lume")"#).eval()?)
})
.unwrap();
assert_eq!("hello lume", hello);
}
fn read_lume_to_string() -> String {
r#"return "hello lume""#.to_string()
}