mirror of
https://github.com/quinton-ashley/java2js
synced 2024-12-29 10:11:54 +01:00
1.1.1
This commit is contained in:
parent
103aafc683
commit
2803cbff25
@ -18,10 +18,16 @@ This function imports much of the standard Java lang classes into the global sco
|
|||||||
|
|
||||||
returns the Java file translated into JavaScript
|
returns the Java file translated into JavaScript
|
||||||
|
|
||||||
### jdk.run(translatedJSFile)
|
### jdk.load(translatedJSFile)
|
||||||
|
|
||||||
- translatedJSFile is the translated JS class to run
|
- translatedJSFile is the translated JS class to run
|
||||||
|
|
||||||
|
Loads the JS class file but doesn't run the main method.
|
||||||
|
|
||||||
|
### jdk.run(jvmArgs)
|
||||||
|
|
||||||
|
Runs the main method with the given JVM arguments.
|
||||||
|
|
||||||
## Known limitations
|
## Known limitations
|
||||||
|
|
||||||
- casting to int truncation workaround requires parenthesis around the number being cast
|
- casting to int truncation workaround requires parenthesis around the number being cast
|
||||||
|
20
jdk.js
20
jdk.js
@ -187,10 +187,10 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// hacky support for Array literals
|
// hacky support for Array literals
|
||||||
file = file.replace(/new\s*\w*\s*\[\s*\]\s*\{(.*)\}/gm, 'new Array($1)');
|
file = file.replace(/new\s*\w*\s*\[\s*\]\s*\{([^}]*)}/gm, 'Array.of($1)');
|
||||||
|
|
||||||
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) => {
|
||||||
return 'new Array(' + p2.replace(/\{([^\}]*)\}/gm, 'new Array($1)') + ')';
|
return 'Array.of(' + p2.replace(/\{([^\}]*)\}/gm, 'Array.of($1)') + ')';
|
||||||
});
|
});
|
||||||
|
|
||||||
file = file.replace(/new\s*\w*\s*\[\s*(\d)+\s*\]\s*/gm, 'new Array($1)');
|
file = file.replace(/new\s*\w*\s*\[\s*(\d)+\s*\]\s*/gm, 'new Array($1)');
|
||||||
@ -19129,7 +19129,14 @@
|
|||||||
const classVarsMap = {};
|
const classVarsMap = {};
|
||||||
|
|
||||||
const assignParent = (name) => {
|
const assignParent = (name) => {
|
||||||
if (name in classVarsMap) return `this.${name}`;
|
if (name in classVarsMap) {
|
||||||
|
let v = classData.vars.find((x) => x.name == name);
|
||||||
|
if (!v || !v.static) {
|
||||||
|
return `this.${name}`;
|
||||||
|
} else {
|
||||||
|
return `${classData.name}.${name}`;
|
||||||
|
}
|
||||||
|
}
|
||||||
const mapped = opts.globalVars[name];
|
const mapped = opts.globalVars[name];
|
||||||
if (mapped) {
|
if (mapped) {
|
||||||
const newName = typeof mapped === 'string' ? mapped : name;
|
const newName = typeof mapped === 'string' ? mapped : name;
|
||||||
@ -19374,8 +19381,9 @@
|
|||||||
|
|
||||||
for (const var_ of vars) {
|
for (const var_ of vars) {
|
||||||
if (var_.value === undefined) var_.value = literalInitializers[var_.type] || 'null';
|
if (var_.value === undefined) var_.value = literalInitializers[var_.type] || 'null';
|
||||||
if (var_.static) staticVars.push(`${className}.${var_.name}=${var_.value};`);
|
if (var_.static) {
|
||||||
else {
|
staticVars.push(`${className}.${var_.name}=${var_.value};`);
|
||||||
|
} else {
|
||||||
if (typeof var_.value == 'string') {
|
if (typeof var_.value == 'string') {
|
||||||
for (const vv of vars) {
|
for (const vv of vars) {
|
||||||
var_.value = var_.value.replaceAll(vv.name, 'this.' + vv.name);
|
var_.value = var_.value.replaceAll(vv.name, 'this.' + vv.name);
|
||||||
|
@ -16,6 +16,9 @@ jdk.imports['java.io.PrintStream'].load = async () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_printArray(arr) {
|
_printArray(arr) {
|
||||||
|
console.error(
|
||||||
|
'ERROR: In Java, printing a primitive array prints the memory location of that array in the Java Virtual Machine. To print the contents of the array use a loop!'
|
||||||
|
);
|
||||||
let md = MessageDigest.getInstance();
|
let md = MessageDigest.getInstance();
|
||||||
let str = '[Ljava.lang.';
|
let str = '[Ljava.lang.';
|
||||||
let hash = md.digest(arr.toString()).slice(0, 8);
|
let hash = md.digest(arr.toString()).slice(0, 8);
|
||||||
|
@ -2,80 +2,82 @@ jdk.imports['java.util.AbstractList'].load = async () => {
|
|||||||
const Itr = await jdk.import('java.util.Itr');
|
const Itr = await jdk.import('java.util.Itr');
|
||||||
|
|
||||||
class AbstractList {
|
class AbstractList {
|
||||||
constructor(content) {
|
constructor(arr) {
|
||||||
// TODO
|
// TODO
|
||||||
this.content = content || [];
|
this.arr = arr || [];
|
||||||
}
|
}
|
||||||
|
|
||||||
addAll(index, vals) {
|
addAll(index, vals) {
|
||||||
const tempArray = vals.toArray(null);
|
this.arr.splice(index, 0, ...vals);
|
||||||
for (let i = 0; i < tempArray.length; i++) {
|
|
||||||
this.content.push(tempArray[i]);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
clear() {
|
||||||
this.content = [];
|
this.arr = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
poll() {
|
poll() {
|
||||||
return this.content.shift();
|
return this.arr.shift();
|
||||||
}
|
}
|
||||||
|
|
||||||
remove(indexOrElem) {
|
remove(indexOrElem) {
|
||||||
this.content.splice(indexOrElem, 1);
|
let index = indexOrElem;
|
||||||
return true;
|
if (typeof indexOrElem != 'number') {
|
||||||
|
index = this.arr.indexOf(indexOrElem);
|
||||||
|
}
|
||||||
|
return this.arr.splice(index, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeAll() {
|
removeAll() {
|
||||||
this.content = [];
|
this.arr = [];
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
toArray(a) {
|
toArray() {
|
||||||
return this.content;
|
return this.arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
toString() {
|
||||||
|
return '[' + this.arr.toString() + ']';
|
||||||
}
|
}
|
||||||
|
|
||||||
size() {
|
size() {
|
||||||
return this.content.length;
|
return this.arr.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
add(index, elem) {
|
add(index, elem) {
|
||||||
if (typeof elem !== 'undefined') {
|
if (typeof elem == 'undefined') {
|
||||||
this.content.splice(index, 0, elem);
|
return this.arr.push(index);
|
||||||
} else {
|
} else {
|
||||||
this.content.push(index);
|
return this.arr.splice(index, 0, elem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get(index) {
|
get(index) {
|
||||||
return this.content[index];
|
return this.arr[index];
|
||||||
}
|
}
|
||||||
|
|
||||||
contains(val) {
|
contains(val) {
|
||||||
return this.content.indexOf(val) != -1;
|
return this.arr.indexOf(val) != -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
containsAll(elems) {
|
// containsAll(elems) {
|
||||||
return false;
|
// }
|
||||||
}
|
|
||||||
|
|
||||||
isEmpty() {
|
isEmpty() {
|
||||||
return this.content.length == 0;
|
return this.arr.length == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
set(index, element) {
|
set(index, element) {
|
||||||
this.content[index] = element;
|
this.arr[index] = element;
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
indexOf(element) {
|
indexOf(element) {
|
||||||
return this.content.indexOf(element);
|
return this.arr.indexOf(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
lastIndexOf(element) {
|
lastIndexOf(element) {
|
||||||
return this.content.lastIndexOf(element);
|
return this.arr.lastIndexOf(element);
|
||||||
}
|
}
|
||||||
|
|
||||||
iterator() {
|
iterator() {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "java2js",
|
"name": "java2js",
|
||||||
"version": "1.1.0",
|
"version": "1.1.1",
|
||||||
"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": "jdk.js",
|
"main": "jdk.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user