From e4114ab2cca5dacfbffe97f1ce9b1af4ff81f7a9 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Fri, 4 Oct 2024 17:51:56 -0700 Subject: [PATCH] simplify run_script logic --- src/runner/instance.rs | 22 +++++++++------------- src/runner/runner.rs | 10 ++-------- 2 files changed, 11 insertions(+), 21 deletions(-) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index c3152d5..213aa91 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -250,22 +250,18 @@ class_composition!(Workspace,(Instance)); class!(Lighting); class_composition!(Lighting,(Instance)); -#[derive(Debug)] -pub enum GetScriptError{ - NoScript, - NoSource, -} - class!(Script); class_composition!(Script,(Instance)); impl Script{ - pub fn get_name_source(&self,context:&crate::context::Context)->Result<(String,String),GetScriptError>{ - let instance=context.dom.get_by_ref(self.referent).ok_or(GetScriptError::NoScript)?; - let source=match instance.properties.get("Source").ok_or(GetScriptError::NoSource)?{ - rbx_dom_weak::types::Variant::String(s)=>s.clone(), - _=>Err(GetScriptError::NoSource)?, - }; - Ok((get_full_name(&context.dom,instance),source)) + pub fn get_name_source(&self,lua:&mlua::Lua)->Result<(String,String),mlua::Error>{ + dom(lua,|dom|{ + let instance=self.get(dom)?; + let source=match instance.properties.get("Source"){ + Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(), + _=>Err(mlua::Error::external("Missing script.Source"))?, + }; + Ok((get_full_name(dom,instance),source)) + }) } } diff --git a/src/runner/runner.rs b/src/runner/runner.rs index 5d7180d..a6935cb 100644 --- a/src/runner/runner.rs +++ b/src/runner/runner.rs @@ -10,8 +10,6 @@ pub enum Error{ error:mlua::Error }, RustLua(mlua::Error), - Script(super::instance::GetScriptError), - NoContext, NoServices, } impl std::fmt::Display for Error{ @@ -79,12 +77,8 @@ impl Runnable<'_>{ } } 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) - }; + let (name,source)=script.get_name_source(&self.lua).map_err(Error::RustLua)?; + self.lua.globals().set("script",script).map_err(Error::RustLua)?; self.lua.load(source.as_str()) .set_name(name) .exec().map_err(|error|Error::Lua{source,error})