Moves JSObject and others into a separate project

This commit is contained in:
konsoletyper 2014-02-13 17:52:56 +04:00
parent e13accc7e4
commit e5ea6b51a7
31 changed files with 218 additions and 41 deletions

View File

@ -18,6 +18,7 @@
<module>teavm-maven-plugin</module>
<module>teavm-samples</module>
<module>teavm-dom</module>
<module>teavm-jso</module>
</modules>
<build>

View File

@ -303,7 +303,7 @@ public class JavascriptBuilder implements JavascriptBuilderHost {
}
public void installPlugins() {
for (JavascriptBuilderPlugin plugin : ServiceLoader.load(JavascriptBuilderPlugin.class)) {
for (JavascriptBuilderPlugin plugin : ServiceLoader.load(JavascriptBuilderPlugin.class, classLoader)) {
plugin.install(this);
}
}

View File

@ -20,14 +20,12 @@ import java.util.List;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.MethodHolder;
/**
*
* @author Alexey Andreev
*/
class JavascriptProcessedClassSource implements ClassHolderSource {
private ThreadLocal<JavascriptNativeProcessor> processor = new ThreadLocal<>();
private ClassHolderSource innerSource;
private List<ClassHolderTransformer> transformers = new ArrayList<>();
@ -49,22 +47,8 @@ class JavascriptProcessedClassSource implements ClassHolderSource {
}
private void transformClass(ClassHolder cls) {
JavascriptNativeProcessor processor = getProcessor();
processor.processClass(cls);
for (MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null) {
processor.processProgram(method.getProgram());
}
}
for (ClassHolderTransformer transformer : transformers) {
transformer.transformClass(cls);
transformer.transformClass(cls, innerSource);
}
}
private JavascriptNativeProcessor getProcessor() {
if (processor.get() == null) {
processor.set(new JavascriptNativeProcessor(innerSource));
}
return processor.get();
}
}

View File

