From a56a9c15a78ee703858e34e7062ae374f9b6cb72 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 25 Feb 2015 11:48:28 +0100 Subject: [PATCH 01/14] Modifying the jbox2d benchmark to also generate Bck2Brwsr version of the sample --- pom.xml | 15 -- teavm-samples/teavm-samples-benchmark/pom.xml | 128 +++++++++++++----- .../benchmark/bck2brwsr/BenchmarkStarter.java | 27 ++++ .../src/main/webapp/bck2brwsr.html | 46 +++++++ .../src/main/webapp/index.html | 5 +- 5 files changed, 171 insertions(+), 50 deletions(-) create mode 100644 teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java create mode 100644 teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html diff --git a/pom.xml b/pom.xml index 5fe03e8cd..987c6dcaa 100644 --- a/pom.xml +++ b/pom.xml @@ -218,21 +218,6 @@ org.apache.maven.plugins maven-checkstyle-plugin 2.11 - - - validate - validate - - UTF-8 - true - true - false - - - check - - - config_loc=${basedir}/.. ../checkstyle.xml diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index 711bcca06..22c1ddfc9 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -33,16 +33,19 @@ org.teavm teavm-classlib ${project.version} + provided org.teavm teavm-jso ${project.version} + provided org.teavm teavm-dom ${project.version} + provided org.jbox2d @@ -54,6 +57,13 @@ jbox2d-library 2.2.1.1 sources + provided + + + org.apidesign.bck2brwsr + emul + 1.0-SNAPSHOT + rt com.google.gwt @@ -77,44 +87,96 @@ - org.teavm - teavm-maven-plugin - ${project.version} + maven-jar-plugin + 2.5 - - web-client - prepare-package - - build-javascript - - - ${project.build.directory}/generated/js/teavm - org.teavm.samples.benchmark.teavm.BenchmarkStarter - SEPARATE - false - true - - + + prepare-package + + jar + + + + + org.teamvm.samples.benchmark.bck2brwsr.BenchmarkStarter + true + lib/ + false + + + + **/gwt/* + **/teavm/* + + + - org.codehaus.mojo - gwt-maven-plugin - 2.7.0 - - - - compile - - - 9 - - org.jbox2d:jbox2d-library - - - - + org.teavm + teavm-maven-plugin + ${project.version} + + + web-client + prepare-package + + build-javascript + + + ${project.build.directory}/generated/js/teavm + org.teavm.samples.benchmark.teavm.BenchmarkStarter + SEPARATE + false + true + + + + + org.codehaus.mojo + gwt-maven-plugin + 2.7.0 + + + + compile + + + 9 + + org.jbox2d:jbox2d-library + + + + + + + org.apidesign.bck2brwsr + bck2brwsr-maven-plugin + 1.0-SNAPSHOT + + + prepare-package + + aot + + + + + NONE + lib + ${project.build.directory}/generated/js/b2b-benchmark.js + ${project.build.directory}/generated/js/bck2brwsr.js + + org.teavm.samples.benchmark.bck2brwsr.BenchmarkStarter + org/jbox2d/collision/shapes/ + org/jbox2d/common/ + org/jbox2d/dynamics/ + org/jbox2d/dynamics/joints/ + + + org.apache.maven.plugins maven-checkstyle-plugin diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java new file mode 100644 index 000000000..5b6a4e95a --- /dev/null +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java @@ -0,0 +1,27 @@ +/* + * Copyright 2015 Jaroslav Tulach. + * + * 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.samples.benchmark.bck2brwsr; + +import org.teavm.samples.benchmark.Scene; + +public class BenchmarkStarter { + private static Scene s = new Scene(); + + public static void main(String... args) { + s.calculate(); + throw new IllegalStateException("Success!"); + } +} diff --git a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html new file mode 100644 index 000000000..9d34d8fa4 --- /dev/null +++ b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html @@ -0,0 +1,46 @@ + + + + + + Bck2Brwsr jbox2d benchmark + + +

Bck2Brwsr performance

+
+ +
+ + + + + + + + + +
SecondTime spent computing, ms
+ + + + + + diff --git a/teavm-samples/teavm-samples-benchmark/src/main/webapp/index.html b/teavm-samples/teavm-samples-benchmark/src/main/webapp/index.html index 6c8f5de14..f5f789deb 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/webapp/index.html +++ b/teavm-samples/teavm-samples-benchmark/src/main/webapp/index.html @@ -17,13 +17,14 @@ - TeaVM vs. GWT performance comparison + TeaVM vs. GWT vs. Bck2Brwsr performance comparison -

TeaVM vs. GWT performance

+

TeaVM vs. GWT vs. Bck2Brwsr performance

\ No newline at end of file From 2318df15609d3b39a3dbe622e1f49a8aa4b89027 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 25 Feb 2015 12:12:27 +0100 Subject: [PATCH 02/14] Bck2Brwsr initializes on background, no need to wait for onload message --- .../teavm-samples-benchmark/src/main/webapp/bck2brwsr.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html index 9d34d8fa4..b02c8c3c2 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html +++ b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html @@ -19,7 +19,7 @@ Bck2Brwsr jbox2d benchmark - +

Bck2Brwsr performance

From 3bb171e4a1af0f063ed63c6feb3e63f9489c2e24 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Wed, 25 Feb 2015 12:23:16 +0100 Subject: [PATCH 03/14] Using DukeScript's canvas API --- teavm-samples/teavm-samples-benchmark/pom.xml | 5 + .../benchmark/bck2brwsr/BenchmarkStarter.java | 93 ++++++++++++++++++- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index 22c1ddfc9..7b2bbe6d3 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -71,6 +71,11 @@ 2.7.0 provided + + com.dukescript.canvas + html5-canvas + 0.7.1 + diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java index 5b6a4e95a..522073531 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java @@ -15,13 +15,98 @@ */ package org.teavm.samples.benchmark.bck2brwsr; +import com.dukescript.api.canvas.GraphicsContext2D; +import com.dukescript.api.canvas.Style; +import com.dukescript.canvas.html.HTML5Graphics; +import java.util.Timer; +import java.util.TimerTask; +import org.jbox2d.collision.shapes.CircleShape; +import org.jbox2d.collision.shapes.PolygonShape; +import org.jbox2d.collision.shapes.Shape; +import org.jbox2d.collision.shapes.ShapeType; +import org.jbox2d.common.Vec2; +import org.jbox2d.dynamics.Body; +import org.jbox2d.dynamics.Fixture; import org.teavm.samples.benchmark.Scene; public class BenchmarkStarter { - private static Scene s = new Scene(); + private static final Timer TIMER = new Timer("Make Step"); + private static final Scene scene = new Scene(); + private static double startMillisecond; + private static int currentSecond; + private static double timeSpentCalculating; - public static void main(String... args) { - s.calculate(); - throw new IllegalStateException("Success!"); + public static void main(String[] args) { + startMillisecond = System.currentTimeMillis(); + makeStep(); + } + + private static void makeStep() { + double start = System.currentTimeMillis(); + scene.calculate(); + double end = System.currentTimeMillis(); + int second = (int)((System.currentTimeMillis() - startMillisecond) / 1000); + if (second > currentSecond) { + /* + HTMLElement row = document.createElement("tr"); + resultTableBody.appendChild(row); + HTMLElement secondCell = document.createElement("td"); + row.appendChild(secondCell); + secondCell.appendChild(document.createTextNode(String.valueOf(second))); + HTMLElement timeCell = document.createElement("td"); + row.appendChild(timeCell); + timeCell.appendChild(document.createTextNode(String.valueOf(timeSpentCalculating))); +*/ + timeSpentCalculating = 0; + currentSecond = second; + } + timeSpentCalculating += end - start; + render(); + TIMER.schedule(new TimerTask() { + @Override + public void run() { + makeStep(); + } + }, scene.timeUntilNextStep()); + } + + private static void render() { + GraphicsContext2D context = HTML5Graphics.getOrCreate("benchmark-canvas"); + context.setFillStyle(new Style.Color("white")); + context.setStrokeStyle(new Style.Color("grey")); + context.fillRect(0, 0, 600, 600); + context.save(); + context.translate(0, 600); + context.scale(1, -1); + context.scale(100, 100); + context.setLineWidth(0.01); + for (Body body = scene.getWorld().getBodyList(); body != null; body = body.getNext()) { + Vec2 center = body.getPosition(); + context.save(); + context.translate(center.x, center.y); + context.rotate(body.getAngle()); + for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) { + Shape shape = fixture.getShape(); + if (shape.getType() == ShapeType.CIRCLE) { + CircleShape circle = (CircleShape)shape; + context.beginPath(); + context.arc(circle.m_p.x, circle.m_p.y, circle.getRadius(), 0, Math.PI * 2, true); + context.closePath(); + context.stroke(); + } else if (shape.getType() == ShapeType.POLYGON) { + PolygonShape poly = (PolygonShape)shape; + Vec2[] vertices = poly.getVertices(); + context.beginPath(); + context.moveTo(vertices[0].x, vertices[0].y); + for (int i = 1; i < poly.getVertexCount(); ++i) { + context.lineTo(vertices[i].x, vertices[i].y); + } + context.closePath(); + context.stroke(); + } + } + context.restore(); + } + context.restore(); } } From cf8645decfdd3fbbe843e567b9791b74ca80e24e Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 26 Feb 2015 12:04:51 +0100 Subject: [PATCH 04/14] Adding FX WebView based sample to demonstrate the performance when running on top of HotSpot VM. Use following command to try: teavm-samples/teavm-samples-benchmark$ mvn clean install && mvn -Pfx exec:java --- teavm-samples/teavm-samples-benchmark/pom.xml | 42 +++++++++++++++++++ .../benchmark/bck2brwsr/BenchmarkFX.java | 29 +++++++++++++ .../benchmark/bck2brwsr/BenchmarkStarter.java | 39 ++++++++++++----- .../teavm/samples/benchmark/bck2brwsr/fx.html | 37 ++++++++++++++++ 4 files changed, 136 insertions(+), 11 deletions(-) create mode 100644 teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java create mode 100644 teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index 7b2bbe6d3..ffdbe10cd 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -76,6 +76,11 @@ html5-canvas 0.7.1 + + org.netbeans.html + net.java.html.boot + 1.1 + @@ -194,10 +199,47 @@ org.apache.maven.plugins maven-source-plugin + + org.netbeans.html + html4j-maven-plugin + 1.1 + + + + process-js-annotations + + + + org.apache.maven.plugins maven-javadoc-plugin + + + fx + + + org.netbeans.html + net.java.html.boot.fx + 1.1 + runtime + + + + + + org.codehaus.mojo + exec-maven-plugin + 1.3.2 + + org.teavm.samples.benchmark.bck2brwsr.BenchmarkFX + + + + + + \ No newline at end of file diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java new file mode 100644 index 000000000..0a0336f4c --- /dev/null +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015 jarda. + * + * 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.samples.benchmark.bck2brwsr; + +import net.java.html.boot.BrowserBuilder; + +public class BenchmarkFX { + public static void main(String... args) { + BrowserBuilder.newBrowser().loadPage("fx.html") + .loadClass(BenchmarkStarter.class) + .invoke("main") + .showAndWait(); + System.exit(0); + } + +} diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java index 522073531..e0346b627 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java @@ -20,6 +20,8 @@ import com.dukescript.api.canvas.Style; import com.dukescript.canvas.html.HTML5Graphics; import java.util.Timer; import java.util.TimerTask; +import net.java.html.BrwsrCtx; +import net.java.html.js.JavaScriptBody; import org.jbox2d.collision.shapes.CircleShape; import org.jbox2d.collision.shapes.PolygonShape; import org.jbox2d.collision.shapes.Shape; @@ -35,28 +37,30 @@ public class BenchmarkStarter { private static double startMillisecond; private static int currentSecond; private static double timeSpentCalculating; + private static BrwsrCtx ctx; public static void main(String[] args) { startMillisecond = System.currentTimeMillis(); + ctx = BrwsrCtx.findDefault(BenchmarkStarter.class); makeStep(); } + + static void makeStep() { + ctx.execute(new Runnable() { + @Override + public void run() { + makeStep0(); + } + }); + } - private static void makeStep() { + private static void makeStep0() { double start = System.currentTimeMillis(); scene.calculate(); double end = System.currentTimeMillis(); int second = (int)((System.currentTimeMillis() - startMillisecond) / 1000); if (second > currentSecond) { - /* - HTMLElement row = document.createElement("tr"); - resultTableBody.appendChild(row); - HTMLElement secondCell = document.createElement("td"); - row.appendChild(secondCell); - secondCell.appendChild(document.createTextNode(String.valueOf(second))); - HTMLElement timeCell = document.createElement("td"); - row.appendChild(timeCell); - timeCell.appendChild(document.createTextNode(String.valueOf(timeSpentCalculating))); -*/ + publishResults(second, timeSpentCalculating); timeSpentCalculating = 0; currentSecond = second; } @@ -109,4 +113,17 @@ public class BenchmarkStarter { } context.restore(); } + + @JavaScriptBody(args = { "second", "timeSpentCalculating" }, body = + "var resultTableBody = document.getElementById('result-table-body');\n" + + "var row = document.createElement(\"tr\");\n" + + "resultTableBody.appendChild(row);\n" + + "var secondCell = document.createElement(\"td\");\n" + + "row.appendChild(secondCell);\n" + + "secondCell.appendChild(document.createTextNode(second));\n" + + "var timeCell = document.createElement(\"td\");\n" + + "row.appendChild(timeCell);\n" + + "timeCell.appendChild(document.createTextNode(timeSpentCalculating));\n" + ) + private static native void publishResults(int second, double time); } diff --git a/teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html b/teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html new file mode 100644 index 000000000..f37320506 --- /dev/null +++ b/teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html @@ -0,0 +1,37 @@ + + + + + + HotSpot/JavaFX jbox2d benchmark + + +

HotSpot/JavaFX performance

+
+ +
+ + + + + + + + + +
SecondTime spent computing, ms
+ From e048987b4f51368a25a1e0e2b4ed66ab3f6d3efe Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 27 Feb 2015 08:39:55 +0100 Subject: [PATCH 05/14] Using the HTML/Java name as the benchmark is generic and can also work on JavaFX --- teavm-samples/teavm-samples-benchmark/pom.xml | 6 +++--- .../benchmark/{bck2brwsr => htmljava}/BenchmarkFX.java | 2 +- .../benchmark/{bck2brwsr => htmljava}/BenchmarkStarter.java | 2 +- .../teavm/samples/benchmark/{bck2brwsr => htmljava}/fx.html | 0 .../teavm-samples-benchmark/src/main/webapp/bck2brwsr.html | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) rename teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/{bck2brwsr => htmljava}/BenchmarkFX.java (95%) rename teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/{bck2brwsr => htmljava}/BenchmarkStarter.java (99%) rename teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/{bck2brwsr => htmljava}/fx.html (100%) diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index ffdbe10cd..1f302e97d 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -108,7 +108,7 @@ - org.teamvm.samples.benchmark.bck2brwsr.BenchmarkStarter + org.teamvm.samples.benchmark.htmljava.BenchmarkStarter true lib/ false @@ -179,7 +179,7 @@ ${project.build.directory}/generated/js/b2b-benchmark.js ${project.build.directory}/generated/js/bck2brwsr.js - org.teavm.samples.benchmark.bck2brwsr.BenchmarkStarter + org.teavm.samples.benchmark.htmljava.BenchmarkStarter org/jbox2d/collision/shapes/ org/jbox2d/common/ org/jbox2d/dynamics/ @@ -235,7 +235,7 @@ exec-maven-plugin 1.3.2 - org.teavm.samples.benchmark.bck2brwsr.BenchmarkFX + org.teavm.samples.benchmark.htmljava.BenchmarkFX diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkFX.java similarity index 95% rename from teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java rename to teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkFX.java index 0a0336f4c..2349c1e61 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkFX.java +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkFX.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.samples.benchmark.bck2brwsr; +package org.teavm.samples.benchmark.htmljava; import net.java.html.boot.BrowserBuilder; diff --git a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkStarter.java similarity index 99% rename from teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java rename to teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkStarter.java index e0346b627..a0606f347 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/bck2brwsr/BenchmarkStarter.java +++ b/teavm-samples/teavm-samples-benchmark/src/main/java/org/teavm/samples/benchmark/htmljava/BenchmarkStarter.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.teavm.samples.benchmark.bck2brwsr; +package org.teavm.samples.benchmark.htmljava; import com.dukescript.api.canvas.GraphicsContext2D; import com.dukescript.api.canvas.Style; diff --git a/teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html b/teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/htmljava/fx.html similarity index 100% rename from teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/bck2brwsr/fx.html rename to teavm-samples/teavm-samples-benchmark/src/main/resources/org/teavm/samples/benchmark/htmljava/fx.html diff --git a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html index b02c8c3c2..7aa17d72c 100644 --- a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html +++ b/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html @@ -39,7 +39,7 @@ From 078c2f294294187ef030364747a6302faf0172f9 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Sat, 28 Feb 2015 17:28:53 +0100 Subject: [PATCH 06/14] Switching to stable version of Bck2Brwsr VM --- teavm-samples/teavm-samples-benchmark/pom.xml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index 4844ea982..e34e5d39c 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -27,6 +27,10 @@ TeaVM performance benchmark Compares performance of the JavaScript code produced by TeaVM and GWT + + + 0.14 + @@ -62,7 +66,7 @@ org.apidesign.bck2brwsr emul - 1.0-SNAPSHOT + ${bck2brwsr.version} rt @@ -164,7 +168,7 @@ org.apidesign.bck2brwsr bck2brwsr-maven-plugin - 1.0-SNAPSHOT + ${bck2brwsr.version} prepare-package From 39d90b1a027a51963bc38d3d7ae7f04b1edf789e Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 19 Mar 2015 21:13:49 +0100 Subject: [PATCH 07/14] Using precompiled versions of some bck2brwsr libraries --- teavm-samples/teavm-samples-benchmark/pom.xml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/teavm-samples/teavm-samples-benchmark/pom.xml b/teavm-samples/teavm-samples-benchmark/pom.xml index ea86e0ad5..8ec352a2d 100644 --- a/teavm-samples/teavm-samples-benchmark/pom.xml +++ b/teavm-samples/teavm-samples-benchmark/pom.xml @@ -70,6 +70,20 @@ html5-canvas 0.7.2 + + com.dukescript.canvas + canvas-api + 0.7.2 + bck2brwsr + provided + + + org.apidesign.bck2brwsr + ko-bck2brwsr + ${bck2brwsr.version} + bck2brwsr + provided + org.netbeans.html net.java.html.boot From ef71b871d511c400ddf82c53b0be3c53ce2c9b7e Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 23 Oct 2015 08:50:01 +0300 Subject: [PATCH 08/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 874fb7664..10bf583a5 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ The easiest way to create a new TeaVM project is to type in the command line: mvn -DarchetypeCatalog=local \ -DarchetypeGroupId=org.teavm \ -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. Deploy this `war` in Tomcat or another container, or simply unzip it and open the `index.html` page. From 2c47c2167cfbaa6e8001673d73526e1908771547 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 23 Oct 2015 16:42:45 +0300 Subject: [PATCH 09/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 10bf583a5..35ab03220 100644 --- a/README.md +++ b/README.md @@ -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. 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. Also you can read [project's wiki](https://github.com/konsoletyper/teavm/wiki/). From 4aec89967b83e6119657b4c82c224174b5d2bcac Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 06:46:37 +0100 Subject: [PATCH 10/14] Simple test to show expected behavior of resources --- .../java/org/teavm/classlib/java/lang/TClass.java | 5 +++++ .../org/teavm/html4j/test/JavaScriptBodyTest.java | 15 +++++++++++++++ .../test/resources/org/teavm/html4j/test/jvm.txt | 1 + 3 files changed, 21 insertions(+) create mode 100644 html4j/src/test/resources/org/teavm/html4j/test/jvm.txt diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index 8aa91e7e5..d5bf22ead 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.java.lang; +import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -266,4 +267,8 @@ public class TClass extends TObject implements TAnnotatedElement { annotationsByType.put((TClass) (Object) annot.annotationType(), annot); } } + + public InputStream getResourceAsStream(String name) { + throw new IllegalStateException("Cannot read resource " + name); + } } diff --git a/html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyTest.java b/html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyTest.java index 02ebb9e75..9c3f679f4 100644 --- a/html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyTest.java +++ b/html4j/src/test/java/org/teavm/html4j/test/JavaScriptBodyTest.java @@ -15,9 +15,14 @@ */ 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 java.util.Calendar; import net.java.html.js.JavaScriptBody; +import static org.junit.Assert.assertNotNull; import org.junit.Test; /** @@ -25,6 +30,16 @@ import org.junit.Test; * @author Alexey Andreev */ 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 public void javaScriptBodyHandled() { assertEquals(23, simpleNativeMethod()); diff --git a/html4j/src/test/resources/org/teavm/html4j/test/jvm.txt b/html4j/src/test/resources/org/teavm/html4j/test/jvm.txt new file mode 100644 index 000000000..6d8fe9182 --- /dev/null +++ b/html4j/src/test/resources/org/teavm/html4j/test/jvm.txt @@ -0,0 +1 @@ +TeaVM From d9ee68315e52f309cd65acc0d4b16fa4d7f35476 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 07:33:52 +0100 Subject: [PATCH 11/14] Can include resources in the generated file --- .../java/org/teavm/html4j/HTML4JPlugin.java | 1 + .../teavm/html4j/ResourcesInterceptor.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java diff --git a/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java b/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java index 1530d308d..118a3ab80 100644 --- a/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java +++ b/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java @@ -29,5 +29,6 @@ public class HTML4JPlugin implements TeaVMPlugin { host.add(new JavaScriptBodyTransformer()); host.add(new JCLHacks()); host.add(new JavaScriptResourceInterceptor()); + host.add(new ResourcesInterceptor()); } } diff --git a/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java b/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java new file mode 100644 index 000000000..6c7cc0715 --- /dev/null +++ b/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java @@ -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 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(); + } + } +} From ecfdcd34c3b96e43699b62ea7f531cd75c787d28 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 07:53:36 +0100 Subject: [PATCH 12/14] Class.getResourceAsStream can find the generated resource --- .../org/teavm/classlib/java/lang/TClass.java | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index d5bf22ead..22277a27a 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -22,6 +22,7 @@ import java.util.Map; import org.teavm.classlib.impl.DeclaringClassMetadataGenerator; import org.teavm.classlib.java.lang.annotation.TAnnotation; import org.teavm.classlib.java.lang.reflect.TAnnotatedElement; +import org.teavm.jso.JSBody; import org.teavm.platform.Platform; import org.teavm.platform.PlatformClass; import org.teavm.platform.metadata.ClassResource; @@ -268,7 +269,22 @@ public class TClass extends TObject implements TAnnotatedElement { } } - public InputStream getResourceAsStream(String name) { - throw new IllegalStateException("Cannot read resource " + name); - } + @JSBody(params = "res", script = + "if (!window.teaVMResources) return null;\n" + + "var data = window.teaVMResources[res];\n" + + "return data ? 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; + } + throw new IllegalStateException("Read resource " + resName + " yields " + readResource(resName)); + } } From de117b61ed90ddde1cbbcb537f8560d7a19bc167 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 07:59:41 +0100 Subject: [PATCH 13/14] Converts the resource from base64 encoding before returning it --- .../src/main/java/org/teavm/classlib/java/lang/TClass.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java index 22277a27a..4ea2a9129 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TClass.java @@ -15,6 +15,7 @@ */ package org.teavm.classlib.java.lang; +import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.HashMap; import java.util.Map; @@ -272,7 +273,7 @@ public class TClass extends TObject implements TAnnotatedElement { @JSBody(params = "res", script = "if (!window.teaVMResources) return null;\n" + "var data = window.teaVMResources[res];\n" - + "return data ? data : null;\n" + + "return data ? window.atob(data) : null;\n" ) private static native String readResource(String message); @@ -285,6 +286,7 @@ public class TClass extends TObject implements TAnnotatedElement { } else { resName = clazzName.substring(0, lastDot).replace('.', '/') + "/" + name; } - throw new IllegalStateException("Read resource " + resName + " yields " + readResource(resName)); + String data = readResource(resName); + return data == null ? null : new ByteArrayInputStream(data.getBytes()); } } From ba3de15e6010fe2fcc8c8785c4ab76ce28815627 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Fri, 25 Dec 2015 22:52:10 +0300 Subject: [PATCH 14/14] Move bck2brwsr.html to a new location --- .../benchmark}/src/main/webapp/bck2brwsr.html | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {teavm-samples/teavm-samples-benchmark => samples/benchmark}/src/main/webapp/bck2brwsr.html (100%) diff --git a/teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html b/samples/benchmark/src/main/webapp/bck2brwsr.html similarity index 100% rename from teavm-samples/teavm-samples-benchmark/src/main/webapp/bck2brwsr.html rename to samples/benchmark/src/main/webapp/bck2brwsr.html