Unify tool error handling
This commit is contained in:
parent
e4dc725ec0
commit
aa293ed7e1
@ -3,7 +3,7 @@ use std::io::{Result, Write};
|
||||
use parity_wasm::{deserialize_file, elements::Module};
|
||||
use wasm_ast::builder::TypeInfo;
|
||||
|
||||
type Translator = fn(&Module, &TypeInfo, &mut dyn Write) -> Result<()>;
|
||||
type Translate = fn(&Module, &TypeInfo, &mut dyn Write) -> Result<()>;
|
||||
|
||||
fn parse_module(name: &str) -> Module {
|
||||
let wasm = deserialize_file(name).expect("Failed to parse Wasm file");
|
||||
@ -11,23 +11,24 @@ fn parse_module(name: &str) -> Module {
|
||||
wasm.parse_names().unwrap_or_else(|v| v.1)
|
||||
}
|
||||
|
||||
fn run_translator(wasm: &Module, runtime: &str, translator: Translator) {
|
||||
fn run_translator(wasm: &Module, runtime: &str, translate: Translate) -> Result<()> {
|
||||
let pipe = std::io::stdout();
|
||||
let lock = &mut pipe.lock();
|
||||
let type_info = TypeInfo::from_module(wasm);
|
||||
|
||||
write!(lock, "local rt = (function() {runtime} end)() ").unwrap();
|
||||
translator(wasm, &type_info, lock).unwrap();
|
||||
write!(lock, "local rt = (function() {runtime} end)() ")?;
|
||||
translate(wasm, &type_info, lock)
|
||||
}
|
||||
|
||||
fn do_translate(name: &str, file: &str) {
|
||||
let wasm = &parse_module(file);
|
||||
|
||||
match name.to_lowercase().as_str() {
|
||||
let result = match name.to_lowercase().as_str() {
|
||||
"luajit" => run_translator(wasm, codegen_luajit::RUNTIME, codegen_luajit::translate),
|
||||
"luau" => run_translator(wasm, codegen_luau::RUNTIME, codegen_luau::translate),
|
||||
_ => panic!("Bad language: {name}"),
|
||||
}
|
||||
};
|
||||
|
||||
result.expect("Failed to translate file");
|
||||
}
|
||||
|
||||
fn do_help() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user