Skip to content

Commit 49537fc

Browse files
committed
Merge branch 'shared-links-frontend' of https://github.com/source-academy/frontend into shared-links-frontend
2 parents cc412f0 + cf4c625 commit 49537fc

File tree

9 files changed

+330
-530
lines changed

9 files changed

+330
-530
lines changed

craco.config.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const cracoConfig = {
1818
plugin => plugin.constructor.name === 'InjectManifest'
1919
);
2020
if (injectManifestPlugin) {
21-
injectManifestPlugin.config.maximumFileSizeToCacheInBytes = 17 * 1024 * 1024;
21+
injectManifestPlugin.config.maximumFileSizeToCacheInBytes = 20 * 1024 * 1024;
2222
}
2323

2424
// add rules to pack WASM (for Sourceror)
@@ -47,9 +47,10 @@ const cracoConfig = {
4747
'https': require.resolve('https-browserify'),
4848
'os': require.resolve('os-browserify/browser'),
4949
'path/posix': require.resolve('path-browserify'),
50+
'process/browser': require.resolve('process/browser'),
5051
'stream': require.resolve('stream-browserify'),
5152
'timers': require.resolve('timers-browserify'),
52-
'url': require.resolve('url/')
53+
'url': require.resolve('url/'),
5354
};
5455

5556
// workaround .mjs files by Acorn
@@ -138,6 +139,7 @@ const cracoConfig = {
138139
'split-on-first',
139140
'filter-obj',
140141
'@sourceacademy/c-slang',
142+
'java-parser'
141143
),
142144
'^.+\\.module\\.(css|sass|scss)$'
143145
];

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@
5151
"flexboxgrid": "^6.3.1",
5252
"flexboxgrid-helpers": "^1.1.3",
5353
"hastscript": "^9.0.0",
54-
"java-slang": "^1.0.6",
5554
"js-slang": "^1.0.66",
55+
"java-slang": "^1.0.13",
5656
"js-yaml": "^4.1.0",
5757
"konva": "^9.2.0",
5858
"lodash": "^4.17.21",
@@ -92,7 +92,6 @@
9292
"rehype-react": "^8.0.0",
9393
"showdown": "^2.1.0",
9494
"sourceror": "^0.8.5",
95-
"typesafe-actions": "^5.1.0",
9695
"unified": "^11.0.0",
9796
"uuid": "^9.0.0",
9897
"xlsx": "https://cdn.sheetjs.com/xlsx-0.20.2/xlsx-0.20.2.tgz",

src/commons/utils/JavaHelper.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { compileFromSource, typeCheck } from 'java-slang';
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';
@@ -9,6 +11,33 @@ import DisplayBufferService from './DisplayBufferService';
911
export async function javaRun(javaCode: string, context: Context) {
1012
let compiled = {};
1113

14+
const stderr = (type: 'TypeCheck' | 'Compile' | 'Runtime', msg: string) => {
15+
context.errors.push({
16+
type: type as any,
17+
severity: 'Error' as any,
18+
location: { start: { line: -1, column: -1 }, end: { line: -1, column: -1 } },
19+
explain: () => msg,
20+
elaborate: () => msg
21+
});
22+
};
23+
24+
const typeCheckResult = typeCheck(javaCode);
25+
if (typeCheckResult.hasTypeErrors) {
26+
const typeErrMsg = typeCheckResult.errorMsgs.join('\n');
27+
stderr('TypeCheck', typeErrMsg);
28+
return Promise.resolve({ status: 'error' });
29+
}
30+
31+
try {
32+
const classFile = compileFromSource(javaCode);
33+
compiled = {
34+
'Main.class': Buffer.from(new BinaryWriter().generateBinary(classFile)).toString('base64')
35+
};
36+
} catch (e) {
37+
stderr('Compile', e);
38+
return Promise.resolve({ status: 'error' });
39+
}
40+
1241
let files = {};
1342
let buffer: string[] = [];
1443

@@ -46,6 +75,7 @@ export async function javaRun(javaCode: string, context: Context) {
4675
}
4776
return parseBin(new DataView(bytes.buffer));
4877
};
78+
4979
const loadNatives = async (path: string) => {
5080
// dynamic load modules
5181
if (path.startsWith('modules')) {
@@ -56,6 +86,7 @@ export async function javaRun(javaCode: string, context: Context) {
5686
}
5787
return await import(`java-slang/dist/jvm/stdlib/${path}.js`);
5888
};
89+
5990
const stdout = (str: string) => {
6091
if (str.endsWith('\n')) {
6192
buffer.push(str);
@@ -67,33 +98,6 @@ export async function javaRun(javaCode: string, context: Context) {
6798
buffer.push(str);
6899
}
69100
};
70-
const stderr = (msg: string) => {
71-
context.errors.push({
72-
type: 'Runtime' as any,
73-
severity: 'Error' as any,
74-
location: {
75-
start: {
76-
line: -1,
77-
column: -1
78-
},
79-
end: {
80-
line: -1,
81-
column: -1
82-
}
83-
},
84-
explain: () => msg,
85-
elaborate: () => msg
86-
});
87-
};
88-
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-
}
97101

98102
// load cached classfiles from IndexedDB
99103
return loadCachedFiles(() =>
@@ -119,12 +123,20 @@ export async function javaRun(javaCode: string, context: Context) {
119123
readFileSync: readClassFiles,
120124
readFile: loadNatives,
121125
stdout,
122-
stderr,
126+
stderr: (msg: string) => stderr('Runtime', msg),
123127
onFinish: () => {
124128
resolve(
125129
context.errors.length
126130
? { status: 'error' }
127-
: { status: 'finished', context, value: '' }
131+
: {
132+
status: 'finished',
133+
context,
134+
value: new (class {
135+
toString() {
136+
return ' ';
137+
}
138+
})()
139+
}
128140
);
129141
}
130142
},

0 commit comments

Comments
 (0)