use luac in `main`

main
_ 2023-09-26 13:33:59 -05:00
parent d9469b4440
commit 7b1fb49775
4 changed files with 38 additions and 37 deletions

View File

@ -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 <u8>) -> Vec <u8> {
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 <Inst>
{
let opcode = buf [0] & 0x7f;

View File

@ -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 ()
};

View File

@ -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 <u8>) -> Vec <u8> {
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);

Binary file not shown.