From 7acc81ce4d60db1a0a1599d8560884efca5de62a Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 21 Oct 2023 20:07:49 +0200 Subject: [PATCH] JS: remove lazy creation of primitive classes --- .../lang/reflect/ArrayNativeGenerator.java | 6 +- .../rendering/RenderingContext.java | 18 +-- .../org/teavm/backend/javascript/array.js | 16 +-- .../org/teavm/backend/javascript/runtime.js | 120 +++++------------- .../teavm/html4j/JavaScriptConvGenerator.java | 10 +- .../org/teavm/jso/impl/JSNativeGenerator.java | 14 +- 6 files changed, 65 insertions(+), 119 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java index 177a182ef..206f9e29b 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/reflect/ArrayNativeGenerator.java @@ -113,7 +113,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { String length = context.getParameterName(2); writer.append("if (").append(type).append(".$meta.primitive) {").softNewLine().indent(); 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.outdent().append("}").softNewLine(); } @@ -134,7 +134,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { if (cls == null || cls.getMethod(methodRef.getDescriptor()) == null) { 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.outdent().append("} else "); } @@ -160,7 +160,7 @@ public class ArrayNativeGenerator implements Generator, DependencyPlugin { writer.append(" else "); } 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.outdent().append("}"); } diff --git a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java index f82751ef5..dc800b3f1 100644 --- a/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java +++ b/core/src/main/java/org/teavm/backend/javascript/rendering/RenderingContext.java @@ -312,33 +312,33 @@ public abstract class RenderingContext { ValueType.Object objType = (ValueType.Object) type; writer.appendClass(objType.getClassName()); } else if (type instanceof ValueType.Void) { - writer.append("$rt_voidcls()"); + writer.append("$rt_voidcls"); } else if (type instanceof ValueType.Primitive) { ValueType.Primitive primitiveType = (ValueType.Primitive) type; switch (primitiveType.getKind()) { case BOOLEAN: - writer.append("$rt_booleancls()"); + writer.append("$rt_booleancls"); break; case CHARACTER: - writer.append("$rt_charcls()"); + writer.append("$rt_charcls"); break; case BYTE: - writer.append("$rt_bytecls()"); + writer.append("$rt_bytecls"); break; case SHORT: - writer.append("$rt_shortcls()"); + writer.append("$rt_shortcls"); break; case INTEGER: - writer.append("$rt_intcls()"); + writer.append("$rt_intcls"); break; case LONG: - writer.append("$rt_longcls()"); + writer.append("$rt_longcls"); break; case FLOAT: - writer.append("$rt_floatcls()"); + writer.append("$rt_floatcls"); break; case DOUBLE: - writer.append("$rt_doublecls()"); + writer.append("$rt_doublecls"); break; default: throw new IllegalArgumentException("The type is not renderable"); diff --git a/core/src/main/resources/org/teavm/backend/javascript/array.js b/core/src/main/resources/org/teavm/backend/javascript/array.js index 7aa6930f0..cd728109f 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/array.js +++ b/core/src/main/resources/org/teavm/backend/javascript/array.js @@ -14,11 +14,11 @@ * limitations under the License. */ -var $rt_booleanArrayCls = $rt_arraycls($rt_booleancls()); -var $rt_charArrayCls = $rt_arraycls($rt_charcls()); -var $rt_byteArrayCls = $rt_arraycls($rt_bytecls()); -var $rt_shortArrayCls = $rt_arraycls($rt_shortcls()); -var $rt_intArrayCls = $rt_arraycls($rt_intcls()); -var $rt_longArrayCls = $rt_arraycls($rt_longcls()); -var $rt_floatArrayCls = $rt_arraycls($rt_floatcls()); -var $rt_doubleArrayCls = $rt_arraycls($rt_doublecls()); \ No newline at end of file +var $rt_booleanArrayCls = $rt_arraycls($rt_booleancls); +var $rt_charArrayCls = $rt_arraycls($rt_charcls); +var $rt_byteArrayCls = $rt_arraycls($rt_bytecls); +var $rt_shortArrayCls = $rt_arraycls($rt_shortcls); +var $rt_intArrayCls = $rt_arraycls($rt_intcls); +var $rt_longArrayCls = $rt_arraycls($rt_longcls); +var $rt_floatArrayCls = $rt_arraycls($rt_floatcls); +var $rt_doubleArrayCls = $rt_arraycls($rt_doublecls); \ No newline at end of file diff --git a/core/src/main/resources/org/teavm/backend/javascript/runtime.js b/core/src/main/resources/org/teavm/backend/javascript/runtime.js index 7bba5bfb7..e6e2c539a 100644 --- a/core/src/main/resources/org/teavm/backend/javascript/runtime.js +++ b/core/src/main/resources/org/teavm/backend/javascript/runtime.js @@ -227,69 +227,15 @@ function $rt_createPrimitiveCls(name, binaryName) { cls.$meta.enclosingClass = null; return cls; } -var $rt_booleanclsCache = null; -function $rt_booleancls() { - if ($rt_booleanclsCache === null) { - $rt_booleanclsCache = $rt_createPrimitiveCls("boolean", "Z"); - } - return $rt_booleanclsCache; -} -var $rt_charclsCache = null; -function $rt_charcls() { - 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; -} +var $rt_booleancls = $rt_createPrimitiveCls("boolean", "Z"); +var $rt_charcls = $rt_createPrimitiveCls("char", "C"); +var $rt_bytecls = $rt_createPrimitiveCls("byte", "B"); +var $rt_shortcls = $rt_createPrimitiveCls("short", "S"); +var $rt_intcls = $rt_createPrimitiveCls("int", "I"); +var $rt_longcls = $rt_createPrimitiveCls("long", "J"); +var $rt_floatcls = $rt_createPrimitiveCls("float", "F"); +var $rt_doublecls = $rt_createPrimitiveCls("double", "D"); +var $rt_voidcls = $rt_createPrimitiveCls("void", "V"); function $rt_throw(ex) { throw $rt_exception(ex); } @@ -357,90 +303,90 @@ function $rt_createMultiArray(cls, dimensions) { function $rt_createByteMultiArray(dimensions) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_bytecls(), dimensions); + return $rt_createMultiArray($rt_bytecls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { arrays[i] = $rt_createByteArray(firstDim); } - return $rt_createMultiArrayImpl($rt_bytecls(), arrays, dimensions); + return $rt_createMultiArrayImpl($rt_bytecls, arrays, dimensions); } function $rt_createCharMultiArray(dimensions) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_charcls(), dimensions); + return $rt_createMultiArray($rt_charcls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_booleancls(), dimensions); + return $rt_createMultiArray($rt_booleancls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_shortcls(), dimensions); + return $rt_createMultiArray($rt_shortcls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_intcls(), dimensions); + return $rt_createMultiArray($rt_intcls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_longcls(), dimensions); + return $rt_createMultiArray($rt_longcls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_floatcls(), dimensions); + return $rt_createMultiArray($rt_floatcls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var arrays = new Array($rt_primitiveArrayCount(dimensions, 0)); if (arrays.length === 0) { - return $rt_createMultiArray($rt_doublecls(), dimensions); + return $rt_createMultiArray($rt_doublecls, dimensions); } var firstDim = dimensions[0] | 0; for (var i = 0; i < arrays.length; i = (i + 1) | 0) { 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) { var val = dimensions[start + 1] | 0; @@ -792,21 +738,21 @@ function $dbg_class(obj) { cls = cls.$meta.item; } var clsName = ""; - if (cls === $rt_booleancls()) { + if (cls === $rt_booleancls) { clsName = "boolean"; - } else if (cls === $rt_bytecls()) { + } else if (cls === $rt_bytecls) { clsName = "byte"; - } else if (cls === $rt_shortcls()) { + } else if (cls === $rt_shortcls) { clsName = "short"; - } else if (cls === $rt_charcls()) { + } else if (cls === $rt_charcls) { clsName = "char"; - } else if (cls === $rt_intcls()) { + } else if (cls === $rt_intcls) { clsName = "int"; - } else if (cls === $rt_longcls()) { + } else if (cls === $rt_longcl) { clsName = "long"; - } else if (cls === $rt_floatcls()) { + } else if (cls === $rt_floatcls) { clsName = "float"; - } else if (cls === $rt_doublecls()) { + } else if (cls === $rt_doublecls) { clsName = "double"; } else { clsName = cls.$meta ? (cls.$meta.name || ("a/" + cls.name)) : "@" + cls.name; diff --git a/html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java b/html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java index 4f8455f28..e1fb4e71e 100644 --- a/html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java +++ b/html4j/src/main/java/org/teavm/html4j/JavaScriptConvGenerator.java @@ -157,17 +157,17 @@ public class JavaScriptConvGenerator implements Generator { .append(") {").indent().softNewLine(); writer.append("return ").appendMethodBody(valueOfDoubleMethod).append("(" + obj + ");").softNewLine(); - writer.outdent().append("} else if (" + type + " === $rt_intcls() || " + type + " === $rt_bytecls() || " - + type + " === $rt_shortcls() ||" + type + " == $rt_charcls()) {").indent().softNewLine(); + writer.outdent().append("} else if (" + type + " === $rt_intcls || " + type + " === $rt_bytecls || " + + type + " === $rt_shortcls ||" + type + " == $rt_charcls) {").indent().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.outdent().append("} else if (" + type + " === $rt_doublecls() || " + type + " == $rt_floatcls()) {") + writer.outdent().append("} else if (" + type + " === $rt_doublecls || " + type + " == $rt_floatcls) {") .indent().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.outdent().append("} else if (" + obj + " instanceof Array) {").indent().softNewLine(); diff --git a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java index 273682571..25b123fc2 100644 --- a/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java +++ b/jso/impl/src/main/java/org/teavm/jso/impl/JSNativeGenerator.java @@ -182,37 +182,37 @@ public class JSNativeGenerator implements Injector, DependencyPlugin, Generator break; case "dataToByteArray": - writer.append("$rt_wrapArray($rt_bytecls(),").ws(); + writer.append("$rt_wrapArray($rt_bytecls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToShortArray": - writer.append("$rt_wrapArray($rt_shortcls(),").ws(); + writer.append("$rt_wrapArray($rt_shortcls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToCharArray": - writer.append("$rt_wrapArray($rt_charcls(),").ws(); + writer.append("$rt_wrapArray($rt_charcls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToIntArray": - writer.append("$rt_wrapArray($rt_intcls(),").ws(); + writer.append("$rt_wrapArray($rt_intcls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToFloatArray": - writer.append("$rt_wrapArray($rt_floatcls(),").ws(); + writer.append("$rt_wrapArray($rt_floatcls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToDoubleArray": - writer.append("$rt_wrapArray($rt_doublecls(),").ws(); + writer.append("$rt_wrapArray($rt_doublecls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break; case "dataToArray": - writer.append("$rt_wrapArray($rt_objcls(),").ws(); + writer.append("$rt_wrapArray($rt_objcls,").ws(); context.writeExpr(context.getArgument(0), Precedence.min()); writer.append(")"); break;