Skip to content

Commit

Permalink
Main class (#3)
Browse files Browse the repository at this point in the history
* added command for running the main class

Signed-off-by: Georgi Georgiev <[email protected]>

* remove unnecessary comments in tests

Signed-off-by: Georgi Georgiev <[email protected]>

---------

Signed-off-by: Georgi Georgiev <[email protected]>
  • Loading branch information
georg-getz authored Jul 6, 2023
1 parent 91600ed commit 8e007ec
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ run-example:
javac -classpath "../${JAR}" ${EXAMPLE}Example.java; \
java -Djava.library.path=$(CURDIR)/artifacts/$(build_os)-$(build_arch) -classpath ".:../${JAR}" -enableassertions ${EXAMPLE}Example

# Runs the main class, cd to java is necessary because java.org.wasmer is a "Prohibited package name"
run-main:
$(eval JAR := $(shell find ./build/libs/ -name "wasmer-jni-*.jar"))
@cd src; \
javac -sourcepath ./java -classpath "./${JAR}" java/org/wasmer/Main.java; \
cd java && java -Djava.library.path=$(CURDIR)/artifacts/$(build_os)-$(build_arch) -classpath ".:../${JAR}" org/wasmer/Main; \
find org -type f -name "*.class" -delete
# Clean
clean:
cargo clean
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ releases page](https://github.com/wasmerio/wasmer-java/releases)!
architecture, see [the Development Section](#development) to learn
more.

# Main

To compile and execute the main class located at `src/java/org/wasmer/Main.java` run the following command: `make run-main`

# Example

There is a toy program in `java/src/test/resources/simple.rs`, written
There is a toy program in `test/resources/simple.rs`, written
in Rust (or any other language that compiles to WebAssembly):

```rust
Expand Down
8 changes: 0 additions & 8 deletions include/org_wasmer_Imports.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions src/java/org/wasmer/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.wasmer;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Main {
public static void main(String[] args) throws IOException {
//Purely for demonstration purposes, delete all of it when writing actual code
byte[] bytes = Files.readAllBytes(Paths.get(System.getProperty("user.dir"), "../../examples/runtime.wasm"));
Module module = new Module(bytes);
System.out.println("Success");
}
}
16 changes: 7 additions & 9 deletions test/org/wasmer/ModuleTest.java
Original file line number Diff line number Diff line change
@@ -1,45 +1,43 @@
package org.wasmer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.lang.RuntimeException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

class ModuleTest {
private byte[] getBytes(String filename) throws IOException,Exception {
private byte[] getBytes(String filename) throws Exception {
Path modulePath = Paths.get(getClass().getClassLoader().getResource(filename).toURI());
return Files.readAllBytes(modulePath);
}

@Test
void validate() throws IOException,Exception {
void validate() throws Exception {
assertTrue(Module.validate(getBytes("tests.wasm")));
}

@Test
void invalidate() throws IOException,Exception {
void invalidate() throws Exception {
assertFalse(Module.validate(getBytes("invalid.wasm")));
}

@Test
void compile() throws IOException,Exception {
void compile() throws Exception {
assertTrue(new Module(getBytes("tests.wasm")) instanceof Module);
}

@Test
void failedToCompile() throws IOException,Exception {
void failedToCompile() {
Exception exception = Assertions.assertThrows(RuntimeException.class, () -> {
Module module = new Module(getBytes("invalid.wasm"));
});

String expected = "Failed to compile the module: Validation error: invalid leading byte in type definition";
String expected = "Failed to compile the module: Validate(\"invalid leading byte in type definition";
assertTrue(exception.getMessage().startsWith(expected));
}

Expand All @@ -55,7 +53,7 @@ void failedToCompile() throws IOException,Exception {
// }

@Test
void serialize() throws IOException,Exception {
void serialize() throws Exception {
Module module = new Module(getBytes("tests.wasm"));
assertTrue(module.serialize() instanceof byte[]);
module.close();
Expand Down

0 comments on commit 8e007ec

Please sign in to comment.