Add runtime to Transpiler
				
					
				
			This commit is contained in:
		
							parent
							
								
									25e2730a0c
								
							
						
					
					
						commit
						822465cd70
					
				@ -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),
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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\")")?;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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)")?;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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<()>;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user