#include <stdio.h>
+#include <string.h>
#include "api.h"
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;
+}
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);
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);
}
}
}