From 42be95959ba3df7426b42d75e1756e7072f7f8d5 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 20 Nov 2017 02:00:25 +0300 Subject: [PATCH] Minor fixes for bootstrapping TeaVM --- .../org/teavm/classlib/impl/JCLPlugin.java | 81 ++++++++++++------- .../org/teavm/classlib/java/lang/TSystem.java | 1 - .../teavm/platform/plugin/PlatformPlugin.java | 40 +++++---- 3 files changed, 75 insertions(+), 47 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java b/classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java index 66c844bb3..bd22aceb0 100644 --- a/classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java +++ b/classlib/src/main/java/org/teavm/classlib/impl/JCLPlugin.java @@ -15,11 +15,6 @@ */ package org.teavm.classlib.impl; -import java.lang.invoke.CallSite; -import java.lang.invoke.LambdaMetafactory; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; import java.util.ArrayList; import java.util.List; import java.util.ServiceLoader; @@ -27,8 +22,11 @@ import org.teavm.backend.javascript.TeaVMJavaScriptHost; import org.teavm.classlib.ReflectionSupplier; import org.teavm.classlib.impl.lambda.LambdaMetafactorySubstitutor; import org.teavm.classlib.impl.unicode.CLDRReader; +import org.teavm.classlib.java.lang.SystemNativeGenerator; import org.teavm.classlib.java.lang.reflect.AnnotationDependencyListener; +import org.teavm.interop.PlatformMarker; import org.teavm.model.MethodReference; +import org.teavm.model.ValueType; import org.teavm.platform.PlatformClass; import org.teavm.vm.spi.TeaVMHost; import org.teavm.vm.spi.TeaVMPlugin; @@ -36,42 +34,65 @@ import org.teavm.vm.spi.TeaVMPlugin; public class JCLPlugin implements TeaVMPlugin { @Override public void install(TeaVMHost host) { - ServiceLoaderSupport serviceLoaderSupp = new ServiceLoaderSupport(host.getClassLoader()); - host.add(serviceLoaderSupp); - MethodReference loadServicesMethod = new MethodReference(ServiceLoader.class, "loadServices", - PlatformClass.class, Object[].class); - TeaVMJavaScriptHost jsExtension = host.getExtension(TeaVMJavaScriptHost.class); - if (jsExtension != null) { - jsExtension.add(loadServicesMethod, serviceLoaderSupp); + if (!isBootstrap()) { + ServiceLoaderSupport serviceLoaderSupp = new ServiceLoaderSupport(host.getClassLoader()); + host.add(serviceLoaderSupp); + MethodReference loadServicesMethod = new MethodReference(ServiceLoader.class, "loadServices", + PlatformClass.class, Object[].class); + TeaVMJavaScriptHost jsExtension = host.getExtension(TeaVMJavaScriptHost.class); + if (jsExtension != null) { + jsExtension.add(loadServicesMethod, serviceLoaderSupp); + } + + JavacSupport javacSupport = new JavacSupport(); + host.add(javacSupport); } - JavacSupport javacSupport = new JavacSupport(); - host.add(javacSupport); + if (!isBootstrap()) { + host.registerService(CLDRReader.class, CLDRReader.getInstance(host.getProperties(), host.getClassLoader())); - host.registerService(CLDRReader.class, CLDRReader.getInstance(host.getProperties(), host.getClassLoader())); + host.add(new ClassForNameTransformer()); + } - host.add(new ClassForNameTransformer()); host.add(new AnnotationDependencyListener()); LambdaMetafactorySubstitutor lms = new LambdaMetafactorySubstitutor(); - host.add(new MethodReference(LambdaMetafactory.class, "metafactory", MethodHandles.Lookup.class, - String.class, MethodType.class, MethodType.class, MethodHandle.class, MethodType.class, - CallSite.class), lms); - host.add(new MethodReference(LambdaMetafactory.class, "altMetafactory", MethodHandles.Lookup.class, - String.class, MethodType.class, Object[].class, CallSite.class), lms); + host.add(new MethodReference("java.lang.invoke.LambdaMetafactory", "metafactory", + ValueType.object("java.lang.invoke.MethodHandles$Lookup"), ValueType.object("java.lang.String"), + ValueType.object("java.lang.invoke.MethodType"), ValueType.object("java.lang.invoke.MethodType"), + ValueType.object("java.lang.invoke.MethodHandle"), ValueType.object("java.lang.invoke.MethodType"), + ValueType.object("java.lang.invoke.CallSite")), lms); + host.add(new MethodReference("java.lang.invoke.LambdaMetafactory", "altMetafactory", + ValueType.object("java.lang.invoke.MethodHandles$Lookup"), + ValueType.object("java.lang.String"), ValueType.object("java.lang.invoke.MethodType"), + ValueType.arrayOf(ValueType.object("java.lang.Object")), + ValueType.object("java.lang.invoke.CallSite")), lms); - host.add(new ScalaHacks()); + if (!isBootstrap()) { + host.add(new ScalaHacks()); + } host.add(new NumericClassTransformer()); - List reflectionSuppliers = new ArrayList<>(); - for (ReflectionSupplier supplier : ServiceLoader.load(ReflectionSupplier.class, host.getClassLoader())) { - reflectionSuppliers.add(supplier); - } - ReflectionDependencyListener reflection = new ReflectionDependencyListener(reflectionSuppliers); - host.registerService(ReflectionDependencyListener.class, reflection); - host.add(reflection); + if (!isBootstrap()) { + List reflectionSuppliers = new ArrayList<>(); + for (ReflectionSupplier supplier : ServiceLoader.load(ReflectionSupplier.class, host.getClassLoader())) { + reflectionSuppliers.add(supplier); + } + ReflectionDependencyListener reflection = new ReflectionDependencyListener(reflectionSuppliers); + host.registerService(ReflectionDependencyListener.class, reflection); + host.add(reflection); - host.add(new PlatformMarkerSupport()); + host.add(new PlatformMarkerSupport()); + } + + TeaVMJavaScriptHost jsHost = host.getExtension(TeaVMJavaScriptHost.class); + jsHost.add(new MethodReference("java.lang.System", "currentTimeMillis", ValueType.LONG), + new SystemNativeGenerator()); + } + + @PlatformMarker + private static boolean isBootstrap() { + return false; } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java index 0997f5b94..39d0644ff 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TSystem.java @@ -101,7 +101,6 @@ public final class TSystem extends TObject { Allocator.moveMemoryBlock(srcAddress, destAddress, length * itemSize); } - @GeneratedBy(SystemNativeGenerator.class) @DelegateTo("currentTimeMillisLowLevel") public static native long currentTimeMillis(); diff --git a/platform/src/main/java/org/teavm/platform/plugin/PlatformPlugin.java b/platform/src/main/java/org/teavm/platform/plugin/PlatformPlugin.java index 15a76de3f..70a59f9bc 100644 --- a/platform/src/main/java/org/teavm/platform/plugin/PlatformPlugin.java +++ b/platform/src/main/java/org/teavm/platform/plugin/PlatformPlugin.java @@ -21,6 +21,7 @@ import org.teavm.backend.wasm.TeaVMWasmHost; import org.teavm.backend.wasm.intrinsics.WasmIntrinsic; import org.teavm.backend.wasm.intrinsics.WasmIntrinsicManager; import org.teavm.backend.wasm.model.expression.WasmExpression; +import org.teavm.interop.PlatformMarker; import org.teavm.model.MethodReference; import org.teavm.vm.spi.TeaVMHost; import org.teavm.vm.spi.TeaVMPlugin; @@ -33,27 +34,29 @@ public class PlatformPlugin implements TeaVMPlugin { host.add(new ResourceTransformer()); host.add(new ResourceAccessorTransformer(host)); host.add(new ResourceAccessorDependencyListener()); - } else { + } else if (!isBootstrap()) { host.add(new StringAmplifierTransformer()); } - TeaVMWasmHost wasmHost = host.getExtension(TeaVMWasmHost.class); - if (wasmHost != null) { - wasmHost.add(ctx -> new MetadataIntrinsic(ctx.getClassSource(), ctx.getClassLoader(), ctx.getServices(), - ctx.getProperties())); - wasmHost.add(ctx -> new ResourceReadIntrinsic(ctx.getClassSource(), ctx.getClassLoader())); + if (!isBootstrap()) { + TeaVMWasmHost wasmHost = host.getExtension(TeaVMWasmHost.class); + if (wasmHost != null) { + wasmHost.add(ctx -> new MetadataIntrinsic(ctx.getClassSource(), ctx.getClassLoader(), ctx.getServices(), + ctx.getProperties())); + wasmHost.add(ctx -> new ResourceReadIntrinsic(ctx.getClassSource(), ctx.getClassLoader())); - wasmHost.add(ctx -> new WasmIntrinsic() { - @Override - public boolean isApplicable(MethodReference methodReference) { - return methodReference.getClassName().equals(StringAmplifier.class.getName()); - } + wasmHost.add(ctx -> new WasmIntrinsic() { + @Override + public boolean isApplicable(MethodReference methodReference) { + return methodReference.getClassName().equals(StringAmplifier.class.getName()); + } - @Override - public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) { - return manager.generate(invocation.getArguments().get(0)); - } - }); + @Override + public WasmExpression apply(InvocationExpr invocation, WasmIntrinsicManager manager) { + return manager.generate(invocation.getArguments().get(0)); + } + }); + } } host.add(new AsyncMethodProcessor()); @@ -63,4 +66,9 @@ public class PlatformPlugin implements TeaVMPlugin { host.add(new AnnotationDependencySupport()); host.add(new PlatformDependencyListener()); } + + @PlatformMarker + private static boolean isBootstrap() { + return false; + } }