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