diff options
author | Florian Dold <florian@dold.me> | 2023-10-23 16:02:23 +0200 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2023-10-23 16:02:23 +0200 |
commit | a8da222eee433452801f88fcbce43a77adc8eba8 (patch) | |
tree | fe1743d0dc041cfbb73a99939c2e0e0f0a7f4ebe /taler_wallet_core_lib.c | |
parent | f24451bf9fd2e6893b4897f34bc8a47f3fa20174 (diff) | |
download | quickjs-tart-a8da222eee433452801f88fcbce43a77adc8eba8.tar.gz quickjs-tart-a8da222eee433452801f88fcbce43a77adc8eba8.tar.bz2 quickjs-tart-a8da222eee433452801f88fcbce43a77adc8eba8.zip |
implement global.__nativeLog function
Diffstat (limited to 'taler_wallet_core_lib.c')
-rw-r--r-- | taler_wallet_core_lib.c | 50 |
1 files changed, 48 insertions, 2 deletions
diff --git a/taler_wallet_core_lib.c b/taler_wallet_core_lib.c index 072b6ac..07c8c56 100644 --- a/taler_wallet_core_lib.c +++ b/taler_wallet_core_lib.c @@ -30,6 +30,8 @@ extern const uint32_t qjsc_prelude_size; extern const uint8_t qjsc_wallet_core[]; extern const uint32_t qjsc_wallet_core_size; +static JSClassID js_wallet_instance_handle_id; + struct HostMessage { struct list_head link; char *data; @@ -47,6 +49,9 @@ struct TALER_WALLET_Instance TALER_WALLET_MessageHandlerFn handler_f; void *handler_cls; + + TALER_WALLET_LogHandlerFn log_handler_f; + void *log_handler_cls; }; @@ -94,10 +99,11 @@ TALER_WALLET_set_message_handler(struct TALER_WALLET_Instance *twi, void TALER_WALLET_set_log_handler(struct TALER_WALLET_Instance *twi, - TALER_WALLET_LogHandlerFn *handler_f, + TALER_WALLET_LogHandlerFn handler_f, void *handler_cls) { - // FIXME: Implement! + twi->log_handler_cls = handler_cls; + twi->log_handler_f = handler_f; } @@ -137,6 +143,30 @@ TALER_WALLET_create(void) return wh; } +static JSValue js_native_log(JSContext *ctx, + JSValueConst this_obj, + int argc, JSValueConst *argv, + int magic, JSValue *func_data) +{ + struct TALER_WALLET_Instance *wh; + const char *tag = NULL; + const char *msg = NULL; + uint32_t level = 0; + wh = JS_GetOpaque(func_data[0], js_wallet_instance_handle_id); + if (NULL != wh->log_handler_f) { + JS_ToUint32(ctx, &level, argv[0]); + tag = JS_ToCString(ctx, argv[1]); + msg = JS_ToCString(ctx, argv[2]); + wh->log_handler_f(wh->log_handler_cls, + level, + tag, + msg); + } + JS_FreeCString(ctx, tag); + JS_FreeCString(ctx, msg); + return JS_UNDEFINED; +} + static void * run(void *cls) @@ -155,12 +185,27 @@ run(void *cls) return NULL; } + JS_NewClassID(&js_wallet_instance_handle_id); + JS_SetHostPromiseRejectionTracker(wh->rt, js_std_promise_rejection_tracker, NULL); js_std_add_helpers(wh->ctx, 0, NULL); + // install native log handler + if (NULL != wh->log_handler_f) { + JSValue global_obj; + JSValue data; + + data = JS_NewObjectClass(wh->ctx, js_wallet_instance_handle_id); + JS_SetOpaque(data, wh); + global_obj = JS_GetGlobalObject(wh->ctx); + // We could also try to add this to "os" or "tart", but we are lazy. + JS_SetPropertyStr(wh->ctx, global_obj, "__nativeLog", + JS_NewCFunctionData(wh->ctx, js_native_log, 3, 0, 1, &data)); + } + fprintf(stderr, "qtart: loading JS code\n"); js_std_eval_binary(wh->ctx, qjsc_prelude, qjsc_prelude_size, 0); js_std_eval_binary(wh->ctx, qjsc_wallet_core, qjsc_wallet_core_size, 0); @@ -168,6 +213,7 @@ run(void *cls) js_os_set_host_message_handler(wh->ctx, wallet_host_message_handler, wh); + pthread_mutex_unlock(&wh->handle_mutex); eval_buf(wh->ctx, "installNativeWalletListener()", "<talerwalletcore>"); |