Skip to content

Commit 298a1d6

Browse files
committed
feat: compile java
1 parent b5faaf7 commit 298a1d6

File tree

6 files changed

+86
-13
lines changed

6 files changed

+86
-13
lines changed

fixtures/aplusb/source/Main.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.io.*;
2+
import java.util.*;
3+
import java.text.*;
4+
import java.math.*;
5+
6+
public class Main {
7+
static class Solver {
8+
int solve(int a, int b) {
9+
return a + b;
10+
}
11+
}
12+
13+
public static void main(String[] args) {
14+
Scanner in = new Scanner(System.in);
15+
int a = in.nextInt();
16+
int b = in.nextInt();
17+
int sum = new Solver().solve(a, b);
18+
System.out.println(sum);
19+
}
20+
}

src/context/mod.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct CatBox {
2626

2727
/// CatBoxContext for storing running result
2828
pub trait CatBoxContext {
29-
fn add_result(&mut self, label: &String, result: CatBoxResult);
29+
fn add_result(&mut self, label: &String, result: CatBoxResult) -> bool;
3030

3131
fn report(&self) {
3232
let is_tty = isatty(STDOUT_FILENO).unwrap_or(false);
@@ -100,9 +100,10 @@ impl CatBox {
100100
/// Run all the commands
101101
pub fn start(&mut self) -> Result<(), CatBoxError> {
102102
for option in self.options.iter() {
103-
dbg!(&option);
104103
let result = crate::run(&option)?;
105-
self.context.add_result(&option.label.clone(), result);
104+
if !self.context.add_result(&option.label.clone(), result) {
105+
break;
106+
}
106107
}
107108
Ok(())
108109
}
@@ -189,12 +190,13 @@ impl CatBoxRunContext {
189190
}
190191

191192
impl CatBoxContext for CatBoxRunContext {
192-
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
193+
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
193194
self.max_time = max(self.max_time, result.time);
194195
self.max_memory = max(self.max_memory, result.memory);
195196
self.sum_time += result.time;
196197
self.sum_memory += result.memory;
197198
self.results.push(result);
199+
true
198200
}
199201

200202
fn report_human(&self) {
@@ -254,11 +256,16 @@ impl CatBoxCompileContext {
254256
}
255257

256258
impl CatBoxContext for CatBoxCompileContext {
257-
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
259+
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
258260
if self.ok {
259261
if result.status.unwrap_or(1) == 0 {
260-
self.ok = false;
262+
self.ok = true;
263+
true
264+
} else {
265+
false
261266
}
267+
} else {
268+
false
262269
}
263270
}
264271

@@ -272,7 +279,7 @@ impl CatBoxContext for CatBoxCompileContext {
272279
}
273280

274281
impl CatBoxContext for CatBoxJudgeContext {
275-
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
282+
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
276283
todo!()
277284
}
278285

src/preset/default/java.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
use lazy_static::lazy_static;
2+
3+
use crate::preset::preset::{
4+
CompileOption, ExecuteCommand, ExecuteOption, LanguagePreset, UserType,
5+
};
6+
7+
lazy_static! {
8+
pub(crate) static ref JAVA_PRESET: LanguagePreset = LanguagePreset {
9+
compile: CompileOption::new("java")
10+
.command(
11+
ExecuteCommand::new("javac", vec!["-encoding", "utf8", "-d", ".", "${source}",])
12+
.default_time_limit(10 * 1000)
13+
.default_memory_limit(1024 * 1024)
14+
.default_user(UserType::Current)
15+
.default_process(10)
16+
.default_ptrace(vec![])
17+
.default_chroot(true)
18+
.append_read_mount("/proc", "/proc")
19+
.append_read_mount("/dev", "/dev")
20+
)
21+
.command(
22+
// Use bash to expand *.class
23+
ExecuteCommand::new("bash", vec!["-c", "jar -cvf ${executable} *.class"])
24+
.default_time_limit(10 * 1000)
25+
.default_memory_limit(1024 * 1024)
26+
.default_user(UserType::Current)
27+
.default_process(10)
28+
.default_ptrace(vec![])
29+
.default_chroot(true)
30+
.append_read_mount("/proc", "/proc")
31+
.append_read_mount("/dev", "/dev")
32+
),
33+
execute: ExecuteOption::new().command(ExecuteCommand::new(
34+
"java",
35+
vec!["-cp", "${executable}", "Main"]
36+
)),
37+
};
38+
}

src/preset/default/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pub(crate) use c::C_PRESET;
22
pub(crate) use cpp::CPP_PRESET;
3+
pub(crate) use java::JAVA_PRESET;
34

45
mod c;
56
mod cpp;
7+
mod java;

src/preset/mod.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
use std::collections::HashMap;
2-
use std::fs::canonicalize;
32
use std::path::PathBuf;
43

54
use lazy_static::lazy_static;
65
use log::info;
76
use path_absolutize::*;
87

9-
use crate::context::{CatBoxBuilder, CatBoxOption};
8+
use crate::context::CatBoxBuilder;
109
use crate::error::CatBoxError;
11-
use crate::preset::default::{CPP_PRESET, C_PRESET};
10+
use crate::preset::default::{CPP_PRESET, C_PRESET, JAVA_PRESET};
1211
use crate::preset::preset::UserType;
1312
use crate::Commands;
1413

@@ -66,11 +65,15 @@ pub(crate) fn make_compile_params(
6665
let preset = match language.as_str() {
6766
"c" => C_PRESET.clone(),
6867
"cpp" => CPP_PRESET.clone(),
69-
default => return Err(CatBoxError::cli("Can not find language preset")),
68+
"java" => JAVA_PRESET.clone(),
69+
_ => return Err(CatBoxError::cli("Can not find language preset")),
7070
};
7171

7272
info!("Compile language {}", &language);
7373

74+
// let root_dir = tempdir().unwrap();
75+
// let root_dir = root_dir.into_path();
76+
7477
let submission = PathBuf::from(&submission);
7578
let submission = submission.absolutize().unwrap();
7679
let submission_dir = submission.parent().unwrap();
@@ -90,6 +93,7 @@ pub(crate) fn make_compile_params(
9093
.set_chroot(command.chroot)
9194
.mount_read(submission_dir, submission_dir)
9295
.mount_write(output_dir, output_dir)
96+
.cwd(&output_dir)
9397
.disable_ptrace();
9498

9599
let mut option_builder = match command.user {

src/preset/preset.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::context::CatBoxOption;
1+
use std::path::PathBuf;
22
use crate::syscall::RestrictedSyscall;
33
use crate::utils::mount::MountPoint;
44
use crate::utils::{MemoryLimitType, TimeLimitType};
@@ -130,7 +130,9 @@ impl ExecuteCommand {
130130
self
131131
}
132132

133-
pub(crate) fn append_read_mount(mut self) -> Self {
133+
pub(crate) fn append_read_mount(mut self, src: impl Into<PathBuf>, dst: impl Into<PathBuf>) -> Self {
134+
let point = MountPoint::read(src.into(), dst.into());
135+
self.mounts.push(point);
134136
self
135137
}
136138

0 commit comments

Comments
 (0)