Add runtime to Transpiler

This commit is contained in:
Rerumu 2021-12-31 15:28:59 -05:00
parent 25e2730a0c
commit 822465cd70
4 changed files with 22 additions and 5 deletions

View File

@ -5,9 +5,6 @@ mod analyzer;
mod ast;
mod writer;
static LUAJIT_RUNTIME: &str = include_str!("../runtime/luajit.lua");
static LUAU_RUNTIME: &str = include_str!("../runtime/luau.lua");
fn parse_module(name: &str) -> Module {
let wasm = deserialize_file(name).expect("Failed to parse Wasm file");
@ -26,6 +23,12 @@ fn run_translator<'a, T: Transpiler<'a>>(wasm: &'a Module) {
.expect("Failed to transpile");
}
fn run_runtime<'a, T: Transpiler<'a>>() {
let output = std::io::stdout();
T::runtime(&mut output.lock()).expect("Failed to fetch runtime");
}
fn do_translate(name: &str, file: &str) {
let wasm = &parse_module(file);
@ -38,8 +41,8 @@ fn do_translate(name: &str, file: &str) {
fn do_runtime(name: &str) {
match name.to_lowercase().as_str() {
"luajit" => println!("{}", LUAJIT_RUNTIME),
"luau" => println!("{}", LUAU_RUNTIME),
"luajit" => run_runtime::<LuaJIT>(),
"luau" => run_runtime::<Luau>(),
_ => panic!("Bad runtime: {}", name),
}
}

View File

@ -720,6 +720,8 @@ fn write_list(name: &str, len: usize, w: Writer) -> Result<()> {
write!(w, "local {} = table_new({}, {})", name, len, hash)
}
static LUAJIT_RUNTIME: &str = include_str!("../../runtime/luajit.lua");
impl<'a> Transpiler<'a> for LuaJIT<'a> {
fn new(wasm: &'a Module) -> Self {
let arity = Arities::new(wasm);
@ -727,6 +729,10 @@ impl<'a> Transpiler<'a> for LuaJIT<'a> {
Self { wasm, arity }
}
fn runtime(writer: Writer) -> Result<()> {
write!(writer, "{}", LUAJIT_RUNTIME)
}
fn transpile(&self, w: Writer) -> Result<()> {
write!(w, "local rt = require(\"luajit\")")?;

View File

@ -709,6 +709,8 @@ fn write_list(name: &str, len: usize, w: Writer) -> Result<()> {
write!(w, "local {} = table.create({})", name, len)
}
static LUAU_RUNTIME: &str = include_str!("../../runtime/luau.lua");
impl<'a> Transpiler<'a> for Luau<'a> {
fn new(wasm: &'a Module) -> Self {
let arity = Arities::new(wasm);
@ -716,6 +718,10 @@ impl<'a> Transpiler<'a> for Luau<'a> {
Self { wasm, arity }
}
fn runtime(writer: Writer) -> Result<()> {
write!(writer, "{}", LUAU_RUNTIME)
}
fn transpile(&self, w: Writer) -> Result<()> {
write!(w, "local rt = require(script.Runtime)")?;

View File

@ -9,6 +9,8 @@ pub trait Transpiler<'a> {
where
Self: Sized;
fn runtime(writer: Writer) -> Result<()>;
/// # Errors
/// Returns `Err` if writing to `Writer` failed.
fn transpile(&self, writer: Writer) -> Result<()>;