From 8e007ec1103a7ed463a23c1dc206abfa8e157a0d Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Thu, 6 Jul 2023 16:09:13 +0300 Subject: [PATCH] Main class (#3) * added command for running the main class Signed-off-by: Georgi Georgiev * remove unnecessary comments in tests Signed-off-by: Georgi Georgiev --------- Signed-off-by: Georgi Georgiev --- Makefile | 7 +++++++ README.md | 6 +++++- include/org_wasmer_Imports.h | 8 -------- src/java/org/wasmer/Main.java | 14 ++++++++++++++ test/org/wasmer/ModuleTest.java | 16 +++++++--------- 5 files changed, 33 insertions(+), 18 deletions(-) create mode 100644 src/java/org/wasmer/Main.java diff --git a/Makefile b/Makefile index 8e14e63..27445f6 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/README.md b/README.md index 9e76e59..1e33b36 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/include/org_wasmer_Imports.h b/include/org_wasmer_Imports.h index 4fe2120..96d1820 100644 --- a/include/org_wasmer_Imports.h +++ b/include/org_wasmer_Imports.h @@ -31,14 +31,6 @@ JNIEXPORT jlong JNICALL Java_org_wasmer_Imports_nativeImportsChain JNIEXPORT jlong JNICALL Java_org_wasmer_Imports_nativeImportsWasi (JNIEnv *, jclass, jlong); -/* - * Class: org_wasmer_Imports - * Method: nativeDrop - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_org_wasmer_Imports_nativeDrop - (JNIEnv *, jclass, jlong); - #ifdef __cplusplus } #endif diff --git a/src/java/org/wasmer/Main.java b/src/java/org/wasmer/Main.java new file mode 100644 index 0000000..bcbaa4d --- /dev/null +++ b/src/java/org/wasmer/Main.java @@ -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"); + } +} diff --git a/test/org/wasmer/ModuleTest.java b/test/org/wasmer/ModuleTest.java index 2fda0eb..aa6d0f9 100644 --- a/test/org/wasmer/ModuleTest.java +++ b/test/org/wasmer/ModuleTest.java @@ -1,11 +1,9 @@ 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; @@ -13,33 +11,33 @@ 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)); } @@ -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();