Apache ShenYu WASM SDK for java.

Clone this repo:
  1. 50597f2 update NOTICE (#14) by dragon-zhang · 11 months ago master
  2. fff25c6 1.0.0 release (#13) by dragon-zhang · 11 months ago
  3. ce37cbb fix lincenses (#12) by dragon-zhang · 11 months ago
  4. 2219a39 init shenyu-wasm-dist module (#11) by dragon-zhang · 11 months ago
  5. 68a47a5 [DOC] update docs (#9) by dragon-zhang · 12 months ago

shenyu-wasm

This repository is forked from wasmer-java, which simplifies usage by directly packing the dylib into the jar, and decompressing and loading the dylib at runtime, ready to use out of the box.

If you find anything unreasonable, please send email dev@shenyu.apache.org or zhangzicheng@apache.org.

Note: Only 64-bit CPU architecture is supported now !

If shenyu-wasm does not support your CPU architecture or system, you need to build dylib yourself, check Build dylib on my own below.

How to use this library ?

step1 add dependency

maven

<dependency>
    <groupId>org.apache.shenyu</groupId>
    <artifactId>shenyu-wasm-runtime</artifactId>
    <version>${x.y.z}</version>
</dependency>

gradle

compile "org.apache.shenyu:shenyu-wasm-runtime:${x.y.z}"

step2 write native source code

Below is a example of rust(any other language that compiles to WebAssembly can be used here):

#[no_mangle]
pub extern fn sum(x: i32, y: i32) -> i32 {
    x + y
}

step3 compile source code to a wasm file

step4 execute wasm in java

class Example {
    public static void main(String[] args) {
        // `simple.wasm` is located at `tests/resources/`.
        Path wasmPath = Paths.get(Example.class.getClassLoader().getResource("simple.wasm").getPath());
        // Reads the WebAssembly module as bytes.
        byte[] wasmBytes = Files.readAllBytes(wasmPath);
        // Instantiates the WebAssembly module.
        Instance instance = new Instance(wasmBytes);
        // Calls an exported function, and returns an object array.
        Object[] results = instance.exports.getFunction("sum").apply(5, 37);
        System.out.println(results[0]); // 42
        // Drops an instance object pointer which is stored in Rust.
        instance.close();
    }
}

Build dylib on my own

step1 install rust

step2 build dylib

cd ~
git clone https://github.com/apache/shenyu-wasm.git
cd shenyu-wasm/shenyu-wasm-build/
cargo build --release

In macos, you will get the dylib in ~/shenyu-wasm/shenyu-wasm-build/target/libshenyu_wasm.dylib;

In linux, you will get the so in ~/shenyu-wasm/shenyu-wasm-build/target/libshenyu_wasm.so;

In windows, you will get the dll in ~/shenyu-wasm/shenyu-wasm-build/target/libshenyu_wasm.dll;

step3 use dylib

Renaming the dylib we built in the previous step, with a naming format of libshenyu_wasm_$(architecture).$(os_cdylib_suffix)(e.g. libshenyu_wasm_x86_64.dylib), then put it in your own project module(the full path should like ${your_java_project_name}/${your_module_name}/src/main/resources/libshenyu_wasm_$(architecture).$(os_cdylib_suffix)).

Finally, at runtime, native-lib-loader will load it.