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-12 12:36:32 -05:00
parent d1290927ab
commit 7770050412
6 changed files with 40 additions and 28 deletions

View File

@ -6,28 +6,28 @@ java2js can translate simple Java programs to JavaScript and runs them using a J
## Java classes included in the java2js JDK
| java.io | | |
| ------- | ----------- | ----------- |
| File | InputStream | PrintStream |
| `java.io` | | |
| :-------- | ----------- | ----------- |
| File | InputStream | PrintStream |
| java.lang | | |
| --------- | ------------- | --------- |
| Boolean | Byte | Character |
| Double | Exception | Float |
| Integer | Long | Short |
| String | StringBuilder | System |
| Thread | Throwable | |
| `java.lang` | | |
| :---------- | ------------- | --------- |
| Boolean | Byte | Character |
| Double | Exception | Float |
| Integer | Long | Short |
| String | StringBuilder | System |
| Thread | Throwable | |
| java.security | | |
| ------------- | --- | --- |
| MessageDigest | | |
| `java.security` |
| :-------------- |
| MessageDigest |
| java.time | | |
| --------- | --- | --- |
| Instant | | |
| `java.time` |
| :---------- |
| Instant |
| java.util | | |
| ------------------ | ---------------------- | ----------- |
| `java.util` | | |
| :----------------- | ---------------------- | ----------- |
| AbstractCollection | AbstractList | AbstractMap |
| AbstractSet | ArrayList | Arrays |
| Collections | Formatter | HashMap |
@ -48,6 +48,7 @@ The java2js transpiler supports:
- two dimensional arrays
- lambda arrow functions
- triple quote strings
- try/catch blocks
## API
@ -55,7 +56,7 @@ The java2js transpiler supports:
This function imports the standard java.lang classes into the global scope. You must use it before translating or running files.
- root: (optional) path to the JS JDK folder, by default it is `./jdk` (the java2js JS JDK), for online use on codepen or similar code sharing sites you can use this link as the root path: 'https://unpkg.com/java2js/jdk'
- root: (optional) path to the JS JDK folder, by default it is `./jdk` (the java2js JS JDK), for online use on codepen or similar code sharing sites you can use this link as the root path: 'https://quinton-ashley.github.io/java2js/jdk'
### await jdk.transpile(javaFile)

10
ide.js
View File

@ -2,11 +2,15 @@
jdk.log = document.getElementById('javaConsole');
await jdk.init('./jdk');
let file0 = document.getElementById('javaFile');
file0.onchange = async () => {
async function run() {
jdk.log.value = '';
let translation = await jdk.transpile(file0.value);
console.log(translation);
jdk.run();
};
}
let file0 = document.getElementById('javaFile');
file0.onchange = run;
run();
})();

View File

@ -55,7 +55,14 @@
autocorrect="off"
autocapitalize="off"
placeholder="Create a Java class with a main method here..."
></textarea>
>
public class HelloWorld {
public static void main(String[] args){
System.out.println("hello world!");
}
}
</textarea
>
<div class="mid" onclick="location.href = 'https:\/\/github.com/quinton-ashley/java2js';">
<img src="favicon.ico" />
<p>j</p>

2
jdk.js
View File

@ -171,7 +171,7 @@
let imports = file.slice(0, classLine);
imports = imports.match(/(?<=^import )[^;]*/gm) || [];
let className = file.slice(classLine + 13, file.indexOf(' {', classLine + 13));
let className = file.slice(classLine + 13, file.indexOf('{', classLine + 13));
// workaround hack for converting triple quotes to a normal string
file = file.replace(/"""([^"]*)"""/gm, (match, p1) => {

View File

@ -32,7 +32,7 @@ jdk.imports['java.util.Scanner'].load = async () => {
return await this.next(/.*\n/);
}
async next(pattern) {
while (this._loading || !this.hasNext(pattern)) {
while (this._loading || !(await this.hasNext(pattern))) {
await new Promise((done) => setTimeout(() => done(), 100));
}
let buf = this.in.stream.slice(this.in.mark);

View File

@ -1,11 +1,11 @@
{
"name": "java2js",
"version": "1.2.4",
"version": "1.2.5",
"description": "Converts Java to JavaScript and runs it with a JS JDK",
"main": "jdk.js",
"scripts": {
"start": "open demo.html",
"test": "open demo.html",
"start": "open index.html",
"test": "open index.html",
"v": "npm version patch --force",
"V": "npm version minor --force",
"version": "git add -A",