Skip to content

Commit c7916c2

Browse files
committed
Added SBT build and a piece of test code
1 parent f22e47b commit c7916c2

File tree

10 files changed

+119
-76
lines changed

10 files changed

+119
-76
lines changed

source/ports/scala_port/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
target/
2+
.bloop/
3+
.vscode/
4+
.bsp/
5+
.metals/
6+
*.log
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
version = "2.7.4"

source/ports/scala_port/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Metacall Scala Port
2+
3+
## Setup
4+
5+
To set up Scala & SBT, use [Coursier](https://get-coursier.io/docs/cli-installation). After getting the `cs` executable, run `cs setup` and follow the prompt.
6+
7+
## Testing
8+
9+
To run the tests, run `sbt test` in this README's directory.
10+
11+
Don't forget to set these environment variables:
12+
```
13+
LD_LIBRARY_PATH
14+
LOADER_SCRIPT_PATH
15+
LOADER_LIBRARY_PATH
16+
CONFIGURATION_PATH
17+
SERIAL_LIBRARY_PATH
18+
DETOUR_LIBRARY_PATH
19+
PORT_LIBRARY_PATH
20+
```
21+
22+
> Note: You'll find the bindings and the code that runs on `sbt test` in `src/main/scala/Metacall.scala`.

source/ports/scala_port/metacall.scala

Lines changed: 0 additions & 48 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// DO NOT EDIT! This file is auto-generated.
2+
// This file enables sbt-bloop to create bloop config files.
3+
4+
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.6-15-209c2a5c")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// DO NOT EDIT! This file is auto-generated.
2+
// This file enables sbt-bloop to create bloop config files.
3+
4+
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.6-15-209c2a5c")
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package metacall
2+
3+
import com.sun.jna._
4+
5+
trait MetacallBindings extends Library {
6+
def metacall_load_from_file(
7+
runtime: String,
8+
paths: Array[String],
9+
size: Int,
10+
handle: Array[Pointer]
11+
): Int
12+
13+
def metacallv_s(
14+
functionName: String,
15+
args: Array[Pointer],
16+
argsSize: Int
17+
): Pointer
18+
19+
def metacall_initialize(): Int
20+
21+
def metacall_value_create_string(str: String, byteSize: Int): Pointer
22+
23+
def metacall_value_create_int(i: Int): Pointer
24+
25+
def metacall_destroy(value: Pointer): Int
26+
}
27+
28+
object Metacall {
29+
// CAUTION: Always check pointers passed to metacall (should not be Java null)
30+
31+
val mc = Native.load("metacall", classOf[MetacallBindings])
32+
import mc._
33+
34+
metacall_initialize()
35+
36+
println(
37+
metacall_load_from_file(
38+
"node",
39+
Array("./src/test/scala/main.js"),
40+
1,
41+
Array.empty
42+
)
43+
) // outputs 1
44+
45+
// Call hello
46+
val arg = metacall_value_create_string("Jack", "Jack".getBytes().length)
47+
println("ARG: " + arg.getString(0)) // works!
48+
val r = metacallv_s("hello", Array(arg), 1)
49+
println("R1: " + r) // does not work...
50+
metacall_destroy(arg) // works!
51+
52+
// Call increment
53+
val n = metacall_value_create_int(50)
54+
println("N: " + n.getInt(0)) // works!
55+
val r2 = metacallv_s("increment", Array(n), 1)
56+
println("R2: " + r2) // does not work...
57+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package metacall
2+
3+
import org.scalatest.flatspec.AnyFlatSpec
4+
5+
class MetacallSpec extends AnyFlatSpec {
6+
"Metacall" should "work" in {
7+
Metacall
8+
}
9+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function hello(name) {
2+
console.log('Argument: ', name)
3+
console.log(name)
4+
if (name) return `Hello, ${name}!`
5+
else return "Hello!"
6+
}
7+
8+
function increment(n) {
9+
return n + 1
10+
}
11+
12+
function env() {
13+
console.log(process.env);
14+
}
15+
16+
module.exports = { hello, env, increment }

source/ports/scala_port/test.scala

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)