From fd13315614b113c82d39e9f296356846bfb006d1 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Tue, 25 Mar 2025 17:46:40 +0200 Subject: [PATCH 1/4] added a bunch of comments and logs Signed-off-by: Georgi Georgiev --- src/java/org/wasmer/Exports.java | 10 +++++++- src/java/org/wasmer/Global.java | 2 +- src/java/org/wasmer/ImportObject.java | 29 ++++++++++++----------- src/java/org/wasmer/Imports.java | 4 +++- src/java/org/wasmer/Instance.java | 12 ++++------ src/java/org/wasmer/Module.java | 13 +++++----- src/java/org/wasmer/Native.java | 4 ++-- src/java/org/wasmer/Type.java | 2 +- src/java/org/wasmer/Util.java | 2 +- src/java/org/wasmer/exports/Function.java | 5 +--- 10 files changed, 44 insertions(+), 39 deletions(-) diff --git a/src/java/org/wasmer/Exports.java b/src/java/org/wasmer/Exports.java index 153e31d..b6129f0 100644 --- a/src/java/org/wasmer/Exports.java +++ b/src/java/org/wasmer/Exports.java @@ -6,10 +6,11 @@ import java.lang.ClassCastException; import java.util.HashMap; import java.util.Map; +import java.util.logging.Logger; /** * `Exports` is a Java class that represents the set of WebAssembly exports. - * + *

* Example: *

{@code
  * Instance instance = new Instance(wasmBytes);
@@ -22,7 +23,10 @@
  * Object[] result = ((Function) sum).apply(1, 2);
  * }
*/ +// Used in Rust +@SuppressWarnings("unused") public class Exports { + private static final Logger logger = Logger.getLogger(Exports.class.getName()); /** * Lambda expression for currying. @@ -49,6 +53,7 @@ protected Exports(Instance instance) { * @param name Name of the export to return. */ public Export get(String name) { + logger.fine("Called get with arg: " + name); return this.inner.get(name); } @@ -58,6 +63,7 @@ public Export get(String name) { * @param name Name of the exported function. */ public Function getFunction(String name) throws ClassCastException { + logger.fine("Called getFunction with arg: " + name); return (Function) this.inner.get(name); } @@ -67,6 +73,7 @@ public Function getFunction(String name) throws ClassCastException { * @param name Name of the exported memory. */ public Memory getMemory(String name) throws ClassCastException { + logger.fine("Called getMemory with arg: " + name); return (Memory) this.inner.get(name); } @@ -76,6 +83,7 @@ public Memory getMemory(String name) throws ClassCastException { * @param name Name of the exported global. */ public Global getGlobal(String name) throws ClassCastException { + logger.fine("Called getGlobal with arg: " + name); return (Global) this.inner.get(name); } diff --git a/src/java/org/wasmer/Global.java b/src/java/org/wasmer/Global.java index b34144f..7d2451c 100644 --- a/src/java/org/wasmer/Global.java +++ b/src/java/org/wasmer/Global.java @@ -2,10 +2,10 @@ import org.wasmer.exports.Export; +// Used in Rust @SuppressWarnings("unused") public class Global implements Export { private String value; - //TODO: maybe find a way to use the Type enum for this type as well private String type; private Global() { diff --git a/src/java/org/wasmer/ImportObject.java b/src/java/org/wasmer/ImportObject.java index 468491e..abda2c6 100644 --- a/src/java/org/wasmer/ImportObject.java +++ b/src/java/org/wasmer/ImportObject.java @@ -3,13 +3,18 @@ import java.util.Collections; import java.util.List; import java.util.function.Function; +import java.util.logging.Logger; import java.util.stream.Collectors; import java.util.stream.IntStream; +// The fields producing these warnings are accessed in Rust and would +// break Rust code if changed +@SuppressWarnings({"FieldCanBeLocal", "unused"}) public class ImportObject { + private static final Logger logger = Logger.getLogger(ImportObject.class.getName()); static { if (!Native.LOADED_EMBEDDED_LIBRARY) { - System.loadLibrary("wasmer_jni"); + System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT); } } @@ -17,6 +22,7 @@ public class ImportObject { private final String name; public ImportObject(String namespace, String name) { + logger.finer(String.format("Initializing import %s with namespace %s", name, namespace)); this.name = name; this.namespace = namespace; } @@ -30,20 +36,13 @@ public static class FuncImport extends ImportObject { public FuncImport(String namespace, String name, Function, List> function, List argTypes, List retTypes) { super(namespace, name); + logger.fine(String.format("Initialized function import %s with namespace %s", name, namespace)); this.function = (long[] argv) -> { - List lret = function.apply(IntStream.range(0, argTypes.size()).mapToObj((int i) -> { - switch (argTypes.get(i)) { - case I32: - return (int) argv[i]; - case I64: - return argv[i]; - case F32: - return Float.intBitsToFloat((int) argv[i]); - case F64: - return Double.longBitsToDouble(argv[i]); - default: - throw new RuntimeException("Unreachable (argument type)"); - } + List lret = function.apply(IntStream.range(0, argTypes.size()).mapToObj((int i) -> switch (argTypes.get(i)) { + case I32 -> (int) argv[i]; + case I64 -> argv[i]; + case F32 -> Float.intBitsToFloat((int) argv[i]); + case F64 -> Double.longBitsToDouble(argv[i]); }).collect(Collectors.toList())); long[] ret = argv.length >= retTypes.size() ? argv : new long[retTypes.size()]; for (int i = 0; i < retTypes.size(); i++) @@ -77,6 +76,7 @@ public static class MemoryImport extends ImportObject { public MemoryImport(String namespace, int minPages, Integer maxPages, boolean shared) { super(namespace, "memory"); + logger.fine(String.format("Initialized memory import with namespace %s", namespace)); this.minPages = minPages; this.maxPages = maxPages; this.shared = shared; @@ -84,6 +84,7 @@ public MemoryImport(String namespace, int minPages, Integer maxPages, boolean sh public MemoryImport(String namespace, int minPages, boolean shared) { super(namespace, "memory"); + logger.fine(String.format("Initialized memory import with namespace %s", namespace)); this.minPages = minPages; this.maxPages = null; this.shared = shared; diff --git a/src/java/org/wasmer/Imports.java b/src/java/org/wasmer/Imports.java index 5a457da..3e9fbf6 100644 --- a/src/java/org/wasmer/Imports.java +++ b/src/java/org/wasmer/Imports.java @@ -2,11 +2,13 @@ import java.util.List; +// Used in Rust +@SuppressWarnings("unused") public class Imports { static { if (!Native.LOADED_EMBEDDED_LIBRARY) { - System.loadLibrary("wasmer_jni"); + System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT); } } private static native long nativeImportsInstantiate(List imports, long modulePointer) throws RuntimeException; diff --git a/src/java/org/wasmer/Instance.java b/src/java/org/wasmer/Instance.java index 1dbb868..04467a9 100644 --- a/src/java/org/wasmer/Instance.java +++ b/src/java/org/wasmer/Instance.java @@ -2,21 +2,19 @@ /** * `Instance` is a Java class that represents a WebAssembly instance. - * + *

