mirror of
https://github.com/quinton-ashley/java2js
synced 2024-12-29 10:11:54 +01:00
1.0.2
This commit is contained in:
parent
fcf49e5a8d
commit
e9c4dd302e
@ -33,6 +33,5 @@
|
||||
<textarea id="javaConsole" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off"></textarea>
|
||||
<script type="text/javascript" src="jre.js"></script>
|
||||
<script type="text/javascript" src="ide.js"></script>
|
||||
<script type="text/javascript" src="transpiler.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
83
ide.js
83
ide.js
@ -1,76 +1,9 @@
|
||||
function ide(require, module, exports) {
|
||||
const log = console.log;
|
||||
window.ide = {};
|
||||
ide.log = document.getElementById('javaConsole');
|
||||
ide.file0 = document.getElementById('javaFile');
|
||||
|
||||
window.java = {};
|
||||
java.log = document.getElementById('javaConsole');
|
||||
java.file0 = document.getElementById('javaFile');
|
||||
|
||||
window.java2js = (file) => {
|
||||
const java_to_javascript = require('java-to-javascript');
|
||||
|
||||
let classLine = file.indexOf('public class');
|
||||
let imports = file.slice(0, classLine);
|
||||
imports = imports.match(/(?<=^import )[^;]*/gm) || [];
|
||||
|
||||
let userName = window?.QuintOS?.userName || 'quinton-ashley';
|
||||
let className = file.slice(classLine + 13, file.indexOf(' {', classLine + 13));
|
||||
|
||||
let prefix = `(jre.imports['com.${userName}.${className}'] = {}).load = () => {\n\n`;
|
||||
|
||||
// handle Java class imports
|
||||
for (let i = 0; i < imports.length; i++) {
|
||||
let imp = imports[i];
|
||||
let impPath = imp.split('.');
|
||||
let impName = impPath[impPath.length - 1];
|
||||
prefix += `let ${impName} = jre.import('${imp}');\n`;
|
||||
}
|
||||
prefix += '\n';
|
||||
|
||||
// hacky support for Java 15 triple quotes
|
||||
file = file.replaceAll(/"""([^"]*)"""/g, (match, str) => {
|
||||
str = str.replaceAll(/(.*)(\n|$)/g, '"$1\\n"+').slice(0, -1);
|
||||
return str;
|
||||
});
|
||||
|
||||
// hacky support for Array literals
|
||||
file = file.replaceAll(/=\s*new \w*\[\]\s*\{/g, '= {');
|
||||
|
||||
// convert string .length() method
|
||||
file = file.replaceAll(/\.length\(\)/g, '.length');
|
||||
|
||||
// cast to int, truncates the number (just removes decimal value)
|
||||
file = file.replace(/\(int\)\s*/gm, 'Math.floor');
|
||||
file = file.replace(/\(int\)\s*\-/gm, 'Math.ceil');
|
||||
|
||||
// log(file);
|
||||
|
||||
let suffix = `\njre.main = ${className}.main;\n}`;
|
||||
|
||||
window.file0 = file;
|
||||
|
||||
let trans = java_to_javascript(file);
|
||||
|
||||
// log(trans);
|
||||
|
||||
trans = trans.replace(/(\([^\)]*\) =>)/gm, 'async $1');
|
||||
trans = trans.replace(/([\w_\$]+\.next(Int|Float|Double|Line|Short|Long)*\(\))/gm, 'await $1');
|
||||
|
||||
trans = prefix + trans + suffix;
|
||||
|
||||
log(trans);
|
||||
|
||||
try {
|
||||
eval(trans);
|
||||
jre.run();
|
||||
} catch (e) {
|
||||
java.log.value += e;
|
||||
}
|
||||
};
|
||||
|
||||
java.file0.onchange = () => {
|
||||
java.log.value = '';
|
||||
let file = java.file0.value;
|
||||
|
||||
java2js(file);
|
||||
};
|
||||
}
|
||||
ide.file0.onchange = () => {
|
||||
ide.log.value = '';
|
||||
let file = ide.file0.value;
|
||||
jre.run(file);
|
||||
};
|
||||
|
@ -3,9 +3,11 @@ jre.imports['java.io.InputStream'].load = () => {
|
||||
constructor() {
|
||||
this.reset();
|
||||
let _this = this;
|
||||
java.log.onkeyup = () => {
|
||||
_this.stream = java.log.value;
|
||||
};
|
||||
if (window?.ide) {
|
||||
ide.log.onkeyup = () => {
|
||||
_this.stream = ide.log.value;
|
||||
};
|
||||
}
|
||||
}
|
||||
reset() {
|
||||
this.stream = '';
|
||||
|
@ -16,8 +16,8 @@ jre.imports['java.io.PrintStream'].load = () => {
|
||||
print(arg) {
|
||||
let str = arg.toString();
|
||||
this.log += str;
|
||||
if (java.log) {
|
||||
java.log.value += str;
|
||||
if (window?.ide) {
|
||||
ide.log.value += str;
|
||||
this._onPrint(str.length);
|
||||
}
|
||||
}
|
||||
@ -25,8 +25,8 @@ jre.imports['java.io.PrintStream'].load = () => {
|
||||
println(arg) {
|
||||
let str = arg.toString() + '\n';
|
||||
this.log += str;
|
||||
if (java.log) {
|
||||
java.log.value += str;
|
||||
if (window?.ide) {
|
||||
ide.log.value += str;
|
||||
this._onPrint(str.length);
|
||||
}
|
||||
}
|
||||
@ -34,8 +34,8 @@ jre.imports['java.io.PrintStream'].load = () => {
|
||||
printf(format, ...args) {
|
||||
let str = new Formatter().format(format, args);
|
||||
this.log += str;
|
||||
if (java.log) {
|
||||
java.log.value += str;
|
||||
if (window?.ide) {
|
||||
ide.log.value += str;
|
||||
this._onPrint(str.length);
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ jre.imports['java.lang.System'].load = () => {
|
||||
};
|
||||
System.exit = (code) => {
|
||||
console.log('Exited with code: ' + code);
|
||||
if (window.exit) exit();
|
||||
if (window?.exit) exit();
|
||||
};
|
||||
jre.java.lang.System = System;
|
||||
};
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "java2js",
|
||||
"version": "1.0.1",
|
||||
"version": "1.0.2",
|
||||
"description": "Converts Java to JavaScript with support for p5.js and QuintOS.",
|
||||
"main": "java2js.js",
|
||||
"scripts": {
|
||||
|
19275
transpiler.js
19275
transpiler.js
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user