Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/build_publish.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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') }}
Expand Down Expand Up @@ -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
asset_content_type: application/java-archive
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
publish = false
name = "wasmer-jni"
version = "1.1.0"
version = "1.1.1"
edition = "2021"

[lib]
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```

Expand Down Expand Up @@ -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.

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/java/org/wasmer/Exports.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
* <p>
* Example:
* <pre>{@code
* Instance instance = new Instance(wasmBytes);
Expand All @@ -22,7 +23,10 @@
* Object[] result = ((Function) sum).apply(1, 2);
* }</pre>
*/
// Used in Rust
@SuppressWarnings("unused")
public class Exports {
private static final Logger logger = Logger.getLogger(Exports.class.getName());

/**
* Lambda expression for currying.
Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/java/org/wasmer/Global.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
29 changes: 15 additions & 14 deletions src/java/org/wasmer/ImportObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,26 @@
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);
}
}

private final String namespace;
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;
}
Expand All @@ -30,20 +36,13 @@ public static class FuncImport extends ImportObject {

public FuncImport(String namespace, String name, Function<List<Number>, List<Number>> function, List<Type> argTypes, List<Type> retTypes) {
super(namespace, name);
logger.fine(String.format("Initialized function import %s with namespace %s", name, namespace));
this.function = (long[] argv) -> {
List<Number> 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<Number> 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++)
Expand Down Expand Up @@ -77,13 +76,15 @@ 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;
}

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;
Expand Down
4 changes: 3 additions & 1 deletion src/java/org/wasmer/Imports.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<ImportObject> imports, long modulePointer) throws RuntimeException;
Expand Down
12 changes: 5 additions & 7 deletions src/java/org/wasmer/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

/**
* `Instance` is a Java class that represents a WebAssembly instance.
*
* <p>
* Example:
* <pre>{@code
* Instance instance = new Instance(wasmBytes);
* }</pre>
*/
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;
Expand Down Expand Up @@ -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;
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/java/org/wasmer/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* `Module` is a Java class that represents a WebAssembly module.
*
* <p>
* Example:
* <pre>{@code
* boolean isValid = Module.validate(wasmBytes);
Expand All @@ -11,15 +11,15 @@
* Instance instance = module.instantiate();
* }</pre>
*/
// 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);
Expand Down Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/java/org/wasmer/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand All @@ -105,7 +106,6 @@ private static boolean loadEmbeddedLibrary() {
} catch (IOException x) {
logger.log(Level.SEVERE, "Failed to load native library", x);
}

}

return usingEmbedded;
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/wasmer/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public enum Type {
F32((byte)3),
F64((byte)4);

byte i;
final byte i;

Type(byte i) {
this.i = i;
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/wasmer/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Util {
static {
if (!Native.LOADED_EMBEDDED_LIBRARY) {
System.loadLibrary("wasmer_jni");
System.loadLibrary(Native.DYNAMIC_LIBRARY_NAME_SHORT);
}
}

Expand Down
5 changes: 1 addition & 4 deletions src/java/org/wasmer/exports/Function.java
Original file line number Diff line number Diff line change
@@ -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.
*
* <p>
* 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);
}
Loading