Compare commits

...

2 Commits

Author SHA1 Message Date
e4114ab2cc simplify run_script logic 2024-10-04 17:51:56 -07:00
9b077d6db7 no double enum database access 2024-10-04 17:34:17 -07:00
4 changed files with 36 additions and 38 deletions

View File

@ -3,8 +3,8 @@ use mlua::IntoLua;
#[derive(Clone,Copy)]
pub struct Enum(u32);
pub struct EnumItems;
pub struct EnumItem{
name:String
pub struct EnumItem<'a>{
ed:&'a rbx_reflection::EnumDescriptor<'a>,
}
impl Into<rbx_types::Enum> for Enum{
@ -13,9 +13,9 @@ impl Into<rbx_types::Enum> for Enum{
}
}
impl EnumItem{
const fn new(name:String)->Self{
Self{name}
impl<'a> EnumItem<'a>{
const fn new(ed:&'a rbx_reflection::EnumDescriptor)->Self{
Self{ed}
}
}
@ -23,32 +23,28 @@ pub fn set_globals(_lua:&mlua::Lua,globals:&mlua::Table<'_>)->Result<(),mlua::Er
globals.set("Enum",EnumItems)
}
impl mlua::UserData for EnumItem{
impl mlua::UserData for EnumItem<'_>{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(Self,String)|{
let db=rbx_reflection_database::get();
match db.enums.get(this.name.as_str()){
Some(e)=>match e.items.get(val.as_str()){
Some(&id)=>Enum(id).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
},
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,val):(EnumItem<'lua>,mlua::String)|{
match this.ed.items.get(val.to_str()?){
Some(&id)=>Enum(id).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
}
});
}
}
type_from_lua_userdata!(EnumItem);
type_from_lua_userdata_lua_lifetime!(EnumItem);
impl mlua::UserData for EnumItems{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
}
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,String)|{
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(_,val):(Self,mlua::String)|{
let db=rbx_reflection_database::get();
match db.enums.get(val.as_str()){
Some(_)=>EnumItem::new(val).into_lua(lua),
match db.enums.get(val.to_str()?){
Some(ed)=>EnumItem::new(ed).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
}
});

View File

@ -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))
})
}
}

View File

@ -10,3 +10,15 @@ macro_rules! type_from_lua_userdata{
}
};
}
macro_rules! type_from_lua_userdata_lua_lifetime{
($asd:ident)=>{
impl<'lua> mlua::FromLua<'lua> for $asd<'lua>{
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->Result<Self,mlua::Error>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($asd),other))),
}
}
}
};
}

View File

@ -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})