Skip to content

Commit 3ee2ffc

Browse files
committed
Merge pull request #157 from kornilova-l/gsoc-report
Update readme for GSoC report
2 parents 53309dc + 07266be commit 3ee2ffc

File tree

1 file changed

+56
-14
lines changed

1 file changed

+56
-14
lines changed

README.md

Lines changed: 56 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,70 @@
1-
# Scala Native Binding generator
1+
# Scala Native Binding Generator
22

33
[![Build Status](https://travis-ci.com/kornilova-l/scala-native-bindgen.svg?branch=master)](https://travis-ci.com/kornilova-l/scala-native-bindgen)
44

5-
This tool generates Scala Native bindings from C headers. It's built upon clang and Libtooling and thus respects the conventions of clang-tools.
5+
The tool generates Scala Native bindings from C headers.
66

7-
[Documentation](https://kornilova-l.github.io/scala-native-bindgen/)
7+
## Documentation
88

9-
## Releasing
9+
Documentation can be found at [kornilova-l.github.io/scala-native-bindgen](https://kornilova-l.github.io/scala-native-bindgen/).
1010

11-
First build the `scala-native-bindgen` executable for both macOS and
12-
Linux:
11+
## Bindgen Features
1312

14-
> scripts/prepare-release.sh
13+
* possibility to reuse types from existing bindings.
14+
* type casts that make recursive structs be valid Scala Native structs.
15+
* implicit classes for structs and unions that make fields access easier.
16+
* implicit classes that add setters and getters to structs with more than 22 fields (such structs in Scala
17+
Native are represented as arrays of bytes).
18+
* literal defines embedding `#define MY_CONSTANT 42``val MY_CONSTANT: native.CInt = 42`.
19+
* read-only bindings for extern variables (such variables cannot be updated due to Scala Native limitation).
20+
* declarations filtering by prefix.
1521

16-
You should now have `scala-native-bindgen-linux` and
17-
`scala-native-bindgen-darwin` if you ran the script on a macOS machine.
22+
## Example
1823

19-
Then release version `x.y.z` by running:
24+
```c
25+
struct point {
26+
float x;
27+
float y;
28+
};
2029

21-
> sbt -Dproject.version=x.y.z release
30+
struct vector {
31+
struct point a;
32+
struct point b;
33+
};
2234

23-
Finally, upload the `scala-native-bindgen-linux` and
24-
`scala-native-bindgen-darwin` executables to the release page at:
25-
<https://github.com/kornilova-l/scala-native-bindgen/releases/tag/vx.y.z>
35+
struct vector *add(struct vector *v1, struct vector *v2);
36+
```
37+
38+
```scala
39+
import scala.scalanative._
40+
import scala.scalanative.native._
41+
42+
@native.link("vector")
43+
@native.extern
44+
object vector {
45+
type struct_point = native.CStruct2[native.CFloat, native.CFloat]
46+
type struct_vector = native.CStruct2[struct_point, struct_point]
47+
def add(v1: native.Ptr[struct_vector], v2: native.Ptr[struct_vector]): native.Ptr[struct_vector] = native.extern
48+
49+
object implicits {
50+
implicit class struct_point_ops(val p: native.Ptr[struct_point]) extends AnyVal {
51+
def x: native.CFloat = !p._1
52+
def x_=(value: native.CFloat): Unit = !p._1 = value
53+
def y: native.CFloat = !p._2
54+
def y_=(value: native.CFloat): Unit = !p._2 = value
55+
}
56+
def struct_point()(implicit z: native.Zone): native.Ptr[struct_point] = native.alloc[struct_point]
57+
58+
implicit class struct_vector_ops(val p: native.Ptr[struct_vector]) extends AnyVal {
59+
def a: native.Ptr[struct_point] = p._1
60+
def a_=(value: native.Ptr[struct_point]): Unit = !p._1 = !value
61+
def b: native.Ptr[struct_point] = p._2
62+
def b_=(value: native.Ptr[struct_point]): Unit = !p._2 = !value
63+
}
64+
def struct_vector()(implicit z: native.Zone): native.Ptr[struct_vector] = native.alloc[struct_vector]
65+
}
66+
}
67+
```
2668

2769
## License
2870

0 commit comments

Comments
 (0)