weave the thread of fate
lifetime annotations hold &mut Context while scripts are runnable
This commit is contained in:
parent
6b66009c44
commit
f5e9287798
@ -11,6 +11,7 @@ pub enum Error{
|
|||||||
},
|
},
|
||||||
RustLua(mlua::Error),
|
RustLua(mlua::Error),
|
||||||
Script(super::instance::GetScriptError),
|
Script(super::instance::GetScriptError),
|
||||||
|
NoContext,
|
||||||
}
|
}
|
||||||
impl std::fmt::Display for Error{
|
impl std::fmt::Display for Error{
|
||||||
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
|
||||||
@ -42,16 +43,37 @@ impl Runner{
|
|||||||
init(&runner.lua).map_err(Error::RustLua)?;
|
init(&runner.lua).map_err(Error::RustLua)?;
|
||||||
Ok(runner)
|
Ok(runner)
|
||||||
}
|
}
|
||||||
pub fn run_script(&self,script:super::instance::Script,context:&mut Context)->Result<(),Error>{
|
pub fn runnable_context<'a>(self,context:&'a mut Context,services:crate::place::Services)->Result<Runnable<'a>,Error>{
|
||||||
let (name,source)=script.get_name_source(context).map_err(Error::Script)?;
|
|
||||||
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
|
|
||||||
//this makes set_app_data shut up about the lifetime
|
//this makes set_app_data shut up about the lifetime
|
||||||
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
|
||||||
let r=self.lua.load(source.as_str())
|
Ok(Runnable{
|
||||||
.set_name(name)
|
lua:self.lua,
|
||||||
.exec().map_err(|error|Error::Lua{source,error});
|
_lifetime:&std::marker::PhantomData
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//Runnable is the same thing but has context set, which it holds the lifetime for.
|
||||||
|
pub struct Runnable<'a>{
|
||||||
|
lua:mlua::Lua,
|
||||||
|
_lifetime:&'a std::marker::PhantomData<()>
|
||||||
|
}
|
||||||
|
impl Runnable<'_>{
|
||||||
|
pub fn drop_context(self)->Runner{
|
||||||
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
|
||||||
r?;
|
Runner{
|
||||||
Ok(())
|
lua:self.lua,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pub fn run_script(&self,script:super::instance::Script)->Result<(),Error>{
|
||||||
|
let (name,source)={
|
||||||
|
let dom=&mut *self.lua.app_data_mut::<&'static mut rbx_dom_weak::WeakDom>().ok_or(Error::NoContext)?;
|
||||||
|
let (name,source)=script.get_name_source(Context::from_mut(dom)).map_err(Error::Script)?;
|
||||||
|
self.lua.globals().set("script",script).map_err(Error::RustLua)?;
|
||||||
|
(name,source)
|
||||||
|
};
|
||||||
|
self.lua.load(source.as_str())
|
||||||
|
.set_name(name)
|
||||||
|
.exec().map_err(|error|Error::Lua{source,error})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user