* Example: *

{@code
  * Instance instance = new Instance(wasmBytes);
  * }
*/ public class Instance { - /** - * Native bindings. - */ static { if (!Native.LOADED_EMBEDDED_LIBRARY) { - System.loadLibrary("wasmer_jni"); + System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT); } } + private native long nativeInstantiate(Instance self, byte[] moduleBytes) throws RuntimeException; private native void nativeDrop(long instancePointer); protected native Object[] nativeCallExportedFunction(long instancePointer, String exportName, Object[] arguments) throws RuntimeException; @@ -60,9 +58,9 @@ protected Instance() { */ public void close() { // To avoid duplicate native dropping - if(this.instancePointer != 0l) { + if (this.instancePointer != 0L) { this.nativeDrop(this.instancePointer); - this.instancePointer = 0l; + this.instancePointer = 0L; } } diff --git a/src/java/org/wasmer/Module.java b/src/java/org/wasmer/Module.java index 84f2f15..6e113f3 100644 --- a/src/java/org/wasmer/Module.java +++ b/src/java/org/wasmer/Module.java @@ -2,7 +2,7 @@ /** * `Module` is a Java class that represents a WebAssembly module. - * + *

* Example: *

{@code
  * boolean isValid = Module.validate(wasmBytes);
@@ -11,15 +11,15 @@
  * Instance instance = module.instantiate();
  * }
*/ +// Used in Rust +@SuppressWarnings("unused") public class Module { - /** - * Native bindings. - */ static { if (!Native.LOADED_EMBEDDED_LIBRARY) { - System.loadLibrary("wasmer_jni"); + System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT); } } + private native long nativeModuleInstantiate(Module self, byte[] moduleBytes) throws RuntimeException; private native void nativeDrop(long modulePointer); private native long nativeInstantiate(long modulePointer, Instance instance, long importsPointer); @@ -104,8 +104,7 @@ public byte[] serialize() { */ public static Module deserialize(byte[] serializedBytes) { Module module = new Module(); - long modulePointer = Module.nativeDeserialize(module, serializedBytes); - module.modulePointer = modulePointer; + module.modulePointer = Module.nativeDeserialize(module, serializedBytes); return module; } } diff --git a/src/java/org/wasmer/Native.java b/src/java/org/wasmer/Native.java index c4a9397..cb0cf33 100644 --- a/src/java/org/wasmer/Native.java +++ b/src/java/org/wasmer/Native.java @@ -19,6 +19,7 @@ public class Native { public static final boolean LOADED_EMBEDDED_LIBRARY; + public static final String DYNAMIC_LIBRARY_NAME_SHORT = "wasmer_jni"; private static final Logger logger = Logger.getLogger(Native.class.getName()); static { @@ -84,7 +85,7 @@ private static boolean loadEmbeddedLibrary() { if (nativeLibraryUrl != null) { // native library found within JAR, extract and load try { - final File libfile = File.createTempFile("wasmer_jni", ".lib"); + final File libfile = File.createTempFile(DYNAMIC_LIBRARY_NAME_SHORT, ".lib"); libfile.deleteOnExit(); // just in case final InputStream in = nativeLibraryUrl.openStream(); @@ -105,7 +106,6 @@ private static boolean loadEmbeddedLibrary() { } catch (IOException x) { logger.log(Level.SEVERE, "Failed to load native library", x); } - } return usingEmbedded; diff --git a/src/java/org/wasmer/Type.java b/src/java/org/wasmer/Type.java index 2eb9c0c..32d21d1 100644 --- a/src/java/org/wasmer/Type.java +++ b/src/java/org/wasmer/Type.java @@ -6,7 +6,7 @@ public enum Type { F32((byte)3), F64((byte)4); - byte i; + final byte i; Type(byte i) { this.i = i; diff --git a/src/java/org/wasmer/Util.java b/src/java/org/wasmer/Util.java index b8d80cf..4cec267 100644 --- a/src/java/org/wasmer/Util.java +++ b/src/java/org/wasmer/Util.java @@ -3,7 +3,7 @@ public class Util { static { if (!Native.LOADED_EMBEDDED_LIBRARY) { - System.loadLibrary("wasmer_jni"); + System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT); } } diff --git a/src/java/org/wasmer/exports/Function.java b/src/java/org/wasmer/exports/Function.java index 2c6ff00..599886c 100644 --- a/src/java/org/wasmer/exports/Function.java +++ b/src/java/org/wasmer/exports/Function.java @@ -1,17 +1,14 @@ package org.wasmer.exports; -import org.wasmer.exports.Export; - /** * Functional interface for WebAssembly exported functions, i.e. it * creates a new type for a closure that mimics a WebAssembly exported * function. - * + *

* The apply method takes an arbitrary number of arguments and returns * an output. */ @FunctionalInterface public interface Function extends Export { - @SuppressWarnings("unchecked") Object[] apply(Object... inputs); } From 2229000821fdf226b2d03307c87c38a8c0887d1b Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Wed, 26 Mar 2025 12:22:39 +0200 Subject: [PATCH 2/4] update actions cache Signed-off-by: Georgi Georgiev --- .github/workflows/build_publish.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build_publish.yaml b/.github/workflows/build_publish.yaml index db2f1d0..fb993cd 100644 --- a/.github/workflows/build_publish.yaml +++ b/.github/workflows/build_publish.yaml @@ -64,19 +64,19 @@ jobs: override: true - name: Cache Cargo registry - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.cargo/registry key: cargo-registry-${{ hashFiles('**/Cargo.lock') }} - name: Cache Cargo bin - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: ~/.cargo/bin key: cargo-bin-${{ hashFiles('**/Cargo.lock') }} - name: Cache Cargo build - uses: actions/cache@v1 + uses: actions/cache@v3 with: path: target key: cargo-build-target-${{ hashFiles('**/Cargo.lock') }} @@ -131,4 +131,4 @@ jobs: upload_url: ${{ steps.get_release_info.outputs.upload_url }} asset_path: ${{ steps.create_jar.outputs.path }} asset_name: ${{ steps.create_jar.outputs.name }} - asset_content_type: application/java-archive \ No newline at end of file + asset_content_type: application/java-archive From 276a7f794c4e7cb67660fa92e083f5379c980c8d Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Wed, 26 Mar 2025 13:37:16 +0200 Subject: [PATCH 3/4] incremented patch version Signed-off-by: Georgi Georgiev --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- build.gradle | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index aca42b1..bb9595b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1157,7 +1157,7 @@ dependencies = [ [[package]] name = "wasmer-jni" -version = "1.1.0" +version = "1.1.1" dependencies = [ "jni", "wasmer", diff --git a/Cargo.toml b/Cargo.toml index 07113d2..7cbbedd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] publish = false name = "wasmer-jni" -version = "1.1.0" +version = "1.1.1" edition = "2021" [lib] diff --git a/build.gradle b/build.gradle index 1f4f40e..c4b4469 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { allprojects { group "org.wasmer" - version "1.1.0" + version "1.1.1" } // This is needed for the Java plugin to make sure From 63ea2c7241e25b8ff845a7352704745a34815821 Mon Sep 17 00:00:00 2001 From: Georgi Georgiev Date: Wed, 26 Mar 2025 13:46:06 +0200 Subject: [PATCH 4/4] changed readme as well Signed-off-by: Georgi Georgiev --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b543fd7..0d05d7c 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ Wasmer JNI as a dependency, write for instance: ```gradle dependencies { - implementation "org.wasmer:wasmer-jni:1.1.0" + implementation "org.wasmer:wasmer-jni:1.1.1" } ``` @@ -302,7 +302,7 @@ To build the JAR package: $ make package ``` -This will generate the file `build/libs/wasmer-jni-1.1.0.jar`. +This will generate the file `build/libs/wasmer-jni-1.1.1.jar`. ### Native `wasmer-jni` inclusion flow.