Compare commits

..

No commits in common. "d272ac242bf996a920c7921fd6806de99fe6150d" and "9d219004f452e81ffab56b679cbeccd0bd032753" have entirely different histories.

8 changed files with 64 additions and 63 deletions

View File

@ -76,5 +76,11 @@ impl mlua::UserData for CFrame{
);
}
}
type_from_lua_userdata!(CFrame);
impl<'lua> mlua::FromLua<'lua> for CFrame{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected CFrame got {:?}",other))),
}
}
}

View File

@ -40,4 +40,11 @@ impl mlua::UserData for Color3{
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(methods:&mut M){
}
}
type_from_lua_userdata!(Color3);
impl<'lua> mlua::FromLua<'lua> for Color3{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Color3 got {:?}",other))),
}
}
}

View File

@ -39,7 +39,14 @@ impl mlua::UserData for EnumItem{
});
}
}
type_from_lua_userdata!(EnumItem);
impl<'lua> mlua::FromLua<'lua> for EnumItem{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}
impl mlua::UserData for EnumItems{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
@ -54,7 +61,14 @@ impl mlua::UserData for EnumItems{
});
}
}
type_from_lua_userdata!(EnumItems);
impl<'lua> mlua::FromLua<'lua> for EnumItems{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}
impl mlua::UserData for Enum{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(_fields:&mut F){
@ -62,4 +76,11 @@ impl mlua::UserData for Enum{
fn add_methods<'lua,M:mlua::UserDataMethods<'lua,Self>>(_methods:&mut M){
}
}
type_from_lua_userdata!(Enum);
impl<'lua> mlua::FromLua<'lua> for Enum{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take()?,
other=>Err(mlua::Error::runtime(format!("Expected Enum got {:?}",other))),
}
}
}

View File

@ -29,16 +29,6 @@ fn get_full_name(dom:&rbx_dom_weak::WeakDom,instance:&rbx_dom_weak::Instance)->S
full_name
}
//workaround until I have an enum of classes
struct Dereferent(Ref);
impl mlua::UserData for Dereferent{}
type_from_lua_userdata!(Dereferent);
impl Referent for Dereferent{
fn referent(&self)->Ref{
self.0
}
}
trait Referent{
fn referent(&self)->Ref;
fn get<'a>(&self,dom:&'a WeakDom)->mlua::Result<&'a rbx_dom_weak::Instance>{
@ -64,16 +54,20 @@ macro_rules! class{
self.referent
}
}
type_from_lua_userdata!($class);
impl<'lua> mlua::FromLua<'lua> for $class{
fn from_lua(value:mlua::Value<'lua>,_lua:&'lua mlua::Lua)->mlua::Result<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected {} got {:?}",stringify!($class),other))),
}
}
}
};
}
macro_rules! class_composition{
($class:ident,($($superclass:ident),*))=>{
impl mlua::UserData for $class{
fn add_fields<'lua,F:mlua::UserDataFields<'lua,Self>>(fields:&mut F){
fields.add_field_method_get("Referent",|_,this|{
Ok(Dereferent(this.referent()))
});
$(
$superclass::composition_add_fields(fields);
)*
@ -99,10 +93,9 @@ impl Instance{
Ok(Instance::new(instance.parent()))
})
});
fields.add_field_method_set("Parent",|lua,this,val:mlua::AnyUserData|{
let Dereferent(referent)=mlua::AnyUserDataExt::get(&val,"Referent")?;
fields.add_field_method_set("Parent",|lua,this,val:Self|{
dom(lua,|dom|{
dom.transfer_within(this.referent(),referent);
dom.transfer_within(this.referent(),val.referent);
Ok(())
})
});
@ -157,29 +150,10 @@ impl Instance{
Ok(crate::context::class_is_a(instance.class.as_str(),classname.to_str()?))
})
);
methods.add_meta_function(mlua::MetaMethod::Index,|lua,(this,index):(mlua::AnyUserData,mlua::String)|{
let index_str=index.to_str()?;
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(Self,mlua::String,mlua::Value)|
dom(lua,|dom|{
let instance=dereferent.get(dom)?;
//find a child with a matching name
let maybe_child=instance.children()
.iter()
.find(|&&r|
dom.get_by_ref(r)
.is_some_and(|instance|instance.name==index_str)
);
match maybe_child{
Some(&referent)=>Instance::new(referent).into_lua(lua),
None=>mlua::Value::Nil.into_lua(lua),
}
})
});
methods.add_meta_function(mlua::MetaMethod::NewIndex,|lua,(this,index,value):(mlua::AnyUserData,mlua::String,mlua::Value)|{
let dereferent:Dereferent=mlua::AnyUserDataExt::get(&this,"Referent")?;
dom(lua,|dom|{
let instance=dereferent.get_mut(dom)?;
//println!("__newindex t={} i={index:?} v={value:?}",instance.name);
//println!("__newindex t={this:?} i={index:?} v={value:?}");
let instance=this.get_mut(dom)?;
let index_str=index.to_str()?;
let db=rbx_reflection_database::get();
let class=db.classes.get(instance.class.as_str()).ok_or(mlua::Error::runtime("Class missing"))?;
@ -205,7 +179,7 @@ impl Instance{
}
Ok(())
})
});
);
}
}

View File

@ -1,12 +0,0 @@
macro_rules! type_from_lua_userdata{
($asd:ident)=>{
impl<'lua> mlua::FromLua<'lua> for $asd{
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

@ -1,5 +1,3 @@
#[macro_use]
mod macros;
mod runner;
mod r#enum;

View File

@ -16,8 +16,8 @@ pub enum Error{
impl std::fmt::Display for Error{
fn fmt(&self,f:&mut std::fmt::Formatter<'_>)->std::fmt::Result{
match self{
Self::Lua{source,error}=>write!(f,"lua error: source:\n{source}\n{error}"),
Self::RustLua(error)=>write!(f,"rust-side lua error: {error}"),
Self::Lua{source,error:mlua::Error::RuntimeError(s)}=>write!(f,"lua error: {s}\nsource:{source}"),
Self::RustLua(mlua::Error::RuntimeError(s))=>write!(f,"rust-side lua error: {s}"),
other=>write!(f,"{other:?}"),
}
}

View File

@ -73,4 +73,11 @@ impl mlua::UserData for Vector3{
}
}
type_from_lua_userdata!(Vector3);
impl<'lua> mlua::FromLua<'lua> for Vector3{
fn from_lua(value:mlua::prelude::LuaValue<'lua>,_lua:&'lua mlua::prelude::Lua)->mlua::prelude::LuaResult<Self>{
match value{
mlua::Value::UserData(ud)=>ud.take(),
other=>Err(mlua::Error::runtime(format!("Expected Vector3 got {:?}",other))),
}
}
}