From: Not Zed Date: Wed, 8 Jan 2020 20:31:51 +0000 (+1030) Subject: Testing callbacks and so on. X-Git-Url: https://code.zedzone.au/cvs?a=commitdiff_plain;h=3ac54654a36ce2db37b9c4b0e3517c9793f250f8;p=panamaz Testing callbacks and so on. --- diff --git a/test-api/Makefile b/test-api/Makefile index d278024..ba83192 100644 --- a/test-api/Makefile +++ b/test-api/Makefile @@ -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 $@ diff --git a/test-api/api.c b/test-api/api.c index 3458b4a..9dbb2a3 100644 --- a/test-api/api.c +++ b/test-api/api.c @@ -1,5 +1,6 @@ #include +#include #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; +} diff --git a/test-api/api.h b/test-api/api.h index 8931b31..7fc1c3d 100644 --- a/test-api/api.h +++ b/test-api/api.h @@ -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); +}; diff --git a/test-api/api/test/TestAPI.java b/test-api/api/test/TestAPI.java index dc63a85..b463b9d 100644 --- a/test-api/api/test/TestAPI.java +++ b/test-api/api/test/TestAPI.java @@ -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 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 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 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); } } }