-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
14 changed files
with
436 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
src/main/java/com/semantalytics/stardog/kibble/wasm/echo/Cargo.toml
This file was deleted.
Oops, something went wrong.
4 changes: 0 additions & 4 deletions
4
src/main/java/com/semantalytics/stardog/kibble/wasm/echo/src/echo.rs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package org.wasmer; | ||
|
||
import org.wasmer.exports.Export; | ||
import org.wasmer.exports.Function; | ||
|
||
import java.lang.ClassCastException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* `Exports` is a Java class that represents the set of WebAssembly exports. | ||
* | ||
* Example: | ||
* <pre>{@code | ||
* Instance instance = new Instance(wasmBytes); | ||
* | ||
* // Get and run an exported function. | ||
* Object[] result = instance.exports.getFunction("sum").apply(1, 2); | ||
* | ||
* // Get, manually downcast, and run an exported function. | ||
* Export sum = instance.exports.get("sum"); | ||
* Object[] result = ((Function) sum).apply(1, 2); | ||
* }</pre> | ||
*/ | ||
public class Exports { | ||
private Map<String, Export> inner; | ||
private Instance instance; | ||
|
||
/** | ||
* The constructor instantiates new exported functions. | ||
* | ||
* @param instance Instance object which holds the exports object. | ||
*/ | ||
protected Exports(Instance instance) { | ||
this.inner = new HashMap<String, Export>(); | ||
this.instance = instance; | ||
} | ||
|
||
/** | ||
* Return the export with the name `name`. | ||
* | ||
* @param name Name of the export to return. | ||
*/ | ||
public Export get(String name) { | ||
return this.inner.get(name); | ||
} | ||
|
||
/** | ||
* Return the export with the name `name` as an exported function. | ||
* | ||
* @param name Name of the exported function. | ||
*/ | ||
public Function getFunction(String name) throws ClassCastException { | ||
return (Function) this.inner.get(name); | ||
} | ||
|
||
/** | ||
* Return the export with the name `name` as an exported memory. | ||
* | ||
* @param name Name of the exported memory. | ||
*/ | ||
public Memory getMemory(String name) throws ClassCastException { | ||
return (Memory) this.inner.get(name); | ||
} | ||
|
||
/** | ||
* Called by Rust to add a new exported function. | ||
*/ | ||
private void addFunction(String name) { | ||
this.inner.put(name, this.generateFunctionWrapper(name)); | ||
} | ||
|
||
/** | ||
* Called by Rust to add a new exported memory. | ||
*/ | ||
private void addMemory(String name, Memory memory) { | ||
this.inner.put(name, memory); | ||
} | ||
|
||
/** | ||
* Lambda expression for currying. | ||
* This takes a function name and returns the function to call WebAssembly function. | ||
*/ | ||
private java.util.function.Function<String, Function> functionWrapperGenerator = | ||
functionName -> arguments -> this.instance.nativeCallExportedFunction(this.instance.instancePointer, functionName, arguments); | ||
|
||
/** | ||
* Generate the exported function wrapper. | ||
*/ | ||
private Function generateFunctionWrapper(String functionName) { | ||
return this.functionWrapperGenerator.apply(functionName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package org.wasmer; | ||
|
||
import org.scijava.nativelib.NativeLoader; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* `Instance` is a Java class that represents a WebAssembly instance. | ||
* | ||
* Example: | ||
* <pre>{@code | ||
* Instance instance = new Instance(wasmBytes); | ||
* }</pre> | ||
*/ | ||
public class Instance { | ||
/** | ||
* Native bindings. | ||
*/ | ||
static { | ||
try { | ||
NativeLoader.loadLibrary("wasmer_jni"); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
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; | ||
protected static native void nativeInitializeExportedFunctions(long instancePointer); | ||
protected static native void nativeInitializeExportedMemories(long instancePointer); | ||
|
||
/** | ||
* All WebAssembly exports. | ||
*/ | ||
public final Exports exports; | ||
|
||
/** | ||
The instance pointer. | ||
*/ | ||
protected long instancePointer; | ||
|
||
/** | ||
* The constructor instantiates a new WebAssembly instance based on | ||
* WebAssembly bytes. | ||
* | ||
* @param moduleBytes WebAssembly bytes. | ||
*/ | ||
public Instance(byte[] moduleBytes) throws RuntimeException { | ||
this.exports = new Exports(this); | ||
|
||
long instancePointer = this.nativeInstantiate(this, moduleBytes); | ||
this.instancePointer = instancePointer; | ||
|
||
this.nativeInitializeExportedFunctions(instancePointer); | ||
this.nativeInitializeExportedMemories(instancePointer); | ||
} | ||
|
||
protected Instance() { | ||
this.exports = new Exports(this); | ||
} | ||
|
||
/** | ||
* Delete an instance object pointer. | ||
*/ | ||
public void close() { | ||
this.nativeDrop(this.instancePointer); | ||
} | ||
|
||
/** | ||
* Delete an instance object pointer, which is called by the garbage collector | ||
* before an object is removed from the memory. | ||
*/ | ||
public void finalize() { | ||
this.close(); | ||
} | ||
} |
Oops, something went wrong.