Skip to content

Commit 7d69a46

Browse files
committed
#104 Support Idris 2
1 parent a518f66 commit 7d69a46

File tree

11 files changed

+122
-24
lines changed

11 files changed

+122
-24
lines changed

idris-jvm-ffi/src/main/idris/IdrisJvm/FFI.idr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ interface Inherits a b where {}
146146

147147
Inherits (JVM_Native t) (JVM_Native t) where {}
148148

149+
IdrisObject : Type
150+
IdrisObject = javaClass "io/github/mmhelloworld/idrisjvm/runtime/IdrisObject"
151+
149152
namespace Object
150153
ObjectClass : JVM_NativeTy
151154
ObjectClass = Class "java/lang/Object"

idris-jvm-ffi/src/main/idris/IdrisJvm/File.idr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ export
3333
FileReadError : FileError
3434
FileReadError = believe_me $ unsafePerformIO $ IOException.new "File Read Error"
3535

36+
export
37+
GenericFileError : Int -> FileError
38+
GenericFileError errNo = believe_me $ unsafePerformIO $ IOException.new $ "Generic File Error " ++ show errNo
39+
3640
export
3741
unableToOpenFile : String -> JVM_IO IOException
3842
unableToOpenFile fileName = IOException.new ("Unable to open file: " ++ fileName)
@@ -208,3 +212,9 @@ socketListenAndAccept port = do
208212
case byteBufferIoOrError of
209213
Left throwable => Left <$> Objects.toString throwable
210214
Right byteBufferIo => pure . Right $ MkFileClientServerSocket byteBufferIo
215+
216+
export
217+
fRemove : (s : String) -> JVM_IO Bool
218+
fRemove f = do
219+
path <- Files.createPath f
220+
deleteIfExists path

idris-jvm-ffi/src/main/idris/IdrisJvm/Files.idr

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ namespace Files
3939
createPath : String -> JVM_IO Path
4040
createPath = invokeStatic FilesClass "createPath" (String -> JVM_IO Path)
4141

42+
deleteIfExists : Path -> JVM_IO Bool
43+
deleteIfExists = invokeStatic FilesClass "deleteIfExists" (Path -> JVM_IO Bool)
44+

idris-jvm-ffi/src/main/idris/IdrisJvm/System.idr

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,58 @@ import Java.Util
66
import Java.Math
77
import Java.Lang
88

9-
public export
9+
%access public export
10+
1011
getArgs : JVM_IO (List String)
1112
getArgs = do
1213
argsList <- invokeStatic RuntimeClass "getProgramArgs" (JVM_IO JList)
1314
Iterator.toList !(JList.iterator argsList)
1415

15-
public export
1616
time : JVM_IO Integer
1717
time = believe_me <$> invokeStatic RuntimeClass "time" (JVM_IO BigInteger)
1818

19-
public export
2019
getEnv : String -> JVM_IO (Maybe String)
2120
getEnv = System.getenv
2221

23-
public export
2422
system : String -> JVM_IO Int
2523
system = invokeStatic RuntimeClass "runCommand" (String -> JVM_IO Int)
2624

27-
public export
2825
usleep : (i : Int) -> { auto prf : So (i >= 0 && i <= 1000000) } -> JVM_IO ()
2926
usleep interval = invokeStatic RuntimeClass "usleep" (Int -> JVM_IO ()) interval
3027

31-
public export
3228
exit : Int -> JVM_IO a
3329
exit = believe_me . Java.Lang.System.exit
30+
31+
||| Programs can either terminate successfully, or end in a caught
32+
||| failure.
33+
data ExitCode : Type where
34+
||| Terminate successfully.
35+
ExitSuccess : ExitCode
36+
||| Program terminated for some prescribed reason.
37+
|||
38+
||| @errNo A non-zero numerical value indicating failure.
39+
||| @prf Proof that the int value is non-zero.
40+
ExitFailure : (errNo : Int)
41+
-> {auto prf : So (not $ errNo == 0)}
42+
-> ExitCode
43+
44+
||| Terminate the program with an `ExitCode`. This code indicates the
45+
||| success of the program's execution, and returns the success code
46+
||| to the program's caller.
47+
|||
48+
||| @code The `ExitCode` for program.
49+
exitWith : (code : ExitCode) -> JVM_IO a
50+
exitWith ExitSuccess = exit 0
51+
exitWith (ExitFailure errNo) = exit errNo
52+
53+
||| Wall clock time
54+
record Clock where
55+
constructor MkClock
56+
seconds : Integer
57+
nanoseconds : Integer
58+
59+
||| Get the system's wall clock time.
60+
clockTime : JVM_IO Clock
61+
clockTime =
62+
believe_me <$> invokeStatic (Class "io/github/mmhelloworld/idrisjvm/runtime/IdrisSystem")
63+
"getIdrisClock" (JVM_IO IdrisObject)

