From d9469b444050760103b62449e94a44e69f21d26f Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 12:04:17 -0500 Subject: [PATCH] :recycle: refactor: invoke luac during tests so I don't have to version artifacts --- Cargo.lock | 55 +++++++++++++++++++++++++++++++ Cargo.toml | 5 +++ src/main.rs | 11 ++----- src/tests.rs | 40 ++++++++++++++++++++++- test_vectors/fma.lua | 2 ++ test_vectors/fma.txt | 70 ++++++++++++++++++++++++++++++++++++++++ test_vectors/is_93.luac | Bin 274 -> 0 bytes test_vectors/math.lua | 1 + 8 files changed, 175 insertions(+), 9 deletions(-) create mode 100644 test_vectors/fma.txt delete mode 100644 test_vectors/is_93.luac diff --git a/Cargo.lock b/Cargo.lock index aad4ef7..93d6461 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,61 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "arrayref" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b4930d2cb77ce62f89ee5d5289b4ac049559b1c45539271f5ed4fdc7db34545" + +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + +[[package]] +name = "blake3" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0231f06152bf547e9c2b5194f247cd97aacf6dcd8b15d8e5ec0663f64580da87" +dependencies = [ + "arrayref", + "arrayvec", + "cc", + "cfg-if", + "constant_time_eq", +] + +[[package]] +name = "cc" +version = "1.0.83" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0" +dependencies = [ + "libc", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "constant_time_eq" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" + +[[package]] +name = "libc" +version = "0.2.148" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" + [[package]] name = "lunar_wave_vm" version = "0.1.0" +dependencies = [ + "blake3", +] diff --git a/Cargo.toml b/Cargo.toml index 54fad2d..8ead428 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,8 @@ version = "0.1.0" edition = "2021" [dependencies] + +[dev-dependencies] + +# blake3, used to hash test vectors +blake3 = "1.5.0" diff --git a/src/main.rs b/src/main.rs index 7085930..a492b3c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,11 +8,10 @@ fn main() { use state::State; let lua_file = { - let data = std::fs::read ("test_vectors/closure.luac").unwrap (); + let data = std::fs::read ("test_vectors/fma.luac").unwrap (); let mut rdr = std::io::Cursor::new (data); loader::parse_chunk (&mut rdr).unwrap () }; - assert_eq! (lua_file.blocks.len (), 3); let mut vm = State::default (); if std::env::var("LUA_DEBUG").is_ok() { @@ -22,12 +21,8 @@ fn main() { let upvalues = State::upvalues_from_args (std::env::args ()); vm.breakpoints.push (state::Breakpoint { - block_idx: 2, - program_counter: 3, - }); - vm.breakpoints.push (state::Breakpoint { - block_idx: 0, - program_counter: 10, + block_idx: 3, + program_counter: 0, }); println! ("Returned: {:?}", vm.execute_chunk (&lua_file, &upvalues)); diff --git a/src/tests.rs b/src/tests.rs index b3e13ff..5913949 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -5,6 +5,39 @@ use crate::state::{ State, }; +// Invoke `luac` as a subprocess +// Luckily luac is single-pass, so we can just pipe in and out + +fn compile_bytecode (source: Vec ) -> Vec { + use std::{ + io::Write, + process::{ + Command, + Stdio, + }, + }; + + let mut child = Command::new ("luac") + .arg ("-o") // Output to... + .arg ("-") // Standard output + .arg ("-") // Input from standard input + .stdin (Stdio::piped ()) + .stdout (Stdio::piped ()) + .spawn () + .expect ("failed to execute `luac`. Is Lua installed?"); + + let mut stdin = child.stdin.take ().expect ("failed to get stdin"); + std::thread::spawn (move || { + stdin.write_all (&source).expect ("failed to write to stdin"); + }); + + let output = child + .wait_with_output () + .expect ("failed to wait on child"); + + output.stdout.as_slice ().to_vec () +} + #[test] fn bools () { /* @@ -170,7 +203,12 @@ fn fma () { #[test] fn is_93 () { - let bytecode = include_bytes! ("../test_vectors/is_93.luac"); + let source = include_bytes! ("../test_vectors/is_93.lua"); + assert_eq! (&blake3::hash (source).to_hex (), "a058869ed5142cbc8ccb2372991ef8b37657af38dc3f5e34814a7274b14d1e52"); + + let bytecode = compile_bytecode (source.to_vec ()); + assert_eq! (&blake3::hash (&bytecode).to_hex (), "b59ce3e054500beb109c247e784cc4a0ff09ac42f8086dc5cdbf711bce1ba071"); + let mut rdr = std::io::Cursor::new (bytecode); let file = crate::loader::parse_chunk (&mut rdr).unwrap (); diff --git a/test_vectors/fma.lua b/test_vectors/fma.lua index f807eef..f1c8ff4 100644 --- a/test_vectors/fma.lua +++ b/test_vectors/fma.lua @@ -1,3 +1,5 @@ +-- This one fails :( I haven't implemented closures properly yet. + local function add (aa, bb) return aa + bb end diff --git a/test_vectors/fma.txt b/test_vectors/fma.txt new file mode 100644 index 0000000..73616d9 --- /dev/null +++ b/test_vectors/fma.txt @@ -0,0 +1,70 @@ + +main (14 instructions at 0x55789d62dc90) +0+ params, 7 slots, 1 upvalue, 4 locals, 1 constant, 3 functions + 1 [1] VARARGPREP 0 + 2 [3] CLOSURE 0 0 ; 0x55789d62de30 + 3 [7] CLOSURE 1 1 ; 0x55789d62df90 + 4 [11] CLOSURE 2 2 ; 0x55789d62e0f0 + 5 [13] MOVE 3 2 + 6 [13] LOADI 4 10 + 7 [13] LOADI 5 11 + 8 [13] LOADI 6 12 + 9 [13] CALL 3 4 2 ; 3 in 1 out + 10 [14] GETTABUP 4 0 0 ; _ENV "print" + 11 [14] MOVE 5 3 + 12 [14] CALL 4 2 1 ; 1 in 0 out + 13 [15] RETURN 3 2 1k ; 1 out + 14 [15] RETURN 4 1 1k ; 0 out +constants (1) for 0x55789d62dc90: + 0 S "print" +locals (4) for 0x55789d62dc90: + 0 add 3 15 + 1 mul 4 15 + 2 fma 5 15 + 3 hh 10 15 +upvalues (1) for 0x55789d62dc90: + 0 _ENV 1 0 + +function (4 instructions at 0x55789d62de30) +2 params, 3 slots, 0 upvalues, 2 locals, 0 constants, 0 functions + 1 [2] ADD 2 0 1 + 2 [2] MMBIN 0 1 6 ; __add + 3 [2] RETURN1 2 + 4 [3] RETURN0 +constants (0) for 0x55789d62de30: +locals (2) for 0x55789d62de30: + 0 aa 1 5 + 1 bb 1 5 +upvalues (0) for 0x55789d62de30: + +function (4 instructions at 0x55789d62df90) +2 params, 3 slots, 0 upvalues, 2 locals, 0 constants, 0 functions + 1 [6] MUL 2 0 1 + 2 [6] MMBIN 0 1 8 ; __mul + 3 [6] RETURN1 2 + 4 [7] RETURN0 +constants (0) for 0x55789d62df90: +locals (2) for 0x55789d62df90: + 0 cc 1 5 + 1 dd 1 5 +upvalues (0) for 0x55789d62df90: + +function (9 instructions at 0x55789d62e0f0) +3 params, 7 slots, 2 upvalues, 3 locals, 0 constants, 0 functions + 1 [10] GETUPVAL 3 0 ; add + 2 [10] GETUPVAL 4 1 ; mul + 3 [10] MOVE 5 0 + 4 [10] MOVE 6 1 + 5 [10] CALL 4 3 2 ; 2 in 1 out + 6 [10] MOVE 5 2 + 7 [10] TAILCALL 3 3 0 ; 2 in + 8 [10] RETURN 3 0 0 ; all out + 9 [11] RETURN0 +constants (0) for 0x55789d62e0f0: +locals (3) for 0x55789d62e0f0: + 0 ee 1 10 + 1 ff 1 10 + 2 gg 1 10 +upvalues (2) for 0x55789d62e0f0: + 0 add 1 0 + 1 mul 1 1 diff --git a/test_vectors/is_93.luac b/test_vectors/is_93.luac deleted file mode 100644 index 47d463f12c7f690a8226fa9c05561089586e2b83..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 274 zcmb34DNPJvketlRCB?YnU;>%K#OY)0~^)rj(EsgbZN)sCz z7#Nu+1p*cO193MH_cAat?qOhL*ulim0OT_t zxq*Sf1!#mj(Ap-L&i2y0(&E&VMxcB{3s3`NLqlV0ysKZ>Bt|ACpdd&HXaI<0Y-sF) NsE$v|YnlX61_0C?LO1{b diff --git a/test_vectors/math.lua b/test_vectors/math.lua index 1e57079..2e7dc12 100644 --- a/test_vectors/math.lua +++ b/test_vectors/math.lua @@ -2,4 +2,5 @@ local function add (a, b) return a + b end + print (("1 + 2 = %i"):format (add (1, 2)))