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 2021-11-01 14:49:10 -05:00
parent c3914fb6c0
commit 8e4c614400
3 changed files with 96 additions and 68 deletions

13
ide.js
View File

@ -1,13 +1,10 @@
(async () => { (async () => {
window.ide = {}; jdk.log = document.getElementById('javaConsole');
ide.log = document.getElementById('javaConsole');
ide.file0 = document.getElementById('javaFile');
await jdk.init(); await jdk.init();
ide.file0.onchange = () => { let file0 = document.getElementById('javaFile');
ide.log.value = ''; file0.onchange = () => {
let file = ide.file0.value; jdk.log.value = '';
jdk.run(file); jdk.run(file0.value);
}; };
})(); })();

87
jdk.js
View File

@ -47,7 +47,8 @@
class JDK { class JDK {
constructor() {} constructor() {}
async init() { async init(root) {
this.root = root || '.';
this.java = {}; this.java = {};
let pkgs = ['com', 'lang', 'org', 'io', 'util']; let pkgs = ['com', 'lang', 'org', 'io', 'util'];
for (let pkg of pkgs) { for (let pkg of pkgs) {
@ -81,6 +82,15 @@
await this.import(lang); await this.import(lang);
} catch (e) {} } catch (e) {}
// make java.lang classes global
for (let name in this.java.lang) {
if (name == 'String') continue;
window[name] = this.java.lang[name];
}
System.in.reset();
System.out.reset();
// stub main for now // stub main for now
this.main = () => {}; this.main = () => {};
} }
@ -97,15 +107,6 @@
// } // }
launch() { launch() {
// make java.lang classes global
for (let name in this.java.lang) {
if (name == 'String') continue;
window[name] = this.java.lang[name];
}
System.in.reset();
System.out.reset();
this.main(this.jvmArgs); this.main(this.jvmArgs);
} }
@ -139,7 +140,7 @@
imp.load = null; imp.load = null;
imp.classPath = className.split('.'); imp.classPath = className.split('.');
let src = './jdk'; let src = this.root + '/jdk';
for (let part of imp.classPath) { for (let part of imp.classPath) {
src += '/' + part; src += '/' + part;
} }
@ -167,7 +168,12 @@
return classes; return classes;
} }
async run(file) { async translate(file) {
if (!this.java) {
console.error(
'java2js error: JDK not initialized! You must call `await jdk.init()` before using `jdk.run()`'
);
}
let classLine = file.indexOf('public class'); let classLine = file.indexOf('public class');
let imports = file.slice(0, classLine); let imports = file.slice(0, classLine);
imports = imports.match(/(?<=^import )[^;]*/gm) || []; imports = imports.match(/(?<=^import )[^;]*/gm) || [];
@ -187,7 +193,7 @@
// workaround hack for converting lambda expressions to Runnables // workaround hack for converting lambda expressions to Runnables
let lambdaRegex = /\(\)\s*\->\s*\{(([^\{\}]*\{[^\}]*\})*[^\}]*)\}/g; let lambdaRegex = /\(\)\s*\->\s*\{(([^\{\}]*\{[^\}]*\})*[^\}]*)\}/g;
file = file.replaceAll(lambdaRegex, (match, in0) => { file = file.replaceAll(lambdaRegex, (match, in0) => {
log(in0); // log(in0);
if (lambdaRegex.test(in0)) { if (lambdaRegex.test(in0)) {
in0 = in0.replaceAll(lambdaRegex, (match, in1) => { in0 = in0.replaceAll(lambdaRegex, (match, in1) => {
return 'new Runnable() {\n@Override\npublic void run() {' + in1 + '}}'; return 'new Runnable() {\n@Override\npublic void run() {' + in1 + '}}';
@ -203,9 +209,7 @@
file = file.replace(/\(int\)\s*/gm, 'Math.floor'); file = file.replace(/\(int\)\s*/gm, 'Math.floor');
file = file.replace(/\(int\)\s*\-/gm, 'Math.ceil'); file = file.replace(/\(int\)\s*\-/gm, 'Math.ceil');
log(file); let trans = await java_to_javascript(file);
let trans = java_to_javascript(file);
// log(trans); // log(trans);
@ -233,14 +237,35 @@
trans = prefix + trans + suffix; trans = prefix + trans + suffix;
log(trans); return trans;
try {
eval(trans);
} catch (e) {
console.error(e);
if (window?.ide) ide.log.value += e;
} }
run(trans) {
return new Promise(async (resolve, reject) => {
const script = document.createElement('script');
script.async = false;
script.onload = function () {
// log('loaded: ' + src);
resolve();
};
script.onerror = (e) => {
reject(e);
};
// prevent page loading from the browser's cache
// if (QuintOS.context == 'live') {
// src += '?' + Date.now();
// }
script.innerHTML = trans;
document.body.appendChild(script);
});
// try {
// eval(trans);
// } catch (e) {
// console.error(e);
// if (this.ide) this.log.value += e;
// }
} }
} }
window.jdk = new JDK(); window.jdk = new JDK();
@ -19419,7 +19444,10 @@
* @return {string} - Converted JavaScript * @return {string} - Converted JavaScript
*/ */
const javaToJavascript = (javaString, options = {}, progress) => { const javaToJavascript = (javaString, options = {}, progress) => {
if (typeof javaString !== 'string') throw 'java-to-javascript: First argument must be a string'; return new Promise((resolve, reject) => {
if (typeof javaString !== 'string') {
reject('java-to-javascript: First argument must be a string');
}
// Reset opts parameters // Reset opts parameters
Object.assign(opts, DEFAULT_OPTIONS); Object.assign(opts, DEFAULT_OPTIONS);
@ -19441,8 +19469,8 @@
javaAST$$1 = javaAST.parse(javaString); javaAST$$1 = javaAST.parse(javaString);
} catch (e) { } catch (e) {
let line = e.location.start.line; let line = e.location.start.line;
if (e.location) throw `on line ${line}: \n\n${javaString.split('\n')[line - 1].trim()}\n\n${e.stack}`; if (e.location) reject(`on line ${line}: \n\n${javaString.split('\n')[line - 1].trim()}\n\n${e.stack}`);
else throw e; else reject(e);
} }
if (progress) progress(0.5, 'Converting to JavaScript'); if (progress) progress(0.5, 'Converting to JavaScript');
@ -19451,7 +19479,9 @@
if (options.p5) { if (options.p5) {
jsString = globalsToJs(parseClass(javaAST$$1.types[0], true)); jsString = globalsToJs(parseClass(javaAST$$1.types[0], true));
} else { } else {
jsString = javaAST$$1.types.map((globalClass) => classToJs(parseClass(globalClass))).join(opts.separator); jsString = javaAST$$1.types
.map((globalClass) => classToJs(parseClass(globalClass)))
.join(opts.separator);
} }
if (progress) progress(0.75, 'Beautifying'); if (progress) progress(0.75, 'Beautifying');
@ -19465,7 +19495,8 @@
if (progress) progress(1.0, 'Success'); if (progress) progress(1.0, 'Success');
return jsString; resolve(jsString);
});
}; };
var lib = javaToJavascript; var lib = javaToJavascript;

View File

@ -1,6 +1,6 @@
{ {
"name": "java2js", "name": "java2js",
"version": "1.0.6", "version": "1.0.7",
"description": "Converts Java to JavaScript with support for p5.js and QuintOS.", "description": "Converts Java to JavaScript with support for p5.js and QuintOS.",
"main": "java2js.js", "main": "java2js.js",
"scripts": { "scripts": {