Skip to content

Commit

Permalink
Merge pull request #24 from utam0k/args-pathbuf
Browse files Browse the repository at this point in the history
Use PathBuf type as args instead of String
  • Loading branch information
ainozaki authored Feb 19, 2024
2 parents ee2b98b + df76ae2 commit f860385
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 64 deletions.
10 changes: 5 additions & 5 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> = std::fs::read(args.input_file.as_str()).expect("error read file");
log::info!("input: {}", args.input_file.as_path().display());
let buf: Vec<u8> = std::fs::read(&args.input_file).expect("error read file");

// If input is *.wat, convert it into *wasm
// If input is *.wasm, do nothing
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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,
Expand Down
52 changes: 26 additions & 26 deletions tests/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
8 changes: 4 additions & 4 deletions tests/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
28 changes: 14 additions & 14 deletions tests/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
20 changes: 10 additions & 10 deletions tests/numeric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
Expand All @@ -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");
}
4 changes: 2 additions & 2 deletions tests/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
4 changes: 2 additions & 2 deletions tests/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down

0 comments on commit f860385

Please sign in to comment.