Add __newindex metamethod

This commit is contained in:
Sofia 2026-03-21 14:50:24 +02:00
parent 90c3b90065
commit 875cfc1220

View File

@ -566,28 +566,60 @@ impl ClosureRunner {
.unwrap_or(Value::Nil); .unwrap_or(Value::Nil);
} }
Instruction::SetTable(tablereg, indexreg, valuereg) => { Instruction::SetTable(tablereg, indexreg, valuereg) => {
let table = self.stack.get(tablereg); let table_value = self.stack.get(tablereg);
match table { match table_value {
Some(value) => { Some(value) => {
let mut table = value.borrow_mut(); let mut table = value.borrow_mut();
if let Value::Table { contents, .. } = &mut *table { if let Value::Table {
contents,
metatable,
} = &mut *table
{
let index_value = self let index_value = self
.stack .stack
.get(indexreg) .get(indexreg)
.map(|v| v.borrow().clone()) .map(|v| v.borrow().clone())
.unwrap_or(Value::Nil) .unwrap_or(Value::Nil);
.as_indexable()?;
let value = self let value = self
.stack .stack
.get(valuereg) .get(valuereg)
.map(|v| v.borrow().clone()) .map(|v| v.borrow().clone())
.unwrap_or(Value::Nil); .unwrap_or(Value::Nil);
match value { if contents
Value::Nil => { .borrow()
contents.borrow_mut().remove(&index_value); .contains_key(&index_value.clone().as_indexable()?)
{
match value {
Value::Nil => {
contents
.borrow_mut()
.remove(&index_value.as_indexable()?);
}
_ => {
contents
.borrow_mut()
.insert(index_value.as_indexable()?, value);
}
} }
_ => { } else {
contents.borrow_mut().insert(index_value, value); match self.call_metamethod(
metatable,
"__newindex",
vec![table_value.unwrap().borrow().clone()],
) {
Ok(_) => {}
Err(_) => match value {
Value::Nil => {
contents
.borrow_mut()
.remove(&index_value.as_indexable()?);
}
_ => {
contents
.borrow_mut()
.insert(index_value.as_indexable()?, value);
}
},
} }
} }
} else { } else {