commit a8da222eee433452801f88fcbce43a77adc8eba8
parent f24451bf9fd2e6893b4897f34bc8a47f3fa20174
Author: Florian Dold <florian@dold.me>
Date: Mon, 23 Oct 2023 16:02:23 +0200
implement global.__nativeLog function
Diffstat:
3 files changed, 56 insertions(+), 3 deletions(-)
diff --git 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>");
diff --git a/taler_wallet_core_lib.h b/taler_wallet_core_lib.h
@@ -84,7 +84,7 @@ typedef void (*TALER_WALLET_LogHandlerFn)(void *log_p,
*/
void
TALER_WALLET_set_log_handler(struct TALER_WALLET_Instance *twi,
- TALER_WALLET_LogHandlerFn *handler_f,
+ TALER_WALLET_LogHandlerFn handler_f,
void *handler_p);
diff --git a/wallet-client-example.c b/wallet-client-example.c
@@ -29,12 +29,19 @@ my_handler(void *cls, const char *message)
printf("got message: %s\n", message);
}
+void
+log_handler(void *cls, enum TALER_WALLET_LogLevel level, const char *tag, const char *msg)
+{
+ printf("got log message, level %d, tag %s: %s\n", (int) level, tag, msg);
+}
+
int main(int argc, char **argv)
{
struct TALER_WALLET_Instance *wh = TALER_WALLET_create();
TALER_WALLET_set_message_handler(wh, &my_handler, NULL);
+ TALER_WALLET_set_log_handler(wh, &log_handler, NULL);
TALER_WALLET_run(wh);