From aa0bee9f69822b4b781c300e35458d25f2c8a884 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 20 Sep 2024 13:51:17 -0700 Subject: [PATCH] the "transmute lifetimes" solution --- src/context.rs | 7 ++++--- src/runner/instance.rs | 3 ++- src/runner/runner.rs | 25 +++++++++++-------------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/context.rs b/src/context.rs index cc5f4e0..4ca6f66 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,16 +10,17 @@ pub fn class_is_a(class:&str,superclass:&str)->bool{ ) } +#[repr(transparent)] pub struct Context{ pub(crate)dom:WeakDom, } impl Context{ - pub fn new(dom:WeakDom)->Context{ + pub const fn new(dom:WeakDom)->Context{ Context{dom} } - pub fn into_inner(self)->WeakDom{ - self.dom + pub fn from_mut(dom:&mut WeakDom)->&mut Context{ + unsafe{&mut *(dom as *mut WeakDom as *mut Context)} } /// Creates an iterator over all items of a particular class. pub fn superclass_iter<'a>(&'a self,superclass:&'a str)->impl Iterator+'a{ diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 5ef7b6a..1289826 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -26,7 +26,8 @@ impl Instance{ // LMAO look at this function! fn dom(lua:&mlua::Lua,mut f:impl FnMut(&mut WeakDom)->mlua::Result)->mlua::Result{ - f(&mut *lua.app_data_mut::().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{ diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 0d68d5e..fc0fbb6 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -76,19 +76,16 @@ impl Runner{ init(&runner.lua)?; Ok(runner) } - pub fn run_script(&self,script:crate::script::Script,context:Context)->(Context,Result<(),Error>){ - let yoink=script.name_source(&context); - self.lua.set_app_data(context.dom); - let r=(||{ - let (name,source)=yoink.map_err(Error::Script)?; - self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(Error::Lua)?; - self.lua.load(source) - .set_name(name) - .exec().map_err(Error::Lua)?; - Ok(()) - })(); - let dom=self.lua.remove_app_data() - .expect("Fatal: Lua lost the dom"); - (Context::new(dom),r) + pub fn run_script(&self,script:crate::script::Script,context:&mut Context)->Result<(),Error>{ + let yoink=script.name_source(context); + //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)}); + let (name,source)=yoink.map_err(Error::Script)?; + self.lua.globals().set("script",super::instance::Instance::from(script)).map_err(Error::Lua)?; + self.lua.load(source) + .set_name(name) + .exec().map_err(Error::Lua)?; + self.lua.remove_app_data::<&'static mut rbx_dom_weak::WeakDom>(); + Ok(()) } }