diff --git a/examples/test.lua b/examples/test.lua index 9674c22..6945ecb 100644 --- a/examples/test.lua +++ b/examples/test.lua @@ -21,7 +21,7 @@ function f(x) end global sometable = {} -sometable["hello"] = { add(10)(15) } +sometable["hello"] = { 100, 150, add(10)(15) } print(#sometable["hello"]) sometable["hello"].there = "my dude" print(sometable.hello.there) diff --git a/luac.out b/luac.out index 40c5796..a6e2bf7 100644 Binary files a/luac.out and b/luac.out differ diff --git a/src/compile.rs b/src/compile.rs index f3078ee..5d7c797 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -647,7 +647,7 @@ impl Expression { instructions.push(Instruction::NewTable(reg)); let mut counter = 1; - for (key, value) in entries { + for (i, (key, value)) in entries.iter().enumerate() { if let Some(key) = key { let (instr, key_regs) = key.kind.compile(state, scope, Some(1)); instructions.extend(instr); @@ -662,7 +662,11 @@ impl Expression { let (instr, value_regs) = value.kind.compile( state, scope, - if entries.len() == 1 { None } else { Some(1) }, + if i == entries.len() - 1 { + None + } else { + Some(1) + }, ); instructions.extend(instr); @@ -679,7 +683,7 @@ impl Expression { )); counter += 1; } else { - instructions.push(Instruction::SetList(reg)); + instructions.push(Instruction::SetList(reg, counter as u32)); } } } diff --git a/src/vm.rs b/src/vm.rs index 8149c69..1670185 100644 --- a/src/vm.rs +++ b/src/vm.rs @@ -124,8 +124,8 @@ pub enum Instruction { SetUpVal(u16, u16), /// R(A)[R(B)] := R(C) SetTable(u16, u16, u16), - /// R(A) := all values returned from previous function - SetList(u16), + /// R(A)[B...] := all values returned from previous function + SetList(u16, u32), /// R(A) := R(B)[R(C)] GetTable(u16, u16, u16), /// R(A) := {} @@ -176,7 +176,7 @@ impl Debug for Instruction { Instruction::GetTable(arg0, arg1, arg2) => { write!(f, "GETTABLE {} {} {}", arg0, arg1, arg2) } - Instruction::SetList(arg0) => write!(f, "SETLIST {}", arg0), + Instruction::SetList(arg0, arg1) => write!(f, "SETLIST {} {}", arg0, arg1), Instruction::NewTable(arg0) => write!(f, "NEWTABLE {}", arg0), Instruction::Jmp(arg0) => write!(f, "JMP {}", arg0), Instruction::Test(arg0, arg1, arg2) => write!(f, "TEST {} {} {}", arg0, arg1, arg2), @@ -817,13 +817,13 @@ impl ClosureRunner { self.set_stack(*res, value); } - Instruction::SetList(reg) => { + Instruction::SetList(reg, start_idx) => { let table = self.stack.get(reg).cloned(); match table { Some(value) => { let mut table = value.borrow_mut(); if let Value::Table(table) = &mut *table { - let mut counter = 1; + let mut counter = *start_idx as i64; for i in self.function_register..self.top { let value = self.get_stack(i + 1); match value {