From 4fd412b415c29ff4a1980d85111dd61b15d97678 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Mon, 25 Sep 2023 01:57:57 -0500 Subject: [PATCH] :construction: wip: I'm not handling nested functions in luac files properly will fix soon --- src/loader.rs | 7 +++++-- src/state.rs | 2 +- src/tests.rs | 1 + test_vectors/bools.luac | Bin 0 -> 278 bytes test_vectors/closure.lua | 4 +++- test_vectors/closure.luac | Bin 271 -> 285 bytes test_vectors/fma.lua | 18 +++++++++--------- test_vectors/fma.luac | Bin 0 -> 351 bytes 8 files changed, 19 insertions(+), 13 deletions(-) create mode 100644 test_vectors/bools.luac create mode 100644 test_vectors/fma.luac diff --git a/src/loader.rs b/src/loader.rs index ab26d07..ff5ce88 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -30,6 +30,7 @@ pub fn parse_inst (buf: [u8; 4]) -> Option 0x0b => Inst::GetTabUp (a, b, c), 0x0d => Inst::GetI (a, b, c), 0x22 => Inst::Add (a, b, c), + 0x24 => Inst::Mul (a, b, c), 0x2e => Inst::MmBin (a, b, c), 0x3c => Inst::EqK (a, b, c), 0x38 => Inst::Jmp (s_j), @@ -207,8 +208,10 @@ mod tests { #[test] fn parse_header () { for (input, expected) in [ - // Bytes 0 and 1 are line and column for debugging - // Byte 4 is slot count + // Bytes 0 and 1 are first line and last line for debugging + // Byte 2 is numparams + // Byte 3 is is_vararg + // Byte 4 is slot count / max stack size // Byte 5 is instruction count ([0x80, 0x80, 0x00, 0x01, 0x04, 0x92], (18,)), diff --git a/src/state.rs b/src/state.rs index 8d831ea..3b98641 100644 --- a/src/state.rs +++ b/src/state.rs @@ -487,7 +487,7 @@ impl State { } else { // 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) => { diff --git a/src/tests.rs b/src/tests.rs index 187ea6f..7371af3 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -157,6 +157,7 @@ fn fma () { let bytecode = include_bytes! ("../test_vectors/fma.luac"); let mut rdr = std::io::Cursor::new (bytecode); let file = crate::loader::parse_chunk (&mut rdr).unwrap (); + assert_eq! (file.blocks.len (), 4); for (arg, expected) in [ (vec! ["_exe_name"], vec! [122.into ()]), diff --git a/test_vectors/bools.luac b/test_vectors/bools.luac new file mode 100644 index 0000000000000000000000000000000000000000..b63d50660f57bc1be7b87be3c0e5c7dda631f37c GIT binary patch literal 278 zcmYk1K}rKb5Jl@3LkN)^z<2>y8gb(?h`N%9AlVKR2_ghXW;!~{>c%94?sPF8#RG&S z$MUxzTJWLj_xsh|dzZ89Ms`2Con9wtx2uu-)&BDUp9423UHUY+b$L-9j`Mt49!zJM znILJd313lfF$9r0bq19cr(bXR&og2>5Okq9CKt65ReEAT%wjEy3jiTmJQnv4E_um{ zJ5Ruy=Ps>Jx#R?5UXE-lDqqZc>;Lo{zQW9E-XT0v^I>eYHE1bQ(TsL1wnW2g{88%i Pv~$vOGOrA4Vty-jJYy-gJzy^XE$u6|(vhFBN) delta 76 zcmbQs)Xy}*p0RtPgNP_MBLhPNBO?Q26Klf}MkWRqAk%GPVZC}cBP$aF15g$S8k%}@ b6SGs}lXLQmON&yQx|`BkyBk~MUH!rUn+g<% diff --git a/test_vectors/fma.lua b/test_vectors/fma.lua index 7525a54..f807eef 100644 --- a/test_vectors/fma.lua +++ b/test_vectors/fma.lua @@ -1,15 +1,15 @@ -local function add (a, b) - return a + b +local function add (aa, bb) + return aa + bb end -local function mul (a, b) - return a * b +local function mul (cc, dd) + return cc * dd end -local function fma (a, b, c) - return add (mul (a, b), c) +local function fma (ee, ff, gg) + return add (mul (ee, ff), gg) end -local x = fma (10, 11, 12) -print (x) -return x +local hh = fma (10, 11, 12) +print (hh) +return hh diff --git a/test_vectors/fma.luac b/test_vectors/fma.luac new file mode 100644 index 0000000000000000000000000000000000000000..e36b422627c3040080be0fb9211cda57e79a80a1 GIT binary patch literal 351 zcmZvYzfJ=&42NwerAHAD0BneXsk}l5{tXC(gt}Jg-L*w(0kwC)=u1*igp>t|CxSBa zN;ojEU@7tN`|{b!&RK1)VC!|Wv)$2I_SlEN+JETbTdyh?)!@F&tK0eFU^Fw`X>FJR z+woG+3prmb9T6A_&}wd|#pqi6j+!m;dq6O(r2W J%fZok{~I>NRr>${ literal 0 HcmV?d00001