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-06-19 16:05:19 -05:00
parent 724da27d51
commit 848226ee2f
2 changed files with 14 additions and 11 deletions

23
jdk.js
View File

@ -189,7 +189,8 @@
file = file.replace(/new\s*\w*\s*\[\s*\]\s*\{([^}]*)}/gm, 'Array.of($1)'); file = file.replace(/new\s*\w*\s*\[\s*\]\s*\{([^}]*)}/gm, 'Array.of($1)');
// 2D arrays // 2D arrays
file = file.replace(/new\s*\w*(\s*\[\s*\])*\s*\{([^}]*)\}/gm, (match, p1, p2) => { file = file.replace(/new\s*\w*(\s*\[\s*\])*\s*\{([^;]*)/gm, (match, p1, p2) => {
p2 = p2.slice(0, -1);
return 'Array.of(' + p2.replace(/\{([^\}]*)\}/gm, 'Array.of($1)') + ')'; return 'Array.of(' + p2.replace(/\{([^\}]*)\}/gm, 'Array.of($1)') + ')';
}); });
@ -204,10 +205,10 @@
// // log(in0); // // log(in0);
// if (lambdaRegex.test(in0)) { // if (lambdaRegex.test(in0)) {
// in0 = in0.replace(lambdaRegex, (match, in1) => { // in0 = in0.replace(lambdaRegex, (match, in1) => {
// return 'new Runnable() {\\n@Override\\npublic void run() {' + in1 + '}\\n}'; // return 'new Runnable() {\n@Override\npublic void run() {' + in1 + '}\n}';
// }); // });
// } // }
// return 'new Runnable() {\\n@Override\\npublic void run() {' + in0 + '}\\n}'; // return 'new Runnable() {\n@Override\npublic void run() {' + in0 + '}\n}';
// }); // });
// TODO fix this by adding real support for lambda // TODO fix this by adding real support for lambda
@ -215,16 +216,18 @@
// log(in0); // log(in0);
if (lambdaRegex.test(in0)) { if (lambdaRegex.test(in0)) {
in0 = in0.replace(lambdaRegex, (match, in1) => { in0 = in0.replace(lambdaRegex, (match, in1) => {
in1.replaceAll('\n', '\\n'); // in1.replaceAll('\n', '\\n');
return 'new Runnable("' + in1 + '")'; return '"");\n' + in1 + '\n"";//';
}); });
} }
in0 = in0.replaceAll('\n', '\\n'); // in0 = in0.replaceAll('\n', '\\n');
return 'new Runnable("' + in0 + '")'; return '"");\n' + in0 + '\n"";//';
}); });
let packageName = (file.match(/package\s+([^;]+)/gm) || [])[1] || 'default'; let packageName = (file.match(/package\s+([^;]+)/gm) || [])[1] || 'default';
log(file);
let trans; let trans;
if (this.workerPath) { if (this.workerPath) {
let worker = new Worker(this.workerPath); let worker = new Worker(this.workerPath);
@ -241,15 +244,15 @@
}); });
} else { } else {
console.warn( console.warn(
"java2js might cause the main thread to stall when transpiling a large Java file. This can cause your website to appear unresponsive/frozen. You can utilize the java2js worker script to transpile Java asynchronously in a seperate JS thread. However due to CORS security 'jav2js_worker.js' must be hosted on your own domain, define 'jdk.workerPath' to be the location of that file." "java2js might cause the main thread to stall when transpiling a large Java file. This can cause your website to appear unresponsive/frozen. You can utilize the java2js worker script to transpile Java asynchronously in a separate JS thread. However due to CORS security 'jav2js_worker.js' must be hosted on your own domain, define 'jdk.workerPath' to be the location of that file."
); );
trans = java_to_javascript(file); trans = java_to_javascript(file);
} }
// log(trans); // log(trans);
// TODO fix this by adding real support for lambda // TODO fix this by adding real support for lambda
trans = trans.replace(/new\s*Runnable\('([^]*)'\)/gm, (match, p1) => { trans = trans.replace(/''\);([^]*)';/gm, (match, p1) => {
return '() => {' + p1.replaceAll('\\n', '\n') + '}'; return '() => {' + p1.slice(0, -1) + '});';
}); });
trans.replace(/catch \(\w*/gm, 'catch ('); trans.replace(/catch \(\w*/gm, 'catch (');

View File

@ -1,6 +1,6 @@
{ {
"name": "java2js", "name": "java2js",
"version": "1.2.17", "version": "1.2.18",
"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": {