public static void handleNatives(TeaVMHost host, Class<?> cls) { if (!isBootstrap()) { return; } handleNativesImpl(host, host.getExtension(TeaVMJavaScriptHost.class), cls); }
exceptionsGenerator); TeaVMPluginUtil.handleNatives(host, JS.class);
private static Value<MethodReference> methodToReference(ReflectMethod method) { int signatureSize = method.getParameterCount() + 1; Value<ValueType[]> signature = emit(() -> new ValueType[signatureSize]); for (int i = 0; i < method.getParameterCount(); ++i) { Value<ValueType> paramType = classToValueType(method.getParameterType(i)); int index = i; emit(() -> signature.get()[index] = paramType.get()); } Value<ValueType> returnType = classToValueType(method.getReturnType()); emit(() -> signature.get()[signatureSize - 1] = returnType.get()); String className = method.getDeclaringClass().getName(); String name = method.getName(); return emit(() -> new MethodReference(className, new MethodDescriptor(name, signature.get()))); }
private static void handleNativesImpl(Value<TeaVMHost> host, Value<TeaVMJavaScriptHost> jsHost, ReflectClass<?> cls) { for (ReflectMethod method : cls.getDeclaredMethods()) { if (!Modifier.isNative(method.getModifiers())) { continue; } GeneratedBy generatedBy = method.getAnnotation(GeneratedBy.class); if (generatedBy != null) { ReflectClass<?> generatorClass = findClass(generatedBy.value().getName()); ReflectMethod generatorConstructor = generatorClass.getMethod("<init>"); Value<MethodReference> methodRef = methodToReference(method); emit(() -> jsHost.get().add(methodRef.get(), (Generator) generatorConstructor.construct())); } InjectedBy injectedBy = method.getAnnotation(InjectedBy.class); if (injectedBy != null) { ReflectClass<?> generatorClass = findClass(injectedBy.value().getName()); ReflectMethod generatorConstructor = generatorClass.getMethod("<init>"); Value<MethodReference> methodRef = methodToReference(method); emit(() -> jsHost.get().add(methodRef.get(), (Injector) generatorConstructor.construct())); } PluggableDependency dependency = method.getAnnotation(PluggableDependency.class); if (dependency != null) { ReflectClass<?> generatorClass = findClass(dependency.value().getName()); ReflectMethod generatorConstructor = generatorClass.getMethod("<init>"); Value<MethodReference> methodRef = methodToReference(method); emit(() -> host.get().add(methodRef.get(), (DependencyPlugin) generatorConstructor.construct())); } } }
TeaVMPluginUtil.handleNatives(host, Class.class); TeaVMPluginUtil.handleNatives(host, ClassLoader.class); TeaVMPluginUtil.handleNatives(host, System.class); TeaVMPluginUtil.handleNatives(host, Array.class); TeaVMPluginUtil.handleNatives(host, Math.class);
private static Value<ValueType> classToValueType(ReflectClass<?> cls) { if (cls.isArray()) { Value<ValueType> itemType = classToValueType(cls.getComponentType()); return emit(() -> ValueType.arrayOf(itemType.get())); } else if (cls.isPrimitive()) {
host.add(new AsyncDependencyListener()); TeaVMPluginUtil.handleNatives(host, Platform.class); TeaVMPluginUtil.handleNatives(host, PlatformQueue.class);