Updated for openjdk-19-internal
[panamaz] / test-api / src / api / test / TestAPI.java
1
2 package api.test;
3
4 import jdk.incubator.foreign.*;
5
6 import proto.api.*;
7 import static proto.api.APILib.*;
8 import java.lang.invoke.*;
9
10 public class TestAPI {
11
12         public static void main(String[] args) {
13                 System.loadLibrary("api");
14
15                 try (Frame frame = Memory.createFrame();
16                         ResourceScope scope = ResourceScope.newConfinedScope()) {
17
18                         data a = data.create(frame);
19                         data b = data.create(frame);
20
21                         Memory.FunctionPointer<Call__i32> cb = Call__i32.upcall(() -> {
22                                         return 56;
23                                 }, scope);
24
25
26                         a.setNext(b);
27                         a.setA(1);
28                         a.setB(2);
29                         a.setC(3);                      //a.setC((byte)3);
30                         a.setD(4);                      //a.setD((byte)4);
31
32
33                         b.setA(5);
34                         b.setB(6);
35                         b.setC((byte)255);
36                         b.setD((byte)255);
37
38                         b.setTest_a(cb);
39
40                         System.out.println("from a");
41                         print_data(a);
42                         System.out.println("from b");
43                         print_data(b);
44
45                         //api api = proto.api.api.create(frame);
46                         //api.setFunca(api_func(Memory.ByteArray.create("funca", frame)));
47                         //api.setFuncb(api_func(Memory.ByteArray.create("funcb", frame)));
48                         //api.setFuncc(api_func(Memory.ByteArray.create("funcc", frame)));
49
50                         // dynamic lookup
51                         System.out.println("call funca via symbol lookup");
52                         Memory.FunctionPointer<Call_i32_v> funca = Call_i32_v.downcall(api_func(Memory.ByteArray.create("funca", frame)), scope);
53                         System.out.printf(" %s\n", funca.symbol());
54                         funca.function().call(12);
55
56                         api api = api_create();
57
58                         System.out.println("call funca via function table");
59                         api.getFunca().function().call(99);
60
61                         api.setFunca(Call_i32_v.upcall(
62                                         i-> System.out.printf("java.funca: %d\n", i),
63                                         scope));
64
65                         System.out.println("call funca via java upcall");
66                         api.getFunca().function().call(22);
67                 }
68         }
69 }