diff --git a/src/compiler.rs b/src/compiler.rs index acc537c..3319097 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -12,17 +12,17 @@ use wat; #[derive(Parser, Debug)] pub struct Args { - pub input_file: String, + pub input_file: path::PathBuf, #[arg(short, long, default_value = "./wasm.o")] - pub output_file: String, + pub output_file: path::PathBuf, } /// Receive a path to a Wasm binary or WAT and compile it into ELF binary. pub fn compile_wasm_from_file(args: &Args) -> Result<()> { // Load bytes as either *.wat or *.wasm - log::info!("input: {}", args.input_file); - let buf: Vec = std::fs::read(args.input_file.as_str()).expect("error read file"); + log::info!("input: {}", args.input_file.as_path().display()); + let buf: Vec = std::fs::read(&args.input_file).expect("error read file"); // If input is *.wat, convert it into *wasm // If input is *.wasm, do nothing @@ -40,7 +40,7 @@ pub fn compile_wasm(wasm: &[u8], args: &Args) -> Result<()> { let builder = context.create_builder(); let (inkwell_types, inkwell_insts) = init_inkwell(&context, &module); let mut environment = Environment { - output_file: args.output_file.as_str(), + output_file: args.output_file.as_path(), context: &context, module: &module, builder, diff --git a/src/environment.rs b/src/environment.rs index 978eaf3..764437e 100644 --- a/src/environment.rs +++ b/src/environment.rs @@ -9,6 +9,7 @@ use inkwell::{ types::{BasicTypeEnum, FunctionType}, values::{BasicValueEnum, FunctionValue, GlobalValue, IntValue}, }; +use std::path::Path; use crate::inkwell::{InkwellInsts, InkwellTypes}; use crate::insts::control::{ControlFrame, UnreachableReason}; @@ -25,7 +26,7 @@ pub enum Global<'a> { pub struct Environment<'a, 'b> { // Output dir - pub output_file: &'b str, + pub output_file: &'b Path, // Inkwell code generator pub context: &'a Context, diff --git a/tests/control.rs b/tests/control.rs index 902a72b..5c25110 100644 --- a/tests/control.rs +++ b/tests/control.rs @@ -4,8 +4,8 @@ use wasker::compiler; fn example() { let wat = "./tests/wat/block.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -14,8 +14,8 @@ fn example() { fn block() { let wat = "./tests/wat/block.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -24,8 +24,8 @@ fn block() { fn ret() { let wat = "./tests/wat/return.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -34,8 +34,8 @@ fn ret() { fn call() { let wat = "./tests/wat/call.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -44,8 +44,8 @@ fn call() { fn call_indirect() { let wat = "./tests/wat/call_indirect.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -54,8 +54,8 @@ fn call_indirect() { fn select() { let wat = "./tests/wat/select.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -64,8 +64,8 @@ fn select() { fn br() { let wat = "./tests/wat/br.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -74,8 +74,8 @@ fn br() { fn br_table() { let wat = "./tests/wat/br_table.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -84,8 +84,8 @@ fn br_table() { fn br_if() { let wat = "./tests/wat/br_if.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -94,8 +94,8 @@ fn br_if() { fn iff() { let wat = "./tests/wat/if.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -104,8 +104,8 @@ fn iff() { fn loopp() { let wat = "./tests/wat/loop.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -114,8 +114,8 @@ fn loopp() { fn switch() { let wat = "./tests/wat/switch.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -124,8 +124,8 @@ fn switch() { fn bulk() { let wat = "./tests/wat/bulk.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } diff --git a/tests/local.rs b/tests/local.rs index ce09e93..466a57f 100644 --- a/tests/local.rs +++ b/tests/local.rs @@ -4,8 +4,8 @@ use wasker::compiler; fn local_get() { let wat = "./tests/wat/local_get.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -14,8 +14,8 @@ fn local_get() { fn global() { let wat = "./tests/wat/global.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } diff --git a/tests/memory.rs b/tests/memory.rs index e540c4c..4ea20f7 100644 --- a/tests/memory.rs +++ b/tests/memory.rs @@ -4,8 +4,8 @@ use wasker::compiler; fn memory_size() { let wat = "./tests/wat/memory_size.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -14,8 +14,8 @@ fn memory_size() { fn memory_copy() { let wat = "./tests/wat/memory_copy.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -24,8 +24,8 @@ fn memory_copy() { fn memory_fill() { let wat = "./tests/wat/memory_fill.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -34,8 +34,8 @@ fn memory_fill() { fn endianness() { let wat = "./tests/wat/endianness.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -44,8 +44,8 @@ fn endianness() { fn address32() { let wat = "./tests/wat/address32.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -54,8 +54,8 @@ fn address32() { fn address64() { let wat = "./tests/wat/address64.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -64,8 +64,8 @@ fn address64() { fn align() { let wat = "./tests/wat/align.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } diff --git a/tests/numeric.rs b/tests/numeric.rs index b7bd046..f330935 100644 --- a/tests/numeric.rs +++ b/tests/numeric.rs @@ -4,8 +4,8 @@ use wasker::compiler; fn i64() { let wat = "./tests/wat/i64.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -14,8 +14,8 @@ fn i64() { fn convert() { let wat = "./tests/wat/convert.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -24,8 +24,8 @@ fn convert() { fn f64() { let wat = "./tests/wat/f64.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -34,8 +34,8 @@ fn f64() { fn f64_cmp() { let wat = "./tests/wat/f64_cmp.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } @@ -44,8 +44,8 @@ fn f64_cmp() { fn f64_bitwise() { let wat = "./tests/wat/f64_bitwise.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } diff --git a/tests/rust.rs b/tests/rust.rs index d40cd7f..17c10d9 100644 --- a/tests/rust.rs +++ b/tests/rust.rs @@ -4,8 +4,8 @@ use wasker::compiler; fn rust() { let wat = "./helloworld.wat"; let args = compiler::Args { - input_file: wat.to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: wat.into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm_from_file(&args).expect("fail compile"); } diff --git a/tests/spec.rs b/tests/spec.rs index 188b048..f41ddd2 100644 --- a/tests/spec.rs +++ b/tests/spec.rs @@ -32,8 +32,8 @@ fn _run_spec_test(testname: &str) { // Compile test println!("Compile Module {:?}", name); let args = compiler::Args { - input_file: "tmp.wasm".to_string(), - output_file: "/tmp/wasm.o".to_string(), + input_file: "tmp.wasm".into(), + output_file: "/tmp/wasm.o".into(), }; compiler::compile_wasm(&module_binary, &args).expect("compile failed"); }