From 7b1fb49775289b9fc7719649f6a1409a02d47aa5 Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Tue, 26 Sep 2023 13:33:59 -0500 Subject: [PATCH] use luac in `main` --- src/loader.rs | 33 +++++++++++++++++++++++++++++++++ src/main.rs | 7 ++++--- src/tests.rs | 35 +---------------------------------- test_vectors/hello.luac | Bin 104 -> 0 bytes 4 files changed, 38 insertions(+), 37 deletions(-) delete mode 100644 test_vectors/hello.luac diff --git a/src/loader.rs b/src/loader.rs index b399efa..471203d 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -6,6 +6,39 @@ use crate::state::{ Instruction as Inst, }; +/// Invoke `luac` as a subprocess +/// Luckily luac is single-pass, so we can just pipe in and out + +pub (crate) 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 () +} + pub fn parse_inst (buf: [u8; 4]) -> Option { let opcode = buf [0] & 0x7f; diff --git a/src/main.rs b/src/main.rs index a492b3c..deadf44 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,13 @@ mod state; #[cfg (test)] mod tests; -fn main() { +fn main () { use state::State; let lua_file = { - let data = std::fs::read ("test_vectors/fma.luac").unwrap (); - let mut rdr = std::io::Cursor::new (data); + let source = std::fs::read ("test_vectors/hello.lua").expect ("couldn't load Lua source code"); + let bytecode = loader::compile_bytecode(source); + let mut rdr = std::io::Cursor::new (bytecode); loader::parse_chunk (&mut rdr).unwrap () }; diff --git a/src/tests.rs b/src/tests.rs index 5913949..b053adf 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -5,39 +5,6 @@ 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 () { /* @@ -206,7 +173,7 @@ fn is_93 () { let source = include_bytes! ("../test_vectors/is_93.lua"); assert_eq! (&blake3::hash (source).to_hex (), "a058869ed5142cbc8ccb2372991ef8b37657af38dc3f5e34814a7274b14d1e52"); - let bytecode = compile_bytecode (source.to_vec ()); + let bytecode = crate::loader::compile_bytecode (source.to_vec ()); assert_eq! (&blake3::hash (&bytecode).to_hex (), "b59ce3e054500beb109c247e784cc4a0ff09ac42f8086dc5cdbf711bce1ba071"); let mut rdr = std::io::Cursor::new (bytecode); diff --git a/test_vectors/hello.luac b/test_vectors/hello.luac deleted file mode 100644 index b64953e78adcfd4eb0775b4efe3d815b7fb82318..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 104 zcmb34DNPJvketlRCB?