1
+ import { compileFromSource } from 'java-slang/dist/compiler' ;
2
+ import { BinaryWriter } from 'java-slang/dist/compiler/binary-writer' ;
1
3
import setupJVM , { parseBin } from 'java-slang/dist/jvm' ;
2
4
import { createModuleProxy , loadCachedFiles } from 'java-slang/dist/jvm/utils/integration' ;
3
5
import { Context } from 'js-slang' ;
@@ -7,7 +9,23 @@ import Constants from './Constants';
7
9
import DisplayBufferService from './DisplayBufferService' ;
8
10
9
11
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
+ }
11
29
12
30
let files = { } ;
13
31
let buffer : string [ ] = [ ] ;
@@ -86,15 +104,6 @@ export async function javaRun(javaCode: string, context: Context) {
86
104
} ) ;
87
105
} ;
88
106
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
-
98
107
// load cached classfiles from IndexedDB
99
108
return loadCachedFiles ( ( ) =>
100
109
// Initial loader to fetch commonly used classfiles
0 commit comments