simplify run_script logic

This commit is contained in:
Quaternions 2024-10-04 17:51:56 -07:00
parent 9b077d6db7
commit e4114ab2cc
2 changed files with 11 additions and 21 deletions

View File

@ -250,22 +250,18 @@ class_composition!(Workspace,(Instance));
class!(Lighting); class!(Lighting);
class_composition!(Lighting,(Instance)); class_composition!(Lighting,(Instance));
#[derive(Debug)]
pub enum GetScriptError{
NoScript,
NoSource,
}
class!(Script); class!(Script);
class_composition!(Script,(Instance)); class_composition!(Script,(Instance));
impl Script{ impl Script{
pub fn get_name_source(&self,context:&crate::context::Context)->Result<(String,String),GetScriptError>{ pub fn get_name_source(&self,lua:&mlua::Lua)->Result<(String,String),mlua::Error>{
let instance=context.dom.get_by_ref(self.referent).ok_or(GetScriptError::NoScript)?; dom(lua,|dom|{
let source=match instance.properties.get("Source").ok_or(GetScriptError::NoSource)?{ let instance=self.get(dom)?;
rbx_dom_weak::types::Variant::String(s)=>s.clone(), let source=match instance.properties.get("Source"){
_=>Err(GetScriptError::NoSource)?, Some(rbx_dom_weak::types::Variant::String(s))=>s.clone(),
}; _=>Err(mlua::Error::external("Missing script.Source"))?,
Ok((get_full_name(&context.dom,instance),source)) };
Ok((get_full_name(dom,instance),source))
})
} }
} }

View File

@ -10,8 +10,6 @@ pub enum Error{
error:mlua::Error error:mlua::Error
}, },
RustLua(mlua::Error), RustLua(mlua::Error),
Script(super::instance::GetScriptError),
NoContext,
NoServices, NoServices,
} }
impl std::fmt::Display for Error{ impl std::fmt::Display for Error{
@ -79,12 +77,8 @@ impl Runnable<'_>{
} }
} }
pub fn run_script(&self,script:super::instance::Script)->Result<(),Error>{ pub fn run_script(&self,script:super::instance::Script)->Result<(),Error>{
let (name,source)={ let (name,source)=script.get_name_source(&self.lua).map_err(Error::RustLua)?;
let dom=&mut *self.lua.app_data_mut::<&'static mut rbx_dom_weak::WeakDom>().ok_or(Error::NoContext)?; self.lua.globals().set("script",script).map_err(Error::RustLua)?;
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()) self.lua.load(source.as_str())
.set_name(name) .set_name(name)
.exec().map_err(|error|Error::Lua{source,error}) .exec().map_err(|error|Error::Lua{source,error})