JS: remove lazy creation of primitive classes

This commit is contained in:
Alexey Andreev 2023-10-21 20:07:49 +02:00
parent 60d0b07f83
commit 7acc81ce4d
6 changed files with 65 additions and 119 deletions

View File

@ -113,7 +113,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin {
String length = context.getParameterName(2); String length = context.getParameterName(2);
writer.append("if (").append(type).append(".$meta.primitive) {").softNewLine().indent(); writer.append("if (").append(type).append(".$meta.primitive) {").softNewLine().indent();
for (String primitive : primitives) { for (String primitive : primitives) {
writer.append("if (" + type + " == $rt_" + primitive.toLowerCase() + "cls()) {").indent().softNewLine(); writer.append("if (" + type + " == $rt_" + primitive.toLowerCase() + "cls) {").indent().softNewLine();
writer.append("return $rt_create" + primitive + "Array(" + length + ");").softNewLine(); writer.append("return $rt_create" + primitive + "Array(" + length + ");").softNewLine();
writer.outdent().append("}").softNewLine(); writer.outdent().append("}").softNewLine();
} }
@ -134,7 +134,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin {
if (cls == null || cls.getMethod(methodRef.getDescriptor()) == null) { if (cls == null || cls.getMethod(methodRef.getDescriptor()) == null) {
continue; continue;
} }
writer.append("if (type === $rt_" + primitives[i].toLowerCase() + "cls()) {").indent().softNewLine(); writer.append("if (type === $rt_" + primitives[i].toLowerCase() + "cls) {").indent().softNewLine();
writer.append("return ").appendMethodBody(methodRef).append("(item);").softNewLine(); writer.append("return ").appendMethodBody(methodRef).append("(item);").softNewLine();
writer.outdent().append("} else "); writer.outdent().append("} else ");
} }
@ -160,7 +160,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin {
writer.append(" else "); writer.append(" else ");
} }
first = false; first = false;
writer.append("if (type === $rt_" + primitives[i].toLowerCase() + "cls()) {").indent().softNewLine(); writer.append("if (type === $rt_" + primitives[i].toLowerCase() + "cls) {").indent().softNewLine();
writer.append(item + " = ").appendMethodBody(methodRef).append("(" + item + ");").softNewLine(); writer.append(item + " = ").appendMethodBody(methodRef).append("(" + item + ");").softNewLine();
writer.outdent().append("}"); writer.outdent().append("}");
} }

View File