@ -22,7 +22,7 @@ import java.util.Map;
*
* @author Alexey Andreev <konsoletyper@gmail.com>
*/
public class AnnotationContainer {
public class AnnotationContainer implements AnnotationContainerReader {
private Map<String, AnnotationHolder> annotations = new HashMap<>();
public void add(AnnotationHolder annotation) {
@ -32,6 +32,7 @@ public class AnnotationContainer {
annotations.put(annotation.getType(), annotation);
}
@Override
public AnnotationHolder get(String type) {
return annotations.get(type);
}
@ -48,6 +49,7 @@ public class AnnotationContainer {
annotations.remove(type);
}
@Override
public Iterable<AnnotationHolder> all() {
return annotations.values();
}

View File

@ -0,0 +1,26 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.model;
/**
*
* @author Alexey Andreev
*/
public interface AnnotationContainerReader {
AnnotationHolder get(String type);
Iterable<? extends AnnotationReader> all();
}

View File

@ -22,7 +22,7 @@ import java.util.Map;
* Represents an annotation of Java element.
* @author Alexey Andreev
*/
public class AnnotationHolder {
public class AnnotationHolder implements AnnotationReader {
private String type;
private Map<String, AnnotationValue> values = new HashMap<>();
@ -30,6 +30,7 @@ public class AnnotationHolder {
this.type = type;
}
@Override
public String getType() {
return type;
}
@ -37,4 +38,14 @@ public class AnnotationHolder {
public Map<String, AnnotationValue> getValues() {
return values;
}
@Override
public AnnotationValue getValue(String fieldName) {
return values.get(fieldName);
}
@Override
public Iterable<String> getAvailableFields() {
return values.keySet();
}
}

View File

@ -0,0 +1,28 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.model;
/**
*
* @author Alexey Andreev
*/
public interface AnnotationReader {
String getType();
AnnotationValue getValue(String fieldName);
Iterable<String> getAvailableFields();
}

View File

@ -21,5 +21,5 @@ package org.teavm.model;
* @author Alexey Andreev
*/
public interface ClassHolderTransformer {
void transformClass(ClassHolder cls);
void transformClass(ClassHolder cls, ClassReaderSource innerSource);
}

View File

@ -59,6 +59,7 @@ public abstract class ElementHolder implements ElementReader {
return name;
}
@Override
public AnnotationContainer getAnnotations() {
return annotations;
}

View File

@ -29,4 +29,6 @@ public interface ElementReader {
boolean hasModifier(ElementModifier modifier);
String getName();
AnnotationContainerReader getAnnotations();
}

View File

@ -31,5 +31,10 @@
<version>0.0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-jso</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
</project>

4
teavm-jso/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/target
/.settings
/.classpath
/.project

35
teavm-jso/pom.xml Normal file
View File

@ -0,0 +1,35 @@
<!--
Copyright 2014 Alexey Andreev.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.teavm</groupId>
<artifactId>teavm</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>teavm-jso</artifactId>
<dependencies>
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,30 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript.ni.plugin;
import org.teavm.javascript.JavascriptBuilderHost;
import org.teavm.javascript.JavascriptBuilderPlugin;
/**
*
* @author Alexey Andreev
*/
public class JSObjectBuilderPlugin implements JavascriptBuilderPlugin {
@Override
public void install(JavascriptBuilderHost host) {
host.add(new JSObjectClassTransformer());
}
}

View File

@ -0,0 +1,47 @@
/*
* Copyright 2014 Alexey Andreev.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript.ni.plugin;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.MethodHolder;
/**
*
* @author Alexey Andreev
*/
class JSObjectClassTransformer implements ClassHolderTransformer {
private ThreadLocal<JavascriptNativeProcessor> processor = new ThreadLocal<>();
@Override
public void transformClass(ClassHolder cls, ClassReaderSource innerSource) {
JavascriptNativeProcessor processor = getProcessor(innerSource);
processor.processClass(cls);
for (MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null) {
processor.processProgram(method.getProgram());
}
}
}
private JavascriptNativeProcessor getProcessor(ClassReaderSource innerSource) {
if (processor.get() == null) {
processor.set(new JavascriptNativeProcessor(innerSource));
}
return processor.get();
}
}

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.javascript.ni.plugin;
import java.util.*;
import org.teavm.javascript.ni.*;
@ -25,12 +25,12 @@ import org.teavm.model.instructions.*;
* @author Alexey Andreev
*/
class JavascriptNativeProcessor {
private ClassHolderSource classSource;
private ClassReaderSource classSource;
private Program program;
private List<Instruction> replacement = new ArrayList<>();
private NativeJavascriptClassRepository nativeRepos;
public JavascriptNativeProcessor(ClassHolderSource classSource) {
public JavascriptNativeProcessor(ClassReaderSource classSource) {
this.classSource = classSource;
nativeRepos = new NativeJavascriptClassRepository(classSource);
}
@ -51,8 +51,8 @@ class JavascriptNativeProcessor {
}
private void addPreservedMethods(String ifaceName, Set<MethodDescriptor> methods) {
ClassHolder iface = classSource.get(ifaceName);
for (MethodHolder method : iface.getMethods()) {
ClassReader iface = classSource.get(ifaceName);
for (MethodReader method : iface.getMethods()) {
methods.add(method.getDescriptor());
}
for (String superIfaceName : iface.getInterfaces()) {
@ -75,7 +75,7 @@ class JavascriptNativeProcessor {
continue;
}
replacement.clear();
MethodHolder method = getMethod(invoke.getMethod());
MethodReader method = getMethod(invoke.getMethod());
if (method.getAnnotations().get(JSProperty.class.getName()) != null) {
if (isProperGetter(method.getDescriptor())) {
String propertyName = method.getName().charAt(0) == 'i' ? cutPrefix(method.getName(), 2) :
@ -268,7 +268,7 @@ class JavascriptNativeProcessor {
private Variable wrapArgument(Variable var, ValueType type) {
if (type instanceof ValueType.Object) {
String className = ((ValueType.Object)type).getClassName();
ClassHolder cls = classSource.get(className);
ClassReader cls = classSource.get(className);
if (cls.getAnnotations().get(JSFunctor.class.getName()) != null) {
return wrapFunctor(var, cls);
}
@ -276,7 +276,7 @@ class JavascriptNativeProcessor {
return wrap(var, type);
}
private Variable wrapFunctor(Variable var, ClassHolder type) {
private Variable wrapFunctor(Variable var, ClassReader type) {
if (!type.hasModifier(ElementModifier.INTERFACE) || type.getMethods().size() != 1) {
throw new RuntimeException("Wrong functor: " + type.getName());
}
@ -312,9 +312,9 @@ class JavascriptNativeProcessor {
return result;
}
private MethodHolder getMethod(MethodReference ref) {
ClassHolder cls = classSource.get(ref.getClassName());
MethodHolder method = cls.getMethod(ref.getDescriptor());
private MethodReader getMethod(MethodReference ref) {
ClassReader cls = classSource.get(ref.getClassName());
MethodReader method = cls.getMethod(ref.getDescriptor());
if (method != null) {
return method;
}

View File

@ -13,13 +13,13 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.javascript;
package org.teavm.javascript.ni.plugin;
import java.util.HashMap;
import java.util.Map;
import org.teavm.javascript.ni.JSObject;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderSource;
import org.teavm.model.ClassReader;
import org.teavm.model.ClassReaderSource;
import org.teavm.model.ElementModifier;
/**
@ -27,10 +27,10 @@ import org.teavm.model.ElementModifier;
* @author Alexey Andreev
*/
class NativeJavascriptClassRepository {
private ClassHolderSource classSource;
private ClassReaderSource classSource;
private Map<String, Boolean> knownJavaScriptClasses = new HashMap<>();
public NativeJavascriptClassRepository(ClassHolderSource classSource) {
public NativeJavascriptClassRepository(ClassReaderSource classSource) {
this.classSource = classSource;
knownJavaScriptClasses.put(JSObject.class.getName(), true);
}
@ -45,8 +45,8 @@ class NativeJavascriptClassRepository {
}
private boolean figureOutIfJavaScriptClass(String className) {
ClassHolder cls = classSource.get(className);
if (cls == null || !cls.getModifiers().contains(ElementModifier.INTERFACE)) {
ClassReader cls = classSource.get(className);
if (cls == null || !cls.hasModifier(ElementModifier.INTERFACE)) {
return false;
}
for (String iface : cls.getInterfaces()) {

View File

@ -0,0 +1 @@
org.teavm.javascript.ni.plugin.JSObjectBuilderPlugin