Andy Weidenbaum 2021-03-24 07:43:52 +11:00
parent 801a04802a
commit 406c84dabf
2 changed files with 66 additions and 29 deletions

View File

@ -1,5 +1,5 @@
use rlua::prelude::LuaError; use rlua::prelude::LuaError;
use rlua::{Context, MetaMethod, RegistryKey, Table, UserData, UserDataMethods, Value}; use rlua::{Context, Function, MetaMethod, RegistryKey, Table, UserData, UserDataMethods, Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
@ -132,20 +132,38 @@ where
/// up an `rlua::Context` with Rust code. /// up an `rlua::Context` with Rust code.
/// ///
/// Enables exposing `UserData` types to an `rlua::Context`. /// Enables exposing `UserData` types to an `rlua::Context`.
///
/// Closures must return an `rlua::Result`-wrapped `&'static str`. This string is
/// subsequently passed into `rlua::Context.load()` and evaluated.
pub struct ClosureSearcher { pub struct ClosureSearcher {
/// Closures must accept an `rlua::Context` as their only parameter, and can do with /// Closures must accept three parameters:
/// it what they wish. Closures must return an `rlua::Result`-wrapped `&'static str`. ///
modules: HashMap<&'static str, Box<dyn Fn(Context) -> rlua::Result<&'static str> + Send>>, /// 1. An `rlua::Context`, which the closure can do what it wants with.
///
/// 2. An `rlua::Table` containing globals (i.e. Luas `_G`), which can be passed
/// to `Chunk.set_environment()`.
///
/// 3. The name of the module to be loaded (`&str`).
///
/// Closures must return an `rlua::Result`-wrapped `Function`. This `Function`
/// acts as the module loader.
modules: HashMap<
&'static str,
Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
+ Send,
>,
>,
globals: RegistryKey, globals: RegistryKey,
} }
impl ClosureSearcher { impl ClosureSearcher {
pub fn new( pub fn new(
modules: HashMap<&'static str, Box<dyn Fn(Context) -> rlua::Result<&'static str> + Send>>, modules: HashMap<
&'static str,
Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
+ Send,
>,
>,
globals: RegistryKey, globals: RegistryKey,
) -> Self { ) -> Self {
Self { modules, globals } Self { modules, globals }
@ -157,22 +175,20 @@ impl UserData for ClosureSearcher {
where where
M: UserDataMethods<'lua, Self>, M: UserDataMethods<'lua, Self>,
{ {
methods.add_meta_method(MetaMethod::Call, |lua_ctx, this, name: String| { methods.add_meta_method(
MetaMethod::Call,
|lua_ctx: Context<'lua>, this, name: String| {
let name = name.as_str(); let name = name.as_str();
match this.modules.get(name) { match this.modules.get(name) {
Some(ref loader) => { Some(ref closure) => Ok(Value::Function(closure(
let content = loader(lua_ctx)?; lua_ctx,
Ok(Value::Function( lua_ctx.registry_value::<Table>(&this.globals)?,
lua_ctx name,
.load(content) )?)),
.set_name(name)?
.set_environment(lua_ctx.registry_value::<Table>(&this.globals)?)?
.into_function()?,
))
}
None => Ok(Value::Nil), None => Ok(Value::Nil),
} }
}); },
);
} }
} }
@ -207,7 +223,13 @@ pub trait AddSearcher {
/// setup. /// setup.
fn add_closure_searcher( fn add_closure_searcher(
&self, &self,
modules: HashMap<&'static str, Box<dyn Fn(Context) -> rlua::Result<&'static str> + Send>>, modules: HashMap<
&'static str,
Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
+ Send,
>,
>,
) -> Result<()>; ) -> Result<()>;
} }
@ -266,7 +288,13 @@ impl<'a> AddSearcher for Context<'a> {
fn add_closure_searcher( fn add_closure_searcher(
&self, &self,
modules: HashMap<&'static str, Box<dyn Fn(Context) -> rlua::Result<&'static str> + Send>>, modules: HashMap<
&'static str,
Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
+ Send,
>,
>,
) -> Result<()> { ) -> Result<()> {
let globals = self.globals(); let globals = self.globals();
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?; let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;

View File

@ -1,4 +1,4 @@
use rlua::{Context, Lua, Table, UserData, UserDataMethods, Value}; use rlua::{Context, Function, Lua, Table, UserData, UserDataMethods, Value};
use rlua_searcher::{AddSearcher, Result}; use rlua_searcher::{AddSearcher, Result};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File; use std::fs::File;
@ -166,10 +166,15 @@ fn add_closure_searcher_works() {
let mut modules: HashMap< let mut modules: HashMap<
&'static str, &'static str,
Box<dyn Fn(Context) -> rlua::Result<&'static str> + Send>, Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
+ Send,
>,
> = HashMap::new(); > = HashMap::new();
let instrument_loader = Box::new(|lua_ctx: Context| { let instrument_loader: Box<
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>> + Send,
> = Box::new(|lua_ctx, env, name| {
let globals = lua_ctx.globals(); let globals = lua_ctx.globals();
let new = lua_ctx.create_function(|_, (name, sound): (String, String)| { let new = lua_ctx.create_function(|_, (name, sound): (String, String)| {
Ok(Instrument::new(name, sound)) Ok(Instrument::new(name, sound))
@ -177,7 +182,11 @@ fn add_closure_searcher_works() {
let tbl = lua_ctx.create_table()?; let tbl = lua_ctx.create_table()?;
tbl.set("new", new)?; tbl.set("new", new)?;
globals.set("instrument", tbl)?; globals.set("instrument", tbl)?;
Ok("return instrument") Ok(lua_ctx
.load("return instrument")
.set_name(name)?
.set_environment(env)?
.into_function()?)
}); });
modules.insert("instrument", instrument_loader); modules.insert("instrument", instrument_loader);