Add option to disable assertions

This commit is contained in:
Alexey Andreev 2022-07-15 19:51:47 +03:00
parent c42b9fac28
commit ddddfcf217
8 changed files with 92 additions and 0 deletions

View File

@ -0,0 +1,62 @@
/*
* Copyright 2022 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.transformation;
import org.teavm.model.BasicBlock;
import org.teavm.model.ClassHolder;
import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassHolderTransformerContext;
import org.teavm.model.Instruction;
import org.teavm.model.MethodHolder;
import org.teavm.model.MethodReference;
import org.teavm.model.Program;
import org.teavm.model.instructions.IntegerConstantInstruction;
import org.teavm.model.instructions.InvokeInstruction;
public class AssertionRemoval implements ClassHolderTransformer {
private static final MethodReference ASSERTION_METHOD = new MethodReference(Class.class, "desiredAssertionStatus",
boolean.class);
@Override
public void transformClass(ClassHolder cls, ClassHolderTransformerContext context) {
for (MethodHolder method : cls.getMethods()) {
if (method.getProgram() != null) {
removeAssertions(method.getProgram());
}
}
}
private void removeAssertions(Program program) {
for (BasicBlock block : program.getBasicBlocks()) {
for (Instruction instruction : block) {
if (instruction instanceof InvokeInstruction) {
InvokeInstruction invoke = (InvokeInstruction) instruction;
if (invoke.getInstance() != null && invoke.getMethod().equals(ASSERTION_METHOD)) {
if (invoke.getReceiver() == null) {
invoke.delete();
} else {
IntegerConstantInstruction replacement = new IntegerConstantInstruction();
replacement.setConstant(0);
replacement.setReceiver(invoke.getReceiver());
replacement.setLocation(invoke.getLocation());
invoke.replace(replacement);
}
}
}
}
}
}
}

View File

@ -57,6 +57,7 @@ import org.teavm.model.ClassHolderTransformer;
import org.teavm.model.ClassReader; import org.teavm.model.ClassReader;
import org.teavm.model.PreOptimizingClassHolderSource; import org.teavm.model.PreOptimizingClassHolderSource;
import org.teavm.model.ReferenceCache; import org.teavm.model.ReferenceCache;
import org.teavm.model.transformation.AssertionRemoval;
import org.teavm.parsing.ClasspathClassHolderSource; import org.teavm.parsing.ClasspathClassHolderSource;
import org.teavm.tooling.sources.SourceFileProvider; import org.teavm.tooling.sources.SourceFileProvider;
import org.teavm.tooling.sources.SourceFilesCopier; import org.teavm.tooling.sources.SourceFilesCopier;
@ -111,6 +112,7 @@ public class TeaVMTool {
private boolean longjmpSupported = true; private boolean longjmpSupported = true;
private boolean heapDump; private boolean heapDump;
private boolean shortFileNames; private boolean shortFileNames;
private boolean assertionsRemoved;
public File getTargetDirectory() { public File getTargetDirectory() {
return targetDirectory; return targetDirectory;
@ -268,6 +270,10 @@ public class TeaVMTool {
this.shortFileNames = shortFileNames; this.shortFileNames = shortFileNames;
} }
public void setAssertionsRemoved(boolean assertionsRemoved) {
this.assertionsRemoved = assertionsRemoved;
}
public void setProgressListener(TeaVMProgressListener progressListener) { public void setProgressListener(TeaVMProgressListener progressListener) {
this.progressListener = progressListener; this.progressListener = progressListener;
} }
@ -406,6 +412,10 @@ public class TeaVMTool {
vm.setProgressListener(progressListener); vm.setProgressListener(progressListener);
} }
if (assertionsRemoved) {
vm.add(new AssertionRemoval());
}
vm.setProperties(properties); vm.setProperties(properties);
vm.setProgramCache(incremental ? programCache : EmptyProgramCache.INSTANCE); vm.setProgramCache(incremental ? programCache : EmptyProgramCache.INSTANCE);
vm.setCacheStatus(cacheStatus); vm.setCacheStatus(cacheStatus);

View File

@ -84,5 +84,7 @@ public interface BuildStrategy {
void setShortFileNames(boolean shortFileNames); void setShortFileNames(boolean shortFileNames);
void setAssertionsRemoved(boolean assertionsRemoved);
BuildResult build() throws BuildException; BuildResult build() throws BuildException;
} }

View File

@ -69,6 +69,7 @@ public class InProcessBuildStrategy implements BuildStrategy {
private Properties properties = new Properties(); private Properties properties = new Properties();
private TeaVMToolLog log = new EmptyTeaVMToolLog(); private TeaVMToolLog log = new EmptyTeaVMToolLog();
private boolean shortFileNames; private boolean shortFileNames;
private boolean assertionsRemoved;
public InProcessBuildStrategy(ClassLoaderFactory classLoaderFactory) { public InProcessBuildStrategy(ClassLoaderFactory classLoaderFactory) {
this.classLoaderFactory = classLoaderFactory; this.classLoaderFactory = classLoaderFactory;
@ -226,6 +227,11 @@ public class InProcessBuildStrategy implements BuildStrategy {
this.shortFileNames = shortFileNames; this.shortFileNames = shortFileNames;
} }
@Override
public void setAssertionsRemoved(boolean assertionsRemoved) {
this.assertionsRemoved = assertionsRemoved;
}
@Override @Override
public BuildResult build() throws BuildException { public BuildResult build() throws BuildException {
TeaVMTool tool = new TeaVMTool(); TeaVMTool tool = new TeaVMTool();
@ -257,6 +263,7 @@ public class InProcessBuildStrategy implements BuildStrategy {
tool.setLongjmpSupported(longjmpSupported); tool.setLongjmpSupported(longjmpSupported);
tool.setHeapDump(heapDump); tool.setHeapDump(heapDump);
tool.setShortFileNames(shortFileNames); tool.setShortFileNames(shortFileNames);
tool.setAssertionsRemoved(assertionsRemoved);
tool.getProperties().putAll(properties); tool.getProperties().putAll(properties);

View File

@ -200,6 +200,11 @@ public class RemoteBuildStrategy implements BuildStrategy {
request.shortFileNames = shortFileNames; request.shortFileNames = shortFileNames;
} }
@Override
public void setAssertionsRemoved(boolean assertionsRemoved) {
request.assertionsRemoved = assertionsRemoved;
}
@Override @Override
public BuildResult build() throws BuildException { public BuildResult build() throws BuildException {
RemoteBuildResponse response; RemoteBuildResponse response;

View File

@ -164,6 +164,7 @@ public class BuildDaemon extends UnicastRemoteObject implements RemoteBuildServi
tool.setLongjmpSupported(request.longjmpSupported); tool.setLongjmpSupported(request.longjmpSupported);
tool.setHeapDump(request.heapDump); tool.setHeapDump(request.heapDump);
tool.setShortFileNames(request.shortFileNames); tool.setShortFileNames(request.shortFileNames);
tool.setAssertionsRemoved(request.assertionsRemoved);
for (String sourceDirectory : request.sourceDirectories) { for (String sourceDirectory : request.sourceDirectories) {
tool.addSourceFileProvider(new DirectorySourceFileProvider(new File(sourceDirectory))); tool.addSourceFileProvider(new DirectorySourceFileProvider(new File(sourceDirectory)));

View File

@ -51,4 +51,5 @@ public class RemoteBuildRequest implements Serializable {
public boolean longjmpSupported; public boolean longjmpSupported;
public boolean heapDump; public boolean heapDump;
public boolean shortFileNames; public boolean shortFileNames;
public boolean assertionsRemoved;
} }

View File

@ -161,6 +161,9 @@ public class TeaVMCompileMojo extends AbstractMojo {
@Parameter(property = "teavm.shortFileNames", defaultValue = "false") @Parameter(property = "teavm.shortFileNames", defaultValue = "false")
private boolean shortFileNames; private boolean shortFileNames;
@Parameter(property = "teavm.assertionsRemoved", defaultValue = "false")
private boolean assertionsRemoved;
private void setupBuilder(BuildStrategy builder) throws MojoExecutionException { private void setupBuilder(BuildStrategy builder) throws MojoExecutionException {
builder.setLog(new MavenTeaVMToolLog(getLog())); builder.setLog(new MavenTeaVMToolLog(getLog()));
try { try {
@ -186,6 +189,7 @@ public class TeaVMCompileMojo extends AbstractMojo {
builder.setMinHeapSize(minHeapSize * 1024 * 1024); builder.setMinHeapSize(minHeapSize * 1024 * 1024);
builder.setMaxHeapSize(maxHeapSize * 1024 * 1024); builder.setMaxHeapSize(maxHeapSize * 1024 * 1024);
builder.setShortFileNames(shortFileNames); builder.setShortFileNames(shortFileNames);
builder.setAssertionsRemoved(assertionsRemoved);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw new MojoExecutionException("Unexpected error occurred", e); throw new MojoExecutionException("Unexpected error occurred", e);
} }