|
1 |
| -# Scala Native Binding generator |
| 1 | +# Scala Native Binding Generator |
2 | 2 |
|
3 | 3 | [](https://travis-ci.com/kornilova-l/scala-native-bindgen)
|
4 | 4 |
|
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. |
6 | 6 |
|
7 |
| -[Documentation](https://kornilova-l.github.io/scala-native-bindgen/) |
| 7 | +## Documentation |
8 | 8 |
|
9 |
| -## Releasing |
| 9 | +Documentation can be found at [kornilova-l.github.io/scala-native-bindgen](https://kornilova-l.github.io/scala-native-bindgen/). |
10 | 10 |
|
11 |
| -First build the `scala-native-bindgen` executable for both macOS and |
12 |
| -Linux: |
| 11 | +## Bindgen Features |
13 | 12 |
|
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. |
15 | 21 |
|
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 |
18 | 23 |
|
19 |
| -Then release version `x.y.z` by running: |
| 24 | +```c |
| 25 | +struct point { |
| 26 | + float x; |
| 27 | + float y; |
| 28 | +}; |
20 | 29 |
|
21 |
| - > sbt -Dproject.version=x.y.z release |
| 30 | +struct vector { |
| 31 | + struct point a; |
| 32 | + struct point b; |
| 33 | +}; |
22 | 34 |
|
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 | +``` |
26 | 68 |
|
27 | 69 | ## License
|
28 | 70 |
|
|
0 commit comments