Merge branch 'jtulach_resources' into release-0.4.x

This commit is contained in:
Alexey Andreev 2015-12-25 22:52:27 +03:00
commit de7f0910e9
9 changed files with 208 additions and 5 deletions

View File

@ -30,7 +30,7 @@ The easiest way to create a new TeaVM project is to type in the command line:
mvn -DarchetypeCatalog=local \ mvn -DarchetypeCatalog=local \
-DarchetypeGroupId=org.teavm \ -DarchetypeGroupId=org.teavm \
-DarchetypeArtifactId=teavm-maven-webapp \ -DarchetypeArtifactId=teavm-maven-webapp \
-DarchetypeVersion=0.3.2 archetype:generate -DarchetypeVersion=0.4.0 archetype:generate
Now you can execute `mvn clean package` and get the generated `war` file. Now you can execute `mvn clean package` and get the generated `war` file.
Deploy this `war` in Tomcat or another container, or simply unzip it and open the `index.html` page. Deploy this `war` in Tomcat or another container, or simply unzip it and open the `index.html` page.
@ -38,7 +38,7 @@ Deploy this `war` in Tomcat or another container, or simply unzip it and open th
It is much easier to develop TeaVM applications using Eclipse. It is much easier to develop TeaVM applications using Eclipse.
If you prefer Eclipse, please read [this tutorial](https://github.com/konsoletyper/teavm/wiki/Eclipse-tutorial). If you prefer Eclipse, please read [this tutorial](https://github.com/konsoletyper/teavm/wiki/Eclipse-tutorial).
To learn TeaVM deeper, you take a look at the [teavm-samples](teavm-samples) module, To learn TeaVM deeper, you take a look at the [samples](samples) module,
containing examples of TeaVM-based projects. containing examples of TeaVM-based projects.
Also you can read [project's wiki](https://github.com/konsoletyper/teavm/wiki/). Also you can read [project's wiki](https://github.com/konsoletyper/teavm/wiki/).

View File

@ -15,12 +15,15 @@
*/ */
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.teavm.classlib.impl.DeclaringClassMetadataGenerator; import org.teavm.classlib.impl.DeclaringClassMetadataGenerator;
import org.teavm.classlib.java.lang.annotation.TAnnotation; import org.teavm.classlib.java.lang.annotation.TAnnotation;
import org.teavm.classlib.java.lang.reflect.TAnnotatedElement; import org.teavm.classlib.java.lang.reflect.TAnnotatedElement;
import org.teavm.jso.JSBody;
import org.teavm.platform.Platform; import org.teavm.platform.Platform;
import org.teavm.platform.PlatformClass; import org.teavm.platform.PlatformClass;
import org.teavm.platform.metadata.ClassResource; import org.teavm.platform.metadata.ClassResource;
@ -266,4 +269,24 @@ public class TClass<T> extends TObject implements TAnnotatedElement {
annotationsByType.put((TClass<?>) (Object) annot.annotationType(), annot); annotationsByType.put((TClass<?>) (Object) annot.annotationType(), annot);
} }
} }
@JSBody(params = "res", script =
"if (!window.teaVMResources) return null;\n"
+ "var data = window.teaVMResources[res];\n"
+ "return data ? window.atob(data) : null;\n"
)
private static native String readResource(String message);
public InputStream getResourceAsStream(String name) {
TString clazzName = getName();
int lastDot = clazzName.lastIndexOf('.');
String resName;
if (lastDot == -1) {
resName = name;
} else {
resName = clazzName.substring(0, lastDot).replace('.', '/') + "/" + name;
}
String data = readResource(resName);
return data == null ? null : new ByteArrayInputStream(data.getBytes());
}
} }

View File

@ -29,5 +29,6 @@ public class HTML4JPlugin implements TeaVMPlugin {
host.add(new JavaScriptBodyTransformer()); host.add(new JavaScriptBodyTransformer());
host.add(new JCLHacks()); host.add(new JCLHacks());
host.add(new JavaScriptResourceInterceptor()); host.add(new JavaScriptResourceInterceptor());
host.add(new ResourcesInterceptor());
} }
} }

View File

@ -0,0 +1,66 @@
/*
* 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.html4j;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.io.IOUtils;
import org.teavm.codegen.SourceWriter;
import org.teavm.javascript.RenderingContext;
import org.teavm.vm.BuildTarget;
import org.teavm.vm.spi.AbstractRendererListener;
/**
*
* @author Jaroslav Tulach
*/
public class ResourcesInterceptor extends AbstractRendererListener {
private final Set<String> processed = new HashSet<>();
@Override
public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException {
boolean hasOneResource = false;
for (String className : context.getClassSource().getClassNames()) {
final int lastDot = className.lastIndexOf('.');
if (lastDot == -1) {
continue;
}
String packageName = className.substring(0, lastDot);
String resourceName = packageName.replace('.', '/') + "/" + "jvm.txt";
try (InputStream input = context.getClassLoader().getResourceAsStream(resourceName)) {
if (input == null || !processed.add(resourceName)) {
continue;
}
ByteArrayOutputStream arr = new ByteArrayOutputStream();
IOUtils.copy(input, arr);
String base64 = Base64.getEncoder().encodeToString(arr.toByteArray());
input.close();
final SourceWriter w = context.getWriter();
w.append("// Resource " + resourceName + " included by " + className).newLine();
w.append("if (!window.teaVMResources) window.teaVMResources = {};").newLine();
w.append("window.teaVMResources['" + resourceName + "'] = '");
w.append(base64).append("';").newLine().newLine();
}
hasOneResource = true;
}
if (hasOneResource) {
context.getWriter().append("// TeaVM generated classes").newLine();
}
}
}