idris-jvm-ffi/src/main/idris/Java/Lang.idr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ vectToArray {elemTy} xs
270270
%inline
271271
listToArray : List elemTy -> {auto jvmType: JVM_Types elemTy} -> JVM_IO (JVM_Array elemTy)
272272
listToArray {elemTy} xs
273-
= listToArray' xs
273+
= assert_total $ listToArray' xs
274274
(newArray elemTy (length xs))
275275
(setArray (JVM_Array elemTy -> Int -> elemTy -> JVM_IO ()))
276276
where

idris-jvm-integration-test/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@
8686
<artifactId>junit</artifactId>
8787
<scope>test</scope>
8888
</dependency>
89+
90+
<dependency>
91+
<groupId>org.assertj</groupId>
92+
<artifactId>assertj-core</artifactId>
93+
<scope>test</scope>
94+
</dependency>
95+
8996
</dependencies>
9097

9198
</project>

idris-jvm-integration-test/src/test/java/idrisjvm/integrationtest/IdrisJvmTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,13 @@
3333
import static java.util.Objects.requireNonNull;
3434
import static java.util.concurrent.TimeUnit.MINUTES;
3535
import static java.util.stream.Collectors.toList;
36-
import static org.hamcrest.CoreMatchers.hasItems;
37-
import static org.junit.Assert.assertThat;
36+
import static org.assertj.core.api.Assertions.assertThat;
3837

