X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=ts%2Fjs-wasm.h;h=aa5eec522d6d3fd7fbc2e2c2e3a48e4d88b6c08d;hb=HEAD;hp=84c27ffdaecf9f8a4475ff2a193a88cbea2661c4;hpb=a937cedf387378f24883ab14c5b52a4d13be58bc;p=ldk-java diff --git a/ts/js-wasm.h b/ts/js-wasm.h index 84c27ffd..c3fb7cf4 100644 --- a/ts/js-wasm.h +++ b/ts/js-wasm.h @@ -2,47 +2,55 @@ #include #include -static inline size_t strlen(const char *str) { - const char *s; - for (s = str; *s; ++s) ; - return (s - str); -} +extern size_t strlen(const char *s); + +// We only support two call types - u32 and u64. u32 is mapped to a JavaScript +// "number" whereas u64 is mapped to a JavaScript "bigint". Ultimately it all +// calls through to the same function, but at the FFI boundary itself we have +// to use proper types. typedef uint32_t JSValue; -extern JSValue js_invoke_function(JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue); -extern void js_free_function_ptr(JSValue); - -static inline JSValue js_invoke_function_0(JSValue fn){ - return js_invoke_function(fn,0,0,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_1(JSValue fn, JSValue a){ - return js_invoke_function(fn,a,0,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_2(JSValue fn, JSValue a, JSValue b){ - return js_invoke_function(fn,a,b,0,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_3(JSValue fn, JSValue a, JSValue b, JSValue c){ - return js_invoke_function(fn,a,b,c,0,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_4(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d){ - return js_invoke_function(fn,a,b,c,d,0,0,0,0,0,0); -} -static inline JSValue js_invoke_function_5(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e){ - return js_invoke_function(fn,a,b,c,d,e,0,0,0,0,0); -} -static inline JSValue js_invoke_function_6(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f){ - return js_invoke_function(fn,a,b,c,d,e,f,0,0,0,0); -} -static inline JSValue js_invoke_function_7(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g){ - return js_invoke_function(fn,a,b,c,d,e,f,g,0,0,0); -} -static inline JSValue js_invoke_function_8(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h){ - return js_invoke_function(fn,a,b,c,d,e,f,g,h,0,0); -} -static inline JSValue js_invoke_function_9(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h, JSValue i){ - return js_invoke_function(fn,a,b,c,d,e,f,g,h,i,0); -} -static inline JSValue js_invoke_function_10(JSValue fn, JSValue a, JSValue b, JSValue c, JSValue d, JSValue e, JSValue f, JSValue g, JSValue h, JSValue i, JSValue j){ - return js_invoke_function(fn,a,b,c,d,e,f,g,h,i,j); -} + +#define N_TYPE_u uint32_t +#define N_TYPE_b uint64_t + +#define TOK(a) a +#define R_CAT2(a, b) a ## b +#define CAT2(a, b) R_CAT2(a, b) +#define R_CAT3(a, b, c) a ## b ## c +#define CAT3(a, b, c) R_CAT3(a, b, c) +#define R_CAT4(a, b, c, d) a ## b ## c ## d +#define CAT4(a, b, c, d) R_CAT4(a, b, c, d) +#define R_CAT5(a, b, c, d, e) a ## b ## c ## d ## e +#define CAT5(a, b, c, d, e) R_CAT5(a, b, c, d, e) +#define R_CAT6(a, b, c, d, e, f) a ## b ## c ## d ## e ## f +#define CAT6(a, b, c, d, e, f) R_CAT6(a, b, c, d, e, f) + +#define _GET_CONCAT(_1, _2, _3, _4, _5, _6, CONC, ...) CONC +#define CAT(...) _GET_CONCAT(__VA_ARGS__, CAT6, CAT5, CAT4, CAT3, CAT2, TOK)(__VA_ARGS__) + +#define DO_STRING(a) #a +#define STRINGIZE(a) DO_STRING(a) + +#define DECL_IMPORT(A1, A2, A3, A4, A5, A6) \ + extern uint64_t CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6)) \ + (JSValue obj, JSValue fn, N_TYPE_##A1, N_TYPE_##A2, N_TYPE_##A3, N_TYPE_##A4, N_TYPE_##A5, N_TYPE_##A6) \ + __attribute__((import_name(STRINGIZE(CAT(js_invoke_function_, CAT(A1, A2, A3, A4, A5, A6)))))); + +DECL_IMPORT(u, u, u, u, u, u) +DECL_IMPORT(b, u, u, u, u, u) +DECL_IMPORT(b, b, u, u, u, u) +DECL_IMPORT(b, b, b, u, u, u) +DECL_IMPORT(b, b, b, b, u, u) +DECL_IMPORT(b, b, b, b, b, u) +DECL_IMPORT(b, b, b, b, b, b) +DECL_IMPORT(u, b, u, u, b, u) +DECL_IMPORT(u, b, u, u, u, u) +DECL_IMPORT(u, b, b, u, u, u) +DECL_IMPORT(u, u, b, u, u, u) +DECL_IMPORT(u, u, b, b, u, u) +DECL_IMPORT(u, u, b, u, b, u) +DECL_IMPORT(u, b, u, b, u, u) +DECL_IMPORT(u, u, u, u, b, u) + #endif