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-10-31 14:16:44 -05:00
parent d15fcec9e9
commit c3914fb6c0
5 changed files with 35 additions and 14 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.DS_Store
*.log
package-lock.json
node_modules
dev

View File

@ -1,8 +1,8 @@
# Java to Javascript for QuintOS
I've built on top of the "Java to Javascript" transpiler by @wyattades and got inspiration from the JavaDevelopmentKit implementation in "java2javascript" by @BobHanson and others.
I've built on top of the [java-to-javascript](https://github.com/wyattades/java-to-javascript) transpiler by @wyattades and got inspiration from the JDK (Java Development Kit) implementation in [java2javascript](https://github.com/java2script/java2script) by @BobHanson, @zhourenjian, @abego, and others.
The purpose of this project was to allow intro level CS students to write Java code but still use my QuintOS library which is web based instead of just having them run their programs in a Java console which is boring. I made a barebones JavaDevelopmentKit implementation in modern Javascript to acheive this.
My purpose in creating this project is to allow intro level CS students to write Java code but still use my QuintOS library which is web based. Yes, I went to all this trouble just so some teenagers don't have to run their programs in a boring Java console! I made a barebones JDK implementation in modern Javascript to acheive this.
## Known limitations

View File

@ -31,6 +31,7 @@
<body>
<textarea id="javaFile" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off"></textarea>
<textarea id="javaConsole" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off"></textarea>
<script type="text/javascript" src="jdk.js"></script>
<script type="text/javascript" src="ide.js"></script>
</body>

37
jdk.js
View File

@ -127,7 +127,7 @@
let classes = [];
for (let className of classNames) {
log('importing ' + className);
// log('importing ' + className);
let imp = this.imports[className];
if (imp) {
// class is ready for use
@ -175,7 +175,7 @@
let userName = window?.QuintOS?.userName || 'quinton-ashley';
let className = file.slice(classLine + 13, file.indexOf(' {', classLine + 13));
// hacky support for Java 15 triple quotes
// workaround hack for converting triple quotes to a normal string
file = file.replaceAll(/"""([^"]*)"""/g, (match, str) => {
str = str.replaceAll(/(.*)(\n|$)/g, '"$1\\n"+').slice(0, -1);
return str;
@ -184,6 +184,18 @@
// hacky support for Array literals
file = file.replaceAll(/=\s*new \w*\[\]\s*\{/g, '= {');
// workaround hack for converting lambda expressions to Runnables
let lambdaRegex = /\(\)\s*\->\s*\{(([^\{\}]*\{[^\}]*\})*[^\}]*)\}/g;
file = file.replaceAll(lambdaRegex, (match, in0) => {
log(in0);
if (lambdaRegex.test(in0)) {
in0 = in0.replaceAll(lambdaRegex, (match, in1) => {
return 'new Runnable() {\n@Override\npublic void run() {' + in1 + '}}';
});
}
return 'new Runnable() {\n@Override\npublic void run() {' + in0 + '}}';
});
// convert string .length() method
file = file.replaceAll(/\.length\(\)/g, '.length');
@ -191,9 +203,7 @@
file = file.replace(/\(int\)\s*/gm, 'Math.floor');
file = file.replace(/\(int\)\s*\-/gm, 'Math.ceil');
// log(file);
window.file0 = file;
log(file);
let trans = java_to_javascript(file);
@ -202,14 +212,20 @@
trans = trans.replace(/(\([^\)]*\) =>)/gm, 'async $1');
trans = trans.replace(/([\w_\$]+\.next(Int|Float|Double|Line|Short|Long)*\(\))/gm, 'await $1');
let prefix = `((jdk.imports['com.${userName}.${className}'] = {}).load = async () => {\n\n`;
let prefix = `((jdk.imports['games_java.${className}'] = {}).load = async () => {\n\n`;
// handle Java class imports
for (let i = 0; i < imports.length; i++) {
let imp = imports[i];
// skip static imports for now (like QuintOS)
if (imp.includes('static')) continue;
let impPath = imp.split('.');
let impName = impPath[impPath.length - 1];
prefix += `let ${impName} = await jdk.import('${imp}');\n`;
if (impName == '*') {
prefix += `await jdk.import('${imp}');\n`;
} else {
prefix += `let ${impName} = await jdk.import('${imp}');\n`;
}
}
prefix += '\n';
@ -243,8 +259,6 @@
? define(factory)
: ((global = global || self), (global.javaToJavascript = factory()));
})(this, function () {
'use strict';
function unwrapExports(x) {
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x.default : x;
}
@ -19405,7 +19419,7 @@
* @return {string} - Converted JavaScript
*/
const javaToJavascript = (javaString, options = {}, progress) => {
if (typeof javaString !== 'string') throw new Error('java-to-javascript: First argument must be a string');
if (typeof javaString !== 'string') throw 'java-to-javascript: First argument must be a string';
// Reset opts parameters
Object.assign(opts, DEFAULT_OPTIONS);
@ -19426,7 +19440,8 @@
try {
javaAST$$1 = javaAST.parse(javaString);
} catch (e) {
if (e.location) throw new Error(`Line ${e.location.start.line}\n\n${e.stack}`);
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}`;
else throw e;
}

View File

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