implement Instance.__index

This commit is contained in:
Quaternions 2024-10-03 19:09:02 -07:00
parent 40655cdf44
commit e67f0f0889

View File

@ -152,6 +152,23 @@ 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):(Self,mlua::String)|{
let index_str=index.to_str()?;
dom(lua,|dom|{
let instance=this.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):(Self,mlua::String,mlua::Value)|
dom(lua,|dom|{
//println!("__newindex t={this:?} i={index:?} v={value:?}");