Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaTP committed Oct 8, 2023
1 parent bf8fa82 commit d8e9f92
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dylibso.chicory.maven.wast.Command;
import com.dylibso.chicory.maven.wast.CommandType;
import com.dylibso.chicory.maven.wast.WasmValue;
import com.dylibso.chicory.maven.wast.Wast;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.javaparser.ast.CompilationUnit;
Expand All @@ -24,6 +25,8 @@
import java.util.stream.Collectors;

import static com.dylibso.chicory.maven.StringUtils.*;
import static com.dylibso.chicory.maven.wast.ActionType.INVOKE;
import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;

public class JavaTestGen {

Expand All @@ -32,16 +35,18 @@ public class JavaTestGen {
private final Log log;
private final File baseDir;
private final File sourceTargetFolder;
private final List<String> excludedTests;

public JavaTestGen(Log log, File baseDir, File sourceTargetFolder) {
public JavaTestGen(Log log, File baseDir, File sourceTargetFolder, List<String> excludedTests) {
this.log = log;
this.baseDir = baseDir;
this.sourceTargetFolder = sourceTargetFolder;
this.excludedTests = excludedTests;
}

private String INSTANCE_NAME = "instance";
private static final String INSTANCE_NAME = "instance";

public void generate(File specFile, File wasmFilesFolder, List<String> excludedTests) {
public void generate(File specFile, File wasmFilesFolder) {
Wast wast;
try {
wast = mapper.readValue(specFile, Wast.class);
Expand Down Expand Up @@ -77,7 +82,7 @@ public void generate(File specFile, File wasmFilesFolder, List<String> excludedT

var testClass = cu.addClass(testName);

MethodDeclaration method = null;
MethodDeclaration method;
int testNumber = 0;
int moduleInstantiationNumber = 0;
for (var cmd: wast.getCommands()) {
Expand All @@ -86,7 +91,7 @@ public void generate(File specFile, File wasmFilesFolder, List<String> excludedT
switch (cmd.getType()) {
case MODULE:
testClass.addFieldWithInitializer(
new ClassOrInterfaceType("Instance"),
parseClassOrInterfaceType("Instance"),
INSTANCE_NAME + moduleInstantiationNumber++,
generateModuleInstantiation(cmd, wasmFilesFolder));
break;
Expand Down Expand Up @@ -134,7 +139,7 @@ private Optional<Expression> generateFieldExport(String varName, Command cmd, in
if (cmd.getAction() != null && cmd.getAction().getField() != null) {
var declarator = new VariableDeclarator()
.setName(varName)
.setType(new ClassOrInterfaceType("ExportFunction"))
.setType(parseClassOrInterfaceType("ExportFunction"))
.setInitializer(new NameExpr(INSTANCE_NAME + instanceNumber + ".getExport(\"" + cmd.getAction().getField() + "\")"));
Expression varDecl = new VariableDeclarationExpr(declarator);
return Optional.of(varDecl);
Expand All @@ -160,12 +165,12 @@ private List<Expression> generateAssert(String varName, Command cmd) {
}
}

String invocationMethod = null;
switch (cmd.getAction().getType()) {
case INVOKE:
var args = Arrays.stream(cmd.getAction().getArgs()).map(arg -> arg.toWasmValue()).collect(Collectors.joining(", "));
invocationMethod = ".apply(" + args + ")";
break;
String invocationMethod;
if (cmd.getAction().getType() == INVOKE) {
var args = Arrays.stream(cmd.getAction().getArgs()).map(WasmValue::toWasmValue).collect(Collectors.joining(", "));
invocationMethod = ".apply(" + args + ")";
} else {
throw new IllegalArgumentException("Unhandled action type " + cmd.getAction().getType());
}

switch (cmd.getType()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static String capitalize(String in) {

public static String escapedCamelCase(String in) {
var escaped = StringEscapeUtils.escapeJava(in);
var sb = new StringBuffer();
var sb = new StringBuilder();
var capitalize = false;
for (var i = 0; i < escaped.length(); i++) {
var character = escaped.charAt(i);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void execute() throws MojoExecutionException {
// Instantiate the utilities
var testSuiteDownloader = new TestSuiteDownloader(log);
var wast2Json = new Wast2JsonWrapper(log, wabtDownloadTargetFolder, wabtReleasesURL, wabtVersion, osName, compiledWastTargetFolder);
var testGen = new JavaTestGen(log, project.getBasedir(), sourceDestinationFolder);
var testGen = new JavaTestGen(log, project.getBasedir(), sourceDestinationFolder, clean(excludedTests));

// Create destination folders
compiledWastTargetFolder.mkdirs();
Expand All @@ -113,7 +113,7 @@ public void execute() throws MojoExecutionException {
throw new IllegalArgumentException("Wast file " + wastFile.getAbsolutePath() + " not found");
}
var wasmFilesFolder = wast2Json.execute(testsuiteFolder.toPath().resolve(spec).toFile());
testGen.generate(wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder, clean(excludedTests));
testGen.generate(wasmFilesFolder.toPath().resolve(SPEC_JSON).toFile(), wasmFilesFolder);
}
} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;

import static java.util.Collections.singleton;
Expand All @@ -22,7 +23,7 @@ public void downloadTestsuite(String testSuiteRepo, String testSuiteRepoRef, Fil
log.warn("Testsuite folder exists but looks corrupted, replacing.");
Files.walk(testSuiteFolder.toPath())
.sorted(Comparator.reverseOrder())
.map(x -> x.toFile())
.map(Path::toFile)
.forEach(File::delete);
} else {
log.debug("Testsuite detected, using the cached version.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ private File executeWast2Json(File wastFile) {
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("."));
pb.inheritIO();
Process ps = null;
Process ps;
try {
ps = pb.start();
ps.waitFor(10, TimeUnit.SECONDS);
Expand All @@ -83,14 +83,13 @@ private File executeWast2Json(File wastFile) {
return targetFolder;
}

private File downloadAndExtract(URL url) {
private void downloadAndExtract(URL url) {
wabtDownloadTargetFolder.mkdirs();
final File finalDestination = new File(wabtDownloadTargetFolder, new File(url.getFile()).getName());

try (ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream());
FileOutputStream fileOutputStream = new FileOutputStream(finalDestination)) {
fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
return finalDestination;
} catch (IOException e) {
throw new IllegalArgumentException("Error downloading : " + url, e);
}
Expand Down

0 comments on commit d8e9f92

Please sign in to comment.