3938
@RunWith(Parameterized.class)
4039
public class IdrisJvmTest {
4140
private static final String IDRIS_JVM_HOME = Optional.ofNullable(System.getProperty("IDRIS_JVM_HOME"))
42-
.orElseGet(() -> Optional.ofNullable(System.getenv("IDRIS_JVM_HOME"))
43-
.orElseGet(() -> System.getProperty("user.home")));
41+
.orElseGet(() -> Optional.ofNullable(System.getenv("IDRIS_JVM_HOME"))
42+
.orElseGet(() -> System.getProperty("user.home")));
4443

4544
private static File testOutputRootDir;
4645
private static String runtimeJarPath;
@@ -65,7 +64,7 @@ private void printCodegenServerLog() {
6564
Path logPath = Paths.get(IDRIS_JVM_HOME, "codegen", "idris-jvm-server.log");
6665
try {
6766
Files.lines(logPath)
68-
.forEach(System.err::println);
67+
.forEach(System.err::println);
6968
} catch (IOException e) {
7069
e.printStackTrace();
7170
}
@@ -86,9 +85,9 @@ public static Collection<Object[]> data() throws IOException {
8685
final PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
8786
final Resource[] resources = resolver.getResources("idris-test-sources");
8887
return Arrays.stream(resources)
89-
.flatMap(IdrisJvmTest::getTestDirs)
90-
.map(IdrisJvmTest::getTestFiles)
91-
.collect(toList());
88+
.flatMap(IdrisJvmTest::getTestDirs)
89+
.map(IdrisJvmTest::getTestFiles)
90+
.collect(toList());
9291
}
9392

9493
@BeforeClass
@@ -116,7 +115,8 @@ public void test() throws IOException, InterruptedException {
116115
List<String> actualOutput = readFile(jvmOut);
117116
List<String> expectedOutput = readFile(expectedOutputFile);
118117

119-
assertThat(actualOutput, hasItems(expectedOutput.toArray(new String[0])));
118+
assertThat(actualOutput)
119+
.containsExactlyInAnyOrder(expectedOutput.toArray(new String[0]));
120120
}
121121

122122
private static boolean shouldStartIdrisJvmServer() {
@@ -125,7 +125,7 @@ private static boolean shouldStartIdrisJvmServer() {
125125

126126
private void compile(final File testOutputDir, final File compilerOut) throws IOException, InterruptedException {
127127
ProcessBuilder idrisCompilerProcessBuilder = new ProcessBuilder("idris", "--portable-codegen", "jvm", "-p",
128-
"idrisjvmffi", "-p", "effects", sourceFile.getPath(), "-o", testOutputDir.getPath());
128+
"idrisjvmffi", "-p", "effects", sourceFile.getPath(), "-o", testOutputDir.getPath());
129129
idrisCompilerProcessBuilder.directory(testOutputDir);
130130

131131
idrisCompilerProcessBuilder.redirectErrorStream(true);

idris-jvm-io/src/main/java/io/github/mmhelloworld/idrisjvm/io/Files.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.github.mmhelloworld.idrisjvm.io;
22

3+
import java.io.File;
34
import java.io.IOException;
45
import java.nio.file.FileSystems;
56
import java.nio.file.Path;
@@ -59,18 +60,34 @@ public static void writeFile(String pathString, String content) throws IOExcepti
5960
}
6061

6162
public static Path createPath(String pathStr) {
63+
return createPath(pathStr, workingDir);
64+
}
65+
66+
private static Path createPath(String pathStr, String workingDir) {
67+
if (pathStr.isEmpty()) {
68+
return Paths.get(workingDir);
69+
}
70+
6271
Path path = Paths.get(pathStr);
72+
final Path resolvedPath;
6373
if (path.isAbsolute()) {
64-
return path;
74+
resolvedPath = path;
6575
} else {
66-
if (pathStr.equals(".")) {
67-
return Paths.get(workingDir);
68-
} else if (pathStr.equals("..")) {
69-
return Paths.get(workingDir).getParent();
76+
if (pathStr.startsWith("../")) {
77+
resolvedPath = createPath(pathStr.substring("../".length()),
78+
Paths.get(workingDir).getParent().toString());
79+
} else if (pathStr.startsWith("..")) {
80+
resolvedPath = createPath(pathStr.substring("..".length()),
81+
Paths.get(workingDir).getParent().toString());
82+
} else if (pathStr.startsWith("./")) {
83+
resolvedPath = createPath(pathStr.substring("./".length()), workingDir);
84+
} else if (pathStr.startsWith(".")) {
85+
resolvedPath = createPath(pathStr.substring(".".length()), workingDir);
7086
} else {
71-
return Paths.get(workingDir, pathStr);
87+
resolvedPath = Paths.get(workingDir, pathStr);
7288
}
7389
}
90+
return resolvedPath;
7491
}
7592

7693
public static void createDirectory(String pathString) throws IOException {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.mmhelloworld.idrisjvm.runtime;
2+
3+
import java.math.BigInteger;
4+
5+
import static java.lang.System.currentTimeMillis;
6+
import static java.lang.System.nanoTime;
7+
import static java.util.concurrent.TimeUnit.MILLISECONDS;
8+
9+
public class IdrisSystem {
10+
public static IdrisObject getIdrisClock() {
11+
BigInteger seconds = BigInteger.valueOf(MILLISECONDS.toSeconds(currentTimeMillis()));
12+
BigInteger nanoSeconds = BigInteger.valueOf(nanoTime());
13+
return new IdrisObject(0, seconds, nanoSeconds);
14+
}
15+
}

idris-jvm-runtime/src/main/java/io/github/mmhelloworld/idrisjvm/runtime/Runtime.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.github.mmhelloworld.idrisjvm.runtime;
22

3+
import io.github.mmhelloworld.idrisjvm.io.Files;
4+
35
import java.io.BufferedReader;
6+
import java.io.File;
47
import java.io.IOException;
58
import java.io.InputStreamReader;
69
import java.io.PrintWriter;
@@ -131,7 +134,8 @@ public static BigInteger time() {
131134

132135
public static int runCommand(String command) throws IOException, InterruptedException {
133136
String[] cmdarray = parseCommand(command).toArray(new String[0]);
134-
ProcessBuilder processBuilder = new ProcessBuilder(cmdarray).inheritIO();
137+
ProcessBuilder processBuilder = new ProcessBuilder(cmdarray)
138+
.directory(new File(Files.getWorkingDir()));
135139
return processBuilder.start().waitFor();
136140
}
137141

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
3737
<maven-assembly-plugin.version>3.0.0</maven-assembly-plugin.version>
3838
<spring-boot-dependencies.version>1.5.3.RELEASE</spring-boot-dependencies.version>
39+
<assertj.version>3.12.2</assertj.version>
3940
</properties>
4041

4142
<dependencyManagement>
@@ -60,6 +61,14 @@
6061
<version>${lombok.version}</version>
6162
<scope>provided</scope>
6263
</dependency>
64+
65+
<dependency>
66+
<groupId>org.assertj</groupId>
67+
<artifactId>assertj-core</artifactId>
68+
<version>${assertj.version}</version>
69+
<scope>test</scope>
70+
</dependency>
71+
6372
</dependencies>
6473
</dependencyManagement>
6574

0 commit comments

Comments
 (0)