diff --git a/jdk.js b/jdk.js index 64e978e..48f9bd2 100755 --- a/jdk.js +++ b/jdk.js @@ -229,6 +229,8 @@ return '() => {' + p1.replaceAll('\\n', '\n') + '}'; }); + trans.replace(/catch \(\w*/gm, 'catch ('); + trans = trans.replace(/(\([^\(\)]*\) =>)/gm, 'async $1'); let prefix = `(jdk.imports['${packageName}.${className}'] = {}).load = async () => {\n\n`; @@ -250,6 +252,7 @@ let suffix = '\n'; suffix += `window.${className} = ${className};\n`; + suffix += `console.log("loaded ${className}.java");\n`; suffix += '};'; trans = prefix + trans + suffix; @@ -19116,7 +19119,17 @@ const classVarsMap = {}; let asyncMethods = { - Scanner: ['next', 'nextLine', 'nextInt', 'nextShort', 'nextLong', 'nextFloat', 'nextDouble'] + Scanner: [ + 'hasNext', + 'hasNextLine', + 'next', + 'nextLine', + 'nextInt', + 'nextShort', + 'nextLong', + 'nextFloat', + 'nextDouble' + ] }; if (typeof QuintOS != 'undefined') { Object.assign(asyncMethods, { diff --git a/jdk/java/io/FileNotFoundException.js b/jdk/java/io/FileNotFoundException.js new file mode 100644 index 0000000..9b46c66 --- /dev/null +++ b/jdk/java/io/FileNotFoundException.js @@ -0,0 +1,4 @@ +jdk.imports['java.io.FileNotFoundException'].load = async () => { + class FileNotFoundException {} + jdk.java.io.FileNotFoundException = FileNotFoundException; +}; diff --git a/jdk/java/lang/System.js b/jdk/java/lang/System.js index 9eccb90..f0fea6e 100755 --- a/jdk/java/lang/System.js +++ b/jdk/java/lang/System.js @@ -12,6 +12,10 @@ jdk.imports['java.lang.System'].load = async () => { System.in.mark += length; }; + System.getProperty = (prop) => { + return ''; + }; + System.arraycopy = (src, srcPos, dest, destPos, numElements) => { if ( (dest instanceof Float64Array || dest instanceof Int32Array) && diff --git a/jdk/java/util/Scanner.js b/jdk/java/util/Scanner.js index 9f510aa..2aa5173 100755 --- a/jdk/java/util/Scanner.js +++ b/jdk/java/util/Scanner.js @@ -1,29 +1,38 @@ jdk.imports['java.util.Scanner'].load = async () => { - const File = await jdk.import('java.io.File'); + const InputStream = await jdk.import('java.io.InputStream'); class Scanner { constructor(input) { - if (input instanceof File) { - this.inputType = 'File'; - throw 'unsupported Scanner input type: File'; + if (input.getAbsolutePath) { + this._loading = true; + this._filePath = input.getAbsolutePath(); + return; } this.in = input; } - hasNext(pattern) { + async _loadFile(filePath) { + this.in = new InputStream(); + this.in.stream = await (await fetch(filePath)).text(); + this._loading = false; + } + async hasNext(pattern) { + if (this._loading) { + await this._loadFile(this._filePath); + } if (pattern instanceof RegExp) { return pattern.test(this.in.stream.slice(this.in.mark)); } // if pattern is string - return this.in.stream.includes(pattern); + return this.in.stream.slice(this.in.mark).includes(pattern); } - hasNextLine() { + async hasNextLine() { return this.hasNext('\n'); } async nextLine() { return await this.next(/.*\n/); } async next(pattern) { - while (!this.hasNext(pattern)) { + while (this._loading || !this.hasNext(pattern)) { await new Promise((done) => setTimeout(() => done(), 100)); } let buf = this.in.stream.slice(this.in.mark); diff --git a/package.json b/package.json index 36a1939..d5cdb28 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "java2js", - "version": "1.2.3", + "version": "1.2.4", "description": "Converts Java to JavaScript and runs it with a JS JDK", "main": "jdk.js", "scripts": {