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 |
| Integer | Long | Short |
| String | StringBuilder | System |
| Thread | Throwable | |
| Thread | | |
| `java.security` |
| :-------------- |

6
jdk.js
View File

@ -66,6 +66,7 @@
let names = [
'Character',
'Double',
'Exception',
'Float',
'Integer',
'Long',
@ -19129,12 +19130,13 @@
'nextLong',
'nextFloat',
'nextDouble'
]
],
Thread: ['sleep']
};
if (typeof QuintOS != 'undefined') {
Object.assign(asyncMethods, {
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 = [];

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 () => {
class Thread {
async sleep(millis) {
await setTimeout(millis);
}
}
class Thread {}
Thread.sleep = (millis) => {
return new Promise((resolve) => {
setTimeout(resolve, millis);
});
};
jdk.java.lang.Thread = Thread;
};

View File

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

View File

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