Skip to content

Commit

Permalink
get proof-of-concept working with new API
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodingwizard committed Jul 16, 2024
1 parent 8698726 commit 49df283
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 42 deletions.
28 changes: 5 additions & 23 deletions pages/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function EditorPage() {
isCodeRunning: isRunning,
});
};
const fetchJudge = (code: string, input: string): Promise<Response> => {
const fetchJudge = (code: string, input: string): Promise<any> => {
return submitToJudge(
fileData.settings.language,
code,
Expand Down Expand Up @@ -114,19 +114,9 @@ function EditorPage() {
const code = getMainEditorValue();
fetchJudge(code, input)
.then(async resp => {
const data: JudgeResult = await resp.json();
if (!resp.ok) {
if (data.debugData?.errorType === 'Function.ResponseSizeTooLarge') {
alert(
'Error: Your program printed too much data to stdout/stderr.'
);
} else {
alert('Error: ' + (resp.status + ' - ' + JSON.stringify(data)));
}
} else {
cleanJudgeResult(data, expectedOutput, prefix);
setResultAt(inputTabIndex, data);
}
const data: JudgeResult = resp;
cleanJudgeResult(data, expectedOutput, prefix);
setResultAt(inputTabIndex, data);
})
.catch(e => {
alert(
Expand Down Expand Up @@ -162,15 +152,7 @@ function EditorPage() {
for (let index = 0; index < samples.length; ++index) {
const sample = samples[index];
const resp = await promises[index];
const data: JudgeResult = await resp.json();
if (!resp.ok || data.status === 'internal_error') {
alert(
'Error: ' +
(data.message || resp.status + ' - ' + JSON.stringify(data))
);
console.error(data);
throw new Error('bad judge result');
}
const data: JudgeResult = await resp;
let prefix = 'Sample';
if (samples.length > 1) prefix += ` ${index + 1}`;
prefix += ': ';
Expand Down
2 changes: 1 addition & 1 deletion pages/api/createUSACOFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export default async (
language: 'cpp',
problem,
compilerOptions: {
cpp: '-std=c++17 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wduplicated-cond -Wlogical-op',
cpp: '-std=c++23 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wduplicated-cond -Wlogical-op',
java: '',
py: '',
},
Expand Down
2 changes: 1 addition & 1 deletion pages/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function classNames(...classes: any[]) {
}

export const DEFAULT_COMPILER_OPTIONS = {
cpp: '-std=c++17 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wduplicated-cond -Wlogical-op',
cpp: '-std=c++23 -O2 -Wall -Wextra -Wshadow -Wconversion -Wfloat-equal -Wduplicated-cond -Wlogical-op',
java: '',
py: '',
};
Expand Down
60 changes: 43 additions & 17 deletions src/scripts/judge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,57 @@ export const submitToJudge = (
input: string,
compilerOptions: string,
fileIOName?: string
): Promise<Response> => {
): Promise<any> => {
const data = {
sourceCode: code,
filename: {
cpp: 'main.cpp',
java: extractJavaFilename(code),
py: 'main.py',
}[language],
language,
input,
compilerOptions: compilerOptions,
...(fileIOName
? {
fileIOName,
}
: {}),
compile: {
source_code: code,
compiler_options: compilerOptions,
language:
language === 'java' ? 'java21' : language === 'py' ? 'py12' : 'cpp',
},
execute: {
timeout_ms: 5000,
stdin: input,
},
// todo: fileioname
};
return fetch(
`https://ggzk2rm2ad.execute-api.us-west-1.amazonaws.com/Prod/execute`,
`https://v3nuswv3poqzw6giv37wmrt6su0krxvt.lambda-url.us-east-1.on.aws/compile-and-execute`,
{
method: 'POST',
headers: {
'content-type': 'application/json',
},
body: JSON.stringify(data),
}
);
).then(async resp => {
if (!resp.ok) {
const msg = await resp.text();
throw new Error(msg);
}
const data = await resp.json();
if (data.compile?.exit_code !== 0) {
return {
status: 'compile_error',
stdout: data.compile.stdout,
message: data.compile.stderr,
compilationMessage: data.compile.stderr,
time: data.compile.wall_time,
memory: data.compile.memory_usage,
};
}
return {
status:
data.execute.exit_code === 0
? 'success'
: data.execute.exit_code === 124
? 'time_limit_exceeded'
: 'runtime_error',
stdout: data.execute.stdout,
stderr: data.execute.stderr,
compilationMessage: data.compile.stderr,
time: data.execute.wall_time,
memory: data.execute.memory_usage,
};
});
};

0 comments on commit 49df283

Please sign in to comment.