Skip to content

Commit 1971477

Browse files
1001meibryanlohxz
authored andcommitted
Integrate Java compiler into frontend
1 parent 4cc0204 commit 1971477

File tree

1 file changed

+19
-10
lines changed

1 file changed

+19
-10
lines changed

src/commons/utils/JavaHelper.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { compileFromSource } from 'java-slang/dist/compiler';
2+
import { BinaryWriter } from 'java-slang/dist/compiler/binary-writer';
13
import setupJVM, { parseBin } from 'java-slang/dist/jvm';
24
import { createModuleProxy, loadCachedFiles } from 'java-slang/dist/jvm/utils/integration';
35
import { Context } from 'js-slang';
@@ -7,7 +9,23 @@ import Constants from './Constants';
79
import DisplayBufferService from './DisplayBufferService';
810

911
export async function javaRun(javaCode: string, context: Context) {
10-
let compiled = {};
12+
let compiled = {}
13+
14+
try {
15+
const classFile = compileFromSource(javaCode);
16+
compiled = {
17+
"Main.class": Buffer.from(new BinaryWriter().generateBinary(classFile)).toString('base64')
18+
};
19+
} catch (e) {
20+
context.errors.push({
21+
type: "CompileError" as any,
22+
severity: "Error" as any,
23+
location: { start: { line: -1, column: -1 }, end: { line: -1, column: -1 } },
24+
explain: () => e,
25+
elaborate: () => e
26+
});
27+
return Promise.resolve({ status: 'error' });
28+
}
1129

1230
let files = {};
1331
let buffer: string[] = [];
@@ -86,15 +104,6 @@ export async function javaRun(javaCode: string, context: Context) {
86104
});
87105
};
88106

89-
// FIXME: Remove when the compiler is working
90-
try {
91-
const json = JSON.parse(javaCode);
92-
compiled = json;
93-
} catch (e) {
94-
stderr(e);
95-
return Promise.resolve({ status: 'error' });
96-
}
97-
98107
// load cached classfiles from IndexedDB
99108
return loadCachedFiles(() =>
100109
// Initial loader to fetch commonly used classfiles

0 commit comments

Comments
 (0)