the "transmute lifetimes" solution

This commit is contained in:
Quaternions 2024-09-20 13:51:17 -07:00
parent acfdc1bd5e
commit aa0bee9f69
3 changed files with 17 additions and 18 deletions

View File

@ -10,16 +10,17 @@ pub fn class_is_a(class:&str,superclass:&str)->bool{
) )
} }
#[repr(transparent)]
pub struct Context{ pub struct Context{
pub(crate)dom:WeakDom, pub(crate)dom:WeakDom,
} }
impl Context{ impl Context{
pub fn new(dom:WeakDom)->Context{ pub const fn new(dom:WeakDom)->Context{
Context{dom} Context{dom}
} }
pub fn into_inner(self)->WeakDom{ pub fn from_mut(dom:&mut WeakDom)->&mut Context{
self.dom unsafe{&mut *(dom as *mut WeakDom as *mut Context)}
} }
/// Creates an iterator over all items of a particular class. /// Creates an iterator over all items of a particular class.
pub fn superclass_iter<'a>(&'a self,superclass:&'a str)->impl Iterator<Item=Ref>+'a{ pub fn superclass_iter<'a>(&'a self,superclass:&'a str)->impl Iterator<Item=Ref>+'a{

View File

@ -26,7 +26,8 @@ impl Instance{
// LMAO look at this function! // LMAO look at this function!
fn dom<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{ fn dom<T>(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result<T>)->mlua::Result<T>{
f(&mut *lua.app_data_mut::<WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?) let mut dom=lua.app_data_mut::<&'static mut WeakDom>().ok_or(mlua::Error::runtime("DataModel missing"))?;
f(&mut *dom)
} }
fn coerce_float(value:&mlua::Value)->Option<f32>{ fn coerce_float(value:&mlua::Value)->Option<f32>{

View File

@ -76,19 +76,16 @@ impl Runner{
init(&runner.lua)?; init(&runner.lua)?;
Ok(runner) Ok(runner)
} }
pub fn run_script(&self,script:crate::script::Script,context:Context)->(Context,Result<(),Error>){ pub fn run_script(&self,script:crate::script::Script,context:&mut Context)->Result<(),Error>{
let yoink=script.name_source(&context); let yoink=script.name_source(context);
self.lua.set_app_data(context.dom); //this makes set_app_data shut up about the lifetime
let r=(||{ self.lua.set_app_data::<&'static mut rbx_dom_weak::WeakDom>(unsafe{core::mem::transmute(&mut context.dom)});
let (name,source)=yoink.map_err(Error::Script)?; let (name,source)=yoink.map_err(Error::Script)?;
self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(Error::Lua)?; self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(Error::Lua)?;
self.lua.load(source) self.lua.load(source)
.set_name(name) .set_name(name)
.exec().map_err(Error::Lua)?; .exec().map_err(Error::Lua)?;
Ok(()) self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>();
})(); Ok(())
let dom=self.lua.remove_app_data()
.expect("Fatal: Lua lost the dom");
(Context::new(dom),r)
} }
} }