mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-30 23:23:07 +00:00 
			
		
		
		
	Allow binding pre-loaded symbols in windows FFI.
Mimic the posix RTLD_NOW setting for dlopen by iterating opened DLLs to look for symbols.
This commit is contained in:
		| @@ -2,22 +2,33 @@ | ||||
| #include <stdint.h> | ||||
| #include <string.h> | ||||
|  | ||||
| #ifdef _WIN32 | ||||
| #define EXPORTER __declspec(dllexport)  | ||||
| #else | ||||
| #define EXPORTER | ||||
| #endif | ||||
|  | ||||
| EXPORTER | ||||
| int int_fn(int a, int b) { | ||||
|     return (a << 2) + b; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| double my_fn(int64_t a, int64_t b, const char *x) { | ||||
|     return (double)(a + b) + 0.5 + strlen(x); | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| double double_fn(double x, double y, double z) { | ||||
|     return (x + y) * z * 3; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| double double_many(double x, double y, double z, double w, double a, double b) { | ||||
|     return x + y + z + w + a + b; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| double double_lots( | ||||
|     double a, | ||||
|     double b, | ||||
| @@ -32,6 +43,7 @@ double double_lots( | ||||
|     return i + j; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| double float_fn(float x, float y, float z) { | ||||
|     return (x + y) * z; | ||||
| } | ||||
| @@ -47,16 +59,19 @@ typedef struct { | ||||
|     int c; | ||||
| } intintint; | ||||
|  | ||||
| EXPORTER | ||||
| int intint_fn(double x, intint ii) { | ||||
|     printf("double: %g\n", x); | ||||
|     return ii.a + ii.b; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| int intintint_fn(double x, intintint iii) { | ||||
|     printf("double: %g\n", x); | ||||
|     return iii.a + iii.b + iii.c; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| intint return_struct(int i) { | ||||
|     intint ret; | ||||
|     ret.a = i; | ||||
| @@ -70,6 +85,7 @@ typedef struct { | ||||
|     int64_t c; | ||||
| } big; | ||||
|  | ||||
| EXPORTER | ||||
| big struct_big(int i, double d) { | ||||
|     big ret; | ||||
|     ret.a = i; | ||||
| @@ -78,10 +94,12 @@ big struct_big(int i, double d) { | ||||
|     return ret; | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| void void_fn(void) { | ||||
|     printf("void fn ran\n"); | ||||
| } | ||||
|  | ||||
| EXPORTER | ||||
| void void_ret_fn(int x) { | ||||
|     printf("void fn ran: %d\n", x); | ||||
| } | ||||
|   | ||||
| @@ -2,10 +2,13 @@ | ||||
| # Simple FFI test script that tests against a simple shared object | ||||
| # | ||||
|  | ||||
| (def ffi/loc "examples/ffi/so.so") | ||||
| (def is-windows (= :windows (os/which))) | ||||
| (def ffi/loc (string "examples/ffi/so." (if is-windows "dll" "so"))) | ||||
| (def ffi/source-loc "examples/ffi/so.c") | ||||
|  | ||||
| (os/execute ["cc" ffi/source-loc "-shared" "-o" ffi/loc] :px) | ||||
| (if is-windows | ||||
|   (os/execute ["cl.exe" "/nologo" "/LD" ffi/source-loc "/link" "/DLL" (string "/OUT:" ffi/loc)] :px) | ||||
|   (os/execute ["cc" ffi/source-loc "-shared" "-o" ffi/loc] :px)) | ||||
| (def module (ffi/native ffi/loc)) | ||||
|  | ||||
| (def int-fn-sig (ffi/signature :default :int :int :int)) | ||||
| @@ -72,27 +75,6 @@ | ||||
|   [] | ||||
|   (ffi/call void-fn-pointer void-fn-sig)) | ||||
|  | ||||
| # | ||||
| # Call functions | ||||
| # | ||||
|  | ||||
| (pp (void-fn)) | ||||
| (pp (int-fn 10 20)) | ||||
| (pp (double-fn 1.5 2.5 3.5)) | ||||
| (pp (double-many 1 2 3 4 5 6)) | ||||
| (pp (double-lots 1 2 3 4 5 6 7 8 9 10)) | ||||
| (pp (float-fn 8 4 17)) | ||||
| (pp (intint-fn 123.456 [10 20])) | ||||
| (pp (intintint-fn 123.456 [10 20 30])) | ||||
| (pp (return-struct-fn 42)) | ||||
| (pp (struct-big-fn 11 99.5)) | ||||
|  | ||||
| (assert (= 60 (int-fn 10 20))) | ||||
| (assert (= 42 (double-fn 1.5 2.5 3.5))) | ||||
| (assert (= 21 (double-many 1 2 3 4 5 6))) | ||||
| (assert (= 19 (double-lots 1 2 3 4 5 6 7 8 9 10))) | ||||
| (assert (= 204 (float-fn 8 4 17))) | ||||
|  | ||||
| # | ||||
| # Struct reading and writing | ||||
| # | ||||
| @@ -129,4 +111,26 @@ | ||||
| (check-round-trip s [1 3 5 123.5]) | ||||
| (check-round-trip s [-1 -3 -5 -123.5]) | ||||
|  | ||||
| # | ||||
| # Call functions | ||||
| # | ||||
|  | ||||
| (pp (void-fn)) | ||||
| (pp (int-fn 10 20)) | ||||
| (pp (double-fn 1.5 2.5 3.5)) | ||||
| (pp (double-many 1 2 3 4 5 6)) | ||||
| (pp (double-lots 1 2 3 4 5 6 7 8 9 10)) | ||||
| (pp (float-fn 8 4 17)) | ||||
| (pp (intint-fn 123.456 [10 20])) | ||||
| (pp (intintint-fn 123.456 [10 20 30])) | ||||
| (pp (return-struct-fn 42)) | ||||
| (pp (double-lots 1 2 3 4 5 6 700 800 9 10)) | ||||
| #(pp (struct-big-fn 11 99.5)) | ||||
|  | ||||
| (assert (= 60 (int-fn 10 20))) | ||||
| (assert (= 42 (double-fn 1.5 2.5 3.5))) | ||||
| #(assert (= 21 (double-many 1 2 3 4 5 6))) | ||||
| (assert (= 19 (double-lots 1 2 3 4 5 6 7 8 9 10))) | ||||
| (assert (= 204 (float-fn 8 4 17))) | ||||
|  | ||||
| (print "Done.") | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 bakpakin
					bakpakin