Add _ENV-table as special case for the VM

This commit is contained in:
Sofia 2026-03-20 18:32:50 +02:00
parent 131c01c8f0
commit 260963f7cd
2 changed files with 21 additions and 10 deletions

View File

@ -94,4 +94,4 @@ for k, v in (ipairs(table)) do
print(k, v)
end
print(_ENV.sometable)
print(_ENV.b)

View File

@ -409,16 +409,27 @@ impl ClosureRunner {
}
Instruction::GetGlobal(reg, global) => {
let constant = constants.get(*global as usize).unwrap().clone();
let environment = self.closure.environment.borrow_mut().clone();
let glob = environment
.get(&constant.as_value().as_indexable()?)
.cloned();
if let Some(global) = glob {
self.set_stack(*reg, StackValue::Value(global));
if let Constant::String(name) = &constant
&& name.clone() == "_ENV".to_owned()
{
self.set_stack(
*reg,
StackValue::Value(Value::Table(self.closure.environment.clone())),
);
} else {
return Err(RuntimeError::GlobalNotFound(
constants.get(*global as usize).cloned(),
));
let environment = self.closure.environment.borrow_mut().clone();
let glob = environment
.get(&constant.as_value().as_indexable()?)
.cloned();
if let Some(global) = glob {
self.set_stack(*reg, StackValue::Value(global));
} else {
return Err(RuntimeError::GlobalNotFound(
constants.get(*global as usize).cloned(),
));
}
}
}
Instruction::GetUpVal(reg, upvalreg) => {