Skip to content

Commit

Permalink
Added ability to call Rust code from Scala
Browse files Browse the repository at this point in the history
  • Loading branch information
Martomate committed Dec 26, 2024
1 parent 1707b3b commit bc42f8a
Show file tree
Hide file tree
Showing 9 changed files with 378 additions and 1 deletion.
13 changes: 12 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@ val commonSettings: scala.Seq[Def.Setting[?]] = Defaults.coreDefaultSettings ++

lazy val hexacraft = project
.in(file("."))
.aggregate(common, nbt, window, audio, fs, gpu, system, game, client, server, main)
.aggregate(native, common, nbt, window, audio, fs, gpu, system, game, client, server, main)

lazy val native = project
.in(file("native"))
.settings(commonSettings*)
.settings(libraryDependencies += MUnit)
.settings(
sbtJniCoreScope := Compile,
javah / target := (nativeCompile / sourceDirectory).value / "jni" // just for reference
)
.enablePlugins(JniNative)

lazy val common = project
.in(file("common"))
.dependsOn(native % Runtime)
.settings(commonSettings*)
.settings(
libraryDependencies ++= Seq(Joml) :+ MUnit
Expand Down
7 changes: 7 additions & 0 deletions native/src/main/scala/hexacraft/rs/RustLib.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package hexacraft.rs

import com.github.sbt.jni.syntax.NativeLoader

object RustLib extends NativeLoader("hexacraft_rs") {
@native def hello(): String
}
294 changes: 294 additions & 0 deletions native/src/native/Cargo.lock

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

10 changes: 10 additions & 0 deletions native/src/native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "hexacraft-rs"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
jni = "0.21.1"
12 changes: 12 additions & 0 deletions native/src/native/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## JNI wrapper (calling Rust from Scala)

This folder contains the glue code needed for calling Rust code from Scala. The code is compiled into a dynamic library which is then shipped as a single file inside the JAR file of this sbt project.

### Header files

The header files in the `jni` folder can be generated using the `sbt` command `javah`, and are only there to show what the API should look like. Since the header files are checked in to git it should be easy to see if something has changed since the last commit.

### Useful sbt commands

- `native / test`
- `javah`
21 changes: 21 additions & 0 deletions native/src/native/jni/hexacraft_rs_RustLib_00024.h

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

12 changes: 12 additions & 0 deletions native/src/native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use jni::objects::JObject;
use jni::sys::jstring;
use jni::JNIEnv;

#[no_mangle]
pub extern "system" fn Java_hexacraft_rs_RustLib_00024_hello<'local>(
env: JNIEnv<'local>,
_object: JObject<'local>,
) -> jstring {
let s = env.new_string("Hello from Rust!").unwrap();
s.into_raw()
}
9 changes: 9 additions & 0 deletions native/src/test/scala/hexacraft/rs/RustLibTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package hexacraft.rs

import munit.FunSuite

class RustLibTest extends FunSuite {
test("hello") {
assertEquals(RustLib.hello(), "Hello from Rust!")
}
}
1 change: 1 addition & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.1.0")
addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.3.11")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.5.2")
addSbtPlugin("org.xerial.sbt" % "sbt-pack" % "0.20")
addSbtPlugin("com.github.sbt" % "sbt-jni" % "1.7.1")

0 comments on commit bc42f8a

Please sign in to comment.