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 classes included in the java2js JDK
| java.io | | | | `java.io` | | |
| ------- | ----------- | ----------- | | :-------- | ----------- | ----------- |
| File | InputStream | PrintStream | | File | InputStream | PrintStream |
| java.lang | | | | `java.lang` | | |
| --------- | ------------- | --------- | | :---------- | ------------- | --------- |
| Boolean | Byte | Character | | Boolean | Byte | Character |
| Double | Exception | Float | | Double | Exception | Float |
| Integer | Long | Short | | Integer | Long | Short |
| String | StringBuilder | System | | String | StringBuilder | System |
| Thread | Throwable | | | Thread | Throwable | |
| java.security | | | | `java.security` |
| ------------- | --- | --- | | :-------------- |
| MessageDigest | | | | MessageDigest |
| java.time | | | | `java.time` |
| --------- | --- | --- | | :---------- |
| Instant | | | | Instant |
| java.util | | | | `java.util` | | |
| ------------------ | ---------------------- | ----------- | | :----------------- | ---------------------- | ----------- |
| AbstractCollection | AbstractList | AbstractMap | | AbstractCollection | AbstractList | AbstractMap |
| AbstractSet | ArrayList | Arrays | | AbstractSet | ArrayList | Arrays |
| Collections | Formatter | HashMap | | Collections | Formatter | HashMap |
@ -48,6 +48,7 @@ The java2js transpiler supports:
- two dimensional arrays - two dimensional arrays
- lambda arrow functions - lambda arrow functions
- triple quote strings - triple quote strings
- try/catch blocks
## API ## 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. 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) ### await jdk.transpile(javaFile)

10
ide.js
View File

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

View File

@ -55,7 +55,14 @@
autocorrect="off" autocorrect="off"
autocapitalize="off" autocapitalize="off"
placeholder="Create a Java class with a main method here..." 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';"> <div class="mid" onclick="location.href = 'https:\/\/github.com/quinton-ashley/java2js';">
<img src="favicon.ico" /> <img src="favicon.ico" />
<p>j</p> <p>j</p>

2
jdk.js
View File

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

View File

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

View File

@ -1,11 +1,11 @@
{ {
"name": "java2js", "name": "java2js",
"version": "1.2.4", "version": "1.2.5",
"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": {
"start": "open demo.html", "start": "open index.html",
"test": "open demo.html", "test": "open index.html",
"v": "npm version patch --force", "v": "npm version patch --force",
"V": "npm version minor --force", "V": "npm version minor --force",
"version": "git add -A", "version": "git add -A",