Testing callbacks and so on.
[panamaz] / test-api / api / test / TestAPI.java
1
2 package api.test;
3
4 import java.foreign.*;
5 import java.foreign.memory.*;
6 import java.foreign.annotations.*;
7 import java.util.function.IntFunction;
8
9 import api.*;
10
11 public class TestAPI {
12
13         @NativeHeader()
14         public interface APIExt {
15
16                 @NativeFunction(value="(i32)v")
17                 public void funca(int a);
18
19                 @NativeFunction(value="(i32)i32")
20                 public int funcb(short b);
21
22                 @NativeFunction(value="(f32)i32")
23                 public int funcc(float b);
24
25                 @NativeFunction(value="(f32)i32")
26                 public int funcd(float b);
27
28                 public static boolean exists(APILib lib, String func) {
29                 }
30
31                 public static APIExt bind(APILib lib) {
32                         APIExt ext = Libraries.bind(APIExt.class, (String name)->{
33                                         Scope ss = s.fork();
34                                         Pointer<Byte> cname = ss.allocateCString(name);
35                                         Pointer<?> p = lib.api_func(cname);
36                                         ss.close();
37
38                                         return new Library.Symbol() {
39                                                 public String getName() {
40                                                         return name;
41                                                 }
42                                                 public Pointer<?> getAddress() {
43                                                         System.out.printf(" %s -> %016x\n", name, p.addr());
44                                                         return p;
45                                                 }
46                                         };
47                                 });
48                 }
49         }
50
51         public static void main(String[] args) {
52                 APILib lib = APILib.bind;
53                 LayoutType<Data> dataLayout = LayoutType.ofStruct(Data.class);
54
55                 try (Scope s = Libraries.libraryScope(lib).fork()) {
56                         Pointer<Data> a = s.allocate(dataLayout);
57                         Pointer<Data> b = s.allocate(dataLayout);
58                         Callback<Call__I> cb = s.allocateCallback(Call__I.class,
59                                 () -> {
60                                         return 56;
61                                 });
62
63                         Data ad = a.get();
64                         Data bd = b.get();
65
66                         ad.setNext(b);
67                         ad.setA(1);
68                         ad.setB(2);
69                         ad.setC((byte)3);
70                         ad.setD((byte)4);
71
72                         bd.setA(5);
73                         bd.setB(6);
74                         bd.setC((byte)255);
75                         bd.setD((byte)255);
76
77                         bd.setTestA(cb);
78
79                         System.out.println("from a\n");
80                         lib.print_data(a);
81                         System.out.println("\bfrom b\n");
82                         lib.print_data(b);
83
84                         // Try to bind to custom address
85                         APIExt ext = Libraries.bind(APIExt.class, (String name)->{
86                                         Scope ss = s.fork();
87                                         Pointer<Byte> cname = ss.allocateCString(name);
88                                         Pointer<?> p = lib.api_func(cname);
89                                         ss.close();
90
91                                         return new Library.Symbol() {
92                                                 public String getName() {
93                                                         return name;
94                                                 }
95                                                 public Pointer<?> getAddress() {
96                                                         System.out.printf(" %s -> %016x\n", name, p.addr());
97                                                         return p;
98                                                 }
99                                         };
100                                 });
101
102                         System.out.println("invoke ext interface");
103                         ext.funca(42);
104                         System.out.printf("funb -> %d\n", ext.funcb((short)56));
105                         System.out.printf("func -> %d\n", ext.funcc(56.7198273918723f));
106
107                         ext.funcd(12);
108                 }
109         }
110 }