Switch 32-bit platforms to using 64-bit "pointers"
[ldk-java] / ts / js-wasm.h
1 #ifndef JS_H
2 #include <stdint.h>
3 #include <stddef.h>
4
5 extern size_t strlen(const char *s);
6
7 // We only support two call types - u32 and u64. u32 is mapped to a JavaScript
8 // "number" whereas u64 is mapped to a JavaScript "bigint". Ultimately it all
9 // calls through to the same function, but at the FFI boundary itself we have
10 // to use proper types.
11
12 typedef uint32_t JSValue;
13
14 #define N_TYPE_u uint32_t
15 #define N_TYPE_b uint64_t
16
17 #define TOK(a) a
18 #define R_CAT2(a, b) a ## b
19 #define CAT2(a, b) R_CAT2(a, b)
20 #define R_CAT3(a, b, c) a ## b ## c
21 #define CAT3(a, b, c) R_CAT3(a, b, c)
22 #define R_CAT4(a, b, c, d) a ## b ## c ## d
23 #define CAT4(a, b, c, d) R_CAT4(a, b, c, d)
24 #define R_CAT5(a, b, c, d, e) a ## b ## c ## d ## e
25 #define CAT5(a, b, c, d, e) R_CAT5(a, b, c, d, e)
26 #define R_CAT6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f
27 #define CAT6(a, b, c, d, e, f) R_CAT6(a, b, c, d, e, f)
28
29 #define _GET_CONCAT(_1, _2, _3, _4, _5, _6, CONC, ...) CONC
30 #define CAT(...) _GET_CONCAT(__VA_ARGS__, CAT6, CAT5, CAT4, CAT3, CAT2, TOK)(__VA_ARGS__)
31
32 #define DO_STRING(a) #a
33 #define STRINGIZE(a) DO_STRING(a)
34
35 #define DECL_IMPORT(A1, A2, A3, A4, A5, A6) \
36         extern uint64_t CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6)) \
37         (JSValue obj, JSValue fn, N_TYPE_##A1, N_TYPE_##A2, N_TYPE_##A3, N_TYPE_##A4, N_TYPE_##A5, N_TYPE_##A6) \
38         __attribute__((import_name(STRINGIZE(CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6))))));
39
40 DECL_IMPORT(u, u, u, u, u, u)
41 DECL_IMPORT(b, u, u, u, u, u)
42 DECL_IMPORT(b, b, u, u, u, u)
43 DECL_IMPORT(b, b, b, u, u, u)
44 DECL_IMPORT(b, b, b, b, u, u)
45 DECL_IMPORT(b, b, b, b, b, b)
46 DECL_IMPORT(u, b, u, u, b, u)
47 DECL_IMPORT(u, b, u, u, u, u)
48 DECL_IMPORT(u, b, b, u, u, u)
49 DECL_IMPORT(u, u, b, u, u, u)
50 DECL_IMPORT(u, u, b, u, b, u)
51
52 #endif