X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=ts%2Fjs-wasm.h;h=aa5eec522d6d3fd7fbc2e2c2e3a48e4d88b6c08d;hb=HEAD;hp=95db6d2e660c0a217aa936a2b8d8a586c4b706f0;hpb=97c97c3b5dd6455901d84c7a5289b71bf5770146;p=ldk-java diff --git a/ts/js-wasm.h b/ts/js-wasm.h index 95db6d2e..c3fb7cf4 100644 --- a/ts/js-wasm.h +++ b/ts/js-wasm.h @@ -1,63 +1,56 @@ -#ifndef export -#define export __attribute__((visibility("default"))) -#endif - #ifndef JS_H +#include +#include + +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; + +#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) -unsigned int -js_strlen(const char *str) -{ - const char *s; - - for (s = str; *s; ++s) - ; - return (s - str); -} - -typedef double JSValue; -typedef int JSFunction; -extern JSFunction js_register_function(char*,unsigned int); -extern JSValue js_invoke_function(JSFunction,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue,JSValue); -extern void js_release(JSValue); - -JSValue const JS_NULL = 0.0; -JSValue const JS_UNDEFINED = 1.0; -JSValue const DOM_SELF = 2.0; -JSValue const DOM_WINDOW = 2.0; -JSValue const DOM_DOCUMENT = 3.0; -JSValue const DOM_BODY = 4.0; - -static inline JSValue js_invoke_function_0(JSFunction fn){ - return js_invoke_function(fn,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0); -} -static inline JSValue js_invoke_function_1(JSFunction fn, JSValue a){ - return js_invoke_function(fn,a,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0); -} -static inline JSValue js_invoke_function_2(JSFunction fn, JSValue a, JSValue b){ - return js_invoke_function(fn,a,b,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0); -} -static inline JSValue js_invoke_function_3(JSFunction fn, JSValue a, JSValue b, JSValue c){ - return js_invoke_function(fn,a,b,c,0.0,0.0,0.0,0.0,0.0,0.0,0.0); -} -static inline JSValue js_invoke_function_4(JSFunction fn, JSValue a, JSValue b, JSValue c, JSValue d){ - return js_invoke_function(fn,a,b,c,d,0.0,0.0,0.0,0.0,0.0,0.0); -} -static inline JSValue js_invoke_function_5(JSFunction 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.0,0.0,0.0); -} -static inline JSValue js_invoke_function_6(JSFunction 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,0.0,0.0); -} -static inline JSValue js_invoke_function_7(JSFunction 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.0,0.0); -} -static inline JSValue js_invoke_function_8(JSFunction 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,0.0); -} -static inline JSValue js_invoke_function_9(JSFunction 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.0); -} -static inline JSValue js_invoke_function_10(JSFunction 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); -} #endif