aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/zwobble/couscous/tests/backends/csharp/CsharpMethodRunner.java
blob: 68b053def96695fb05a74fa702bcdb9ad3cdae93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package org.zwobble.couscous.tests.backends.csharp;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterators;
import com.google.common.collect.Ordering;
import org.hamcrest.Matchers;
import org.zwobble.couscous.Backend;
import org.zwobble.couscous.ast.LiteralNode;
import org.zwobble.couscous.ast.MethodSignature;
import org.zwobble.couscous.ast.TypeNode;
import org.zwobble.couscous.backends.csharp.CsharpCodeGenerator;
import org.zwobble.couscous.backends.csharp.CsharpSerializer;
import org.zwobble.couscous.tests.MethodRunner;
import org.zwobble.couscous.tests.util.processes.ExecutionResult;
import org.zwobble.couscous.tests.util.processes.Processes;
import org.zwobble.couscous.types.ScalarType;
import org.zwobble.couscous.types.Type;
import org.zwobble.couscous.types.Types;
import org.zwobble.couscous.util.ExtraLists;
import org.zwobble.couscous.values.PrimitiveValue;
import org.zwobble.couscous.values.PrimitiveValues;

import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.List;

import static java.lang.Integer.parseInt;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.zwobble.couscous.ast.MethodCallNode.staticMethodCall;
import static org.zwobble.couscous.tests.util.ExtraFiles.deleteRecursively;
import static org.zwobble.couscous.util.ExtraLists.eagerMap;
import static org.zwobble.couscous.util.ExtraLists.list;
import static org.zwobble.couscous.values.PrimitiveValues.value;

public class CsharpMethodRunner implements MethodRunner {
    static final String NAMESPACE = "ExampleProject.Couscous";

    @Override
    public PrimitiveValue runMethod(List<TypeNode> classNodes, ScalarType className, String methodName, List<PrimitiveValue> arguments, Type returnType) {
        try {
            Path directoryPath = Files.createTempDirectory(null);
            Backend compiler = new CsharpTestBackend(directoryPath, NAMESPACE);
            try {
                compiler.compile(classNodes);
                return runFunction(directoryPath, className, methodName, arguments, returnType);
            } finally {
                deleteRecursively(directoryPath.toFile());
            }
        } catch (final java.lang.Throwable $ex) {
            throw new RuntimeException($ex);
        }
    }

    public static PrimitiveValue runFunction(
        Path workingDirectory,
        ScalarType className,
        String methodName,
        List<PrimitiveValue> arguments,
        Type returnType)
        throws IOException, InterruptedException
    {
        MethodSignature signature = new MethodSignature(
            methodName,
            list(),
            eagerMap(arguments, argument -> argument.getType()),
            returnType);
        String csharpMethodName = CsharpCodeGenerator.NAMING.methodName(signature);
        String value = CsharpSerializer.serialize(
            staticMethodCall(
                ScalarType.topLevel(NAMESPACE + "." + className.getQualifiedName()),
                csharpMethodName,
                eagerMap(arguments, LiteralNode::literal),
                returnType));

        boolean isVoid = returnType.equals(Types.VOID);
        String body = isVoid
            ? value + ";"
            : "var value = " + value + ";" +
                "       System.Console.WriteLine(value.GetType());" +
                "       System.Console.WriteLine(value);";

        String program =
            "public class MethodRunnerExample { " +
            "   public static void Main() { " +
            "       " + body +
            "   }" +
            "}";
        Files.write(workingDirectory.resolve("MethodRunnerExample.cs"), list(program));
        String output = executeSource(
            findCsharpFiles(workingDirectory),
            workingDirectory
        );
        if (isVoid) {
            return PrimitiveValues.UNIT;
        } else {
            return readPrimitive(output);
        }
    }

    private static String executeSource(List<String> paths, Path workingDirectory) throws IOException, InterruptedException {
        // TODO: find cache directory properly
        Path cacheDirectory = Paths.get(System.getProperty("user.home"), ".cache/couscous/tests/csharp");
        String hash = hashFiles(paths);
        Path cacheResult = cacheDirectory.resolve(hash);
        if (!cacheResult.toFile().exists()) {
            cacheDirectory.toFile().mkdirs();
            String executablePath = compileFiles(paths, workingDirectory);
            ExecutionResult result = Processes.run(list("mono", executablePath));
            writeExecuteResult(cacheResult, result);
        }
        ExecutionResult result = readExecutionResult(cacheResult);
        result.assertSuccess();
        return result.getStdout();
    }

    private static ExecutionResult readExecutionResult(Path path) throws IOException {
        JsonObject result = Json.parse(new FileReader(path.toFile())).asObject();
        return new ExecutionResult(
            result.get("exitCode").asInt(),
            result.get("stdout").asString(),
            result.get("stderr").asString()
        );
    }

    private static void writeExecuteResult(Path path, ExecutionResult result) throws IOException {
        JsonObject json = Json.object()
            .add("exitCode", result.getExitCode())
            .add("stdout", result.getStdout())
            .add("stderr", result.getStderr());
        Files.write(path, list(json.toString()));
    }

    private static String compileFiles(List<String> paths, Path workingDirectory) throws IOException, InterruptedException {
        Path executablePath = workingDirectory.resolve("Program.cs");
        ExecutionResult result = Processes.run(
            ExtraLists.concat(
                list("mcs", "-out:" + executablePath),
                paths
            ),
            workingDirectory
        );
        result.assertSuccess();
        return executablePath.toString();
    }

    private static String hashFiles(List<String> paths) throws IOException {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            for (String path : Ordering.natural().immutableSortedCopy(paths)) {
                byte[] bytes = Files.readAllBytes(Paths.get(path));
                digest.update(bytes);
            }
            return bytesToHex(digest.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    private static String bytesToHex(byte[] hash) {
        Formatter formatter = new Formatter();
        for (byte b : hash) {
            formatter.format("%02x", b);
        }
        return formatter.toString();
    }

    private static List<String> findCsharpFiles(Path directoryPath) throws IOException {
        return ImmutableList.copyOf(
            Iterators.transform(
                Iterators.filter(
                    Files.newDirectoryStream(directoryPath).iterator(),
                    path -> path.toFile().isFile() && path.toString().endsWith(".cs")),
                path -> path.toString()));
    }

    private static final String NAMESPACE_PREFIX = NAMESPACE + ".";

    private static PrimitiveValue readPrimitive(String output) {
        String[] lines = output.split("[\\r\\n]+");
        String type = lines[0];
        String value = lines[1];
        switch (type) {
            case "System.Int32":
                return value(parseInt(value));
            case "System.String":
                return value(value);
            case "System.Boolean":
                return value(value.equals("True"));
            case "System.MonoType":
            case "System.RuntimeType":
                if (value.equals("System.String")) {
                    return value(Types.STRING);
                } else {
                    assertThat(value, Matchers.startsWith(NAMESPACE_PREFIX));
                    return value(ScalarType.topLevel(value.substring(NAMESPACE_PREFIX.length())));
                }
            default:
                throw new RuntimeException("Unhandled type: " + type);
        }
    }
}