Testing callbacks and so on.
authorNot Zed <notzed@gmail.com>
Wed, 8 Jan 2020 20:31:51 +0000 (07:01 +1030)
committerNot Zed <notzed@gmail.com>
Wed, 8 Jan 2020 20:31:51 +0000 (07:01 +1030)
test-api/Makefile
test-api/api.c
test-api/api.h
test-api/api/test/TestAPI.java

index d278024..ba83192 100644 (file)
@@ -11,7 +11,7 @@ bin/classes/api/test/TestAPI.class: api/test/TestAPI.java bin/api.classes
        $(JAVAC) -cp bin/classes -d bin/classes $<
 
 bin/api.classes: bin/api.pm
-       ../src/generate -d bin/java -t api -c APILib -lapi ./bin/api.pm
+       ../src/generate -d bin/java -t api -c APILib -lapi -s api ./bin/api.pm
        $(JAVAC) -d bin/classes bin/java/api/*.java
        touch $@
 
index 3458b4a..9dbb2a3 100644 (file)
@@ -1,5 +1,6 @@
 
 #include <stdio.h>
+#include <string.h>
 #include "api.h"
 
 void print_data(struct data *data) {
@@ -11,3 +12,28 @@ void print_data(struct data *data) {
                data = data->next;
        }
 }
+
+static void funca(int a) {
+       printf("funca: %d\n", a);
+}
+
+static int funcb(int b) {
+       printf("funcb: %d\n", b);
+       return b * b;
+}
+
+static int funcc(float f) {
+       printf("funcc: %f\n", f);
+       return (int)f;
+}
+
+void *api_func(const char *name) {
+       if (strcmp(name, "funca") == 0)
+               return funca;
+       else if (strcmp(name, "funcb") == 0)
+               return funcb;
+       else if (strcmp(name, "funcc") == 0)
+               return funcc;
+       else
+               return NULL;
+}
index 8931b31..7fc1c3d 100644 (file)
@@ -10,3 +10,11 @@ struct data {
 };
 
 void print_data(struct data *data);
+
+void *api_func(const char *name);
+
+struct api {
+       void (*funca)(int a);
+       int (*funcb)(int b);
+       int (*funcc)(float b);
+};
index dc63a85..b463b9d 100644 (file)
@@ -9,6 +9,45 @@ import java.util.function.IntFunction;
 import api.*;
 
 public class TestAPI {
+
+       @NativeHeader()
+       public interface APIExt {
+
+               @NativeFunction(value="(i32)v")
+               public void funca(int a);
+
+               @NativeFunction(value="(i32)i32")
+               public int funcb(short b);
+
+               @NativeFunction(value="(f32)i32")
+               public int funcc(float b);
+
+               @NativeFunction(value="(f32)i32")
+               public int funcd(float b);
+
+               public static boolean exists(APILib lib, String func) {
+               }
+
+               public static APIExt bind(APILib lib) {
+                       APIExt ext = Libraries.bind(APIExt.class, (String name)->{
+                                       Scope ss = s.fork();
+                                       Pointer<Byte> cname = ss.allocateCString(name);
+                                       Pointer<?> p = lib.api_func(cname);
+                                       ss.close();
+
+                                       return new Library.Symbol() {
+                                               public String getName() {
+                                                       return name;
+                                               }
+                                               public Pointer<?> getAddress() {
+                                                       System.out.printf(" %s -> %016x\n", name, p.addr());
+                                                       return p;
+                                               }
+                                       };
+                               });
+               }
+       }
+
        public static void main(String[] args) {
                APILib lib = APILib.bind;
                LayoutType<Data> dataLayout = LayoutType.ofStruct(Data.class);
@@ -41,6 +80,31 @@ public class TestAPI {
                        lib.print_data(a);
                        System.out.println("\bfrom b\n");
                        lib.print_data(b);
+
+                       // Try to bind to custom address
+                       APIExt ext = Libraries.bind(APIExt.class, (String name)->{
+                                       Scope ss = s.fork();
+                                       Pointer<Byte> cname = ss.allocateCString(name);
+                                       Pointer<?> p = lib.api_func(cname);
+                                       ss.close();
+
+                                       return new Library.Symbol() {
+                                               public String getName() {
+                                                       return name;
+                                               }
+                                               public Pointer<?> getAddress() {
+                                                       System.out.printf(" %s -> %016x\n", name, p.addr());
+                                                       return p;
+                                               }
+                                       };
+                               });
+
+                       System.out.println("invoke ext interface");
+                       ext.funca(42);
+                       System.out.printf("funb -> %d\n", ext.funcb((short)56));
+                       System.out.printf("func -> %d\n", ext.funcc(56.7198273918723f));
+
+                       ext.funcd(12);
                }
        }
 }