1
0
mirror of https://github.com/quinton-ashley/java2js synced 2024-12-29 10:11:54 +01:00
This commit is contained in:
Quinton Ashley 2022-04-20 22:06:16 -05:00
parent 7770050412
commit ab8675e241
7 changed files with 41 additions and 22 deletions

View File

@ -16,7 +16,7 @@ java2js can translate simple Java programs to JavaScript and runs them using a J
| Double | Exception | Float | | Double | Exception | Float |
| Integer | Long | Short | | Integer | Long | Short |
| String | StringBuilder | System | | String | StringBuilder | System |
| Thread | Throwable | | | Thread | | |
| `java.security` | | `java.security` |
| :-------------- | | :-------------- |

6
jdk.js
View File

@ -66,6 +66,7 @@
let names = [ let names = [
'Character', 'Character',
'Double', 'Double',
'Exception',
'Float', 'Float',
'Integer', 'Integer',
'Long', 'Long',
@ -19129,12 +19130,13 @@
'nextLong', 'nextLong',
'nextFloat', 'nextFloat',
'nextDouble' 'nextDouble'
] ],
Thread: ['sleep']
}; };
if (typeof QuintOS != 'undefined') { if (typeof QuintOS != 'undefined') {
Object.assign(asyncMethods, { Object.assign(asyncMethods, {
Sprite: ['move'], Sprite: ['move'],
window: ['alert', 'delay', 'erase', 'eraseRect', 'frame', 'play', 'prompt', 'text', 'textRect'] window: ['alert', 'delay', 'eraseRect', 'frame', 'play', 'prompt', 'text', 'textRect']
}); });
} }
if (!asyncMethods.window) asyncMethods.window = []; if (!asyncMethods.window) asyncMethods.window = [];

View File

@ -0,0 +1,13 @@
jdk.imports['java.lang.Exception'].load = async () => {
// TODO: should accept a PrintStream or PrintWriter
Error.prototype.printStackTrace = function (p) {
log(this.stack);
};
Error.prototype.getStackTrace = function () {
return this.stack;
};
Error.prototype.getMessage = function () {
return this.message;
};
jdk.java.lang.Exception = Error;
};

View File

@ -1,8 +1,9 @@
jdk.imports['java.lang.Thread'].load = async () => { jdk.imports['java.lang.Thread'].load = async () => {
class Thread { class Thread {}
async sleep(millis) { Thread.sleep = (millis) => {
await setTimeout(millis); return new Promise((resolve) => {
} setTimeout(resolve, millis);
} });
};
jdk.java.lang.Thread = Thread; jdk.java.lang.Thread = Thread;
}; };

View File

@ -25,37 +25,40 @@ jdk.imports['java.util.Scanner'].load = async () => {
// if pattern is string // if pattern is string
return this.in.stream.slice(this.in.mark).includes(pattern); return this.in.stream.slice(this.in.mark).includes(pattern);
} }
async hasNextLine() { hasNextLine() {
return this.hasNext('\n'); return this.hasNext('\n');
} }
async nextLine() { nextLine() {
return await this.next(/.*\n/); return this.next(/.*/);
} }
async next(pattern) { async next(pattern) {
while (this._loading || !(await this.hasNext(pattern))) { while (this._loading || !(await this.hasNext(pattern))) {
await new Promise((done) => setTimeout(() => done(), 100)); await new Promise((resolve) => setTimeout(resolve, 100));
} }
let buf = this.in.stream.slice(this.in.mark); let buf = this.in.stream.slice(this.in.mark);
let substr = buf.match(pattern)[0]; let substr = buf.match(pattern)[0];
let start = buf.indexOf(substr); let start = buf.indexOf(substr);
let end = buf.indexOf('\n'); let end = buf.indexOf('\n');
if (end == -1) {
throw 'NoSuchElementException: No ' + pattern.toString() + ' found in buffer ' + buf;
}
this.in.read(end - start + 1); this.in.read(end - start + 1);
return buf.slice(start, end); return buf.slice(start, substr.length);
} }
async nextShort() { nextShort() {
return await this.nextInt(); return this.nextInt();
} }
async nextInt() { async nextInt() {
return Number(await this.next(/\d+\D/)); return Number(await this.next(/\d+/));
} }
async nextLong() { nextLong() {
return await this.nextInt(); return this.nextInt();
} }
async nextFloat() { async nextFloat() {
return Number(await this.next(/[0-9\.]+[^0-9\.]/)); return Number(await this.next(/[0-9\.]+/));
} }
async nextDouble() { nextDouble() {
return await this.nextFloat(); return this.nextFloat();
} }
close() {} close() {}
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "java2js", "name": "java2js",
"version": "1.2.5", "version": "1.2.6",
"description": "Converts Java to JavaScript and runs it with a JS JDK", "description": "Converts Java to JavaScript and runs it with a JS JDK",
"main": "jdk.js", "main": "jdk.js",
"scripts": { "scripts": {