Skip to content

Commit 64b0548

Browse files
committed
Most robust compile promise handling with undefined
Also fixed regression with accessing testcase by index instead of finding it
1 parent bce5811 commit 64b0548

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

src/extension/providers/JudgeViewProvider.ts

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,13 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
140140
return null;
141141
}
142142

143-
const testcase = this._getTestcase(id);
143+
const testcase = this._findTestcase(id);
144144
if (!testcase) {
145145
return null;
146146
}
147147

148148
const settings = getFileRunSettings(this._currentFile, extraVariables);
149-
if (!settings) {
149+
if (!settings || (testcase.mode === "interactive" && !settings.interactorFile)) {
150150
return null;
151151
}
152152

@@ -157,9 +157,18 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
157157
value: "COMPILING",
158158
});
159159

160-
const compilePromises = [compile(this._currentFile!, this._context)];
160+
const compilePromises = [];
161+
const currentFileCompilePromise = compile(this._currentFile!, this._context);
162+
if (!currentFileCompilePromise) {
163+
return null;
164+
}
165+
compilePromises.push(currentFileCompilePromise);
161166
if (testcase.mode === "interactive") {
162-
compilePromises.push(compile(settings.interactorFile!, this._context));
167+
const interactorCompilePromise = compile(settings.interactorFile!, this._context);
168+
if (!interactorCompilePromise) {
169+
return null;
170+
}
171+
compilePromises.push(interactorCompilePromise);
163172
}
164173
const errored = await Promise.all(compilePromises);
165174
const anyErrored = errored.some((hadError) => hadError);
@@ -246,7 +255,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
246255
});
247256
}
248257

249-
private _launchTestcase(ctx: ExecutionContext, bypassLimits: boolean, debugMode: boolean) {
258+
private async _launchTestcase(ctx: ExecutionContext, bypassLimits: boolean, debugMode: boolean) {
250259
const { token, testcase, languageSettings, cwd } = ctx;
251260
if (!debugMode && !languageSettings.runCommand) {
252261
const logger = getLogger("judge");
@@ -322,6 +331,8 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
322331
bypassLimits ? 0 : this._memoryLimit,
323332
cwd
324333
);
334+
335+
await testcase.process.done;
325336
}
326337

327338
private async _launchInteractiveTestcase(
@@ -968,33 +979,36 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
968979
}
969980

970981
private async _run(id: number, newTestcase: boolean): Promise<void> {
971-
const donePromise = this._state[id].donePromise;
982+
const ctx = await this._getExecutionContext(id);
983+
if (!ctx) {
984+
return;
985+
}
986+
987+
const testcase = this._findTestcase(id);
988+
if (!testcase) {
989+
return;
990+
}
991+
992+
const donePromise = testcase.donePromise;
972993
if (donePromise) {
973994
await donePromise;
974995
return;
975996
}
976997

977-
this._state[id].donePromise = new Promise((resolve) => {
998+
testcase.donePromise = new Promise((resolve) => {
978999
void (async () => {
979-
const ctx = await this._getExecutionContext(id);
980-
if (!ctx) {
981-
resolve();
982-
return;
983-
}
984-
9851000
if (ctx.testcase.mode === "interactive") {
986-
this._launchInteractiveTestcase(ctx, newTestcase, false);
1001+
await this._launchInteractiveTestcase(ctx, newTestcase, false);
9871002
} else {
988-
this._launchTestcase(ctx, newTestcase, false);
1003+
await this._launchTestcase(ctx, newTestcase, false);
9891004
}
9901005

991-
await this._state[id].process.done;
9921006
resolve();
9931007
})();
9941008
});
9951009

996-
await this._state[id].donePromise;
997-
this._state[id].donePromise = null;
1010+
await testcase.donePromise;
1011+
testcase.donePromise = null;
9981012
}
9991013

10001014
private async _debug(id: number): Promise<void> {
@@ -1111,7 +1125,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
11111125
}
11121126

11131127
private _stop(id: number) {
1114-
const testcase = this._getTestcase(id);
1128+
const testcase = this._findTestcase(id);
11151129
if (!testcase) {
11161130
return;
11171131
}
@@ -1142,7 +1156,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
11421156
}
11431157

11441158
private _accept(id: number) {
1145-
const testcase = this._getTestcase(id);
1159+
const testcase = this._findTestcase(id);
11461160
if (!testcase) {
11471161
return;
11481162
}
@@ -1168,7 +1182,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
11681182
}
11691183

11701184
private _decline(id: number) {
1171-
const testcase = this._getTestcase(id);
1185+
const testcase = this._findTestcase(id);
11721186
if (!testcase) {
11731187
return;
11741188
}
@@ -1191,7 +1205,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
11911205
}
11921206

11931207
private _toggleVisibility(id: number) {
1194-
const testcase = this._getTestcase(id);
1208+
const testcase = this._findTestcase(id);
11951209
if (!testcase) {
11961210
return;
11971211
}
@@ -1214,7 +1228,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
12141228
}
12151229

12161230
private _toggleSkip(id: number) {
1217-
const testcase = this._getTestcase(id);
1231+
const testcase = this._findTestcase(id);
12181232
if (!testcase) {
12191233
return;
12201234
}
@@ -1230,7 +1244,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
12301244
}
12311245

12321246
private _compare(id: number) {
1233-
const testcase = this._getTestcase(id);
1247+
const testcase = this._findTestcase(id);
12341248
if (!testcase) {
12351249
return;
12361250
}
@@ -1243,7 +1257,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
12431257
}
12441258

12451259
private _viewStdio({ id, stdio }: v.InferOutput<typeof ViewMessageSchema>) {
1246-
const testcase = this._getTestcase(id);
1260+
const testcase = this._findTestcase(id);
12471261
if (!testcase) {
12481262
return;
12491263
}
@@ -1268,7 +1282,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
12681282
}
12691283

12701284
private _stdin({ id, data }: v.InferOutput<typeof StdinMessageSchema>) {
1271-
const testcase = this._getTestcase(id);
1285+
const testcase = this._findTestcase(id);
12721286
if (!testcase) {
12731287
return;
12741288
}
@@ -1283,7 +1297,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
12831297
}
12841298

12851299
private async _save({ id, stdio, data }: v.InferOutput<typeof SaveMessageSchema>) {
1286-
const testcase = this._getTestcase(id);
1300+
const testcase = this._findTestcase(id);
12871301
if (!testcase) {
12881302
return;
12891303
}
@@ -1364,7 +1378,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
13641378
id,
13651379
stdio,
13661380
}: v.InferOutput<typeof RequestTrimmedDataMessageSchema>) {
1367-
const testcase = this._getTestcase(id);
1381+
const testcase = this._findTestcase(id);
13681382
if (!testcase) {
13691383
return;
13701384
}
@@ -1405,7 +1419,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
14051419
}
14061420

14071421
private _requestFullData({ id, stdio }: v.InferOutput<typeof RequestFullDataMessageSchema>) {
1408-
const testcase = this._getTestcase(id);
1422+
const testcase = this._findTestcase(id);
14091423
if (!testcase) {
14101424
return;
14111425
}
@@ -1444,7 +1458,7 @@ export default class extends BaseViewProvider<typeof ProviderMessageSchema, Webv
14441458
});
14451459
}
14461460

1447-
private _getTestcase(id: number): State | undefined {
1461+
private _findTestcase(id: number): State | undefined {
14481462
return this._state.find((t) => t.id === id);
14491463
}
14501464
}

0 commit comments

Comments
 (0)