Skip to content

Commit

Permalink
feat: compile java
Browse files Browse the repository at this point in the history
  • Loading branch information
yjl9903 committed Jan 30, 2023
1 parent b5faaf7 commit 298a1d6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 13 deletions.
20 changes: 20 additions & 0 deletions fixtures/aplusb/source/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;

public class Main {
static class Solver {
int solve(int a, int b) {
return a + b;
}
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int b = in.nextInt();
int sum = new Solver().solve(a, b);
System.out.println(sum);
}
}
21 changes: 14 additions & 7 deletions src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct CatBox {

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

fn report(&self) {
let is_tty = isatty(STDOUT_FILENO).unwrap_or(false);
Expand Down Expand Up @@ -100,9 +100,10 @@ impl CatBox {
/// Run all the commands
pub fn start(&mut self) -> Result<(), CatBoxError> {
for option in self.options.iter() {
dbg!(&option);
let result = crate::run(&option)?;
self.context.add_result(&option.label.clone(), result);
if !self.context.add_result(&option.label.clone(), result) {
break;
}
}
Ok(())
}
Expand Down Expand Up @@ -189,12 +190,13 @@ impl CatBoxRunContext {
}

impl CatBoxContext for CatBoxRunContext {
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
self.max_time = max(self.max_time, result.time);
self.max_memory = max(self.max_memory, result.memory);
self.sum_time += result.time;
self.sum_memory += result.memory;
self.results.push(result);
true
}

fn report_human(&self) {
Expand Down Expand Up @@ -254,11 +256,16 @@ impl CatBoxCompileContext {
}

impl CatBoxContext for CatBoxCompileContext {
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
if self.ok {
if result.status.unwrap_or(1) == 0 {
self.ok = false;
self.ok = true;
true
} else {
false
}
} else {
false
}
}

Expand All @@ -272,7 +279,7 @@ impl CatBoxContext for CatBoxCompileContext {
}

impl CatBoxContext for CatBoxJudgeContext {
fn add_result(&mut self, _label: &String, result: CatBoxResult) {
fn add_result(&mut self, _label: &String, result: CatBoxResult) -> bool {
todo!()
}

Expand Down
38 changes: 38 additions & 0 deletions src/preset/default/java.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use lazy_static::lazy_static;

use crate::preset::preset::{
CompileOption, ExecuteCommand, ExecuteOption, LanguagePreset, UserType,
};

lazy_static! {
pub(crate) static ref JAVA_PRESET: LanguagePreset = LanguagePreset {
compile: CompileOption::new("java")
.command(
ExecuteCommand::new("javac", vec!["-encoding", "utf8", "-d", ".", "${source}",])
.default_time_limit(10 * 1000)
.default_memory_limit(1024 * 1024)
.default_user(UserType::Current)
.default_process(10)
.default_ptrace(vec![])
.default_chroot(true)
.append_read_mount("/proc", "/proc")
.append_read_mount("/dev", "/dev")
)
.command(
// Use bash to expand *.class
ExecuteCommand::new("bash", vec!["-c", "jar -cvf ${executable} *.class"])
.default_time_limit(10 * 1000)
.default_memory_limit(1024 * 1024)
.default_user(UserType::Current)
.default_process(10)
.default_ptrace(vec![])
.default_chroot(true)
.append_read_mount("/proc", "/proc")
.append_read_mount("/dev", "/dev")
),
execute: ExecuteOption::new().command(ExecuteCommand::new(
"java",
vec!["-cp", "${executable}", "Main"]
)),
};
}
2 changes: 2 additions & 0 deletions src/preset/default/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub(crate) use c::C_PRESET;
pub(crate) use cpp::CPP_PRESET;
pub(crate) use java::JAVA_PRESET;

mod c;
mod cpp;
mod java;
12 changes: 8 additions & 4 deletions src/preset/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::collections::HashMap;
use std::fs::canonicalize;
use std::path::PathBuf;

use lazy_static::lazy_static;
use log::info;
use path_absolutize::*;

use crate::context::{CatBoxBuilder, CatBoxOption};
use crate::context::CatBoxBuilder;
use crate::error::CatBoxError;
use crate::preset::default::{CPP_PRESET, C_PRESET};
use crate::preset::default::{CPP_PRESET, C_PRESET, JAVA_PRESET};
use crate::preset::preset::UserType;
use crate::Commands;

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

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

// let root_dir = tempdir().unwrap();
// let root_dir = root_dir.into_path();

let submission = PathBuf::from(&submission);
let submission = submission.absolutize().unwrap();
let submission_dir = submission.parent().unwrap();
Expand All @@ -90,6 +93,7 @@ pub(crate) fn make_compile_params(
.set_chroot(command.chroot)
.mount_read(submission_dir, submission_dir)
.mount_write(output_dir, output_dir)
.cwd(&output_dir)
.disable_ptrace();

let mut option_builder = match command.user {
Expand Down
6 changes: 4 additions & 2 deletions src/preset/preset.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::context::CatBoxOption;
use std::path::PathBuf;
use crate::syscall::RestrictedSyscall;
use crate::utils::mount::MountPoint;
use crate::utils::{MemoryLimitType, TimeLimitType};
Expand Down Expand Up @@ -130,7 +130,9 @@ impl ExecuteCommand {
self
}

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

Expand Down

0 comments on commit 298a1d6

Please sign in to comment.