File tree Expand file tree Collapse file tree 4 files changed +24
-18
lines changed
src/test/scala/org/scalanative/bindgen/samples Expand file tree Collapse file tree 4 files changed +24
-18
lines changed Original file line number Diff line number Diff line change 1
1
#include "Union.h"
2
2
#include <stdlib.h>
3
3
4
- union values * getValues () {
5
- union values * myValues = malloc (sizeof (union values ));
6
- myValues -> a = 10 ;
7
- return myValues ;
8
- }
4
+ void setIntValue (union values * v ) { v -> i = 10 ; }
5
+
6
+ void setLongValue (union values * v ) { v -> l = 10000000000 ; }
Original file line number Diff line number Diff line change 1
1
union values {
2
- long a ;
3
- int b ;
4
- long long c ;
2
+ long l ;
3
+ int i ;
4
+ long long ll ;
5
5
};
6
6
7
- union values * getValues ();
7
+ void setIntValue (union values * v );
8
+
9
+ void setLongValue (union values * v );
Original file line number Diff line number Diff line change @@ -7,19 +7,20 @@ import scala.scalanative.native._
7
7
@ native.extern
8
8
object Union {
9
9
type union_values = native.CArray [Byte , native.Nat ._8]
10
- def getValues (): native.Ptr [union_values] = native.extern
10
+ def setIntValue (v : native.Ptr [union_values]): Unit = native.extern
11
+ def setLongValue (v : native.Ptr [union_values]): Unit = native.extern
11
12
}
12
13
13
14
import Union ._
14
15
15
16
object UnionHelpers {
16
17
17
18
implicit class union_values_pos (val p : native.Ptr [union_values]) extends AnyVal {
18
- def a : native.Ptr [native.CLong ] = p.cast[native.Ptr [native.CLong ]]
19
- def a_ = (value : native.CLong ): Unit = ! p.cast[native.Ptr [native.CLong ]] = value
20
- def b : native.Ptr [native.CInt ] = p.cast[native.Ptr [native.CInt ]]
21
- def b_ = (value : native.CInt ): Unit = ! p.cast[native.Ptr [native.CInt ]] = value
22
- def c : native.Ptr [native.CLongLong ] = p.cast[native.Ptr [native.CLongLong ]]
23
- def c_ = (value : native.CLongLong ): Unit = ! p.cast[native.Ptr [native.CLongLong ]] = value
19
+ def l : native.Ptr [native.CLong ] = p.cast[native.Ptr [native.CLong ]]
20
+ def l_ = (value : native.CLong ): Unit = ! p.cast[native.Ptr [native.CLong ]] = value
21
+ def i : native.Ptr [native.CInt ] = p.cast[native.Ptr [native.CInt ]]
22
+ def i_ = (value : native.CInt ): Unit = ! p.cast[native.Ptr [native.CInt ]] = value
23
+ def ll : native.Ptr [native.CLongLong ] = p.cast[native.Ptr [native.CLongLong ]]
24
+ def ll_ = (value : native.CLongLong ): Unit = ! p.cast[native.Ptr [native.CLongLong ]] = value
24
25
}
25
26
}
Original file line number Diff line number Diff line change @@ -7,8 +7,13 @@ import org.scalanative.bindgen.samples.UnionHelpers._
7
7
object UnionTests extends TestSuite {
8
8
val tests = Tests {
9
9
' getValues - {
10
- val point = Union .getValues()
11
- assert(point.a == 10 )
10
+ Zone {implicit zone =>
11
+ val structPtr = alloc[Union .union_values]
12
+ Union .setIntValue(structPtr)
13
+ assert(! structPtr.i == 10 )
14
+ Union .setLongValue(structPtr)
15
+ assert(! structPtr.l == 10000000000L )
16
+ }
12
17
}
13
18
}
14
19
}
You can’t perform that action at this time.
0 commit comments