From 40af27811f665b3e67f77d0a7c48874d19e0e339 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Sat, 3 Sep 2016 23:48:05 +0300 Subject: [PATCH] Allow to run WASM generator via maven. --- .../backend/wasm/binary/MetadataWriter.java | 122 ------------------ .../java/org/teavm/tooling/TeaVMTool.java | 2 + ...scriptMojo.java => AbstractTeaVMMojo.java} | 8 +- ...ascriptMojo.java => TeaVMCompileMojo.java} | 22 +++- 4 files changed, 19 insertions(+), 135 deletions(-) delete mode 100644 core/src/main/java/org/teavm/backend/wasm/binary/MetadataWriter.java rename tools/maven/plugin/src/main/java/org/teavm/maven/{AbstractJavascriptMojo.java => AbstractTeaVMMojo.java} (98%) rename tools/maven/plugin/src/main/java/org/teavm/maven/{BuildJavascriptMojo.java => TeaVMCompileMojo.java} (86%) diff --git a/core/src/main/java/org/teavm/backend/wasm/binary/MetadataWriter.java b/core/src/main/java/org/teavm/backend/wasm/binary/MetadataWriter.java deleted file mode 100644 index 0fc766ea9..000000000 --- a/core/src/main/java/org/teavm/backend/wasm/binary/MetadataWriter.java +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright 2016 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.backend.wasm.binary; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -class MetadataWriter { - private byte[] data = new byte[256]; - private int offset; - private Map structureUsages = new HashMap<>(); - - void write(DataType[] types) { - writeLeb(types.length); - for (DataType type : types) { - write(type); - } - } - - private void write(DataType type) { - if (type == DataPrimitives.BYTE) { - writeByte(0); - } else if (type == DataPrimitives.SHORT) { - writeByte(1); - } else if (type == DataPrimitives.INT) { - writeByte(2); - } else if (type == DataPrimitives.LONG) { - writeByte(3); - } else if (type == DataPrimitives.FLOAT) { - writeByte(4); - } else if (type == DataPrimitives.DOUBLE) { - writeByte(5); - } else if (type == DataPrimitives.ADDRESS) { - writeByte(6); - } else if (type instanceof DataArray) { - DataArray array = (DataArray) type; - writeByte(7); - writeLeb(array.getSize()); - write(array.getComponentType()); - } else if (type instanceof DataStructure) { - DataStructure structure = (DataStructure) type; - Integer usage = structureUsages.get(structure); - if (usage == null) { - structureUsages.put(structure, offset); - writeByte(8); - writeByte(structure.getAlignment()); - DataType[] components = structure.getComponents(); - writeLeb(components.length); - for (int i = 0; i < components.length; ++i) { - write(components[i]); - } - } else { - int start = offset; - structureUsages.put(structure, offset); - writeByte(9); - writeLeb(start - usage); - } - } - } - - private void writeLeb(int value) { - if (value >>> 7 == 0) { - writeByte(value); - } else if (value >>> 14 == 0) { - byte first = (byte) ((value & 0x7F) | 0x80); - byte second = (byte) (value >>> 7); - writeBytes(new byte[] { first, second }); - } else if (value >>> 21 == 0) { - byte first = (byte) ((value & 0x7F) | 0x80); - byte second = (byte) (((value >>> 7) & 0x7F) | 0x80); - byte third = (byte) (value >>> 14); - writeBytes(new byte[] { first, second, third }); - } else if (value >>> 28 == 0) { - byte first = (byte) ((value & 0x7F) | 0x80); - byte second = (byte) (((value >>> 7) & 0x7F) | 0x80); - byte third = (byte) (((value >>> 14) & 0x7F) | 0x80); - byte fourth = (byte) (value >>> 21); - writeBytes(new byte[] { first, second, third, fourth }); - } else { - byte first = (byte) ((value & 0x7F) | 0x80); - byte second = (byte) (((value >>> 7) & 0x7F) | 0x80); - byte third = (byte) (((value >>> 14) & 0x7F) | 0x80); - byte fourth = (byte) (((value >>> 21) & 0x7F) | 0x80); - byte fifth = (byte) (value >>> 28); - writeBytes(new byte[] { first, second, third, fourth, fifth }); - } - } - - private void writeByte(int value) { - if (offset >= data.length) { - data = Arrays.copyOf(data, data.length * 2); - } - data[offset++] = (byte) value; - } - - private void writeBytes(byte[] values) { - int count = values.length; - if (offset + count > data.length) { - data = Arrays.copyOf(data, data.length * 2); - } - System.arraycopy(values, 0, data, offset, count); - offset += count; - } - - public byte[] getData() { - return Arrays.copyOf(data, offset); - } -} diff --git a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java index 491c10721..f216f8712 100644 --- a/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java +++ b/tools/core/src/main/java/org/teavm/tooling/TeaVMTool.java @@ -330,6 +330,8 @@ public class TeaVMTool implements BaseTeaVMTool { private WasmTarget prepareWebAssemblyTarget() { webAssemblyTarget = new WasmTarget(); webAssemblyTarget.setDebugging(debugInformationGenerated); + webAssemblyTarget.setCEmitted(debugInformationGenerated); + webAssemblyTarget.setWastEmitted(debugInformationGenerated); return webAssemblyTarget; } diff --git a/tools/maven/plugin/src/main/java/org/teavm/maven/AbstractJavascriptMojo.java b/tools/maven/plugin/src/main/java/org/teavm/maven/AbstractTeaVMMojo.java similarity index 98% rename from tools/maven/plugin/src/main/java/org/teavm/maven/AbstractJavascriptMojo.java rename to tools/maven/plugin/src/main/java/org/teavm/maven/AbstractTeaVMMojo.java index 8863821ba..d9a7a03de 100644 --- a/tools/maven/plugin/src/main/java/org/teavm/maven/AbstractJavascriptMojo.java +++ b/tools/maven/plugin/src/main/java/org/teavm/maven/AbstractTeaVMMojo.java @@ -38,11 +38,7 @@ import org.teavm.model.ClassHolderTransformer; import org.teavm.tooling.BaseTeaVMTool; import org.teavm.tooling.sources.SourceFileProvider; -/** - * - * @author Alexey Andreev - */ -public abstract class AbstractJavascriptMojo extends AbstractMojo { +public abstract class AbstractTeaVMMojo extends AbstractMojo { @Component protected MavenProject project; @@ -180,7 +176,7 @@ public abstract class AbstractJavascriptMojo extends AbstractMojo { } log.info("Using the following classpath for JavaScript generation: " + classpath); classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()]), - AbstractJavascriptMojo.class.getClassLoader()); + AbstractTeaVMMojo.class.getClassLoader()); return classLoader; } catch (MalformedURLException e) { throw new MojoExecutionException("Error gathering classpath information", e); diff --git a/tools/maven/plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java b/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java similarity index 86% rename from tools/maven/plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java rename to tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java index cc90bbc03..eb82ba574 100644 --- a/tools/maven/plugin/src/main/java/org/teavm/maven/BuildJavascriptMojo.java +++ b/tools/maven/plugin/src/main/java/org/teavm/maven/TeaVMCompileMojo.java @@ -27,19 +27,17 @@ import org.apache.maven.plugins.annotations.ResolutionScope; import org.teavm.tooling.ClassAlias; import org.teavm.tooling.MethodAlias; import org.teavm.tooling.RuntimeCopyOperation; +import org.teavm.tooling.TeaVMTargetType; import org.teavm.tooling.TeaVMTool; import org.teavm.tooling.TeaVMToolException; import org.teavm.tooling.sources.DirectorySourceFileProvider; import org.teavm.tooling.sources.SourceFileProvider; +import org.teavm.vm.TeaVMOptimizationLevel; -/** - * - * @author Alexey Andreev - */ @Mojo(name = "compile", requiresDependencyResolution = ResolutionScope.COMPILE_PLUS_RUNTIME, requiresDependencyCollection = ResolutionScope.COMPILE_PLUS_RUNTIME, defaultPhase = LifecyclePhase.PROCESS_CLASSES) -public class BuildJavascriptMojo extends AbstractJavascriptMojo { +public class TeaVMCompileMojo extends AbstractTeaVMMojo { @Parameter(defaultValue = "${project.build.directory}/javascript") private File targetDirectory; @@ -47,7 +45,7 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo { private File sourceDirectory; @Parameter - private String targetFileName = "classes.js"; + private String targetFileName = ""; @Parameter private String mainClass; @@ -67,6 +65,12 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo { @Parameter protected RuntimeCopyOperation runtime = RuntimeCopyOperation.SEPARATE; + @Parameter + private TeaVMOptimizationLevel optimizationLevel = TeaVMOptimizationLevel.SIMPLE; + + @Parameter + private TeaVMTargetType targetType = TeaVMTargetType.JAVASCRIPT; + @Parameter(defaultValue = "${project.build.directory}/teavm-cache") protected File cacheDirectory; @@ -86,7 +90,10 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo { tool.setMainClass(mainClass); tool.setMainPageIncluded(mainPageIncluded); tool.setRuntime(runtime); - tool.setTargetFileName(targetFileName); + if (!targetFileName.isEmpty()) { + tool.setTargetFileName(targetFileName); + } + tool.setOptimizationLevel(optimizationLevel); if (classAliases != null) { tool.getClassAliases().addAll(Arrays.asList(classAliases)); } @@ -94,6 +101,7 @@ public class BuildJavascriptMojo extends AbstractJavascriptMojo { tool.getMethodAliases().addAll(Arrays.asList(methodAliases)); } tool.setCacheDirectory(cacheDirectory); + tool.setTargetType(targetType); tool.generate(); if (stopOnErrors && !tool.getProblemProvider().getSevereProblems().isEmpty()) { throw new MojoExecutionException("Build error");