core: 修复pretest结果不回显的问题

This commit is contained in:
undefined committed 2021-01-14 13:39:33 +08:00
1 parent 25570d6275
commit cce3b3a745
6 files changed
+69 -39

No files matched your search

+54 -9
View File
@@ -5,27 +5,72 @@ import * as STATUS from '../status';
import { run } from '../sandbox';
import compile from '../compile';
import signals from '../signals';
import { compilerText, parseMemoryMB, parseTimeMS } from '../utils';
import { CompileError } from '../error';
export const judge = async (ctx) => {
ctx.stat.judge = new Date();
ctx.next({ status: STATUS.STATUS_COMPILING });
ctx.execute = await compile(ctx.lang, ctx.code, 'code', {}, ctx.next);
try {
ctx.execute = await compile(ctx.lang, ctx.code, 'code', {}, ctx.next);
} catch (e) {
if (e instanceof CompileError) {
ctx.next({
status: STATUS.STATUS_COMPILE_ERROR,
case: {
status: STATUS.STATUS_COMPILE_ERROR,
score: 0,
time_ms: 0,
memory_kb: 0,
message: compilerText(e.stdout, e.stderr),
},
});
ctx.end({
status: STATUS.STATUS_COMPILE_ERROR,
score: 0,
time_ms: 0,
memory_kb: 0,
});
} else {
ctx.next({
status: STATUS.STATUS_SYSTEM_ERROR,
case: {
status: STATUS.STATUS_SYSTEM_ERROR,
score: 0,
time_ms: 0,
memory_kb: 0,
message: `${e.message}\n${JSON.stringify(e.params)}`,
},
});
ctx.end({
status: STATUS.STATUS_COMPILE_ERROR,
score: 0,
time_ms: 0,
memory_kb: 0,
});
}
return;
}
ctx.clean.push(ctx.execute.clean);
ctx.next({ status: STATUS.STATUS_JUDGING, progress: 0 });
const copyIn = { ...ctx.execute.copyIn };
const stdin = path.resolve(ctx.tmpdir, 'stdin');
const stdout = path.resolve(ctx.tmpdir, 'stdout');
const stderr = path.resolve(ctx.tmpdir, 'stderr');
fs.writeFileSync(stdin, ctx.config.input || '');
const { filename } = ctx.config;
if (filename) copyIn[`${filename}.in`] = { content: ctx.config.input };
const copyOut = filename ? [`${filename}.out`] : [];
const stdin = path.resolve(ctx.tmpdir, '0.in');
await fs.writeFile(stdin, ctx.config.input);
const stdout = path.resolve(ctx.tmpdir, '0.out');
const stderr = path.resolve(ctx.tmpdir, '0.err');
const res = await run(
ctx.execute.execute.replace(/\$\{name\}/g, 'code'),
{
stdin,
stdout,
stdin: filename ? null : stdin,
stdout: filename ? null : stdout,
stderr,
copyIn,
time_limit_ms: ctx.config.time,
memory_limit_mb: ctx.config.memory,
copyOut,
time_limit_ms: parseTimeMS(ctx.config.time),
memory_limit_mb: parseMemoryMB(ctx.config.memory),
},
);
const { code, time_usage_ms, memory_usage_kb } = res;
+8 -7
View File
@@ -12,18 +12,19 @@ const MEMORY_UNITS = { k: 0.1, m: 1, g: 1024 };
const EMPTY_STR = /^[ \r\n\t]*$/i;
export const cmd = parse;
export function noop() { }
export function parseTimeMS(str: string) {
const match = TIME_RE.exec(str);
if (!match) throw new FormatError('Error parsing time: {0}', [str]);
export function parseTimeMS(val: string | number) {
if (typeof val === 'number') return val;
const match = TIME_RE.exec(val);
if (!match) throw new FormatError('Error parsing time: {0}', [val]);
return Math.floor(parseFloat(match[1]) * TIME_UNITS[match[2]]);
}
export function parseMemoryMB(str: string) {
const match = MEMORY_RE.exec(str);
if (!match) throw new FormatError('Error parsing memory: {0}', [str]);
export function parseMemoryMB(val: string | number) {
if (typeof val === 'number') return val;
const match = MEMORY_RE.exec(val);
if (!match) throw new FormatError('Error parsing memory: {0}', [val]);
return Math.floor(parseFloat(match[1]) * MEMORY_UNITS[match[2]]);
}