add StaticClosureSearcher
because evidently one does not merely accept `&'static str` OR `String`
This commit is contained in:
parent
9d6faef3d3
commit
76425c0aeb
@ -136,8 +136,8 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like `StaticSearcher`, but with closures as `modules` values, to facilitate setting
|
/// Like `Searcher`, but with closures as `modules` values, to facilitate setting up an
|
||||||
/// up an `rlua::Context` with Rust code.
|
/// `rlua::Context` with Rust code.
|
||||||
///
|
///
|
||||||
/// Enables exposing `UserData` types to an `rlua::Context`.
|
/// Enables exposing `UserData` types to an `rlua::Context`.
|
||||||
pub struct ClosureSearcher {
|
pub struct ClosureSearcher {
|
||||||
@ -200,6 +200,56 @@ impl UserData for ClosureSearcher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Like `ClosureSearcher`, but with `&'static str` keys in `modules`.
|
||||||
|
pub struct StaticClosureSearcher {
|
||||||
|
modules: HashMap<
|
||||||
|
&'static str,
|
||||||
|
Box<
|
||||||
|
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||||
|
+ Send,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
|
||||||
|
globals: RegistryKey,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StaticClosureSearcher {
|
||||||
|
pub fn new(
|
||||||
|
modules: HashMap<
|
||||||
|
&'static str,
|
||||||
|
Box<
|
||||||
|
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||||
|
+ Send,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
globals: RegistryKey,
|
||||||
|
) -> Self {
|
||||||
|
Self { modules, globals }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UserData for StaticClosureSearcher {
|
||||||
|
fn add_methods<'lua, M>(methods: &mut M)
|
||||||
|
where
|
||||||
|
M: UserDataMethods<'lua, Self>,
|
||||||
|
{
|
||||||
|
methods.add_meta_method(
|
||||||
|
MetaMethod::Call,
|
||||||
|
|lua_ctx: Context<'lua>, this, name: String| {
|
||||||
|
let name = name.as_str();
|
||||||
|
match this.modules.get(name) {
|
||||||
|
Some(ref closure) => Ok(Value::Function(closure(
|
||||||
|
lua_ctx,
|
||||||
|
lua_ctx.registry_value::<Table>(&this.globals)?,
|
||||||
|
name,
|
||||||
|
)?)),
|
||||||
|
None => Ok(Value::Nil),
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Extend `rlua::Context` to support `require`ing Lua modules by name.
|
/// Extend `rlua::Context` to support `require`ing Lua modules by name.
|
||||||
pub trait AddSearcher {
|
pub trait AddSearcher {
|
||||||
/// Add a `HashMap` of Lua modules indexed by module name to Lua’s
|
/// Add a `HashMap` of Lua modules indexed by module name to Lua’s
|
||||||
@ -227,8 +277,7 @@ pub trait AddSearcher {
|
|||||||
where
|
where
|
||||||
P: 'static + AsRef<Path> + Send;
|
P: 'static + AsRef<Path> + Send;
|
||||||
|
|
||||||
/// Like `add_static_searcher`, but with user-provided closure for `rlua::Context`
|
/// Like `add_searcher`, but with user-provided closure for `rlua::Context` setup.
|
||||||
/// setup.
|
|
||||||
fn add_closure_searcher(
|
fn add_closure_searcher(
|
||||||
&self,
|
&self,
|
||||||
modules: HashMap<
|
modules: HashMap<
|
||||||
@ -239,6 +288,18 @@ pub trait AddSearcher {
|
|||||||
>,
|
>,
|
||||||
>,
|
>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
|
/// Like `add_closure_searcher`, but with `&'static str` keys in `modules`.
|
||||||
|
fn add_static_closure_searcher(
|
||||||
|
&self,
|
||||||
|
modules: HashMap<
|
||||||
|
&'static str,
|
||||||
|
Box<
|
||||||
|
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||||
|
+ Send,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> AddSearcher for Context<'a> {
|
impl<'a> AddSearcher for Context<'a> {
|
||||||
@ -312,4 +373,23 @@ impl<'a> AddSearcher for Context<'a> {
|
|||||||
.set(searchers.len()? + 1, searcher)
|
.set(searchers.len()? + 1, searcher)
|
||||||
.map_err(|e| e.into())
|
.map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn add_static_closure_searcher(
|
||||||
|
&self,
|
||||||
|
modules: HashMap<
|
||||||
|
&'static str,
|
||||||
|
Box<
|
||||||
|
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||||
|
+ Send,
|
||||||
|
>,
|
||||||
|
>,
|
||||||
|
) -> Result<()> {
|
||||||
|
let globals = self.globals();
|
||||||
|
let searchers: Table = globals.get::<_, Table>("package")?.get("searchers")?;
|
||||||
|
let registry_key = self.create_registry_value(globals)?;
|
||||||
|
let searcher = StaticClosureSearcher::new(modules, registry_key);
|
||||||
|
searchers
|
||||||
|
.set(searchers.len()? + 1, searcher)
|
||||||
|
.map_err(|e| e.into())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -212,6 +212,58 @@ fn add_closure_searcher_works() {
|
|||||||
assert_eq!(sound, "The ukulele goes twang");
|
assert_eq!(sound, "The ukulele goes twang");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn add_static_closure_searcher_works() {
|
||||||
|
let lua = Lua::new();
|
||||||
|
|
||||||
|
let mut modules: HashMap<
|
||||||
|
&'static str,
|
||||||
|
Box<
|
||||||
|
dyn for<'ctx> Fn(Context<'ctx>, Table<'ctx>, &str) -> rlua::Result<Function<'ctx>>
|
||||||
|
+ Send,
|
||||||
|
>,
|
||||||
|
> = HashMap::new();
|
||||||
|
|
||||||
|
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 new = lua_ctx.create_function(|_, (name, sound): (String, String)| {
|
||||||
|
Ok(Instrument::new(name, sound))
|
||||||
|
})?;
|
||||||
|
let tbl = lua_ctx.create_table()?;
|
||||||
|
tbl.set("new", new)?;
|
||||||
|
globals.set("instrument", tbl)?;
|
||||||
|
Ok(lua_ctx
|
||||||
|
.load("return instrument")
|
||||||
|
.set_name(name)?
|
||||||
|
.set_environment(env)?
|
||||||
|
.into_function()?)
|
||||||
|
});
|
||||||
|
|
||||||
|
modules.insert("instrument", instrument_loader);
|
||||||
|
|
||||||
|
let sound = lua
|
||||||
|
.context::<_, Result<String>>(|lua_ctx| {
|
||||||
|
lua_ctx.add_static_closure_searcher(modules)?;
|
||||||
|
|
||||||
|
// Ensure global variable `instrument` is unset.
|
||||||
|
let nil: String = lua_ctx.load("return type(instrument)").eval()?;
|
||||||
|
assert_eq!(nil, "nil");
|
||||||
|
|
||||||
|
Ok(lua_ctx
|
||||||
|
.load(
|
||||||
|
r#"local instrument = require("instrument")
|
||||||
|
local ukulele = instrument.new("ukulele", "twang")
|
||||||
|
return ukulele:play()"#,
|
||||||
|
)
|
||||||
|
.eval()?)
|
||||||
|
})
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
assert_eq!(sound, "The ukulele goes twang");
|
||||||
|
}
|
||||||
|
|
||||||
struct Instrument {
|
struct Instrument {
|
||||||
name: String,
|
name: String,
|
||||||
sound: String,
|
sound: String,
|
||||||
|
Loading…
Reference in New Issue
Block a user