Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/commons/utils/JavaHelper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compileFromSource, ECE, typeCheck } from 'java-slang';
import { BinaryWriter } from 'java-slang/dist/compiler/binary-writer';
import { IOCallbacks } from 'java-slang/dist/ec-evaluator';

Check failure on line 3 in src/commons/utils/JavaHelper.ts

View workflow job for this annotation

GitHub Actions / lint (build)

Module '"java-slang/dist/ec-evaluator"' has no exported member 'IOCallbacks'.

Check failure on line 3 in src/commons/utils/JavaHelper.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Module '"java-slang/dist/ec-evaluator"' has no exported member 'IOCallbacks'.
import setupJVM, { parseBin } from 'java-slang/dist/jvm';
import { createModuleProxy, loadCachedFiles } from 'java-slang/dist/jvm/utils/integration';
import { Context, Result } from 'js-slang';
Expand Down Expand Up @@ -98,17 +99,19 @@
return Promise.resolve({ status: 'error' });
}
} else {
if (isUsingCse) {
const result = await runJavaCseMachine(javaCode, targetStep, context, { stdout, stderr });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The raw stderr function, which expects two arguments, is passed to runJavaCseMachine. The CSE machine will likely call it with one, causing incorrect error handling.
Severity: MEDIUM

Suggested Fix

Wrap the stderr callback when passing it to runJavaCseMachine to hardcode the error type, ensuring it conforms to the expected single-argument signature. For example: stderr: (msg: string) => stderr('Runtime', msg).

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/commons/utils/JavaHelper.ts#L103

Potential issue: The `stderr` function defined in `JavaHelper.ts` requires two
arguments: `type` and `msg`. When passing this function as a callback to
`runJavaCseMachine`, it is not wrapped. External systems like the CSE machine are
expected to provide a single string argument to their I/O callbacks. This mismatch will
result in the `type` parameter receiving the error message and the `msg` parameter being
`undefined`, leading to incorrect error classification and potential runtime errors
within the `stderr` function body. A similar callback for the JVM path is correctly
wrapped, establishing a pattern that was not followed here.

Did we get this right? 👍 / 👎 to inform future reviews.


return result;
}

const typeCheckResult = typeCheck(javaCode);
if (typeCheckResult.hasTypeErrors) {
const typeErrMsg = typeCheckResult.errorMsgs.join('\n');
stderr('TypeCheck', typeErrMsg);
return Promise.resolve({ status: 'error' });
}

if (isUsingCse) {
return await runJavaCseMachine(javaCode, targetStep, context);
}

try {
const classFile = compileFromSource(javaCode);
compiled = {
Expand Down Expand Up @@ -178,7 +181,12 @@
}
}

export async function runJavaCseMachine(code: string, targetStep: number, context: Context) {
export async function runJavaCseMachine(
code: string,
targetStep: number,
context: Context,
ioCallbacks: IOCallbacks
) {
const convertJavaErrorToJsError = (e: ECE.SourceError): SourceError => ({
type: ErrorType.RUNTIME,
severity: ErrorSeverity.ERROR,
Expand All @@ -197,7 +205,7 @@
elaborate: () => e.explain()
});
context.executionMethod = 'cse-machine';
return ECE.runECEvaluator(code, targetStep)
return ECE.runECEvaluator(code, targetStep, ioCallbacks)

Check failure on line 208 in src/commons/utils/JavaHelper.ts

View workflow job for this annotation

GitHub Actions / lint (build)

Expected 1-2 arguments, but got 3.

Check failure on line 208 in src/commons/utils/JavaHelper.ts

View workflow job for this annotation

GitHub Actions / lint (tsc)

Expected 1-2 arguments, but got 3.
.then(result => {
context.runtime.envStepsTotal = result.context.totalSteps;
if (result.status === 'error') {
Expand Down
2 changes: 1 addition & 1 deletion src/features/cseMachine/java/components/Method.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

// Tooltip.
this._tooltipRef = React.createRef();
this._tooltip = astToString(this._method.mtdOrCon);
this._tooltip = astToString(this._method.decl);

Check failure on line 42 in src/features/cseMachine/java/components/Method.tsx

View workflow job for this annotation

GitHub Actions / lint (build)

Property 'decl' does not exist on type 'Closure'.

Check failure on line 42 in src/features/cseMachine/java/components/Method.tsx

View workflow job for this annotation

GitHub Actions / lint (tsc)

Property 'decl' does not exist on type 'Closure'.
}

get method() {
Expand Down
Loading