🚧 wip: I'm not handling nested functions in luac files properly

will fix soon
main
_ 2023-09-25 01:57:57 -05:00
parent 1518781753
commit 4fd412b415
8 changed files with 19 additions and 13 deletions

View File

@ -30,6 +30,7 @@ pub fn parse_inst (buf: [u8; 4]) -> Option <Inst>
0x0b => Inst::GetTabUp (a, b, c), 0x0b => Inst::GetTabUp (a, b, c),
0x0d => Inst::GetI (a, b, c), 0x0d => Inst::GetI (a, b, c),
0x22 => Inst::Add (a, b, c), 0x22 => Inst::Add (a, b, c),
0x24 => Inst::Mul (a, b, c),
0x2e => Inst::MmBin (a, b, c), 0x2e => Inst::MmBin (a, b, c),
0x3c => Inst::EqK (a, b, c), 0x3c => Inst::EqK (a, b, c),
0x38 => Inst::Jmp (s_j), 0x38 => Inst::Jmp (s_j),
@ -207,8 +208,10 @@ mod tests {
#[test] #[test]
fn parse_header () { fn parse_header () {
for (input, expected) in [ for (input, expected) in [
// Bytes 0 and 1 are line and column for debugging // Bytes 0 and 1 are first line and last line for debugging
// Byte 4 is slot count // Byte 2 is numparams
// Byte 3 is is_vararg
// Byte 4 is slot count / max stack size
// Byte 5 is instruction count // Byte 5 is instruction count
([0x80, 0x80, 0x00, 0x01, 0x04, 0x92], (18,)), ([0x80, 0x80, 0x00, 0x01, 0x04, 0x92], (18,)),

View File

@ -487,7 +487,7 @@ impl State {
} }
else { else {
// Return from the entire program // Return from the entire program
return self.registers [a..(a + b + c - 1)].to_vec(); return self.registers [a..(a + b - 1)].to_vec();
} }
}, },
Instruction::Return1 (a) => { Instruction::Return1 (a) => {

View File

@ -157,6 +157,7 @@ fn fma () {
let bytecode = include_bytes! ("../test_vectors/fma.luac"); let bytecode = include_bytes! ("../test_vectors/fma.luac");
let mut rdr = std::io::Cursor::new (bytecode); let mut rdr = std::io::Cursor::new (bytecode);
let file = crate::loader::parse_chunk (&mut rdr).unwrap (); let file = crate::loader::parse_chunk (&mut rdr).unwrap ();
assert_eq! (file.blocks.len (), 4);
for (arg, expected) in [ for (arg, expected) in [
(vec! ["_exe_name"], vec! [122.into ()]), (vec! ["_exe_name"], vec! [122.into ()]),

BIN
test_vectors/bools.luac Normal file

Binary file not shown.

View File

@ -6,4 +6,6 @@ local function make_closure (x)
end end
local f = make_closure (11) local f = make_closure (11)
print (f (12)) local x = f (12)
print (x)
return x

Binary file not shown.

View File

@ -1,15 +1,15 @@
local function add (a, b) local function add (aa, bb)
return a + b return aa + bb
end end
local function mul (a, b) local function mul (cc, dd)
return a * b return cc * dd
end end
local function fma (a, b, c) local function fma (ee, ff, gg)
return add (mul (a, b), c) return add (mul (ee, ff), gg)
end end
local x = fma (10, 11, 12) local hh = fma (10, 11, 12)
print (x) print (hh)
return x return hh

BIN
test_vectors/fma.luac Normal file

Binary file not shown.