View File

@ -15,9 +15,14 @@
*/ */
package org.teavm.html4j.test; package org.teavm.html4j.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.util.Calendar; import java.util.Calendar;
import net.java.html.js.JavaScriptBody; import net.java.html.js.JavaScriptBody;
import static org.junit.Assert.assertNotNull;
import org.junit.Test; import org.junit.Test;
/** /**
@ -25,6 +30,16 @@ import org.junit.Test;
* @author Alexey Andreev * @author Alexey Andreev
*/ */
public class JavaScriptBodyTest { public class JavaScriptBodyTest {
@Test
public void readResource() throws IOException {
InputStream is = JavaScriptBodyTest.class.getResourceAsStream("jvm.txt");
assertNotNull("Resource jvm.txt found", is);
try (BufferedReader r = new BufferedReader(new InputStreamReader(is))) {
String line = r.readLine();
assertEquals("Line read", "TeaVM", line);
}
}
@Test @Test
public void javaScriptBodyHandled() { public void javaScriptBodyHandled() {
assertEquals(23, simpleNativeMethod()); assertEquals(23, simpleNativeMethod());

View File

@ -0,0 +1 @@
TeaVM

View File

@ -18,6 +18,10 @@
<name>TeaVM performance benchmark</name> <name>TeaVM performance benchmark</name>
<description>Compares performance of the JavaScript code produced by TeaVM and GWT</description> <description>Compares performance of the JavaScript code produced by TeaVM and GWT</description>
<properties>
<bck2brwsr.version>0.14</bck2brwsr.version>
</properties>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.teavm</groupId> <groupId>org.teavm</groupId>
@ -43,6 +47,12 @@
<classifier>sources</classifier> <classifier>sources</classifier>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>org.apidesign.bck2brwsr</groupId>
<artifactId>emul</artifactId>
<version>${bck2brwsr.version}</version>
<classifier>rt</classifier>
</dependency>
<dependency> <dependency>
<groupId>com.google.gwt</groupId> <groupId>com.google.gwt</groupId>
<artifactId>gwt-user</artifactId> <artifactId>gwt-user</artifactId>
@ -54,6 +64,20 @@
<artifactId>html5-canvas</artifactId> <artifactId>html5-canvas</artifactId>
<version>0.7.2</version> <version>0.7.2</version>
</dependency> </dependency>
<dependency>
<groupId>com.dukescript.canvas</groupId>
<artifactId>canvas-api</artifactId>
<version>0.7.2</version>
<classifier>bck2brwsr</classifier>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apidesign.bck2brwsr</groupId>
<artifactId>ko-bck2brwsr</artifactId>
<version>${bck2brwsr.version}</version>
<classifier>bck2brwsr</classifier>
<scope>provided</scope>
</dependency>
<dependency> <dependency>
<groupId>org.netbeans.html</groupId> <groupId>org.netbeans.html</groupId>
<artifactId>net.java.html.boot</artifactId> <artifactId>net.java.html.boot</artifactId>
@ -136,6 +160,32 @@
</execution> </execution>
</executions> </executions>
</plugin> </plugin>
<plugin>
<groupId>org.apidesign.bck2brwsr</groupId>
<artifactId>bck2brwsr-maven-plugin</artifactId>
<version>${bck2brwsr.version}</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>aot</goal>
</goals>
</execution>
</executions>
<configuration>
<obfuscation>NONE</obfuscation>
<classPathPrefix>lib</classPathPrefix>
<mainJavaScript>${project.build.directory}/generated/js/b2b-benchmark.js</mainJavaScript>
<vm>${project.build.directory}/generated/js/bck2brwsr.js</vm>
<exports>
<export>org.teavm.samples.benchmark.htmljava.BenchmarkStarter</export>
<export>org/jbox2d/collision/shapes/</export>
<export>org/jbox2d/common/</export>
<export>org/jbox2d/dynamics/</export>
<export>org/jbox2d/dynamics/joints/</export>
</exports>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId> <artifactId>maven-checkstyle-plugin</artifactId>

View File

@ -0,0 +1,46 @@
<!--
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.
-->
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>Bck2Brwsr jbox2d benchmark</title>
</head>
<body>
<h1>Bck2Brwsr performance</h1>
<div>
<canvas id="benchmark-canvas" width="600" height="600"></canvas>
</div>
<table>
<thead>
<tr>
<th>Second</th>
<th>Time spent computing, ms</th>
</tr>
</thead>
<tbody id="result-table-body">
</tbody>
</table>
<!-- boot bck2brwsr -->
<script type="text/javascript" src="bck2brwsr.js"></script>
<script>
var vm = bck2brwsr('b2b-benchmark.js');
var c = vm.loadClass('org.teavm.samples.benchmark.htmljava.BenchmarkStarter');
c.invoke('main');
</script>
</html>

View File

@ -17,13 +17,14 @@
<html> <html>
<head> <head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<title>TeaVM vs. GWT performance comparison</title> <title>TeaVM vs. GWT vs. Bck2Brwsr performance comparison</title>
</head> </head>
<body onload="main()"> <body onload="main()">
<h1>TeaVM vs. GWT performance</h1> <h1>TeaVM vs. GWT vs. Bck2Brwsr performance</h1>
<ul> <ul>
<li><a href="teavm.html">TeaVM</a></li> <li><a href="teavm.html">TeaVM</a></li>
<li><a href="gwt.html">GWT</a></li> <li><a href="gwt.html">GWT</a></li>
<li><a href="bck2brwsr.html">Bck2Brwsr VM</a></li>
</ul> </ul>
</body> </body>
</html> </html>