From 260963f7cdf823a0680f2ea556d4161b3a967ce1 Mon Sep 17 00:00:00 2001 From: Sofia Date: Fri, 20 Mar 2026 18:32:50 +0200 Subject: [PATCH] Add _ENV-table as special case for the VM --- examples/test.lua | 2 +- src/vm/mod.rs | 29 ++++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/examples/test.lua b/examples/test.lua index 62667fb..b9e72e3 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -94,4 +94,4 @@ for k, v in (ipairs(table)) do print(k, v) end -print(_ENV.sometable) \ No newline at end of file +print(_ENV.b) \ No newline at end of file diff --git a/src/vm/mod.rs b/src/vm/mod.rs index 4c8d005..a8a2344 100644 --- a/src/vm/mod.rs +++ b/src/vm/mod.rs @@ -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) => {