From e67f0f08892da1e543a15a4843ec7ea5266a7f8a Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 3 Oct 2024 19:09:02 -0700 Subject: [PATCH] implement Instance.__index --- src/runner/instance.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/runner/instance.rs b/src/runner/instance.rs index 0dad951..ffb950c 100644 --- a/src/runner/instance.rs +++ b/src/runner/instance.rs @@ -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:?}");