@ -312,33 +312,33 @@ public abstract class RenderingContext {
ValueType.Object objType = (ValueType.Object) type; ValueType.Object objType = (ValueType.Object) type;
writer.appendClass(objType.getClassName()); writer.appendClass(objType.getClassName());
} else if (type instanceof ValueType.Void) { } else if (type instanceof ValueType.Void) {
writer.append("$rt_voidcls()"); writer.append("$rt_voidcls");
} else if (type instanceof ValueType.Primitive) { } else if (type instanceof ValueType.Primitive) {
ValueType.Primitive primitiveType = (ValueType.Primitive) type; ValueType.Primitive primitiveType = (ValueType.Primitive) type;
switch (primitiveType.getKind()) { switch (primitiveType.getKind()) {
case BOOLEAN: case BOOLEAN:
writer.append("$rt_booleancls()"); writer.append("$rt_booleancls");
break; break;
case CHARACTER: case CHARACTER:
writer.append("$rt_charcls()"); writer.append("$rt_charcls");
break; break;
case BYTE: case BYTE:
writer.append("$rt_bytecls()"); writer.append("$rt_bytecls");
break; break;
case SHORT: case SHORT:
writer.append("$rt_shortcls()"); writer.append("$rt_shortcls");
break; break;
case INTEGER: case INTEGER:
writer.append("$rt_intcls()"); writer.append("$rt_intcls");
break; break;
case LONG: case LONG:
writer.append("$rt_longcls()"); writer.append("$rt_longcls");
break; break;
case FLOAT: case FLOAT:
writer.append("$rt_floatcls()"); writer.append("$rt_floatcls");
break; break;
case DOUBLE: case DOUBLE:
writer.append("$rt_doublecls()"); writer.append("$rt_doublecls");
break; break;
default: default:
throw new IllegalArgumentException("The type is not renderable"); throw new IllegalArgumentException("The type is not renderable");

View File

@ -14,11 +14,11 @@
* limitations under the License. * limitations under the License.
*/ */
var $rt_booleanArrayCls = $rt_arraycls($rt_booleancls()); var $rt_booleanArrayCls = $rt_arraycls($rt_booleancls);
var $rt_charArrayCls = $rt_arraycls($rt_charcls()); var $rt_charArrayCls = $rt_arraycls($rt_charcls);
var $rt_byteArrayCls = $rt_arraycls($rt_bytecls()); var $rt_byteArrayCls = $rt_arraycls($rt_bytecls);
var $rt_shortArrayCls = $rt_arraycls($rt_shortcls()); var $rt_shortArrayCls = $rt_arraycls($rt_shortcls);
var $rt_intArrayCls = $rt_arraycls($rt_intcls()); var $rt_intArrayCls = $rt_arraycls($rt_intcls);
var $rt_longArrayCls = $rt_arraycls($rt_longcls()); var $rt_longArrayCls = $rt_arraycls($rt_longcls);
var $rt_floatArrayCls = $rt_arraycls($rt_floatcls()); var $rt_floatArrayCls = $rt_arraycls($rt_floatcls);
var $rt_doubleArrayCls = $rt_arraycls($rt_doublecls()); var $rt_doubleArrayCls = $rt_arraycls($rt_doublecls);

View File

@ -227,69 +227,15 @@ function $rt_createPrimitiveCls(name, binaryName) {
cls.$meta.enclosingClass = null; cls.$meta.enclosingClass = null;
return cls; return cls;
} }
var $rt_booleanclsCache = null; var $rt_booleancls = $rt_createPrimitiveCls("boolean", "Z");
function $rt_booleancls() { var $rt_charcls = $rt_createPrimitiveCls("char", "C");
if ($rt_booleanclsCache === null) { var $rt_bytecls = $rt_createPrimitiveCls("byte", "B");
$rt_booleanclsCache = $rt_createPrimitiveCls("boolean", "Z"); var $rt_shortcls = $rt_createPrimitiveCls("short", "S");
} var $rt_intcls = $rt_createPrimitiveCls("int", "I");
return $rt_booleanclsCache; var $rt_longcls = $rt_createPrimitiveCls("long", "J");
} var $rt_floatcls = $rt_createPrimitiveCls("float", "F");
var $rt_charclsCache = null; var $rt_doublecls = $rt_createPrimitiveCls("double", "D");
function $rt_charcls() { var $rt_voidcls = $rt_createPrimitiveCls("void", "V");
if ($rt_charclsCache === null) {
$rt_charclsCache = $rt_createPrimitiveCls("char", "C");
}
return $rt_charclsCache;
}
var $rt_byteclsCache = null;
function $rt_bytecls() {
if ($rt_byteclsCache === null) {
$rt_byteclsCache = $rt_createPrimitiveCls("byte", "B");
}
return $rt_byteclsCache;
}
var $rt_shortclsCache = null;
function $rt_shortcls() {
if ($rt_shortclsCache === null) {
$rt_shortclsCache = $rt_createPrimitiveCls("short", "S");
}
return $rt_shortclsCache;
}
var $rt_intclsCache = null;
function $rt_intcls() {
if ($rt_intclsCache === null) {
$rt_intclsCache = $rt_createPrimitiveCls("int", "I");
}
return $rt_intclsCache;
}
var $rt_longclsCache = null;
function $rt_longcls() {
if ($rt_longclsCache === null) {
$rt_longclsCache = $rt_createPrimitiveCls("long", "J");
}
return $rt_longclsCache;
}
var $rt_floatclsCache = null;
function $rt_floatcls() {
if ($rt_floatclsCache === null) {
$rt_floatclsCache = $rt_createPrimitiveCls("float", "F");
}
return $rt_floatclsCache;
}
var $rt_doubleclsCache = null;
function $rt_doublecls() {
if ($rt_doubleclsCache === null) {
$rt_doubleclsCache = $rt_createPrimitiveCls("double", "D");
}
return $rt_doubleclsCache;
}
var $rt_voidclsCache = null;
function $rt_voidcls() {
if ($rt_voidclsCache === null) {
$rt_voidclsCache = $rt_createPrimitiveCls("void", "V");
}
return $rt_voidclsCache;
}
function $rt_throw(ex) { function $rt_throw(ex) {
throw $rt_exception(ex); throw $rt_exception(ex);
} }
@ -357,90 +303,90 @@ function $rt_createMultiArray(cls, dimensions) {
function $rt_createByteMultiArray(dimensions) { function $rt_createByteMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_bytecls(), dimensions); return $rt_createMultiArray($rt_bytecls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createByteArray(firstDim); arrays[i] = $rt_createByteArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_bytecls(), arrays, dimensions); return $rt_createMultiArrayImpl($rt_bytecls, arrays, dimensions);
} }
function $rt_createCharMultiArray(dimensions) { function $rt_createCharMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_charcls(), dimensions); return $rt_createMultiArray($rt_charcls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createCharArray(firstDim); arrays[i] = $rt_createCharArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_charcls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_charcls, arrays, dimensions, 0);
} }
function $rt_createBooleanMultiArray(dimensions) { function $rt_createBooleanMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_booleancls(), dimensions); return $rt_createMultiArray($rt_booleancls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createBooleanArray(firstDim); arrays[i] = $rt_createBooleanArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_booleancls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_booleancls, arrays, dimensions, 0);
} }
function $rt_createShortMultiArray(dimensions) { function $rt_createShortMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_shortcls(), dimensions); return $rt_createMultiArray($rt_shortcls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createShortArray(firstDim); arrays[i] = $rt_createShortArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_shortcls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_shortcls, arrays, dimensions, 0);
} }
function $rt_createIntMultiArray(dimensions) { function $rt_createIntMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_intcls(), dimensions); return $rt_createMultiArray($rt_intcls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createIntArray(firstDim); arrays[i] = $rt_createIntArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_intcls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_intcls, arrays, dimensions, 0);
} }
function $rt_createLongMultiArray(dimensions) { function $rt_createLongMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_longcls(), dimensions); return $rt_createMultiArray($rt_longcls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createLongArray(firstDim); arrays[i] = $rt_createLongArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_longcls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_longcls, arrays, dimensions, 0);
} }
function $rt_createFloatMultiArray(dimensions) { function $rt_createFloatMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_floatcls(), dimensions); return $rt_createMultiArray($rt_floatcls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createFloatArray(firstDim); arrays[i] = $rt_createFloatArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_floatcls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_floatcls, arrays, dimensions, 0);
} }
function $rt_createDoubleMultiArray(dimensions) { function $rt_createDoubleMultiArray(dimensions) {
var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); var arrays = new Array($rt_primitiveArrayCount(dimensions, 0));
if (arrays.length === 0) { if (arrays.length === 0) {
return $rt_createMultiArray($rt_doublecls(), dimensions); return $rt_createMultiArray($rt_doublecls, dimensions);
} }
var firstDim = dimensions[0] | 0; var firstDim = dimensions[0] | 0;
for (var i = 0; i < arrays.length; i = (i + 1) | 0) { for (var i = 0; i < arrays.length; i = (i + 1) | 0) {
arrays[i] = $rt_createDoubleArray(firstDim); arrays[i] = $rt_createDoubleArray(firstDim);
} }
return $rt_createMultiArrayImpl($rt_doublecls(), arrays, dimensions, 0); return $rt_createMultiArrayImpl($rt_doublecls, arrays, dimensions, 0);
} }
function $rt_primitiveArrayCount(dimensions, start) { function $rt_primitiveArrayCount(dimensions, start) {
var val = dimensions[start + 1] | 0; var val = dimensions[start + 1] | 0;
@ -792,21 +738,21 @@ function $dbg_class(obj) {
cls = cls.$meta.item; cls = cls.$meta.item;
} }
var clsName = ""; var clsName = "";
if (cls === $rt_booleancls()) { if (cls === $rt_booleancls) {
clsName = "boolean"; clsName = "boolean";
} else if (cls === $rt_bytecls()) { } else if (cls === $rt_bytecls) {
clsName = "byte"; clsName = "byte";
} else if (cls === $rt_shortcls()) { } else if (cls === $rt_shortcls) {
clsName = "short"; clsName = "short";
} else if (cls === $rt_charcls()) { } else if (cls === $rt_charcls) {
clsName = "char"; clsName = "char";
} else if (cls === $rt_intcls()) { } else if (cls === $rt_intcls) {
clsName = "int"; clsName = "int";
} else if (cls === $rt_longcls()) { } else if (cls === $rt_longcl) {
clsName = "long"; clsName = "long";
} else if (cls === $rt_floatcls()) { } else if (cls === $rt_floatcls) {
clsName = "float"; clsName = "float";
} else if (cls === $rt_doublecls()) { } else if (cls === $rt_doublecls) {
clsName = "double"; clsName = "double";
} else { } else {
clsName = cls.$meta ? (cls.$meta.name || ("a/" + cls.name)) : "@" + cls.name; clsName = cls.$meta ? (cls.$meta.name || ("a/" + cls.name)) : "@" + cls.name;

View File

@ -157,17 +157,17 @@ public class JavaScriptConvGenerator implements Generator {
.append(") {").indent().softNewLine(); .append(") {").indent().softNewLine();
writer.append("return ").appendMethodBody(valueOfDoubleMethod).append("(" + obj + ");").softNewLine(); writer.append("return ").appendMethodBody(valueOfDoubleMethod).append("(" + obj + ");").softNewLine();
writer.outdent().append("} else if (" + type + " === $rt_intcls() || " + type + " === $rt_bytecls() || " writer.outdent().append("} else if (" + type + " === $rt_intcls || " + type + " === $rt_bytecls || "
+ type + " === $rt_shortcls() ||" + type + " == $rt_charcls()) {").indent().softNewLine(); + type + " === $rt_shortcls ||" + type + " == $rt_charcls) {").indent().softNewLine();
writer.append("return " + obj + "|0;").softNewLine(); writer.append("return " + obj + "|0;").softNewLine();
writer.outdent().append("} else if (" + type + " === $rt_longcls()) {").indent().softNewLine(); writer.outdent().append("} else if (" + type + " === $rt_longcls) {").indent().softNewLine();
writer.append("return Long_fromInt(" + obj + "|0);").softNewLine(); writer.append("return Long_fromInt(" + obj + "|0);").softNewLine();
writer.outdent().append("} else if (" + type + " === $rt_doublecls() || " + type + " == $rt_floatcls()) {") writer.outdent().append("} else if (" + type + " === $rt_doublecls || " + type + " == $rt_floatcls) {")
.indent().softNewLine(); .indent().softNewLine();
writer.append("return " + obj + ";").softNewLine(); writer.append("return " + obj + ";").softNewLine();
writer.outdent().append("} else if (" + type + " === $rt_booleancls()) {").indent().softNewLine(); writer.outdent().append("} else if (" + type + " === $rt_booleancls) {").indent().softNewLine();
writer.append("return " + obj + "?1:0;").softNewLine(); writer.append("return " + obj + "?1:0;").softNewLine();
writer.outdent().append("} else if (" + obj + " instanceof Array) {").indent().softNewLine(); writer.outdent().append("} else if (" + obj + " instanceof Array) {").indent().softNewLine();

View File

@ -182,37 +182,37 @@ public class JSNativeGenerator implements Injector, DependencyPlugin, Generator
break; break;
case "dataToByteArray": case "dataToByteArray":
writer.append("$rt_wrapArray($rt_bytecls(),").ws(); writer.append("$rt_wrapArray($rt_bytecls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToShortArray": case "dataToShortArray":
writer.append("$rt_wrapArray($rt_shortcls(),").ws(); writer.append("$rt_wrapArray($rt_shortcls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToCharArray": case "dataToCharArray":
writer.append("$rt_wrapArray($rt_charcls(),").ws(); writer.append("$rt_wrapArray($rt_charcls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToIntArray": case "dataToIntArray":
writer.append("$rt_wrapArray($rt_intcls(),").ws(); writer.append("$rt_wrapArray($rt_intcls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToFloatArray": case "dataToFloatArray":
writer.append("$rt_wrapArray($rt_floatcls(),").ws(); writer.append("$rt_wrapArray($rt_floatcls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToDoubleArray": case "dataToDoubleArray":
writer.append("$rt_wrapArray($rt_doublecls(),").ws(); writer.append("$rt_wrapArray($rt_doublecls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;
case "dataToArray": case "dataToArray":
writer.append("$rt_wrapArray($rt_objcls(),").ws(); writer.append("$rt_wrapArray($rt_objcls,").ws();
context.writeExpr(context.getArgument(0), Precedence.min()); context.writeExpr(context.getArgument(0), Precedence.min());
writer.append(")"); writer.append(")");
break; break;