add StaticClosureSearcher
because evidently one does not merely accept `&'static str` OR `String`
This commit is contained in:
@@ -212,6 +212,58 @@ fn add_closure_searcher_works() {
|
||||
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 {
|
||||
name: String,
|
||||
sound: String,
|
||||
|
Reference in New Issue
Block a user