mirror of
https://github.com/fluencelabs/asmble
synced 2025-06-21 18:41:34 +00:00
First Rust example for issue #9
This commit is contained in:
2
examples/rust-simple/.cargo/config
Normal file
2
examples/rust-simple/.cargo/config
Normal file
@ -0,0 +1,2 @@
|
||||
[build]
|
||||
target = "wasm32-unknown-unknown"
|
6
examples/rust-simple/Cargo.toml
Normal file
6
examples/rust-simple/Cargo.toml
Normal file
@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "rust_simple"
|
||||
version = "0.1.0"
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
24
examples/rust-simple/README.md
Normal file
24
examples/rust-simple/README.md
Normal file
@ -0,0 +1,24 @@
|
||||
### Example: Rust Simple
|
||||
|
||||
This shows a simple example of compiling Rust to WASM and then to the JVM.
|
||||
|
||||
The [root build script](../../build.gradle) actually has the build commands to build it. But basically it runs
|
||||
`cargo build --release` on this directory which compiles `add_one` from [lib.rs](src/lib.rs) into
|
||||
`target/wasm32-unknown-unknown/release/rust_simple.wasm`. Then the build script takes that wasm file and compiles it
|
||||
to `asmble.generated.RustSimple` in `build/wasm-classes`. The class is used by
|
||||
[Main.java](src/main/java/asmble/examples/rustsimple/Main.java). It is instantiated with a set of memory and then
|
||||
`add_one` is invoked with `25` to return `26`.
|
||||
|
||||
To run it yourself, run the following from the root `asmble` dir (assuming you have built the Gradle wrapper described
|
||||
in the root README's "Building and Testing" section):
|
||||
|
||||
./gradlew --no-daemon :examples:rust-simple:run
|
||||
|
||||
Yes, this does include Rust's std lib, but it's not that big of a deal. The actual method executed for `add_one` looks
|
||||
like this decompiled:
|
||||
|
||||
```java
|
||||
private int $func0(final int n) {
|
||||
return n + 1;
|
||||
}
|
||||
```
|
4
examples/rust-simple/src/lib.rs
Normal file
4
examples/rust-simple/src/lib.rs
Normal file
@ -0,0 +1,4 @@
|
||||
#[no_mangle]
|
||||
pub extern "C" fn add_one(x: i32) -> i32 {
|
||||
x + 1
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package asmble.examples.rustsimple;
|
||||
|
||||
import asmble.generated.RustSimple;
|
||||
|
||||
class Main {
|
||||
// 20 pages is good for now
|
||||
private static final int PAGE_SIZE = 65536;
|
||||
private static final int MAX_MEMORY = 20 * PAGE_SIZE;
|
||||
|
||||
public static void main(String[] args) {
|
||||
RustSimple simple = new RustSimple(MAX_MEMORY);
|
||||
System.out.println("25 + 1 = " + simple.add_one(25));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user